+static void timespec_sub(const struct timespec *a, const struct timespec *b, struct timespec *r) {
+ r->tv_sec = a->tv_sec - b->tv_sec;
+ r->tv_nsec = a->tv_nsec - b->tv_nsec;
+
+ if(r->tv_nsec < 0) {
+ r->tv_sec--, r->tv_nsec += NSEC_PER_SEC;
+ }
+}
+
+static int32_t timespec_diff_usec(const struct timespec *a, const struct timespec *b) {
+ return (a->tv_sec - b->tv_sec) * 1000000 + (a->tv_nsec - b->tv_nsec) / 1000;
+}
+
+static bool timespec_lt(const struct timespec *a, const struct timespec *b) {
+ if(a->tv_sec == b->tv_sec) {
+ return a->tv_nsec < b->tv_nsec;
+ } else {
+ return a->tv_sec < b->tv_sec;
+ }
+}
+
+static void timespec_clear(struct timespec *a) {
+ a->tv_sec = 0;
+ a->tv_nsec = 0;
+}
+
+static bool timespec_isset(const struct timespec *a) {
+ return a->tv_sec;
+}
+
+static long CLOCK_GRANULARITY; // usec
+
+static inline size_t min(size_t a, size_t b) {
+ return a < b ? a : b;
+}
+