]> git.sesse.net Git - vlc/blobdiff - src/misc/pthread.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / src / misc / pthread.c
index ca422cf69766f97eb341b5308137626d78c2d8fd..ebbad780d4c8216759e6d1791a763b9da81a3f84 100644 (file)
@@ -48,6 +48,7 @@
 
 #ifdef __APPLE__
 # include <sys/time.h> /* gettimeofday in vlc_cond_timedwait */
+# include <mach/mach_init.h> /* mach_task_self in semaphores */
 #endif
 
 /**
@@ -379,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
 }
 
 /**
@@ -402,8 +409,13 @@ int vlc_cond_timedwait (vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex,
  */
 void vlc_sem_init (vlc_sem_t *sem, unsigned value)
 {
+#if defined(__APPLE__)
+    if (unlikely(semaphore_create(mach_task_self(), sem, SYNC_POLICY_FIFO, value) != KERN_SUCCESS))
+        abort ();
+#else
     if (unlikely(sem_init (sem, 0, value)))
         abort ();
+#endif
 }
 
 /**
@@ -411,10 +423,20 @@ void vlc_sem_init (vlc_sem_t *sem, unsigned value)
  */
 void vlc_sem_destroy (vlc_sem_t *sem)
 {
+    int val;
+
+#if defined(__APPLE__)
+    if (likely(semaphore_destroy(mach_task_self(), *sem) == KERN_SUCCESS))
+        return;
+
+    val = EINVAL;
+#else
     if (likely(sem_destroy (sem) == 0))
         return;
 
-    int val = errno;
+    val = errno;
+#endif
+
     VLC_THREAD_ASSERT ("destroying semaphore");
 }
 
@@ -424,10 +446,20 @@ void vlc_sem_destroy (vlc_sem_t *sem)
  */
 int vlc_sem_post (vlc_sem_t *sem)
 {
+    int val;
+
+#if defined(__APPLE__)
+    if (likely(semaphore_signal(*sem) == KERN_SUCCESS))
+        return 0;
+
+    val = EINVAL;
+#else
     if (likely(sem_post (sem) == 0))
         return 0;
 
-    int val = errno;
+    val = errno;
+#endif
+
     if (unlikely(val != EOVERFLOW))
         VLC_THREAD_ASSERT ("unlocking semaphore");
     return val;
@@ -441,10 +473,17 @@ void vlc_sem_wait (vlc_sem_t *sem)
 {
     int val;
 
+#if defined(__APPLE__)
+    if (likely(semaphore_wait(*sem) == KERN_SUCCESS))
+        return;
+
+    val = EINVAL;
+#else
     do
         if (likely(sem_wait (sem) == 0))
             return;
     while ((val = errno) == EINTR);
+#endif
 
     VLC_THREAD_ASSERT ("locking semaphore");
 }
@@ -655,6 +694,9 @@ int vlc_clone (vlc_thread_t *p_handle, void * (*entry) (void *), void *data,
 void vlc_cancel (vlc_thread_t thread_id)
 {
     pthread_cancel (thread_id);
+#ifdef HAVE_MAEMO
+    pthread_kill (thread_id, SIGRTMIN);
+#endif
 }
 
 /**