]> git.sesse.net Git - vlc/commitdiff
pthread: fix semaphore error handling
authorRémi Denis-Courmont <remi@remlab.net>
Sun, 17 Jan 2010 10:13:31 +0000 (12:13 +0200)
committerRémi Denis-Courmont <remi@remlab.net>
Sun, 17 Jan 2010 10:14:11 +0000 (12:14 +0200)
POSIX sem_*() functions return -1 on error and the error code in errno
(contrary to most POSIX thread functions).

src/misc/pthread.c

index 2a25b613bfe15a4395f0baa1e5798f0fc0dcd1b3..ee874dabe424c7d0d226ccc01966081a21194857 100644 (file)
@@ -396,17 +396,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;
 }
@@ -418,9 +425,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");
 }