Răsfoiți Sursa

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

Daniel-Constantin Mierla 9 ani în urmă
părinte
comite
a46f13516a
2 a modificat fișierele cu 27 adăugiri și 0 ștergeri
  1. 3 0
      ser_time.h
  2. 24 0
      timer.c

+ 3 - 0
ser_time.h

@@ -37,4 +37,7 @@ time_t ser_time(time_t* t);
  * WARNING: ignores tz (it's obsolete anyway) */
 int ser_gettimeofday(struct timeval* tv, const struct timezone *tz);
 
+/* portable implementation for clock_gettime(CLOCK_REALTIME, ts) */
+int ser_clock_gettime(struct timespec *ts);
+
 #endif /* _ser_time_h */

+ 24 - 0
timer.c

@@ -37,6 +37,12 @@
 #include <stdlib.h> /* random, debugging only */
 #include "error.h"
 #include "signals.h"
+
+#ifdef __OS_darwin
+#include <mach/clock.h>
+#include <mach/mach.h>
+#endif
+
 /*
 #include "config.h"
 */
@@ -1153,4 +1159,22 @@ void slow_timer_main()
 
 }
 
+int ser_clock_gettime(struct timespec *ts)
+{
+#ifdef __OS_darwin
+	clock_serv_t cclock;
+	mach_timespec_t mts;
+
+	/* OS X does not have clock_gettime, use clock_get_time */
+	host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
+	clock_get_time(cclock, &mts);
+	mach_port_deallocate(mach_task_self(), cclock);
+	ts->tv_sec = mts.tv_sec;
+	ts->tv_nsec = mts.tv_nsec;
+	return 0;
+#else
+	return clock_gettime(CLOCK_REALTIME, ts);
+#endif
+}
+
 #endif