From: RĂ©mi Denis-Courmont Date: Sun, 17 Jan 2010 10:13:31 +0000 (+0200) Subject: pthread: fix semaphore error handling X-Git-Tag: 1.1.0-ff~1157 X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=73c88a2e7096b7f9a2ad0af83081bd2c19032945;p=vlc pthread: fix semaphore error handling POSIX sem_*() functions return -1 on error and the error code in errno (contrary to most POSIX thread functions). --- diff --git a/src/misc/pthread.c b/src/misc/pthread.c index 2a25b613bf..ee874dabe4 100644 --- a/src/misc/pthread.c +++ b/src/misc/pthread.c @@ -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"); }