Monado OpenXR Runtime
u_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 Time-keeping: a clock that is steady, convertible to system time, and
6  * ideally high-resolution.
7  *
8  * Designed to suit the needs of OpenXR: you can and should use something
9  * simpler (like @ref aux_os_time) for most purposes that aren't in OpenXR
10  * interface code.
11  *
12  * @author Ryan Pavlik <ryan.pavlik@collabora.com>
13  * @ingroup aux_util
14  *
15  * @see time_state
16  */
17 
18 #pragma once
19 
20 #include <stdint.h>
21 #include <time.h>
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /*!
28  * Integer timestamp type.
29  *
30  * @see time_state
31  * @see time_duration_ns
32  * @ingroup aux_util
33  */
34 typedef int64_t timepoint_ns;
35 
36 /*!
37  * Integer duration type in nanoseconds.
38  *
39  * Logical type of timepoint differences.
40  *
41  * @see time_state
42  * @see timepoint_ns
43  * @ingroup aux_util
44  */
45 typedef int64_t time_duration_ns;
46 
47 /*!
48  * Convert nanoseconds duration to float seconds.
49  *
50  * @see timepoint_ns
51  * @ingroup aux_util
52  */
53 static inline float
54 time_ns_to_s(time_duration_ns ns)
55 {
56  return (float)(ns) / 1000000000.0f;
57 }
58 
59 /*!
60  * Convert float seconds to nanoseconds.
61  *
62  * @see timepoint_ns
63  * @ingroup aux_util
64  */
65 static inline time_duration_ns
66 time_s_to_ns(float duration)
67 {
68  return (time_duration_ns)(duration * 1000000000.0f);
69 }
70 
71 /*!
72  * @struct time_state util/u_time.h
73  * @brief Time-keeping state structure.
74  *
75  * Exposed as an opaque pointer.
76  *
77  * @see timepoint_ns
78  * @ingroup aux_util
79  */
80 struct time_state;
81 
82 /*!
83  * Create a struct time_state.
84  *
85  * @public @memberof time_state
86  * @ingroup aux_util
87  */
88 struct time_state *
90 
91 
92 /*!
93  * Destroy a struct time_state.
94  *
95  * Should not be called simultaneously with any other time_state function.
96  *
97  * @public @memberof time_state
98  * @ingroup aux_util
99  */
100 void
101 time_state_destroy(struct time_state **state);
102 
103 /*!
104  * Get the current time as an integer timestamp.
105  *
106  * Does not update internal state for timekeeping.
107  * Should not be called simultaneously with time_state_get_now_and_update.
108  *
109  * @public @memberof time_state
110  * @ingroup aux_util
111  */
112 timepoint_ns
113 time_state_get_now(struct time_state const *state);
114 
115 /*!
116  * Get the current time as an integer timestamp and update internal state.
117  *
118  * This should be called regularly, but only from one thread.
119  * It updates the association between the timing sources.
120  *
121  * Should not be called simultaneously with any other time_state function.
122  *
123  * @public @memberof time_state
124  * @ingroup aux_util
125  */
126 timepoint_ns
128 
129 /*!
130  * Convert an integer timestamp to a struct timespec (system time).
131  *
132  * Should not be called simultaneously with time_state_get_now_and_update.
133  *
134  * @public @memberof time_state
135  * @ingroup aux_util
136  */
137 void
138 time_state_to_timespec(struct time_state const *state,
139  timepoint_ns timestamp,
140  struct timespec *out);
141 
142 /*!
143  * Convert a struct timespec (system time) to an integer timestamp.
144  *
145  * Should not be called simultaneously with time_state_get_now_and_update.
146  *
147  * @public @memberof time_state
148  * @ingroup aux_util
149  */
150 timepoint_ns
151 time_state_from_timespec(struct time_state const *state,
152  const struct timespec *timespecTime);
153 
154 /*!
155  * Convert a monotonic system time (such as from @ref aux_os_time) to an
156  * adjusted integer timestamp.
157  *
158  * Adjustments may need to be applied to achieve the other guarantees that e.g.
159  * CLOCK_MONOTONIC does not provide: this function performs those adjustments.
160  *
161  * Should not be called simultaneously with time_state_get_now_and_update.
162  *
163  * @public @memberof time_state
164  * @ingroup aux_util
165  */
166 timepoint_ns
167 time_state_from_monotonic_ns(struct time_state const *state,
168  uint64_t monotonic_ns);
169 #ifdef __cplusplus
170 }
171 #endif
timepoint_ns time_state_get_now(struct time_state const *state)
Get the current time as an integer timestamp.
Definition: u_time.cpp:83
timepoint_ns time_state_get_now_and_update(struct time_state *state)
Get the current time as an integer timestamp and update internal state.
Definition: u_time.cpp:92
int64_t time_duration_ns
Integer duration type in nanoseconds.
Definition: u_time.h:45
uint32_t timestamp
Definition: vive_protocol.h:211
int64_t timepoint_ns
Integer timestamp type.
Definition: u_time.h:34
void time_state_to_timespec(struct time_state const *state, timepoint_ns timestamp, struct timespec *out)
Convert an integer timestamp to a struct timespec (system time).
Definition: u_time.cpp:107
void time_state_destroy(struct time_state **state)
Destroy a struct time_state.
Definition: u_time.cpp:70
Time-keeping state structure.
Definition: u_time.cpp:46
timepoint_ns time_state_from_monotonic_ns(struct time_state const *state, uint64_t monotonic_ns)
Convert a monotonic system time (such as from Portable Timekeeping) to an adjusted integer timestamp...
Definition: u_time.cpp:157
uint16_t duration
Definition: vive_protocol.h:210
struct time_state * time_state_create()
Create a struct time_state.
Definition: u_time.cpp:62
timepoint_ns time_state_from_timespec(struct time_state const *state, const struct timespec *timespecTime)
Convert a struct timespec (system time) to an integer timestamp.
Definition: u_time.cpp:132