16 #if !defined( _WIN32 ) && !defined( _WIN32_WCE )
28 #if defined(__APPLE__) && defined(__MACH__)
32 # include <mach/mach_init.h>
33 # include <mach/semaphore.h>
34 # include <mach/task.h>
35 #elif defined(HAVE_PTHREAD)
36 # include <semaphore.h>
47 class Semaphore::SemaphoreImpl
50 SemaphoreImpl(
int value );
56 SemaphoreImpl(
const SemaphoreImpl& );
57 SemaphoreImpl& operator=(
const SemaphoreImpl& );
61 #elif defined(__APPLE__) && defined(__MACH__)
62 semaphore_t m_semaphore;
63 #elif defined( HAVE_PTHREAD )
69 Semaphore::SemaphoreImpl::SemaphoreImpl(
int value )
72 m_semaphore = CreateSemaphore( 0, 0, 123456, 0 );
73 #elif defined(__APPLE__) && defined(__MACH__)
74 kern_return_t result = semaphore_create( mach_task_self(), &m_semaphore, SYNC_POLICY_FIFO, value );
75 if( result != KERN_SUCCESS )
78 printf(
"Semaphore::SemaphoreImpl() call to semaphore_create failed: %d\n", result );
81 #elif defined( HAVE_PTHREAD )
82 int result = sem_init( &m_semaphore, 0, value );
86 printf(
"Semaphore::SemaphoreImpl() call to sem_init failed: %d\n", errno );
92 Semaphore::SemaphoreImpl::~SemaphoreImpl()
95 CloseHandle( m_semaphore );
96 #elif defined(__APPLE__) && defined(__MACH__)
97 semaphore_destroy( mach_task_self(), m_semaphore );
98 #elif defined( HAVE_PTHREAD )
99 sem_destroy( &m_semaphore );
106 WaitForSingleObject( m_semaphore, INFINITE );
107 #elif defined(__APPLE__) && defined(__MACH__)
108 semaphore_wait( m_semaphore );
109 #elif defined( HAVE_PTHREAD )
110 sem_wait( &m_semaphore );
117 return ( WaitForSingleObject( m_semaphore, 0 ) == WAIT_OBJECT_0 );
118 #elif defined(__APPLE__) && defined(__MACH__)
119 mach_timespec_t timeout = { 0, 0 };
120 return ( semaphore_timedwait( m_semaphore, timeout ) == KERN_SUCCESS );
121 #elif defined( HAVE_PTHREAD )
122 return !sem_trywait( &m_semaphore );
130 ReleaseSemaphore( m_semaphore, 1, 0 );
131 #elif defined(__APPLE__) && defined(__MACH__)
132 semaphore_signal( m_semaphore );
133 #elif defined( HAVE_PTHREAD )
134 sem_post( &m_semaphore );
139 : m_semaphore( new SemaphoreImpl( value ) )
155 return m_semaphore->trywait();