/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.9k | #define E9 1000000000 |
13 | 6 | #define E9F 1000000000.0f |
14 | | |
15 | | MVMuint64 MVM_platform_now(void) |
16 | 14.9k | { |
17 | 14.9k | #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.9k | # ifdef CLOCK_REALTIME |
26 | 14.9k | struct timespec ts; |
27 | 14.9k | if (clock_gettime(CLOCK_REALTIME, &ts) != 0) |
28 | 0 | return 0; |
29 | 14.9k | |
30 | 14.9k | return (MVMuint64)ts.tv_sec * E9 + ts.tv_nsec; |
31 | 14.9k | # 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.9k | #endif |
39 | 14.9k | } |
40 | | |
41 | | void MVM_platform_sleep(MVMnum64 second) |
42 | 6 | { |
43 | 6 | struct timespec timeout; |
44 | 6 | timeout.tv_sec = (time_t)second; |
45 | 6 | timeout.tv_nsec = (long)((second - timeout.tv_sec) * E9F); |
46 | 6 | while (nanosleep(&timeout, &timeout) && errno == EINTR); |
47 | 6 | } |
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 | } |