Monado OpenXR Runtime
os_time.h
Go to the documentation of this file.
1 // Copyright 2019-2020, Collabora, Ltd.
2 // SPDX-License-Identifier: BSL-1.0
3 /*!
4  * @file
5  * @brief Wrapper around OS native time functions.
6  *
7  * These should be preferred over directly using native OS time functions in
8  * potentially-portable code. Additionally, in most cases these are preferred
9  * over timepoints from @ref time_state for general usage in drivers, etc.
10  *
11  * @author Drew DeVault <sir@cmpwn.com>
12  * @author Jakob Bornecrantz <jakob@collabora.com>
13  *
14  * @ingroup aux_os
15  */
16 
17 #pragma once
18 
19 #include "xrt/xrt_config_os.h"
20 #include "xrt/xrt_compiler.h"
21 
22 #ifdef XRT_OS_LINUX
23 #include <time.h>
24 #include <sys/time.h>
25 #define XRT_HAVE_TIMESPEC
26 #define XRT_HAVE_TIMEVAL
27 
28 #elif defined(XRT_DOXYGEN)
29 #include <time.h>
30 #define XRT_HAVE_TIMESPEC
31 #define XRT_HAVE_TIMEVAL
32 
33 #else
34 #error "No time support on non-Linux platforms yet."
35 #endif
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 /*!
42  * @defgroup aux_os_time Portable Timekeeping
43  * @ingroup aux_os
44  *
45  * @brief Unifying wrapper around system time retrieval functions.
46  */
47 
48 
49 /*!
50  * @defgroup aux_os_time_extra Extra Timekeeping Utilities
51  * @ingroup aux_os_time
52  *
53  * @brief Less-portable utility functions for manipulating system time, for
54  * interoperation with platform APIs.
55  */
56 
57 
58 /*!
59  * @brief Sleep the given number of nanoseconds.
60  * @ingroup aux_os_time
61  */
62 static inline void
63 os_nanosleep(long nsec)
64 {
65 #ifdef XRT_OS_LINUX
66  struct timespec spec = {
67  .tv_sec = 0,
68  .tv_nsec = nsec,
69  };
70  nanosleep(&spec, NULL);
71 #endif
72 }
73 
74 #ifdef XRT_HAVE_TIMESPEC
75 /*!
76  * @brief Convert a timespec struct to nanoseconds.
77  * @ingroup aux_os_time_extra
78  */
79 static inline uint64_t
80 os_timespec_to_ns(struct timespec *spec)
81 {
82  uint64_t ns = 0;
83  ns += (uint64_t)spec->tv_sec * 1000 * 1000 * 1000;
84  ns += (uint64_t)spec->tv_nsec;
85  return ns;
86 }
87 #endif // XRT_HAVE_TIMESPEC
88 
89 
90 #ifdef XRT_HAVE_TIMEVAL
91 /*!
92  * @brief Convert a timeval struct to nanoseconds.
93  * @ingroup aux_os_time_extra
94  */
95 static inline uint64_t
96 os_timeval_to_ns(struct timeval *val)
97 {
98  uint64_t ns = 0;
99  ns += (uint64_t)val->tv_sec * 1000 * 1000 * 1000;
100  ns += (uint64_t)val->tv_usec * 1000;
101  return ns;
102 }
103 #endif // XRT_HAVE_TIMEVAL
104 
105 
106 /*!
107  * @brief Return a monotonic clock in nanoseconds.
108  * @ingroup aux_os_time
109  */
110 static inline uint64_t
111 os_monotonic_get_ns(void)
112 {
113 #ifdef XRT_OS_LINUX
114  struct timespec ts;
115  int ret = clock_gettime(CLOCK_MONOTONIC, &ts);
116  if (ret != 0) {
117  return 0;
118  }
119 
120  return os_timespec_to_ns(&ts);
121 #endif
122 }
123 
124 
125 #ifdef __cplusplus
126 }
127 #endif
Auto detect OS and certain features.
Header holding common defines.