Browse code

core: added alternative for clock_gettime() on mac os x

Daniel-Constantin Mierla authored on 05/05/2016 06:38:12
Showing 2 changed files
... ...
@@ -37,4 +37,7 @@ time_t ser_time(time_t* t);
37 37
  * WARNING: ignores tz (it's obsolete anyway) */
38 38
 int ser_gettimeofday(struct timeval* tv, const struct timezone *tz);
39 39
 
40
+/* portable implementation for clock_gettime(CLOCK_REALTIME, ts) */
41
+int ser_clock_gettime(struct timespec *ts);
42
+
40 43
 #endif /* _ser_time_h */
... ...
@@ -37,6 +37,12 @@
37 37
 #include <stdlib.h> /* random, debugging only */
38 38
 #include "error.h"
39 39
 #include "signals.h"
40
+
41
+#ifdef __OS_darwin
42
+#include <mach/clock.h>
43
+#include <mach/mach.h>
44
+#endif
45
+
40 46
 /*
41 47
 #include "config.h"
42 48
 */
... ...
@@ -1153,4 +1159,22 @@ void slow_timer_main()
1153 1153
 
1154 1154
 }
1155 1155
 
1156
+int ser_clock_gettime(struct timespec *ts)
1157
+{
1158
+#ifdef __OS_darwin
1159
+	clock_serv_t cclock;
1160
+	mach_timespec_t mts;
1161
+
1162
+	/* OS X does not have clock_gettime, use clock_get_time */
1163
+	host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
1164
+	clock_get_time(cclock, &mts);
1165
+	mach_port_deallocate(mach_task_self(), cclock);
1166
+	ts->tv_sec = mts.tv_sec;
1167
+	ts->tv_nsec = mts.tv_nsec;
1168
+	return 0;
1169
+#else
1170
+	return clock_gettime(CLOCK_REALTIME, ts);
1171
+#endif
1172
+}
1173
+
1156 1174
 #endif