33 lines
680 B
C
33 lines
680 B
C
#ifndef CGE_THREAD_TIMESPEC_H
|
|
#define CGE_THREAD_TIMESPEC_H
|
|
|
|
#include <sys/time.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
static int convertToTimespec(struct timespec *ts, uint32_t timeout) {
|
|
#if (_POSIX_TIMERS > 0)
|
|
if (clock_gettime(CLOCK_REALTIME, ts))
|
|
#endif
|
|
{
|
|
struct timeval tv;
|
|
|
|
if (gettimeofday(&tv, NULL))
|
|
return 0;
|
|
|
|
ts->tv_sec = tv.tv_sec;
|
|
ts->tv_nsec = tv.tv_usec * 1000;
|
|
}
|
|
|
|
ts->tv_sec += timeout / 1000;
|
|
ts->tv_nsec += (timeout % 1000) * 1000000;
|
|
|
|
while (ts->tv_nsec >= 1000000000) {
|
|
ts->tv_nsec -= 1000000000;
|
|
ts->tv_sec += 1;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
#endif /* CGE_THREAD_TIMESPEC_H */
|