Robot Control Library
|
sleep and timing functions
All functions are POSIX compliant and should work on any linux system.
Typedefs | |
typedef struct timespec | timespec |
typedef struct timeval | timeval |
Functions | |
void | rc_nanosleep (uint64_t ns) |
Sleep in nanoseconds. More... | |
void | rc_usleep (unsigned int us) |
Sleep in microseconds. More... | |
uint64_t | rc_nanos_since_epoch (void) |
Returns the number of nanoseconds since epoch using system CLOCK_REALTIME. More... | |
uint64_t | rc_nanos_since_boot (void) |
Returns the number of nanoseconds since system boot using CLOCK_MONOTONIC. More... | |
uint64_t | rc_nanos_thread_time (void) |
Returns the number of nanoseconds from when when the calling thread was started in CPU time. More... | |
uint64_t | rc_timespec_to_micros (timespec ts) |
Returns a number of microseconds corresponding to a timespec struct. More... | |
uint64_t | rc_timespec_to_millis (timespec ts) |
Returns a number of milliseconds corresponding to a timespec struct. More... | |
uint64_t | rc_timeval_to_micros (timeval tv) |
Returns a number of microseconds corresponding to a timeval struct. More... | |
uint64_t | rc_timeval_to_millis (timeval tv) |
Returns a number of milliseconds corresponding to a timeval struct. More... | |
timespec | rc_timespec_diff (timespec A, timespec B) |
Returns the time difference between two timespec structs as another timespec. More... | |
void | rc_timespec_add (timespec *start, double seconds) |
Adds an amount of time in seconds to a timespec struct. More... | |
void rc_nanosleep | ( | uint64_t | ns | ) |
Sleep in nanoseconds.
A wrapper for the normal UNIX nanosleep function which takes a number of nanoseconds instead of a timeval struct. This also handles restarting nanosleep with the remaining time in the event that nanosleep is interrupted by a signal. There is no upper limit on the time requested.
[in] | ns | nanoseconds to sleep |
void rc_usleep | ( | unsigned int | us | ) |
Sleep in microseconds.
The traditional usleep function, however common, is deprecated in linux as it uses SIGALARM which interferes with alarm and timer functions. This uses the new POSIX standard nanosleep to accomplish the same thing which further supports sleeping for lengths longer than 1 second. This also handles restarting nanosleep with the remaining time in the event that nanosleep is interrupted by a signal. There is no upper limit on the time requested.
[in] | us | microseconds to sleep |
uint64_t rc_nanos_since_epoch | ( | void | ) |
Returns the number of nanoseconds since epoch using system CLOCK_REALTIME.
This function itself takes about 1100ns to complete on a beaglebone at 1ghz under ideal circumstances.
uint64_t rc_nanos_since_boot | ( | void | ) |
Returns the number of nanoseconds since system boot using CLOCK_MONOTONIC.
This function itself takes about 1100ns to complete on a bealgebone at 1ghz under ideal circumstances.
uint64_t rc_nanos_thread_time | ( | void | ) |
Returns the number of nanoseconds from when when the calling thread was started in CPU time.
This uses CLOCK_THREAD_CPUTIME_ID. This time only increments when the processor is working on the calling thread and not when the thread is sleeping. This is usually for timing how long blocks of user-code take to execute. This function itself takes about 2100ns to complete on a beaglebone at 1ghz under ideal circumstances.
uint64_t rc_timespec_to_micros | ( | timespec | ts | ) |
Returns a number of microseconds corresponding to a timespec struct.
Useful because timespec structs are annoying.
[in] | ts | timespec struct to convert |
uint64_t rc_timespec_to_millis | ( | timespec | ts | ) |
Returns a number of milliseconds corresponding to a timespec struct.
Useful because timespec structs are annoying.
[in] | ts | timespec struct to convert |
uint64_t rc_timeval_to_micros | ( | timeval | tv | ) |
Returns a number of microseconds corresponding to a timeval struct.
Useful because timeval structs are annoying.
[in] | tv | timeval struct to convert |
uint64_t rc_timeval_to_millis | ( | timeval | tv | ) |
Returns a number of milliseconds corresponding to a timeval struct.
Useful because timespec structs are annoying.
[in] | tv | timeval struct to convert |
Returns the time difference between two timespec structs as another timespec.
Convenient for use with nanosleep() function and accurately timed loops. Unlike timespec_sub defined in time.h, rc_timespec_diff does not care which came first, A or B. A positive difference in time is always returned.
[in] | A | timespec struct |
[in] | B | timespec struct |
void rc_timespec_add | ( | timespec * | start, |
double | seconds | ||
) |
Adds an amount of time in seconds to a timespec struct.
The time added is a double-precision floating point value to make respresenting fractions of a second easier. The timespec is passed as a pointer so it can be modified in place. Seconds may be negative.
start | The start timspec, modified in place | |
[in] | seconds | The seconds |