Coverage Report

Created: 2017-04-15 07:07

/home/travis/build/MoarVM/MoarVM/src/platform/posix/time.c
Line
Count
Source (jump to first uncovered line)
1
#include "moar.h"
2
#include "platform/time.h"
3
4
#include <errno.h>
5
#include <sys/time.h>
6
7
#ifdef __MACH__
8
#include <mach/clock.h>
9
#include <mach/mach.h>
10
#endif
11
12
14.5k
#define E9 1000000000
13
0
#define E9F 1000000000.0f
14
15
MVMuint64 MVM_platform_now(void)
16
14.5k
{
17
14.5k
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
18
    clock_serv_t    cclock;
19
    mach_timespec_t ts;
20
    host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
21
    clock_get_time(cclock, &ts);
22
    mach_port_deallocate(mach_task_self(), cclock);
23
    return (MVMuint64)ts.tv_sec * E9 + ts.tv_nsec;
24
#else
25
14.5k
#  ifdef CLOCK_REALTIME
26
14.5k
    struct timespec ts;
27
14.5k
    if (clock_gettime(CLOCK_REALTIME, &ts) != 0)
28
0
        return 0;
29
14.5k
30
14.5k
    return (MVMuint64)ts.tv_sec * E9 + ts.tv_nsec;
31
14.5k
#  else
32
    struct timeval tv;
33
    if (gettimeofday(&tv, NULL) != 0)
34
        return 0;
35
36
    return (MVMuint64)tv.tv_sec * E9 + tv.tv_usec * 1000;
37
#  endif
38
14.5k
#endif
39
14.5k
}
40
41
void MVM_platform_sleep(MVMnum64 second)
42
0
{
43
0
    struct timespec timeout;
44
0
    timeout.tv_sec = (time_t)second;
45
0
    timeout.tv_nsec = (long)((second - timeout.tv_sec) * E9F);
46
0
    while (nanosleep(&timeout, &timeout) && errno == EINTR);
47
0
}
48
49
void MVM_platform_nanosleep(MVMuint64 nanos)
50
0
{
51
0
    struct timespec timeout;
52
0
    timeout.tv_sec = nanos / E9;
53
0
    timeout.tv_nsec = nanos % E9;
54
0
    while (nanosleep(&timeout, &timeout) && errno == EINTR);
55
0
}