Robot Control Library
time.h
Go to the documentation of this file.
1 /**
2  * @headerfile time.h <rc/time.h>
3  *
4  * @brief sleep and timing functions
5  *
6  * All functions are POSIX compliant and should work on any linux system.
7  *
8  * @author James Strawson
9  * @date 1/31/2018
10  *
11  * @addtogroup time
12  * @{
13  */
14 
15 #ifndef RC_TIME_H
16 #define RC_TIME_H
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 #include <stdint.h>
23 #include <time.h> // for timespec
24 #include <sys/time.h> // for timeval
25 
26 typedef struct timespec timespec;
27 typedef struct timeval timeval;
28 
29 /**
30  * @brief Sleep in nanoseconds
31  *
32  * A wrapper for the normal UNIX nanosleep function which takes a number of
33  * nanoseconds instead of a timeval struct. This also handles restarting
34  * nanosleep with the remaining time in the event that nanosleep is interrupted
35  * by a signal. There is no upper limit on the time requested.
36  *
37  * @param[in] ns nanoseconds to sleep
38  */
39 void rc_nanosleep(uint64_t ns);
40 
41 
42 /**
43  * @brief Sleep in microseconds
44  *
45  * The traditional usleep function, however common, is deprecated in linux as it
46  * uses SIGALARM which interferes with alarm and timer functions. This uses the
47  * new POSIX standard nanosleep to accomplish the same thing which further
48  * supports sleeping for lengths longer than 1 second. This also handles
49  * restarting nanosleep with the remaining time in the event that nanosleep is
50  * interrupted by a signal. There is no upper limit on the time requested.
51  *
52  * @param[in] us microseconds to sleep
53  */
54 void rc_usleep(unsigned int us);
55 
56 
57 /**
58  * @brief Returns the number of nanoseconds since epoch using system
59  * CLOCK_REALTIME
60  *
61  * This function itself takes about 1100ns to complete on a beaglebone at 1ghz
62  * under ideal circumstances.
63  *
64  * @return nanoseconds since epoch
65  */
66 uint64_t rc_nanos_since_epoch(void);
67 
68 
69 /**
70  * @brief Returns the number of nanoseconds since system boot using
71  * CLOCK_MONOTONIC
72  *
73  * This function itself takes about 1100ns to complete on a bealgebone at 1ghz
74  * under ideal circumstances.
75  *
76  * @return nanoseconds since system boot
77  */
78 uint64_t rc_nanos_since_boot(void);
79 
80 
81 /**
82  * @brief Returns the number of nanoseconds from when when the calling
83  * thread was started in CPU time.
84  *
85  * This uses CLOCK_THREAD_CPUTIME_ID. This time only increments when the
86  * processor is working on the calling thread and not when the thread is
87  * sleeping. This is usually for timing how long blocks of user-code take to
88  * execute. This function itself takes about 2100ns to complete on a beaglebone
89  * at 1ghz under ideal circumstances.
90  *
91  * @return nanoseconds of CPU time since thread started
92  */
93 uint64_t rc_nanos_thread_time(void);
94 
95 
96 /**
97  * @brief Returns a number of microseconds corresponding to a timespec
98  * struct.
99  *
100  * Useful because timespec structs are annoying.
101  *
102  * @param[in] ts timespec struct to convert
103  *
104  * @return time in microseconds
105  */
106 uint64_t rc_timespec_to_micros(timespec ts);
107 
108 
109 /**
110  * @brief Returns a number of milliseconds corresponding to a timespec
111  * struct.
112  *
113  * Useful because timespec structs are annoying.
114  *
115  * @param[in] ts timespec struct to convert
116  *
117  * @return time in milliseconds
118  */
119 uint64_t rc_timespec_to_millis(timespec ts);
120 
121 
122 /**
123  * @brief Returns a number of microseconds corresponding to a timeval
124  * struct.
125  *
126  * Useful because timeval structs are annoying.
127  *
128  * @param[in] tv timeval struct to convert
129  *
130  * @return time in microseconds
131  */
132 uint64_t rc_timeval_to_micros(timeval tv);
133 
134 
135 /**
136  * @brief Returns a number of milliseconds corresponding to a timeval
137  * struct.
138  *
139  * Useful because timespec structs are annoying.
140  *
141  * @param[in] tv timeval struct to convert
142  *
143  * @return time in microseconds
144  */
145 uint64_t rc_timeval_to_millis(timeval tv);
146 
147 
148 /**
149  * @brief Returns the time difference between two timespec structs as
150  * another timespec.
151  *
152  *
153  * Convenient for use with nanosleep() function and accurately timed loops.
154  * Unlike timespec_sub defined in time.h, rc_timespec_diff does not care which
155  * came first, A or B. A positive difference in time is always returned.
156  *
157  * @param[in] A timespec struct
158  * @param[in] B timespec struct
159  *
160  * @return timespec struct of the difference, always positive
161  */
163 
164 
165 /**
166  * @brief Adds an amount of time in seconds to a timespec struct.
167  *
168  * The time added is a double-precision floating point value to make
169  * respresenting fractions of a second easier. The timespec is passed as a
170  * pointer so it can be modified in place. Seconds may be negative.
171  *
172  * @param start The start timspec, modified in place
173  * @param[in] seconds The seconds
174  */
175 void rc_timespec_add(timespec* start, double seconds);
176 
177 
178 #ifdef __cplusplus
179 }
180 #endif
181 
182 #endif // RC_TIME_H
183 
184 /** @} end group time*/
void rc_nanosleep(uint64_t ns)
Sleep in nanoseconds.
uint64_t rc_nanos_since_epoch(void)
Returns the number of nanoseconds since epoch using system CLOCK_REALTIME.
struct timespec timespec
Definition: time.h:26
struct timeval timeval
Definition: time.h:27
uint64_t rc_timespec_to_millis(timespec ts)
Returns a number of milliseconds corresponding to a timespec struct.
void rc_usleep(unsigned int us)
Sleep in microseconds.
uint64_t rc_nanos_thread_time(void)
Returns the number of nanoseconds from when when the calling thread was started in CPU time...
timespec rc_timespec_diff(timespec A, timespec B)
Returns the time difference between two timespec structs as another timespec.
uint64_t rc_nanos_since_boot(void)
Returns the number of nanoseconds since system boot using CLOCK_MONOTONIC.
uint64_t rc_timeval_to_micros(timeval tv)
Returns a number of microseconds corresponding to a timeval struct.
uint64_t rc_timespec_to_micros(timespec ts)
Returns a number of microseconds corresponding to a timespec struct.
uint64_t rc_timeval_to_millis(timeval tv)
Returns a number of milliseconds corresponding to a timeval struct.
void rc_timespec_add(timespec *start, double seconds)
Adds an amount of time in seconds to a timespec struct.