[vlc-devel] [PATCH] src: implement monotonic clock for osx
david.fuhrmann at gmail.com
david.fuhrmann at gmail.com
Tue Dec 10 18:10:55 CET 2013
From: David Fuhrmann <david.fuhrmann at googlemail.com>
mach_absolute_time seems to be the fastest (and preferred) way to access
the high precision timer. It counts mach time units since the last system
boot. mach_timebase_info provides a conversion factor to transform the value
into nano seconds.
fixes #10072
---
Hi,
This is a proposal for a monotonic clock using the mach kernel functions.
Regarding the conversion factor, there is no clear documentation
unfortunately. But according to the comment in [1], the factor is always
1 for intel processors (no idea about ios). This means that
mach_absolute_time directly outputs nano seconds. But the documentation in
[2], closest to the official one, states that this conversion is needed,
so it might be sensible to keep it.
Any comments?
Regards,
David
[1] http://www.opensource.apple.com/source/openmpi/openmpi-5/openmpi/opal/mca/timer/darwin/timer_darwin_component.c
[2] https://developer.apple.com/library/mac/qa/qa1398/_index.html
src/posix/thread.c | 23 +++++++++++++++++++++++
1 file changed, 23 insertions(+)
diff --git a/src/posix/thread.c b/src/posix/thread.c
index 514f592..c7e3dfc 100644
--- a/src/posix/thread.c
+++ b/src/posix/thread.c
@@ -51,6 +51,7 @@
#endif
#ifdef __APPLE__
# include <mach/mach_init.h> /* mach_task_self in semaphores */
+# include <mach/mach_time.h>
#endif
#if defined(__SunOS)
# include <sys/processor.h>
@@ -105,6 +106,22 @@ static pthread_once_t vlc_clock_once = PTHREAD_ONCE_INIT;
# define vlc_clock_setup() \
pthread_once(&vlc_clock_once, vlc_clock_setup_once)
+
+#elif defined __APPLE__
+
+static mach_timebase_info_data_t vlc_clock_conversion_factor;
+
+static void vlc_clock_setup_once (void)
+{
+ if (unlikely(mach_timebase_info (&vlc_clock_conversion_factor) != 0))
+ abort ();
+}
+
+static pthread_once_t vlc_clock_once = PTHREAD_ONCE_INIT;
+
+# define vlc_clock_setup() \
+ pthread_once(&vlc_clock_once, vlc_clock_setup_once)
+
#else /* _POSIX_TIMERS */
# include <sys/time.h> /* gettimeofday() */
@@ -912,6 +929,12 @@ mtime_t mdate (void)
return (INT64_C(1000000) * ts.tv_sec) + (ts.tv_nsec / 1000);
+#elif defined __APPLE__
+ vlc_clock_setup ();
+
+ uint64_t time = mach_absolute_time();
+ return (time * vlc_clock_conversion_factor.numer / vlc_clock_conversion_factor.denom) / 1000;
+
#else
struct timeval tv;
--
1.8.3.4 (Apple Git-47)
More information about the vlc-devel
mailing list