aboutsummaryrefslogtreecommitdiff
path: root/src/Platform/Posix/Timespec.h
blob: 95b79f2b1f43eae59227eaacbdc07181eeacb2e6 (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
39
#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>
#include <unistd.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))
#endif
    {
        struct timeval tv;

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

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

    /* 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 */