Build fix: missing cstdint include in time.hpp on macOS
[lttng-tools.git] / src / common / compat / time.hpp
CommitLineData
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 17typedef uint64_t timer_t;
389fbf04
MJ
18
19#include <mach/mach.h>
20#include <mach/clock.h>
c9e313bc 21#include <common/compat/errno.hpp>
389fbf04 22
395d6b02
JG
23#undef NSEC_PER_SEC
24#undef NSEC_PER_MSEC
25#undef NSEC_PER_USEC
efef32e9 26#undef USEC_PER_SEC
395d6b02 27
707de922
JG
28#endif /* __APPLE__ */
29
30/* macOS/OS X 10.12 (Sierra) and up provide clock_gettime() */
31#if defined(__APPLE__) && !defined(LTTNG_HAVE_CLOCK_GETTIME)
32
33typedef int clockid_t;
389fbf04
MJ
34#define CLOCK_REALTIME CALENDAR_CLOCK
35#define CLOCK_MONOTONIC SYSTEM_CLOCK
36
37static inline
38int lttng_clock_gettime(clockid_t clk_id, struct timespec *tp)
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
61deallocate:
62 mach_port_deallocate(mach_task_self(), clock);
63end:
64 return ret;
65}
66
707de922 67#else /* __APPLE__ && !LTTNG_HAVE_CLOCK_GETTIME */
389fbf04
MJ
68
69static inline
70int lttng_clock_gettime(clockid_t clk_id, struct timespec *tp)
71{
72 return clock_gettime(clk_id, tp);
73}
74
707de922 75#endif /* __APPLE__ && !LTTNG_HAVE_CLOCK_GETTIME */
389fbf04
MJ
76
77#endif /* _COMPAT_TIME_H */
This page took 0.055473 seconds and 4 git commands to generate.