Rizin
unix-like reverse engineering framework and cli tools
clock.h
Go to the documentation of this file.
1 #ifndef TREE_SITTER_CLOCK_H_
2 #define TREE_SITTER_CLOCK_H_
3 
4 #include <stdint.h>
5 
7 
8 #ifdef _WIN32
9 
10 // Windows:
11 // * Represent a time as a performance counter value.
12 // * Represent a duration as a number of performance counter ticks.
13 
14 #include <windows.h>
15 typedef uint64_t TSClock;
16 
17 static inline TSDuration duration_from_micros(uint64_t micros) {
18  LARGE_INTEGER frequency;
19  QueryPerformanceFrequency(&frequency);
20  return micros * (uint64_t)frequency.QuadPart / 1000000;
21 }
22 
23 static inline uint64_t duration_to_micros(TSDuration self) {
24  LARGE_INTEGER frequency;
25  QueryPerformanceFrequency(&frequency);
26  return self * 1000000 / (uint64_t)frequency.QuadPart;
27 }
28 
29 static inline TSClock clock_null(void) {
30  return 0;
31 }
32 
33 static inline TSClock clock_now(void) {
34  LARGE_INTEGER result;
35  QueryPerformanceCounter(&result);
36  return (uint64_t)result.QuadPart;
37 }
38 
39 static inline TSClock clock_after(TSClock base, TSDuration duration) {
40  return base + duration;
41 }
42 
43 static inline bool clock_is_null(TSClock self) {
44  return !self;
45 }
46 
47 static inline bool clock_is_gt(TSClock self, TSClock other) {
48  return self > other;
49 }
50 
51 #elif defined(CLOCK_MONOTONIC) && !defined(__APPLE__)
52 
53 // POSIX with monotonic clock support (Linux)
54 // * Represent a time as a monotonic (seconds, nanoseconds) pair.
55 // * Represent a duration as a number of microseconds.
56 //
57 // On these platforms, parse timeouts will correspond accurately to
58 // real time, regardless of what other processes are running.
59 
60 #include <time.h>
61 typedef struct timespec TSClock;
62 
63 static inline TSDuration duration_from_micros(uint64_t micros) {
64  return micros;
65 }
66 
67 static inline uint64_t duration_to_micros(TSDuration self) {
68  return self;
69 }
70 
71 static inline TSClock clock_now(void) {
72  TSClock result;
73  clock_gettime(CLOCK_MONOTONIC, &result);
74  return result;
75 }
76 
77 static inline TSClock clock_null(void) {
78  return (TSClock) {0, 0};
79 }
80 
81 static inline TSClock clock_after(TSClock base, TSDuration duration) {
82  TSClock result = base;
83  result.tv_sec += duration / 1000000;
84  result.tv_nsec += (duration % 1000000) * 1000;
85  return result;
86 }
87 
88 static inline bool clock_is_null(TSClock self) {
89  return !self.tv_sec;
90 }
91 
92 static inline bool clock_is_gt(TSClock self, TSClock other) {
93  if (self.tv_sec > other.tv_sec) return true;
94  if (self.tv_sec < other.tv_sec) return false;
95  return self.tv_nsec > other.tv_nsec;
96 }
97 
98 #else
99 
100 // macOS or POSIX without monotonic clock support
101 // * Represent a time as a process clock value.
102 // * Represent a duration as a number of process clock ticks.
103 //
104 // On these platforms, parse timeouts may be affected by other processes,
105 // which is not ideal, but is better than using a non-monotonic time API
106 // like `gettimeofday`.
107 
108 #include <time.h>
110 
111 static inline TSDuration duration_from_micros(uint64_t micros) {
112  return micros * (uint64_t)CLOCKS_PER_SEC / 1000000;
113 }
114 
116  return self * 1000000 / (uint64_t)CLOCKS_PER_SEC;
117 }
118 
119 static inline TSClock clock_null(void) {
120  return 0;
121 }
122 
123 static inline TSClock clock_now(void) {
124  return (uint64_t)clock();
125 }
126 
127 static inline TSClock clock_after(TSClock base, TSDuration duration) {
128  return base + duration;
129 }
130 
131 static inline bool clock_is_null(TSClock self) {
132  return !self;
133 }
134 
135 static inline bool clock_is_gt(TSClock self, TSClock other) {
136  return self > other;
137 }
138 
139 #endif
140 
141 #endif // TREE_SITTER_CLOCK_H_
uint64_t TSClock
Definition: clock.h:109
static TSClock clock_null(void)
Definition: clock.h:119
uint64_t TSDuration
Definition: clock.h:6
static TSDuration duration_from_micros(uint64_t micros)
Definition: clock.h:111
static uint64_t duration_to_micros(TSDuration self)
Definition: clock.h:115
static bool clock_is_gt(TSClock self, TSClock other)
Definition: clock.h:135
static TSClock clock_after(TSClock base, TSDuration duration)
Definition: clock.h:127
static TSClock clock_now(void)
Definition: clock.h:123
static bool clock_is_null(TSClock self)
Definition: clock.h:131
unsigned long uint64_t
Definition: sftypes.h:28
time_t tv_sec
Definition: sftypes.h:89