Commit f7637422 authored by Juho Vähä-Herttua's avatar Juho Vähä-Herttua Committed by Rémi Denis-Courmont

pthread: Use Mach semaphores instead of POSIX semaphores on Mac OS X

Unnamed POSIX semaphores don't work on Mac OS X at all.
Signed-off-by: default avatarRémi Denis-Courmont <remi@remlab.net>
parent 2a625dda
...@@ -45,7 +45,14 @@ ...@@ -45,7 +45,14 @@
# include <unistd.h> /* _POSIX_SPIN_LOCKS */ # include <unistd.h> /* _POSIX_SPIN_LOCKS */
# include <pthread.h> # include <pthread.h>
/* Unnamed POSIX semaphores not supported on Mac OS X, use Mach semaphores instead */
# if defined (__APPLE__)
# include <mach/semaphore.h>
# include <mach/task.h>
# else
# include <semaphore.h> # include <semaphore.h>
# endif
#endif #endif
...@@ -103,11 +110,16 @@ typedef pthread_mutex_t vlc_mutex_t; ...@@ -103,11 +110,16 @@ typedef pthread_mutex_t vlc_mutex_t;
#define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER #define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER
typedef pthread_cond_t vlc_cond_t; typedef pthread_cond_t vlc_cond_t;
#define VLC_STATIC_COND PTHREAD_COND_INITIALIZER #define VLC_STATIC_COND PTHREAD_COND_INITIALIZER
typedef sem_t vlc_sem_t;
typedef pthread_rwlock_t vlc_rwlock_t; typedef pthread_rwlock_t vlc_rwlock_t;
typedef pthread_key_t vlc_threadvar_t; typedef pthread_key_t vlc_threadvar_t;
typedef struct vlc_timer *vlc_timer_t; typedef struct vlc_timer *vlc_timer_t;
#if defined (__APPLE__)
typedef semaphore_t vlc_sem_t;
#else
typedef sem_t vlc_sem_t;
#endif
#elif defined( WIN32 ) #elif defined( WIN32 )
#if !defined( UNDER_CE ) #if !defined( UNDER_CE )
typedef HANDLE vlc_thread_t; typedef HANDLE vlc_thread_t;
......
...@@ -48,6 +48,7 @@ ...@@ -48,6 +48,7 @@
#ifdef __APPLE__ #ifdef __APPLE__
# include <sys/time.h> /* gettimeofday in vlc_cond_timedwait */ # include <sys/time.h> /* gettimeofday in vlc_cond_timedwait */
# include <mach/mach_init.h> /* mach_task_self in semaphores */
#endif #endif
/** /**
...@@ -402,8 +403,13 @@ int vlc_cond_timedwait (vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex, ...@@ -402,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) 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))) if (unlikely(sem_init (sem, 0, value)))
abort (); abort ();
#endif
} }
/** /**
...@@ -411,10 +417,20 @@ void vlc_sem_init (vlc_sem_t *sem, unsigned value) ...@@ -411,10 +417,20 @@ void vlc_sem_init (vlc_sem_t *sem, unsigned value)
*/ */
void vlc_sem_destroy (vlc_sem_t *sem) 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)) if (likely(sem_destroy (sem) == 0))
return; return;
int val = errno; val = errno;
#endif
VLC_THREAD_ASSERT ("destroying semaphore"); VLC_THREAD_ASSERT ("destroying semaphore");
} }
...@@ -424,10 +440,20 @@ void vlc_sem_destroy (vlc_sem_t *sem) ...@@ -424,10 +440,20 @@ void vlc_sem_destroy (vlc_sem_t *sem)
*/ */
int vlc_sem_post (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)) if (likely(sem_post (sem) == 0))
return 0; return 0;
int val = errno; val = errno;
#endif
if (unlikely(val != EOVERFLOW)) if (unlikely(val != EOVERFLOW))
VLC_THREAD_ASSERT ("unlocking semaphore"); VLC_THREAD_ASSERT ("unlocking semaphore");
return val; return val;
...@@ -441,10 +467,17 @@ void vlc_sem_wait (vlc_sem_t *sem) ...@@ -441,10 +467,17 @@ void vlc_sem_wait (vlc_sem_t *sem)
{ {
int val; int val;
#if defined(__APPLE__)
if (likely(semaphore_wait(*sem) == KERN_SUCCESS))
return;
val = EINVAL;
#else
do do
if (likely(sem_wait (sem) == 0)) if (likely(sem_wait (sem) == 0))
return; return;
while ((val = errno) == EINTR); while ((val = errno) == EINTR);
#endif
VLC_THREAD_ASSERT ("locking semaphore"); VLC_THREAD_ASSERT ("locking semaphore");
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment