| 1 | /* |
| 2 | * Copyright (C) 2016 Michael Jeanson <mjeanson@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #ifndef _COMPAT_TIME_H |
| 9 | #define _COMPAT_TIME_H |
| 10 | |
| 11 | #include <time.h> |
| 12 | |
| 13 | #ifdef __APPLE__ |
| 14 | |
| 15 | typedef uint64_t timer_t; |
| 16 | |
| 17 | #include <mach/mach.h> |
| 18 | #include <mach/clock.h> |
| 19 | #include <common/compat/errno.h> |
| 20 | |
| 21 | #undef NSEC_PER_SEC |
| 22 | #undef NSEC_PER_MSEC |
| 23 | #undef NSEC_PER_USEC |
| 24 | #undef USEC_PER_SEC |
| 25 | |
| 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; |
| 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 | |
| 65 | #else /* __APPLE__ && !LTTNG_HAVE_CLOCK_GETTIME */ |
| 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 | |
| 73 | #endif /* __APPLE__ && !LTTNG_HAVE_CLOCK_GETTIME */ |
| 74 | |
| 75 | #endif /* _COMPAT_TIME_H */ |