Commit | Line | Data |
---|---|---|
389fbf04 MJ |
1 | /* |
2 | * Copyright (C) 2016 Michael Jeanson <mjeanson@efficios.com> | |
3 | * | |
ab5be9fa | 4 | * SPDX-License-Identifier: MIT |
389fbf04 | 5 | * |
389fbf04 MJ |
6 | */ |
7 | ||
8 | #ifndef _COMPAT_TIME_H | |
9 | #define _COMPAT_TIME_H | |
10 | ||
11 | #include <time.h> | |
12 | ||
13 | #ifdef __APPLE__ | |
707de922 | 14 | |
389fbf04 | 15 | typedef uint64_t timer_t; |
389fbf04 MJ |
16 | |
17 | #include <mach/mach.h> | |
18 | #include <mach/clock.h> | |
df6a8b05 | 19 | #include <errno.h> |
389fbf04 | 20 | |
395d6b02 JG |
21 | #undef NSEC_PER_SEC |
22 | #undef NSEC_PER_MSEC | |
23 | #undef NSEC_PER_USEC | |
efef32e9 | 24 | #undef USEC_PER_SEC |
395d6b02 | 25 | |
707de922 JG |
26 | #endif /* __APPLE__ */ |
27 | ||
28 | /* macOS/OS X 10.12 (Sierra) and up provide clock_gettime() */ | |
29 | #if defined(__APPLE__) && !defined(LTTNG_HAVE_CLOCK_GETTIME) | |
30 | ||
31 | typedef int clockid_t; | |
389fbf04 MJ |
32 | #define CLOCK_REALTIME CALENDAR_CLOCK |
33 | #define CLOCK_MONOTONIC SYSTEM_CLOCK | |
34 | ||
35 | static inline | |
36 | int lttng_clock_gettime(clockid_t clk_id, struct timespec *tp) | |
37 | { | |
38 | int ret = 0; | |
39 | clock_serv_t clock; | |
40 | mach_timespec_t now; | |
41 | ||
42 | if (clk_id != CLOCK_REALTIME && clk_id != CLOCK_MONOTONIC) { | |
43 | ret = -1; | |
44 | errno = EINVAL; | |
45 | goto end; | |
46 | } | |
47 | ||
48 | host_get_clock_service(mach_host_self(), clk_id, &clock); | |
49 | ||
50 | ret = clock_get_time(clock, &now); | |
51 | if (ret != KERN_SUCCESS) { | |
52 | ret = -1; | |
53 | goto deallocate; | |
54 | } | |
55 | ||
56 | tp->tv_sec = now.tv_sec; | |
57 | tp->tv_nsec = now.tv_nsec; | |
58 | ||
59 | deallocate: | |
60 | mach_port_deallocate(mach_task_self(), clock); | |
61 | end: | |
62 | return ret; | |
63 | } | |
64 | ||
707de922 | 65 | #else /* __APPLE__ && !LTTNG_HAVE_CLOCK_GETTIME */ |
389fbf04 MJ |
66 | |
67 | static inline | |
68 | int lttng_clock_gettime(clockid_t clk_id, struct timespec *tp) | |
69 | { | |
70 | return clock_gettime(clk_id, tp); | |
71 | } | |
72 | ||
707de922 | 73 | #endif /* __APPLE__ && !LTTNG_HAVE_CLOCK_GETTIME */ |
389fbf04 MJ |
74 | |
75 | #endif /* _COMPAT_TIME_H */ |