mirror of
https://gitlab.freedesktop.org/monado/monado.git
synced 2025-01-01 04:36:07 +00:00
os/time: Add time getting and conversion functions
For Linux it is based on CLOCK_MONOTONIC.
This commit is contained in:
parent
039e29e7ad
commit
d375843b65
|
@ -15,6 +15,7 @@
|
|||
|
||||
#ifdef XRT_OS_LINUX
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
#else
|
||||
#error "No time support on non-Linux platforms yet."
|
||||
#endif
|
||||
|
@ -23,6 +24,7 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/*!
|
||||
* Write an output report to the given device.
|
||||
*/
|
||||
|
@ -38,6 +40,48 @@ os_nanosleep(long nsec)
|
|||
#endif
|
||||
}
|
||||
|
||||
#ifdef XRT_OS_LINUX
|
||||
/*!
|
||||
* Convert a timespec struct to nanoseconds.
|
||||
*/
|
||||
XRT_MAYBE_UNUSED static inline uint64_t
|
||||
os_timespec_to_ns(struct timespec *spec)
|
||||
{
|
||||
uint64_t ns = 0;
|
||||
ns += (uint64_t)spec->tv_sec * 1000 * 1000 * 1000;
|
||||
ns += (uint64_t)spec->tv_nsec;
|
||||
return ns;
|
||||
}
|
||||
|
||||
/*!
|
||||
* Convert a timeval struct to nanoseconds.
|
||||
*/
|
||||
XRT_MAYBE_UNUSED static inline uint64_t
|
||||
os_timeval_to_ns(struct timeval *val)
|
||||
{
|
||||
uint64_t ns = 0;
|
||||
ns += (uint64_t)val->tv_sec * 1000 * 1000 * 1000;
|
||||
ns += (uint64_t)val->tv_usec * 1000;
|
||||
return ns;
|
||||
}
|
||||
|
||||
/*!
|
||||
* Return a monotonic clock in nanoseconds.
|
||||
*/
|
||||
XRT_MAYBE_UNUSED static inline uint64_t
|
||||
os_monotonic_get_ns(void)
|
||||
{
|
||||
struct timespec ts;
|
||||
int ret = clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
if (ret != 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return os_timespec_to_ns(&ts);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue