-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.c
61 lines (48 loc) · 823 Bytes
/
timer.c
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
59
60
61
#include <stdint.h>
#if defined(MACOS)
#include <mach/mach.h>
#include <mach/mach_time.h>
#include <CoreServices/CoreServices.h>
#endif
#if defined(LINUX)
#include <time.h>
#endif
#if defined(MACOS)
static mach_timebase_info_data_t mach_time_info;
void
timer_init(void)
{
(void) mach_timebase_info(&mach_time_info);
}
uint64_t
tick(void)
{
return mach_absolute_time();
}
uint64_t
tick_delta_to_nanoseconds(uint64_t delta)
{
return delta * mach_time_info.numer / mach_time_info.denom;
}
#elif defined(LINUX)
void
timer_init(void)
{
}
uint64_t
tick(void)
{
int rv;
struct timespec tp = {};
rv = clock_gettime(CLOCK_MONOTONIC_RAW, &tp);
if (rv == -1) {
return 0;
}
return (tp.tv_sec * 1000000000) + tp.tv_nsec;
}
uint64_t
tick_delta_to_nanoseconds(uint64_t delta)
{
return delta;
}
#endif