[vlc-commits] commit: pthread: Use pthread_cond_timedwait_relative_np() on Darwin. ( Pierre d'Herbemont )

git at videolan.org git at videolan.org
Sat Sep 25 23:23:59 CEST 2010


vlc | branch: master | Pierre d'Herbemont <pdherbemont at free.fr> | Sun Aug  1 17:15:10 2010 +0200| [6dc4e2605312381b4e8dd5eb4d8463075037dce0] | committer: Pierre d'Herbemont 

pthread: Use pthread_cond_timedwait_relative_np() on Darwin.

Switching to the non monotonic clock was still unsafe.
This fixes some sync issues on the iPad.

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=6dc4e2605312381b4e8dd5eb4d8463075037dce0
---

 src/misc/pthread.c |   24 +++++++++++++++---------
 1 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/src/misc/pthread.c b/src/misc/pthread.c
index 5385d7c..ebbad78 100644
--- a/src/misc/pthread.c
+++ b/src/misc/pthread.c
@@ -380,22 +380,28 @@ int vlc_cond_timedwait (vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex,
                         mtime_t deadline)
 {
 #if defined(__APPLE__) && !defined(__powerpc__) && !defined( __ppc__ ) && !defined( __ppc64__ )
-    /* mdate() is mac_absolute_time on OSX, which we must convert to do
-     * the same base than gettimeofday() which pthread_cond_timedwait
-     * relies on. */
-    mtime_t oldbase = mdate();
-    struct timeval tv;
-    gettimeofday(&tv, NULL);
-    mtime_t newbase = (mtime_t)tv.tv_sec * 1000000 + (mtime_t) tv.tv_usec;
-    deadline = deadline - oldbase + newbase;
-#endif
+    /* mdate() is the monotonic clock, timedwait origin is gettimeofday() which
+     * isn't monotonic. Use imedwait_relative_np() instead
+    */
+    mtime_t base = mdate();
+    deadline -= base;
+    if (deadline < 0)
+        deadline = 0;
     lldiv_t d = lldiv( deadline, CLOCK_FREQ );
     struct timespec ts = { d.quot, d.rem * (1000000000 / CLOCK_FREQ) };
 
+    int val = pthread_cond_timedwait_relative_np(p_condvar, p_mutex, &ts);
+    if (val != ETIMEDOUT)
+        VLC_THREAD_ASSERT ("timed-waiting on condition");
+    return val;
+#else
+    lldiv_t d = lldiv( deadline, CLOCK_FREQ );
+    struct timespec ts = { d.quot, d.rem * (1000000000 / CLOCK_FREQ) };
     int val = pthread_cond_timedwait (p_condvar, p_mutex, &ts);
     if (val != ETIMEDOUT)
         VLC_THREAD_ASSERT ("timed-waiting on condition");
     return val;
+#endif
 }
 
 /**



More information about the vlc-commits mailing list