+ wxLogLastError(_T("ReleaseSemaphore"));
+ }
+}
+
+// --------------------------------------------------------------------------
+// wxSemaphore
+// --------------------------------------------------------------------------
+
+wxSemaphore::wxSemaphore( int initialcount, int maxcount )
+{
+ m_internal = new wxSemaphoreInternal( initialcount, maxcount );
+}
+
+wxSemaphore::~wxSemaphore()
+{
+ delete m_internal;
+}
+
+void wxSemaphore::Wait()
+{
+ m_internal->Wait();
+}
+
+bool wxSemaphore::TryWait()
+{
+ return m_internal->TryWait();
+}
+
+bool wxSemaphore::Wait( unsigned long timeout_millis )
+{
+ return m_internal->Wait( timeout_millis );
+}
+
+void wxSemaphore::Post()
+{
+ m_internal->Post();
+}
+
+
+// ==========================================================================
+// wxCondition
+// ==========================================================================
+
+// --------------------------------------------------------------------------
+// wxConditionInternal
+// --------------------------------------------------------------------------
+
+class wxConditionInternal
+{
+public:
+ wxConditionInternal(wxMutex& mutex);
+
+ void Wait();
+
+ bool Wait( unsigned long timeout_millis );
+
+ void Signal();
+
+ void Broadcast();
+
+private:
+ int m_numWaiters;
+ wxMutex m_mutexNumWaiters;
+
+ wxMutex& m_mutex;
+
+ wxSemaphore m_semaphore;
+
+ DECLARE_NO_COPY_CLASS(wxConditionInternal)
+};
+
+wxConditionInternal::wxConditionInternal(wxMutex& mutex)
+ : m_mutex(mutex)
+{
+
+ m_numWaiters = 0;
+}
+
+void wxConditionInternal::Wait()
+{
+ // increment the number of waiters
+ m_mutexNumWaiters.Lock();
+ m_numWaiters++;
+ m_mutexNumWaiters.Unlock();
+
+ m_mutex.Unlock();
+
+ // a potential race condition can occur here
+ //
+ // after a thread increments nwaiters, and unlocks the mutex and before the
+ // semaphore.Wait() is called, if another thread can cause a signal to be
+ // generated
+ //
+ // this race condition is handled by using a semaphore and incrementing the
+ // semaphore only if 'nwaiters' is greater that zero since the semaphore,
+ // can 'remember' signals the race condition will not occur
+
+ // wait ( if necessary ) and decrement semaphore
+ m_semaphore.Wait();
+
+ m_mutex.Lock();
+}
+
+bool wxConditionInternal::Wait( unsigned long timeout_millis )
+{
+ m_mutexNumWaiters.Lock();
+ m_numWaiters++;
+ m_mutexNumWaiters.Unlock();
+
+ m_mutex.Unlock();
+
+ // a race condition can occur at this point in the code
+ //
+ // please see the comments in Wait(), for details
+
+ bool success = TRUE;
+
+ bool result = m_semaphore.Wait( timeout_millis );
+
+ if ( !result )
+ {
+ // another potential race condition exists here it is caused when a
+ // 'waiting' thread timesout, and returns from WaitForSingleObject, but
+ // has not yet decremented 'nwaiters'.
+ //
+ // at this point if another thread calls signal() then the semaphore
+ // will be incremented, but the waiting thread will miss it.
+ //
+ // to handle this particular case, the waiting thread calls
+ // WaitForSingleObject again with a timeout of 0, after locking
+ // 'nwaiters_mutex'. this call does not block because of the zero
+ // timeout, but will allow the waiting thread to catch the missed
+ // signals.
+ m_mutexNumWaiters.Lock();
+ result = m_semaphore.Wait( 0 );
+
+ if ( !result )