-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock.h
53 lines (38 loc) · 850 Bytes
/
clock.h
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
#pragma once
#include <chrono>
struct clock
{
using duration =
std::chrono::nanoseconds;
using rep =
duration::rep;
using period =
duration::period;
using time_point =
std::chrono::time_point<clock, duration>;
static constexpr bool is_steady{true};
static time_point now() noexcept
{
timespec tp;
clock_gettime(CLOCK_MONOTONIC, &tp);
return time_point(duration(std::chrono::seconds(tp.tv_sec) +
std::chrono::nanoseconds(tp.tv_nsec)));
}
};
void internal_sleep(int64_t delay)
{
timespec tq =
{
.tv_sec = 0,
.tv_nsec = delay
};
timespec tp;
while (true)
{
if (nanosleep(&tq, &tp) == 0)
{
break;
}
tq = tp;
}
}