[vlc-commits] commit: pthread: Use Mach semaphores instead of POSIX semaphores on Mac OS X ( Juho Vähä-Herttua )
git at videolan.org
git at videolan.org
Fri May 7 16:09:32 CEST 2010
vlc | branch: master | Juho Vähä-Herttua <juhovh at iki.fi> | Thu May 6 17:52:57 2010 +0300| [f76374224a753e908a4655f16b1cf1ca85897723] | committer: 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: Rémi Denis-Courmont <remi at remlab.net>
> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=f76374224a753e908a4655f16b1cf1ca85897723
---
include/vlc_threads.h | 16 ++++++++++++++--
src/misc/pthread.c | 37 +++++++++++++++++++++++++++++++++++--
2 files changed, 49 insertions(+), 4 deletions(-)
diff --git a/include/vlc_threads.h b/include/vlc_threads.h
index 3caa97d..e9d1c8d 100644
--- a/include/vlc_threads.h
+++ b/include/vlc_threads.h
@@ -45,7 +45,14 @@
# include <unistd.h> /* _POSIX_SPIN_LOCKS */
# include <pthread.h>
-# include <semaphore.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>
+# endif
#endif
@@ -103,11 +110,16 @@ typedef pthread_mutex_t vlc_mutex_t;
#define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER
typedef pthread_cond_t vlc_cond_t;
#define VLC_STATIC_COND PTHREAD_COND_INITIALIZER
-typedef sem_t vlc_sem_t;
typedef pthread_rwlock_t vlc_rwlock_t;
typedef pthread_key_t vlc_threadvar_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 )
#if !defined( UNDER_CE )
typedef HANDLE vlc_thread_t;
diff --git a/src/misc/pthread.c b/src/misc/pthread.c
index ca422cf..264d1bf 100644
--- a/src/misc/pthread.c
+++ b/src/misc/pthread.c
@@ -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
/**
@@ -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)
{
+#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
}
/**
@@ -411,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");
}
@@ -424,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;
@@ -441,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");
}
More information about the vlc-commits
mailing list