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 | |
8c3f60f3 MJ |
15 | #include <cstdint> |
16 | ||
389fbf04 | 17 | typedef uint64_t timer_t; |
389fbf04 | 18 | |
c9e313bc | 19 | #include <common/compat/errno.hpp> |
389fbf04 | 20 | |
28f23191 JG |
21 | #include <mach/clock.h> |
22 | #include <mach/mach.h> | |
23 | ||
395d6b02 JG |
24 | #undef NSEC_PER_SEC |
25 | #undef NSEC_PER_MSEC | |
26 | #undef NSEC_PER_USEC | |
efef32e9 | 27 | #undef USEC_PER_SEC |
395d6b02 | 28 | |
707de922 JG |
29 | #endif /* __APPLE__ */ |
30 | ||
31 | /* macOS/OS X 10.12 (Sierra) and up provide clock_gettime() */ | |
32 | #if defined(__APPLE__) && !defined(LTTNG_HAVE_CLOCK_GETTIME) | |
33 | ||
34 | typedef int clockid_t; | |
28f23191 | 35 | #define CLOCK_REALTIME CALENDAR_CLOCK |
389fbf04 MJ |
36 | #define CLOCK_MONOTONIC SYSTEM_CLOCK |
37 | ||
28f23191 | 38 | static inline int lttng_clock_gettime(clockid_t clk_id, struct timespec *tp) |
389fbf04 MJ |
39 | { |
40 | int ret = 0; | |
41 | clock_serv_t clock; | |
42 | mach_timespec_t now; | |
43 | ||
44 | if (clk_id != CLOCK_REALTIME && clk_id != CLOCK_MONOTONIC) { | |
45 | ret = -1; | |
46 | errno = EINVAL; | |
47 | goto end; | |
48 | } | |
49 | ||
50 | host_get_clock_service(mach_host_self(), clk_id, &clock); | |
51 | ||
52 | ret = clock_get_time(clock, &now); | |
53 | if (ret != KERN_SUCCESS) { | |
54 | ret = -1; | |
55 | goto deallocate; | |
56 | } | |
57 | ||
44f6feb7 JG |
58 | tp->tv_sec = now.tv_sec; |
59 | tp->tv_nsec = now.tv_nsec; | |
389fbf04 MJ |
60 | |
61 | deallocate: | |
62 | mach_port_deallocate(mach_task_self(), clock); | |
63 | end: | |
64 | return ret; | |
65 | } | |
66 | ||
707de922 | 67 | #else /* __APPLE__ && !LTTNG_HAVE_CLOCK_GETTIME */ |
389fbf04 | 68 | |
28f23191 | 69 | static inline int lttng_clock_gettime(clockid_t clk_id, struct timespec *tp) |
389fbf04 MJ |
70 | { |
71 | return clock_gettime(clk_id, tp); | |
72 | } | |
73 | ||
707de922 | 74 | #endif /* __APPLE__ && !LTTNG_HAVE_CLOCK_GETTIME */ |
389fbf04 MJ |
75 | |
76 | #endif /* _COMPAT_TIME_H */ |