]> git.sesse.net Git - vlc/blobdiff - src/misc/pthread.c
Support for Windows Media Voice natively
[vlc] / src / misc / pthread.c
index ac45a9961387a89f9bbc1b6f655cb5d81fb42937..ca422cf69766f97eb341b5308137626d78c2d8fd 100644 (file)
@@ -143,7 +143,7 @@ void vlc_mutex_init( vlc_mutex_t *p_mutex )
 {
     pthread_mutexattr_t attr;
 
-    if( pthread_mutexattr_init( &attr ) )
+    if (unlikely(pthread_mutexattr_init (&attr)))
         abort();
 #ifdef NDEBUG
     pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_NORMAL );
@@ -155,7 +155,7 @@ void vlc_mutex_init( vlc_mutex_t *p_mutex )
     pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_ERRORCHECK );
 # endif
 #endif
-    if( pthread_mutex_init( p_mutex, &attr ) )
+    if (unlikely(pthread_mutex_init (p_mutex, &attr)))
         abort();
     pthread_mutexattr_destroy( &attr );
 }
@@ -167,13 +167,14 @@ void vlc_mutex_init_recursive( vlc_mutex_t *p_mutex )
 {
     pthread_mutexattr_t attr;
 
-    pthread_mutexattr_init( &attr );
+    if (unlikely(pthread_mutexattr_init (&attr)))
+        abort();
 #if defined (__GLIBC__) && (__GLIBC__ == 2) && (__GLIBC_MINOR__ < 6)
     pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_RECURSIVE_NP );
 #else
     pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_RECURSIVE );
 #endif
-    if( pthread_mutex_init( p_mutex, &attr ) )
+    if (unlikely(pthread_mutex_init (p_mutex, &attr)))
         abort();
     pthread_mutexattr_destroy( &attr );
 }
@@ -255,14 +256,14 @@ 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;
 
-    if (pthread_condattr_init (&attr))
+    if (unlikely(pthread_condattr_init (&attr)))
         abort ();
 #if !defined (_POSIX_CLOCK_SELECTION)
    /* Fairly outdated POSIX support (that was defined in 2001) */
@@ -273,11 +274,22 @@ void vlc_cond_init( vlc_cond_t *p_condvar )
     pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
 #endif
 
-    if (pthread_cond_init (p_condvar, &attr))
+    if (unlikely(pthread_cond_init (p_condvar, &attr)))
         abort ();
     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.
@@ -350,7 +362,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,
@@ -386,7 +402,7 @@ 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 (sem_init (sem, 0, value))
+    if (unlikely(sem_init (sem, 0, value)))
         abort ();
 }
 
@@ -395,17 +411,24 @@ void vlc_sem_init (vlc_sem_t *sem, unsigned value)
  */
 void vlc_sem_destroy (vlc_sem_t *sem)
 {
-    int val = sem_destroy (sem);
+    if (likely(sem_destroy (sem) == 0))
+        return;
+
+    int val = errno;
     VLC_THREAD_ASSERT ("destroying semaphore");
 }
 
 /**
  * Increments the value of a semaphore.
+ * @return 0 on success, EOVERFLOW in case of integer overflow
  */
 int vlc_sem_post (vlc_sem_t *sem)
 {
-    int val = sem_post (sem);
-    if (val != EOVERFLOW)
+    if (likely(sem_post (sem) == 0))
+        return 0;
+
+    int val = errno;
+    if (unlikely(val != EOVERFLOW))
         VLC_THREAD_ASSERT ("unlocking semaphore");
     return val;
 }
@@ -417,9 +440,12 @@ int vlc_sem_post (vlc_sem_t *sem)
 void vlc_sem_wait (vlc_sem_t *sem)
 {
     int val;
+
     do
-        val = sem_wait (sem);
-    while (val == EINTR);
+        if (likely(sem_wait (sem) == 0))
+            return;
+    while ((val = errno) == EINTR);
+
     VLC_THREAD_ASSERT ("locking semaphore");
 }
 
@@ -428,7 +454,7 @@ void vlc_sem_wait (vlc_sem_t *sem)
  */
 void vlc_rwlock_init (vlc_rwlock_t *lock)
 {
-    if (pthread_rwlock_init (lock, NULL))
+    if (unlikely(pthread_rwlock_init (lock, NULL)))
         abort ();
 }
 
@@ -523,10 +549,10 @@ void vlc_threads_setup (libvlc_int_t *p_libvlc)
     if (!initialized)
     {
 #ifndef __APPLE__
-        if (config_GetInt (p_libvlc, "rt-priority"))
+        if (var_InheritBool (p_libvlc, "rt-priority"))
 #endif
         {
-            rt_offset = config_GetInt (p_libvlc, "rt-offset");
+            rt_offset = var_InheritInteger (p_libvlc, "rt-offset");
             rt_priorities = true;
         }
         initialized = true;
@@ -801,7 +827,7 @@ int vlc_timer_create (vlc_timer_t *id, void (*func) (void *), void *data)
 {
     struct vlc_timer *timer = malloc (sizeof (*timer));
 
-    if (timer == NULL)
+    if (unlikely(timer == NULL))
         return ENOMEM;
     vlc_mutex_init (&timer->lock);
     vlc_cond_init (&timer->wait);