Fixed: Unnamed semaphores are deprecated under OS X

This commit is contained in:
kervala 2015-02-14 17:52:15 +01:00
parent da7b88f052
commit f0ebd8d428
2 changed files with 29 additions and 3 deletions

View file

@ -28,7 +28,11 @@
# endif # endif
#elif defined(NL_OS_UNIX) #elif defined(NL_OS_UNIX)
# include <pthread.h> // PThread # include <pthread.h> // PThread
# include <semaphore.h> // PThread POSIX semaphores # ifdef NL_OS_MAC
# include <dispatch/dispatch.h>
# else
# include <semaphore.h> // PThread POSIX semaphores
# endif
# include <unistd.h> # include <unistd.h>
# define __forceinline # define __forceinline
# ifdef NL_OS_MAC # ifdef NL_OS_MAC
@ -532,8 +536,10 @@ private:
#ifdef NL_OS_WINDOWS #ifdef NL_OS_WINDOWS
TNelRtlCriticalSection _Cs; TNelRtlCriticalSection _Cs;
#elif defined NL_OS_UNIX #elif defined(NL_OS_MAC)
sem_t _Sem; dispatch_semaphore_t _Sem;
#elif defined(NL_OS_UNIX)
sem_t _Sem;
#else #else
# error "No fair mutex implementation for this OS" # error "No fair mutex implementation for this OS"
#endif #endif

View file

@ -406,13 +406,21 @@ void CUnfairMutex::leave()
*/ */
CFairMutex::CFairMutex() CFairMutex::CFairMutex()
{ {
#ifdef NL_OS_MAC
_Sem = dispatch_semaphore_create(1);
#else
sem_init( const_cast<sem_t*>(&_Sem), 0, 1 ); sem_init( const_cast<sem_t*>(&_Sem), 0, 1 );
#endif
} }
CFairMutex::CFairMutex( const std::string &name ) CFairMutex::CFairMutex( const std::string &name )
{ {
#ifdef NL_OS_MAC
_Sem = dispatch_semaphore_create(1);
#else
sem_init( const_cast<sem_t*>(&_Sem), 0, 1 ); sem_init( const_cast<sem_t*>(&_Sem), 0, 1 );
#endif
} }
@ -421,7 +429,11 @@ CFairMutex::CFairMutex( const std::string &name )
*/ */
CFairMutex::~CFairMutex() CFairMutex::~CFairMutex()
{ {
#ifdef NL_OS_MAC
dispatch_release(_Sem);
#else
sem_destroy( const_cast<sem_t*>(&_Sem) ); // needs that no thread is waiting on the semaphore sem_destroy( const_cast<sem_t*>(&_Sem) ); // needs that no thread is waiting on the semaphore
#endif
} }
@ -430,7 +442,11 @@ CFairMutex::~CFairMutex()
*/ */
void CFairMutex::enter() void CFairMutex::enter()
{ {
#ifdef NL_OS_MAC
dispatch_semaphore_wait(_Sem, DISPATCH_TIME_FOREVER);
#else
sem_wait( const_cast<sem_t*>(&_Sem) ); sem_wait( const_cast<sem_t*>(&_Sem) );
#endif
} }
@ -439,7 +455,11 @@ void CFairMutex::enter()
*/ */
void CFairMutex::leave() void CFairMutex::leave()
{ {
#ifdef NL_OS_MAC
dispatch_semaphore_signal(_Sem);
#else
sem_post( const_cast<sem_t*>(&_Sem) ); sem_post( const_cast<sem_t*>(&_Sem) );
#endif
} }