-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtime.cc
58 lines (48 loc) · 918 Bytes
/
time.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
* Copyright (c)2020-2021 Pavel Shramov <[email protected]>
*
* tll is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#include "tll/util/time.h"
#include <sys/time.h>
#include <time.h>
struct cached_clock_t
{
long long last = 0;
int enabled = 0;
timespec ts = {};
void enable(bool v)
{
if (v) {
last = now();
enabled++;
} else
enabled--;
}
long long now()
{
clock_gettime(CLOCK_REALTIME, &ts);
last = 1000000000ll * ts.tv_sec + ts.tv_nsec;
return last;
}
long long now_cached()
{
if (enabled)
return last;
return now();
}
};
namespace { thread_local cached_clock_t cached_clock = {}; }
long long tll_time_now()
{
return cached_clock.now();
}
long long tll_time_now_cached()
{
return cached_clock.now_cached();
}
void tll_time_cache_enable(int enable)
{
cached_clock.enable(enable);
}