1 | /** Allows to build on MacOS X |
2 | * |
3 | * Highly experimental, not recommended, disabled by default. |
4 | * |
5 | * To use, include this file with -include compiler parameter. |
6 | */ |
7 | |
8 | #include <port/clock.h> |
9 | |
10 | #ifdef __APPLE__ |
11 | #if !APPLE_HAVE_CLOCK_GETTIME |
12 | |
13 | #include <time.h> |
14 | #include <stdlib.h> |
15 | #include <mach/mach_init.h> |
16 | #include <mach/thread_act.h> |
17 | #include <mach/mach_port.h> |
18 | #include <sys/time.h> |
19 | |
20 | |
21 | int clock_gettime_thread(timespec *spec) { |
22 | thread_port_t thread = mach_thread_self(); |
23 | |
24 | mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT; |
25 | thread_basic_info_data_t info; |
26 | if (KERN_SUCCESS != thread_info(thread, THREAD_BASIC_INFO, reinterpret_cast<thread_info_t>(&info), &count)) |
27 | return -1; |
28 | |
29 | spec->tv_sec = info.user_time.seconds + info.system_time.seconds; |
30 | spec->tv_nsec = info.user_time.microseconds * 1000 + info.system_time.microseconds * 1000; |
31 | mach_port_deallocate(mach_task_self(), thread); |
32 | |
33 | return 0; |
34 | } |
35 | |
36 | int clock_gettime(int clk_id, struct timespec* t) { |
37 | if (clk_id == CLOCK_THREAD_CPUTIME_ID) |
38 | return clock_gettime_thread(t); |
39 | |
40 | struct timeval now; |
41 | int rv = gettimeofday(&now, NULL); |
42 | |
43 | if (rv) |
44 | return rv; |
45 | t->tv_sec = now.tv_sec; |
46 | t->tv_nsec = now.tv_usec * 1000; |
47 | |
48 | return 0; |
49 | } |
50 | |
51 | #endif |
52 | #endif |
53 | |