aboutsummaryrefslogtreecommitdiff
path: root/src/Platform/Posix/Timespec.h
blob: 2ed157a4de50979cc7f8458d7b869d62fe1e97eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#ifndef BH_PLATFORM_POSIX_TIMESPEC_H
#define BH_PLATFORM_POSIX_TIMESPEC_H


#include <Config.h>
#include <BH/Thread.h>
#include <sys/time.h>
#include <time.h>


static int convertToTimespec(struct timespec *ts,
                             uint32_t timeout)
{
#if (_POSIX_TIMERS > 0) || defined(BH_USE_CLOCK_GETTIME)
    if (clock_gettime(CLOCK_REALTIME, ts))
        return BH_ERROR;
#else
    struct timeval tv;

    if (gettimeofday(&tv, NULL))
        return BH_ERROR;

    ts->tv_sec = tv.tv_sec;
    ts->tv_nsec = tv.tv_usec * 1000;
#endif

    /* Calculate absoulute time for timed wait */
    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 BH_OK;
}

#endif /* BH_PLATFORM_POSIX_TIMESPEC_H */