From 4ee76e6182966f730848e02239bacde4292df4b4 Mon Sep 17 00:00:00 2001 From: kervala Date: Sat, 14 Feb 2015 17:52:15 +0100 Subject: [PATCH] Fixed: Unnamed semaphores are deprecated under OS X --- code/nel/include/nel/misc/mutex.h | 12 +++++++++--- code/nel/src/misc/mutex.cpp | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/code/nel/include/nel/misc/mutex.h b/code/nel/include/nel/misc/mutex.h index adbc7e7e5..cb7e9a188 100644 --- a/code/nel/include/nel/misc/mutex.h +++ b/code/nel/include/nel/misc/mutex.h @@ -28,7 +28,11 @@ # endif #elif defined(NL_OS_UNIX) # include // PThread -# include // PThread POSIX semaphores +# ifdef NL_OS_MAC +# include +# else +# include // PThread POSIX semaphores +# endif # include # define __forceinline # ifdef NL_OS_MAC @@ -532,8 +536,10 @@ private: #ifdef NL_OS_WINDOWS TNelRtlCriticalSection _Cs; -#elif defined NL_OS_UNIX - sem_t _Sem; +#elif defined(NL_OS_MAC) + dispatch_semaphore_t _Sem; +#elif defined(NL_OS_UNIX) + sem_t _Sem; #else # error "No fair mutex implementation for this OS" #endif diff --git a/code/nel/src/misc/mutex.cpp b/code/nel/src/misc/mutex.cpp index 3c86e2b29..28f6bae37 100644 --- a/code/nel/src/misc/mutex.cpp +++ b/code/nel/src/misc/mutex.cpp @@ -406,13 +406,21 @@ void CUnfairMutex::leave() */ CFairMutex::CFairMutex() { +#ifdef NL_OS_MAC + _Sem = dispatch_semaphore_create(1); +#else sem_init( const_cast(&_Sem), 0, 1 ); +#endif } CFairMutex::CFairMutex( const std::string &name ) { +#ifdef NL_OS_MAC + _Sem = dispatch_semaphore_create(1); +#else sem_init( const_cast(&_Sem), 0, 1 ); +#endif } @@ -421,7 +429,11 @@ CFairMutex::CFairMutex( const std::string &name ) */ CFairMutex::~CFairMutex() { +#ifdef NL_OS_MAC + dispatch_release(_Sem); +#else sem_destroy( const_cast(&_Sem) ); // needs that no thread is waiting on the semaphore +#endif } @@ -430,7 +442,11 @@ CFairMutex::~CFairMutex() */ void CFairMutex::enter() { +#ifdef NL_OS_MAC + dispatch_semaphore_wait(_Sem, DISPATCH_TIME_FOREVER); +#else sem_wait( const_cast(&_Sem) ); +#endif } @@ -439,7 +455,11 @@ void CFairMutex::enter() */ void CFairMutex::leave() { +#ifdef NL_OS_MAC + dispatch_semaphore_signal(_Sem); +#else sem_post( const_cast(&_Sem) ); +#endif }