-
Notifications
You must be signed in to change notification settings - Fork 1
/
macosx.c
67 lines (60 loc) · 1.35 KB
/
macosx.c
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* Synopsis:
* Mach OS stub function to emulate Posix clock_gettime().
* Blame:
* Note:
* Would be nice to remove macosx.[ch].
* See this page for alternative method
*
* http://stackoverflow.com/questions/5167269/clock-gettime-alternative-in-mac-os-x
*/
#if __APPLE__ == 1 && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101200
#pragma weak clock_gettime
#include <sys/time.h>
#include <sys/resource.h>
#include <mach/mach.h>
#include <mach/clock.h>
#include <mach/mach_time.h>
#include <errno.h>
#include <unistd.h>
#include <sched.h>
#include "macosx.h"
/*
* Synopsis:
* Emulate Posix clock_gettime() using Mach system routines.
*/
int clock_gettime(clockid_t id, struct timespec *tp)
{
clock_serv_t service_id;
clock_id_t mach_id;
mach_timespec_t now;
kern_return_t status;
switch (id) {
case CLOCK_REALTIME:
mach_id = CALENDAR_CLOCK;
break;
case CLOCK_MONOTONIC:
mach_id = SYSTEM_CLOCK;
break;
default:
errno = EINVAL;
return -1;
}
status = host_get_clock_service(mach_host_self(), mach_id, &service_id);
if (status != KERN_SUCCESS) {
errno = EINVAL;
return -1;
}
status = clock_get_time(service_id, &now);
if (status != KERN_SUCCESS) {
errno = EINVAL;
return -1;
}
tp->tv_sec = now.tv_sec;
tp->tv_nsec = now.tv_nsec;
return 0;
}
#endif // __APPLE__
/* EOF */