+wxConditionInternal::wxConditionInternal()
+{
+ if ( pthread_cond_init(&m_condition, (pthread_condattr_t *)NULL) != 0 )
+ {
+ // this is supposed to never happen
+ wxFAIL_MSG( _T("pthread_cond_init() failed") );
+ }
+
+ if ( pthread_mutex_init(&m_mutex, (pthread_mutexattr_t*)NULL) != 0 )
+ {
+ // neither this
+ wxFAIL_MSG( _T("wxCondition: pthread_mutex_init() failed") );
+ }
+
+ // initially the mutex is locked, so no thread can Signal() or Broadcast()
+ // until another thread starts to Wait()
+ if ( pthread_mutex_lock(&m_mutex) != 0 )
+ {
+ wxFAIL_MSG( _T("wxCondition: pthread_mutex_lock() failed") );
+ }
+}
+
+wxConditionInternal::~wxConditionInternal()
+{
+ if ( pthread_cond_destroy( &m_condition ) != 0 )
+ {
+ wxLogDebug(_T("Failed to destroy condition variable (some "
+ "threads are probably still waiting on it?)"));
+ }
+
+ if ( pthread_mutex_unlock( &m_mutex ) != 0 )
+ {
+ wxLogDebug(_T("wxCondition: failed to unlock the mutex"));
+ }
+
+ if ( pthread_mutex_destroy( &m_mutex ) != 0 )
+ {
+ wxLogDebug(_T("Failed to destroy mutex (it is probably locked)"));
+ }
+}
+
+void wxConditionInternal::Wait()
+{
+ if ( pthread_cond_wait( &m_condition, &m_mutex ) != 0 )
+ {
+ // not supposed to ever happen
+ wxFAIL_MSG( _T("pthread_cond_wait() failed") );
+ }
+}
+
+bool wxConditionInternal::WaitWithTimeout(const timespec* ts)
+{
+ switch ( pthread_cond_timedwait( &m_condition, &m_mutex, ts ) )
+ {
+ case 0:
+ // condition signaled
+ return TRUE;
+
+ default:
+ wxLogDebug(_T("pthread_cond_timedwait() failed"));
+
+ // fall through
+
+ case ETIMEDOUT:
+ case EINTR:
+ // wait interrupted or timeout elapsed
+ return FALSE;
+ }
+}
+
+void wxConditionInternal::Signal()
+{
+ MutexLock lock(m_mutex);
+
+ if ( pthread_cond_signal( &m_condition ) != 0 )
+ {
+ // shouldn't ever happen
+ wxFAIL_MSG(_T("pthread_cond_signal() failed"));
+ }
+}
+
+void wxConditionInternal::Broadcast()
+{
+ MutexLock lock(m_mutex);
+
+ if ( pthread_cond_broadcast( &m_condition ) != 0 )
+ {
+ // shouldn't ever happen
+ wxFAIL_MSG(_T("pthread_cond_broadcast() failed"));
+ }
+}
+