+wxMutexError wxMutexInternal::HandleLockResult(int err)
+{
+ // wxPrintf( "err %d\n", err );
+
+ switch ( err )
+ {
+ case EDEADLK:
+ // only error checking mutexes return this value and so it's an
+ // unexpected situation -- hence use assert, not wxLogDebug
+ wxFAIL_MSG( wxT("mutex deadlock prevented") );
+ return wxMUTEX_DEAD_LOCK;
+
+ case EINVAL:
+ wxLogDebug(wxT("pthread_mutex_[timed]lock(): mutex not initialized"));
+ break;
+
+ case ETIMEDOUT:
+ return wxMUTEX_TIMEOUT;
+
+ case 0:
+ if (m_type == wxMUTEX_DEFAULT)
+ m_owningThread = wxThread::GetCurrentId();
+ return wxMUTEX_NO_ERROR;
+
+ default:
+ wxLogApiError(wxT("pthread_mutex_[timed]lock()"), err);
+ }
+
+ return wxMUTEX_MISC_ERROR;
+}
+
+
+wxMutexError wxMutexInternal::TryLock()
+{
+ int err = pthread_mutex_trylock(&m_mutex);
+ switch ( err )
+ {
+ case EBUSY:
+ // not an error: mutex is already locked, but we're prepared for
+ // this
+ return wxMUTEX_BUSY;
+
+ case EINVAL:
+ wxLogDebug(wxT("pthread_mutex_trylock(): mutex not initialized."));
+ break;
+
+ case 0:
+ if (m_type == wxMUTEX_DEFAULT)
+ m_owningThread = wxThread::GetCurrentId();
+ return wxMUTEX_NO_ERROR;
+
+ default:
+ wxLogApiError(wxT("pthread_mutex_trylock()"), err);
+ }
+
+ return wxMUTEX_MISC_ERROR;
+}
+
+wxMutexError wxMutexInternal::Unlock()
+{
+ m_owningThread = 0;
+
+ int err = pthread_mutex_unlock(&m_mutex);
+ switch ( err )
+ {
+ case EPERM:
+ // we don't own the mutex
+ return wxMUTEX_UNLOCKED;
+
+ case EINVAL:
+ wxLogDebug(wxT("pthread_mutex_unlock(): mutex not initialized."));
+ break;
+
+ case 0:
+ return wxMUTEX_NO_ERROR;
+
+ default:
+ wxLogApiError(wxT("pthread_mutex_unlock()"), err);
+ }
+
+ return wxMUTEX_MISC_ERROR;
+}
+
+// ===========================================================================
+// wxCondition implementation
+// ===========================================================================
+
+// ---------------------------------------------------------------------------
+// wxConditionInternal
+// ---------------------------------------------------------------------------
+
+// this is a wrapper around pthread_cond_t associated with a wxMutex (and hence
+// with a pthread_mutex_t)
+class wxConditionInternal
+{
+public:
+ wxConditionInternal(wxMutex& mutex);
+ ~wxConditionInternal();
+
+ bool IsOk() const { return m_isOk && m_mutex.IsOk(); }
+
+ wxCondError Wait();
+ wxCondError WaitTimeout(unsigned long milliseconds);
+
+ wxCondError Signal();
+ wxCondError Broadcast();
+
+private:
+ // get the POSIX mutex associated with us
+ pthread_mutex_t *GetPMutex() const { return &m_mutex.m_internal->m_mutex; }
+
+ wxMutex& m_mutex;
+ pthread_cond_t m_cond;
+
+ bool m_isOk;
+};
+
+wxConditionInternal::wxConditionInternal(wxMutex& mutex)
+ : m_mutex(mutex)