diff options
author | Oscar Lim <olim@ucla.edu> | 2016-05-07 14:37:25 -0700 |
---|---|---|
committer | Oscar Lim <olim@ucla.edu> | 2016-05-08 16:01:40 -0700 |
commit | 8c1a0fca53bf11f2b7ce708c8c5bdb05e5a55701 (patch) | |
tree | 9624af38c4ba35de244da2281e08049589140e20 /src/time_osx.h | |
parent | 040d7066649ddfcb72424b1cb70d6ad00ea84a21 (diff) | |
download | luasystem-8c1a0fca53bf11f2b7ce708c8c5bdb05e5a55701.tar.gz luasystem-8c1a0fca53bf11f2b7ce708c8c5bdb05e5a55701.tar.bz2 luasystem-8c1a0fca53bf11f2b7ce708c8c5bdb05e5a55701.zip |
Support for monotime
Provide `monotime` function with at least 1 millisecond resolution.
Diffstat (limited to 'src/time_osx.h')
-rw-r--r-- | src/time_osx.h | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/src/time_osx.h b/src/time_osx.h new file mode 100644 index 0000000..3308318 --- /dev/null +++ b/src/time_osx.h | |||
@@ -0,0 +1,66 @@ | |||
1 | #ifndef TIME_OSX_H | ||
2 | #define TIME_OSX_H | ||
3 | |||
4 | /* | ||
5 | * clock_gettime() | ||
6 | * | ||
7 | * OS X doesn't implement the clock_gettime() POSIX interface, but does | ||
8 | * provide a monotonic clock through mach_absolute_time(). On i386 and | ||
9 | * x86_64 architectures this clock is in nanosecond units, but not so on | ||
10 | * other devices. mach_timebase_info() provides the conversion parameters. | ||
11 | * | ||
12 | */ | ||
13 | |||
14 | #include <time.h> /* struct timespec */ | ||
15 | #include <errno.h> /* errno EINVAL */ | ||
16 | #include <sys/time.h> /* TIMEVAL_TO_TIMESPEC struct timeval gettimeofday(3) */ | ||
17 | #include <mach/mach_time.h> /* mach_timebase_info_data_t mach_timebase_info() mach_absolute_time() */ | ||
18 | |||
19 | |||
20 | #define CLOCK_REALTIME 0 | ||
21 | #define CLOCK_VIRTUAL 1 | ||
22 | #define CLOCK_PROF 2 | ||
23 | #define CLOCK_MONOTONIC 3 | ||
24 | |||
25 | static mach_timebase_info_data_t clock_timebase = { | ||
26 | .numer = 1, .denom = 1, | ||
27 | }; /* clock_timebase */ | ||
28 | |||
29 | void clock_gettime_init(void) __attribute__((constructor)); | ||
30 | |||
31 | void clock_gettime_init(void) { | ||
32 | if (mach_timebase_info(&clock_timebase) != KERN_SUCCESS) | ||
33 | __builtin_abort(); | ||
34 | } /* clock_gettime_init() */ | ||
35 | |||
36 | static int clock_gettime(int clockid, struct timespec *ts) { | ||
37 | switch (clockid) { | ||
38 | case CLOCK_REALTIME: { | ||
39 | struct timeval tv; | ||
40 | |||
41 | if (0 != gettimeofday(&tv, 0)) | ||
42 | return -1; | ||
43 | |||
44 | TIMEVAL_TO_TIMESPEC(&tv, ts); | ||
45 | |||
46 | return 0; | ||
47 | } | ||
48 | case CLOCK_MONOTONIC: { | ||
49 | unsigned long long abt; | ||
50 | |||
51 | abt = mach_absolute_time(); | ||
52 | abt = abt * clock_timebase.numer / clock_timebase.denom; | ||
53 | |||
54 | ts->tv_sec = abt / 1000000000UL; | ||
55 | ts->tv_nsec = abt % 1000000000UL; | ||
56 | |||
57 | return 0; | ||
58 | } | ||
59 | default: | ||
60 | errno = EINVAL; | ||
61 | |||
62 | return -1; | ||
63 | } /* switch() */ | ||
64 | } /* clock_gettime() */ | ||
65 | |||
66 | #endif /* TIME_OSX_H */ | ||