Clean-up: modernize pretty_xml.cpp
[lttng-tools.git] / src / common / compat / time.hpp
... / ...
CommitLineData
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#include <cstdint>
16
17typedef uint64_t timer_t;
18
19#include <mach/mach.h>
20#include <mach/clock.h>
21#include <common/compat/errno.hpp>
22
23#undef NSEC_PER_SEC
24#undef NSEC_PER_MSEC
25#undef NSEC_PER_USEC
26#undef USEC_PER_SEC
27
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;
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
58 tp->tv_sec = now.tv_sec;
59 tp->tv_nsec = now.tv_nsec;
60
61deallocate:
62 mach_port_deallocate(mach_task_self(), clock);
63end:
64 return ret;
65}
66
67#else /* __APPLE__ && !LTTNG_HAVE_CLOCK_GETTIME */
68
69static inline
70int lttng_clock_gettime(clockid_t clk_id, struct timespec *tp)
71{
72 return clock_gettime(clk_id, tp);
73}
74
75#endif /* __APPLE__ && !LTTNG_HAVE_CLOCK_GETTIME */
76
77#endif /* _COMPAT_TIME_H */
This page took 0.022844 seconds and 4 git commands to generate.