]> git.sesse.net Git - vlc/blobdiff - src/misc/pthread.c
Maemo: work-around segmentation fault when poll() unwinds
[vlc] / src / misc / pthread.c
index 4522db07e953e3ddece631de7a58c5d2c938d500..5385d7c0a08594b43ea54e783023690300eb0481 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
 
 /**
@@ -256,10 +257,10 @@ void vlc_mutex_unlock (vlc_mutex_t *p_mutex)
     VLC_THREAD_ASSERT ("unlocking mutex");
 }
 
-/*****************************************************************************
- * vlc_cond_init: initialize a condition variable
- *****************************************************************************/
-void vlc_cond_init( vlc_cond_t *p_condvar )
+/**
+ * Initializes a condition variable.
+ */
+void vlc_cond_init (vlc_cond_t *p_condvar)
 {
     pthread_condattr_t attr;
 
@@ -279,6 +280,17 @@ void vlc_cond_init( vlc_cond_t *p_condvar )
     pthread_condattr_destroy (&attr);
 }
 
+/**
+ * Initializes a condition variable.
+ * Contrary to vlc_cond_init(), the wall clock will be used as a reference for
+ * the vlc_cond_timedwait() time-out parameter.
+ */
+void vlc_cond_init_daytime (vlc_cond_t *p_condvar)
+{
+    if (unlikely(pthread_cond_init (p_condvar, NULL)))
+        abort ();
+}
+
 /**
  * Destroys a condition variable. No threads shall be waiting or signaling the
  * condition.
@@ -351,7 +363,11 @@ void vlc_cond_wait (vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex)
 
 /**
  * Waits for a condition variable up to a certain date.
- * This works like vlc_cond_wait(), except for the additional timeout.
+ * This works like vlc_cond_wait(), except for the additional time-out.
+ *
+ * If the variable was initialized with vlc_cond_init(), the timeout has the
+ * same arbitrary origin as mdate(). If the variable was initialized with
+ * vlc_cond_init_daytime(), the timeout is expressed from the Unix epoch.
  *
  * @param p_condvar condition variable to wait on
  * @param p_mutex mutex which is unlocked while waiting,
@@ -387,8 +403,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
 }
 
 /**
@@ -396,10 +417,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");
 }
 
@@ -409,10 +440,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;
@@ -426,10 +467,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");
 }
@@ -640,6 +688,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
 }
 
 /**