X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/2f056c85d182ae6c038394cfb9f6b56df3981877..19cf1ef3a7077dd99a90f141847b8a9b67b318c9:/src/mac/carbon/thread.cpp diff --git a/src/mac/carbon/thread.cpp b/src/mac/carbon/thread.cpp index e59ca72ebe..cacf2e5e35 100644 --- a/src/mac/carbon/thread.cpp +++ b/src/mac/carbon/thread.cpp @@ -1,24 +1,15 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: thread.cpp +// Name: src/mac/carbon/thread.cpp // Purpose: wxThread Implementation // Author: Original from Wolfram Gloger/Guilhem Lavaux/Vadim Zeitlin -// Modified by: Stefan Csomor +// Modified by: Aj Lavin, Stefan Csomor // Created: 04/22/98 // RCS-ID: $Id$ // Copyright: (c) Wolfram Gloger (1996, 1997); Guilhem Lavaux (1998), -// Vadim Zeitlin (1999) , Stefan Csomor (2000) +// Vadim Zeitlin (1999), Stefan Csomor (2000) // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -#ifdef __GNUG__ - #pragma implementation "thread.h" -#endif - -// ---------------------------------------------------------------------------- -// headers -// ---------------------------------------------------------------------------- - -// For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" #if defined(__BORLANDC__) @@ -27,275 +18,891 @@ #ifndef WX_PRECOMP #include "wx/wx.h" + #include "wx/module.h" #endif #if wxUSE_THREADS -#include "wx/module.h" #include "wx/thread.h" #ifdef __WXMAC__ -#include +#ifdef __DARWIN__ + #include +#else + #include + #include +#endif + #include "wx/mac/uma.h" #endif -// ---------------------------------------------------------------------------- -// constants -// ---------------------------------------------------------------------------- +#include "wx/mac/macnotfy.h" + -// the possible states of the thread ("=>" shows all possible transitions from -// this state) +// the possible states of the thread: +// ("=>" shows all possible transitions from this state) enum wxThreadState { - STATE_NEW, // didn't start execution yet (=> RUNNING) - STATE_RUNNING, // thread is running (=> PAUSED, CANCELED) - STATE_PAUSED, // thread is temporarily suspended (=> RUNNING) - STATE_CANCELED, // thread should terminate a.s.a.p. (=> EXITED) - STATE_EXITED // thread is terminating + STATE_NEW, // didn't start execution yet (=> RUNNING) + STATE_RUNNING, // thread is running (=> PAUSED, CANCELED) + STATE_PAUSED, // thread is temporarily suspended (=> RUNNING) + STATE_CANCELED, // thread should terminate a.s.a.p. (=> EXITED) + STATE_EXITED // thread is terminating }; // ---------------------------------------------------------------------------- -// this module globals +// globals // ---------------------------------------------------------------------------- -static ThreadID gs_idMainThread = kNoThreadID ; -static bool gs_waitingForThread = FALSE ; +// the task ID of the main thread +static wxThreadIdType gs_idMainThread = kInvalidID; + +// this is the Per-Task Storage for the pointer to the appropriate wxThread +TaskStorageIndex gs_tlsForWXThread = 0; + +// if it's false, some secondary thread is holding the GUI lock +static bool gs_bGuiOwnedByMainThread = true; + +// critical section which controls access to all GUI functions: any secondary +// thread (i.e. except the main one) must enter this crit section before doing +// any GUI calls +static wxCriticalSection *gs_critsectGui = NULL; + +// critical section which protects gs_nWaitingForGui variable +static wxCriticalSection *gs_critsectWaitingForGui = NULL; + +// number of threads waiting for GUI in wxMutexGuiEnter() +static size_t gs_nWaitingForGui = 0; + +// overall number of threads, needed for determining +// the sleep value of the main event loop +size_t g_numberOfThreads = 0; + + +#if wxUSE_GUI +MPCriticalRegionID gs_guiCritical = kInvalidID; +#endif // ============================================================================ // MacOS implementation of thread classes // ============================================================================ -class wxMacStCritical +/* + Notes : + + The implementation is very close to the phtreads implementation, the reason for + using MPServices is the fact that these are also available under OS 9. Thus allowing + for one common API for all current builds. + + As soon as wxThreads are on a 64 bit address space, the TLS must be extended + to use two indices one for each 32 bit part as the MP implementation is limited + to longs. + + I have three implementations for mutexes : + version A based on a binary semaphore, problem - not reentrant, version B based + on a critical region, allows for reentrancy, performance implications not + yet tested, and third a plain pthreads implementation + + The same for condition internal, one implementation by Aj Lavin and the other one + copied from the thrimpl.cpp which I assume has been more broadly tested, I've just + replaced the interlock increment with the appropriate PPC calls +*/ + +// ---------------------------------------------------------------------------- +// wxCriticalSection +// ---------------------------------------------------------------------------- + +wxCriticalSection::wxCriticalSection() { -public : - wxMacStCritical() - { - if ( UMASystemIsInitialized() ) - ThreadBeginCritical() ; - } - ~wxMacStCritical() - { - if ( UMASystemIsInitialized() ) - ThreadEndCritical() ; - } -}; + MPCreateCriticalRegion( (MPCriticalRegionID*) &m_critRegion ); +} + +wxCriticalSection::~wxCriticalSection() +{ + MPDeleteCriticalRegion( (MPCriticalRegionID) m_critRegion ); +} + +void wxCriticalSection::Enter() +{ + MPEnterCriticalRegion( (MPCriticalRegionID) m_critRegion, kDurationForever ); +} + +void wxCriticalSection::Leave() +{ + MPExitCriticalRegion( (MPCriticalRegionID) m_critRegion ); +} // ---------------------------------------------------------------------------- // wxMutex implementation // ---------------------------------------------------------------------------- +#if TARGET_API_MAC_OSX +#define wxUSE_MAC_SEMAPHORE_MUTEX 0 +#define wxUSE_MAC_CRITICAL_REGION_MUTEX 1 +#define wxUSE_MAC_PTHREADS_MUTEX 0 +#else +#define wxUSE_MAC_SEMAPHORE_MUTEX 0 +#define wxUSE_MAC_CRITICAL_REGION_MUTEX 1 +#define wxUSE_MAC_PTHREADS_MUTEX 0 +#endif + +#if wxUSE_MAC_PTHREADS_MUTEX + +#include + + class wxMutexInternal { public: - wxMutexInternal() + wxMutexInternal( wxMutexType mutexType ); + ~wxMutexInternal(); + + wxMutexError Lock(); + wxMutexError TryLock(); + wxMutexError Unlock(); + + bool IsOk() const + { return m_isOk; } + +private: + pthread_mutex_t m_mutex; + bool m_isOk; + + // wxConditionInternal uses our m_mutex + friend class wxConditionInternal; +}; + +#ifdef HAVE_PTHREAD_MUTEXATTR_T +// on some systems pthread_mutexattr_settype() is not in the headers (but it is +// in the library, otherwise we wouldn't compile this code at all) +extern "C" int pthread_mutexattr_settype( pthread_mutexattr_t *, int ); +#endif + +wxMutexInternal::wxMutexInternal( wxMutexType mutexType ) +{ + int err; + switch ( mutexType ) + { + case wxMUTEX_RECURSIVE: + // support recursive locks like Win32, i.e. a thread can lock a + // mutex which it had itself already locked + // + // unfortunately initialization of recursive mutexes is non + // portable, so try several methods +#ifdef HAVE_PTHREAD_MUTEXATTR_T + { + pthread_mutexattr_t attr; + pthread_mutexattr_init( &attr ); + pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_RECURSIVE ); + + err = pthread_mutex_init( &m_mutex, &attr ); + } +#elif defined(HAVE_PTHREAD_RECURSIVE_MUTEX_INITIALIZER) + // we can use this only as initializer so we have to assign it + // first to a temp var - assigning directly to m_mutex wouldn't + // even compile + { + pthread_mutex_t mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; + m_mutex = mutex; + } +#else // no recursive mutexes + err = EINVAL; +#endif // HAVE_PTHREAD_MUTEXATTR_T/... + break; + + default: + wxFAIL_MSG( wxT("unknown mutex type") ); + // fall through + + case wxMUTEX_DEFAULT: + err = pthread_mutex_init( &m_mutex, NULL ); + break; + } + + m_isOk = err == 0; + if ( !m_isOk ) + { + wxLogApiError( wxT("pthread_mutex_init()"), err ); + } +} + +wxMutexInternal::~wxMutexInternal() +{ + if ( m_isOk ) + { + int err = pthread_mutex_destroy( &m_mutex ); + if ( err != 0 ) + { + wxLogApiError( wxT("pthread_mutex_destroy()"), err ); + } + } +} + +wxMutexError wxMutexInternal::Lock() +{ + int err = pthread_mutex_lock( &m_mutex ); + 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_lock(): mutex not initialized.") ); + break; + + case 0: + return wxMUTEX_NO_ERROR; + + default: + wxLogApiError( wxT("pthread_mutex_lock()"), err ); + } + + return wxMUTEX_MISC_ERROR; +} + +wxMutexError wxMutexInternal::TryLock() +{ + int err = pthread_mutex_trylock( &m_mutex ); + switch ( err ) { - m_owner = kNoThreadID ; + case EBUSY: + // not an error: mutex is already locked, but we're prepared for this case + return wxMUTEX_BUSY; + + case EINVAL: + wxLogDebug( wxT("pthread_mutex_trylock(): mutex not initialized.") ); + break; + + case 0: + return wxMUTEX_NO_ERROR; + + default: + wxLogApiError( wxT("pthread_mutex_trylock()"), err ); } - ~wxMutexInternal() + return wxMUTEX_MISC_ERROR; +} + +wxMutexError wxMutexInternal::Unlock() +{ + 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; +} + +#endif + +#if wxUSE_MAC_SEMAPHORE_MUTEX + +class wxMutexInternal +{ public: - ThreadID m_owner ; - wxArrayLong m_waiters ; + wxMutexInternal( wxMutexType mutexType ); + virtual ~wxMutexInternal(); + + bool IsOk() const + { return m_isOk; } + + wxMutexError Lock(); + wxMutexError TryLock(); + wxMutexError Unlock(); + +private: + MPSemaphoreID m_semaphore; + bool m_isOk; }; -wxMutex::wxMutex() +wxMutexInternal::wxMutexInternal(wxMutexType mutexType ) +{ + m_isOk = false; + m_semaphore = kInvalidID; + OSStatus err = noErr; + + switch ( mutexType ) + { + case wxMUTEX_DEFAULT : + verify_noerr( MPCreateBinarySemaphore( &m_semaphore ) ); + m_isOk = ( m_semaphore != kInvalidID ); + break; + + case wxMUTEX_RECURSIVE : + wxFAIL_MSG( wxT("Recursive Mutex not supported yet") ); + break; + + default : + wxFAIL_MSG( wxT("Unknown mutex type") ); + break; + } +} + +wxMutexInternal::~wxMutexInternal() { - m_internal = new wxMutexInternal; + if ( m_semaphore != kInvalidID ) + MPDeleteSemaphore( m_semaphore ); - m_locked = 0; + MPYield(); } -wxMutex::~wxMutex() +wxMutexError wxMutexInternal::Lock() { - if ( m_locked > 0 ) + wxCHECK_MSG( m_isOk, wxMUTEX_MISC_ERROR, wxT("Invalid Mutex") ); + OSStatus err = MPWaitOnSemaphore( m_semaphore, kDurationForever ); + if (err != noErr) { - wxLogDebug(_T("Warning: freeing a locked mutex (%d locks)."), m_locked); + wxLogSysError( wxT("Could not lock mutex") ); + + return wxMUTEX_MISC_ERROR; } - delete m_internal; + return wxMUTEX_NO_ERROR; } -wxMutexError wxMutex::Lock() +wxMutexError wxMutexInternal::TryLock() { - wxMacStCritical critical ; - if ( UMASystemIsInitialized() ) + wxCHECK_MSG( m_isOk, wxMUTEX_MISC_ERROR, wxT("Invalid Mutex") ); + OSStatus err = MPWaitOnSemaphore( m_semaphore, kDurationImmediate ); + if (err != noErr) { - OSErr err ; - ThreadID current = kNoThreadID; - err = ::MacGetCurrentThread(¤t); - // if we are not the owner, add this thread to the list of waiting threads, stop this thread - // and invoke the scheduler to continue executing the owner's thread - while ( m_internal->m_owner != kNoThreadID && m_internal->m_owner != current) - { - m_internal->m_waiters.Add(current); - err = ::SetThreadStateEndCritical(kCurrentThreadID, kStoppedThreadState, m_internal->m_owner); - err = ::ThreadBeginCritical(); - } - m_internal->m_owner = current; + if (err == kMPTimeoutErr) + return wxMUTEX_BUSY; + + wxLogSysError( wxT("Could not try lock mutex") ); + + return wxMUTEX_MISC_ERROR; } - m_locked++; return wxMUTEX_NO_ERROR; } -wxMutexError wxMutex::TryLock() +wxMutexError wxMutexInternal::Unlock() { - wxMacStCritical critical ; - if ( UMASystemIsInitialized() ) + wxCHECK_MSG( m_isOk, wxMUTEX_MISC_ERROR, wxT("Invalid Mutex") ); + OSStatus err = MPSignalSemaphore( m_semaphore ); + + MPYield(); + if (err != noErr) { - OSErr err ; - ThreadID current = kNoThreadID; - ::MacGetCurrentThread(¤t); - // if we are not the owner, give an error back - if ( m_internal->m_owner != kNoThreadID && m_internal->m_owner != current ) - return wxMUTEX_BUSY; + wxLogSysError( wxT("Could not unlock mutex") ); + return wxMUTEX_MISC_ERROR; + } + + return wxMUTEX_NO_ERROR; +} + +#endif + +#if wxUSE_MAC_CRITICAL_REGION_MUTEX + +class wxMutexInternal +{ +public: + wxMutexInternal( wxMutexType mutexType ); + virtual ~wxMutexInternal(); + + bool IsOk() const { return m_isOk; } + + wxMutexError Lock() { return Lock(kDurationForever); } + wxMutexError Lock(unsigned long ms); + wxMutexError TryLock(); + wxMutexError Unlock(); + +private: + MPCriticalRegionID m_critRegion; + bool m_isOk ; +}; - m_internal->m_owner = current; +wxMutexInternal::wxMutexInternal( wxMutexType mutexType ) +{ + m_isOk = false; + m_critRegion = kInvalidID; + + verify_noerr( MPCreateCriticalRegion( &m_critRegion ) ); + m_isOk = ( m_critRegion != kInvalidID ); + if ( !IsOk() ) + { + wxFAIL_MSG( wxT("Error when creating mutex") ); } - m_locked++; +} - return wxMUTEX_NO_ERROR; +wxMutexInternal::~wxMutexInternal() +{ + if ( m_critRegion != kInvalidID ) + MPDeleteCriticalRegion( m_critRegion ); + + MPYield(); } -wxMutexError wxMutex::Unlock() +wxMutexError wxMutexInternal::Lock(unsigned long ms) { - if ( UMASystemIsInitialized() ) + wxCHECK_MSG( m_isOk , wxMUTEX_MISC_ERROR , wxT("Invalid Mutex") ); + + OSStatus err = MPEnterCriticalRegion( m_critRegion, ms ); + switch ( err ) { - OSErr err; - err = ::ThreadBeginCritical(); + case noErr: + break; + + case kMPTimeoutErr: + wxASSERT_MSG( ms != kDurationForever, wxT("unexpected timeout") ); + return wxMUTEX_TIMEOUT; - if (m_locked > 0) - m_locked--; + default: + wxLogSysError(wxT("Could not lock mutex")); + return wxMUTEX_MISC_ERROR; + } - // this mutex is not owned by anybody anmore - m_internal->m_owner = kNoThreadID; + return wxMUTEX_NO_ERROR; +} - // now pass on to the first waiting thread - ThreadID firstWaiting = kNoThreadID; - bool found = false; - while (!m_internal->m_waiters.IsEmpty() && !found) - { - firstWaiting = m_internal->m_waiters[0]; - err = ::SetThreadState(firstWaiting, kReadyThreadState, kNoThreadID); - // in case this was not successful (dead thread), we just loop on and reset the id - found = (err != threadNotFoundErr); - if ( !found ) - firstWaiting = kNoThreadID ; - m_internal->m_waiters.RemoveAt(0) ; - } - // now we have a valid firstWaiting thread, which has been scheduled to run next, just end the - // critical section and invoke the scheduler - err = ::SetThreadStateEndCritical(kCurrentThreadID, kReadyThreadState, firstWaiting); +wxMutexError wxMutexInternal::TryLock() +{ + wxCHECK_MSG( m_isOk , wxMUTEX_MISC_ERROR , wxT("Invalid Mutex") ) ; + + OSStatus err = MPEnterCriticalRegion( m_critRegion, kDurationImmediate); + if (err != noErr) + { + if ( err == kMPTimeoutErr) + return wxMUTEX_BUSY; + + wxLogSysError( wxT("Could not try lock mutex") ); + return wxMUTEX_MISC_ERROR; } - else + + return wxMUTEX_NO_ERROR; +} + +wxMutexError wxMutexInternal::Unlock() +{ + wxCHECK_MSG( m_isOk , wxMUTEX_MISC_ERROR , wxT("Invalid Mutex") ) ; + + OSStatus err = MPExitCriticalRegion( m_critRegion ); + MPYield() ; + + if (err != noErr) { - if (m_locked > 0) - m_locked--; + wxLogSysError( wxT("Could not unlock mutex") ); + + return wxMUTEX_MISC_ERROR; } + return wxMUTEX_NO_ERROR; } +#endif + +// -------------------------------------------------------------------------- +// wxSemaphore +// -------------------------------------------------------------------------- + +class wxSemaphoreInternal +{ +public: + wxSemaphoreInternal( int initialcount, int maxcount ); + virtual ~wxSemaphoreInternal(); + + bool IsOk() const + { return m_isOk; } + + wxSemaError Post(); + wxSemaError WaitTimeout( unsigned long milliseconds ); + + wxSemaError Wait() + { return WaitTimeout( kDurationForever); } + + wxSemaError TryWait() + { + wxSemaError err = WaitTimeout( kDurationImmediate ); + if (err == wxSEMA_TIMEOUT) + err = wxSEMA_BUSY; + + return err; + } + +private: + MPSemaphoreID m_semaphore; + bool m_isOk; +}; + +wxSemaphoreInternal::wxSemaphoreInternal( int initialcount, int maxcount) +{ + m_isOk = false; + m_semaphore = kInvalidID; + if ( maxcount == 0 ) + // make it practically infinite + maxcount = INT_MAX; + + verify_noerr( MPCreateSemaphore( maxcount, initialcount, &m_semaphore ) ); + m_isOk = ( m_semaphore != kInvalidID ); + + if ( !IsOk() ) + { + wxFAIL_MSG( wxT("Error when creating semaphore") ); + } +} + +wxSemaphoreInternal::~wxSemaphoreInternal() +{ + if (m_semaphore != kInvalidID) + MPDeleteSemaphore( m_semaphore ); + + MPYield(); +} + +wxSemaError wxSemaphoreInternal::WaitTimeout( unsigned long milliseconds ) +{ + OSStatus err = MPWaitOnSemaphore( m_semaphore, milliseconds ); + if (err != noErr) + { + if (err == kMPTimeoutErr) + return wxSEMA_TIMEOUT; + + return wxSEMA_MISC_ERROR; + } + + return wxSEMA_NO_ERROR; +} + +wxSemaError wxSemaphoreInternal::Post() +{ + OSStatus err = MPSignalSemaphore( m_semaphore ); + MPYield(); + if (err != noErr) + return wxSEMA_MISC_ERROR; + + return wxSEMA_NO_ERROR; +} + // ---------------------------------------------------------------------------- // wxCondition implementation // ---------------------------------------------------------------------------- +#if 0 + class wxConditionInternal { public: - wxConditionInternal() + wxConditionInternal( wxMutex& mutex ) + : + m_mutex( mutex ), + m_semaphore( 0, 1 ), + m_gate( 1, 1 ) { - m_excessSignals = 0 ; + m_waiters = 0; + m_signals = 0; + m_canceled = 0; } - ~wxConditionInternal() + + virtual ~wxConditionInternal() {} + + bool IsOk() const + { return m_mutex.IsOk(); } + + wxCondError Wait() + { return WaitTimeout( kDurationForever ); } + + wxCondError WaitTimeout( unsigned long msectimeout ); + + wxCondError Signal() + { return DoSignal( false); } + + wxCondError Broadcast() + { return DoSignal( true ); } + +private: + wxCondError DoSignal( bool signalAll ); + + wxMutex& m_mutex; + wxSemaphoreInternal m_semaphore; // Signals the waiting threads. + wxSemaphoreInternal m_gate; + wxCriticalSection m_varSection; + size_t m_waiters; // Number of threads waiting for a signal. + size_t m_signals; // Number of signals to send. + size_t m_canceled; // Number of canceled waiters in m_waiters. +}; + +wxCondError wxConditionInternal::WaitTimeout( unsigned long msectimeout ) +{ + m_gate.Wait(); + + if ( ++ m_waiters == INT_MAX ) { + m_varSection.Enter(); + + m_waiters -= m_canceled; + m_signals -= m_canceled; + m_canceled = 0; + + m_varSection.Leave(); } - bool Wait(unsigned long msectimeout) + m_gate.Post(); + m_mutex.Unlock(); + + wxSemaError err = m_semaphore.WaitTimeout( msectimeout); + wxASSERT( err == wxSEMA_NO_ERROR || err == wxSEMA_TIMEOUT); + + m_varSection.Enter(); + + if ( err != wxSEMA_NO_ERROR ) { - wxMacStCritical critical ; - if ( m_excessSignals > 0 ) + if ( m_signals > m_canceled ) { - --m_excessSignals ; - return TRUE ; + // A signal is being sent after we timed out. + if ( m_waiters == m_signals ) + { + // There are no excess waiters to catch the signal, so + // we must throw it away. + wxSemaError err2 = m_semaphore.Wait(); + if ( err2 != wxSEMA_NO_ERROR ) + { + wxLogSysError( wx("Error while waiting on semaphore") ); + } + + wxASSERT( err2 == wxSEMA_NO_ERROR); + + --m_waiters; + if ( --m_signals == m_canceled ) + { + // This was the last signal. open the gate. + wxASSERT( m_waiters == m_canceled ); + m_gate.Post(); + } + } + else + { + // There are excess waiters to catch the signal, leave it be. + --m_waiters; + } } - else if ( msectimeout == 0 ) + else { - return FALSE ; + // No signals is being sent: + // the gate may be open or closed, so we can't touch m_waiters. + ++m_canceled; + ++m_signals; } - else + } + else + { + // We caught a signal. + wxASSERT( m_signals > m_canceled ); + + --m_waiters; + + if ( --m_signals == m_canceled) { + // This was the last signal. open the gate. + wxASSERT( m_waiters == m_canceled ); + + m_gate.Post(); } - /* - waiters++; + } + + m_varSection.Leave(); + m_mutex.Lock(); + + if (err != noErr) + return err == wxSEMA_TIMEOUT ? wxCOND_TIMEOUT : wxCOND_MISC_ERROR; + + return wxCOND_NO_ERROR; +} + - // FIXME this should be MsgWaitForMultipleObjects() as well probably - DWORD rc = ::WaitForSingleObject(event, timeout); +wxCondError wxConditionInternal::DoSignal( bool signalAll) +{ + m_gate.Wait(); + m_varSection.Enter(); - waiters--; + wxASSERT( m_signals == m_canceled ); - return rc != WAIT_TIMEOUT; - */ - return TRUE ; + if ( m_waiters == m_canceled) + { + m_varSection.Leave(); + m_gate.Post(); + return wxCOND_NO_ERROR; } - void Signal() + + if ( m_canceled > 0) { - wxMacStCritical critical ; + m_waiters -= m_canceled; + m_signals = 0; + m_canceled = 0; } - wxArrayLong m_waiters ; - wxInt32 m_excessSignals ; -}; + m_signals = signalAll ? m_waiters : 1; + size_t n = m_signals; -wxCondition::wxCondition() -{ - m_internal = new wxConditionInternal; + m_varSection.Leave(); + + // Let the waiters inherit the gate lock. + + do + { + wxSemaError err = m_semaphore.Post(); + wxASSERT( err == wxSEMA_NO_ERROR ); + } + while ( --n ); + + return wxCOND_NO_ERROR; } -wxCondition::~wxCondition() +#else +class wxConditionInternal { - delete m_internal; +public: + wxConditionInternal( wxMutex& mutex ); + + bool IsOk() const + { return m_mutex.IsOk() && m_semaphore.IsOk(); } + + wxCondError Wait(); + wxCondError WaitTimeout( unsigned long milliseconds ); + + wxCondError Signal(); + wxCondError Broadcast(); + +private: + // the number of threads currently waiting for this condition + SInt32 m_numWaiters; + + // the critical section protecting m_numWaiters + wxCriticalSection m_csWaiters; + + wxMutex& m_mutex; + wxSemaphore m_semaphore; + + DECLARE_NO_COPY_CLASS(wxConditionInternal) +}; + +wxConditionInternal::wxConditionInternal( wxMutex& mutex ) + : m_mutex(mutex) +{ + // another thread can't access it until we return from ctor, so no need to + // protect access to m_numWaiters here + m_numWaiters = 0; } -void wxCondition::Wait() +wxCondError wxConditionInternal::Wait() { - (void)m_internal->Wait(0xFFFFFFFFL); + // increment the number of waiters + IncrementAtomic( &m_numWaiters ); + + 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 + wxSemaError err = m_semaphore.Wait(); + m_mutex.Lock(); + + return err == wxSEMA_NO_ERROR ? wxCOND_NO_ERROR : wxCOND_MISC_ERROR; } -bool wxCondition::Wait(unsigned long sec, - unsigned long nsec) +wxCondError wxConditionInternal::WaitTimeout( unsigned long milliseconds ) { - return m_internal->Wait(sec*1000 + nsec/1000000); + IncrementAtomic( &m_numWaiters ); + + m_mutex.Unlock(); + + // a race condition can occur at this point in the code + // + // please see the comments in Wait(), for details + + wxSemaError err = m_semaphore.WaitTimeout(milliseconds); + + if ( err == wxSEMA_TIMEOUT ) + { + // 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. + wxCriticalSectionLocker lock(m_csWaiters); + + err = m_semaphore.WaitTimeout(0); + + if ( err != wxSEMA_NO_ERROR ) + { + m_numWaiters--; + } + } + + m_mutex.Lock(); + + return err == wxSEMA_NO_ERROR ? wxCOND_NO_ERROR : wxCOND_MISC_ERROR; } -void wxCondition::Signal() +wxCondError wxConditionInternal::Signal() { - // set the event to signaled: if a thread is already waiting on it, it will - // be woken up, otherwise the event will remain in the signaled state until - // someone waits on it. In any case, the system will return it to a non - // signalled state afterwards. If multiple threads are waiting, only one - // will be woken up. - m_internal->Signal() ; + wxCriticalSectionLocker lock(m_csWaiters); + + if ( m_numWaiters > 0 ) + { + // increment the semaphore by 1 + if ( m_semaphore.Post() != wxSEMA_NO_ERROR ) + return wxCOND_MISC_ERROR; + + m_numWaiters--; + } + + return wxCOND_NO_ERROR; } -void wxCondition::Broadcast() +wxCondError wxConditionInternal::Broadcast() { - // this works because all these threads are already waiting and so each - // SetEvent() inside Signal() is really a PulseEvent() because the event - // state is immediately returned to non-signaled - for ( int i = 0; i < m_internal->m_waiters.Count(); i++ ) + wxCriticalSectionLocker lock(m_csWaiters); + + while ( m_numWaiters > 0 ) { - Signal(); + if ( m_semaphore.Post() != wxSEMA_NO_ERROR ) + return wxCOND_MISC_ERROR; + + m_numWaiters--; } + + return wxCOND_NO_ERROR; } +#endif // ---------------------------------------------------------------------------- // wxCriticalSection implementation // ---------------------------------------------------------------------------- -// it's implemented as a mutex on mac os, so it is defined in the headers +// XXX currently implemented as mutex in headers. Change to critical section. // ---------------------------------------------------------------------------- // wxThread implementation @@ -309,191 +916,337 @@ class wxThreadInternal public: wxThreadInternal() { - m_tid = kNoThreadID ; + m_tid = kInvalidID; m_state = STATE_NEW; - m_priority = WXTHREAD_DEFAULT_PRIORITY; - } + m_prio = WXTHREAD_DEFAULT_PRIORITY; + m_notifyQueueId = kInvalidID; + m_exitcode = 0; + m_cancelled = false ; - ~wxThreadInternal() - { + // set to true only when the thread starts waiting on m_semSuspend + m_isPaused = false; + + // defaults for joinable threads + m_shouldBeJoined = true; + m_isDetached = false; } - void Free() + virtual ~wxThreadInternal() { + if ( m_notifyQueueId) + { + MPDeleteQueue( m_notifyQueueId ); + m_notifyQueueId = kInvalidID ; + } } + // thread function + static OSStatus MacThreadStart(void* arg); + // create a new (suspended) thread (for the given thread object) bool Create(wxThread *thread, unsigned int stackSize); - // suspend/resume/terminate - bool Suspend(); - bool Resume(); - void Cancel() { m_state = STATE_CANCELED; } + // thread actions - // thread state - void SetState(wxThreadState state) { m_state = state; } - wxThreadState GetState() const { return m_state; } + // start the thread + wxThreadError Run(); - // thread priority - void SetPriority(unsigned int priority); - unsigned int GetPriority() const { return m_priority; } + // unblock the thread allowing it to run + void SignalRun() { m_semRun.Post(); } - void SetResult( void *res ) { m_result = res ; } - void *GetResult() { return m_result ; } + // ask the thread to terminate + void Wait(); - // thread handle and id - ThreadID GetId() const { return m_tid; } + // go to sleep until Resume() is called + void Pause(); - // thread function - static pascal void* MacThreadStart(wxThread* arg); + // resume the thread + void Resume(); + + // accessors + // priority + int GetPriority() const + { return m_prio; } + void SetPriority(int prio); + + // state + wxThreadState GetState() const + { return m_state; } + void SetState(wxThreadState state) + { m_state = state; } + + // Get the ID of this thread's underlying MP Services task. + MPTaskID GetId() const + { return m_tid; } + + void SetCancelFlag() + { m_cancelled = true; } + + bool WasCancelled() const + { return m_cancelled; } + + // exit code + void SetExitCode(wxThread::ExitCode exitcode) + { m_exitcode = exitcode; } + wxThread::ExitCode GetExitCode() const + { return m_exitcode; } + + // the pause flag + void SetReallyPaused(bool paused) + { m_isPaused = paused; } + bool IsReallyPaused() const + { return m_isPaused; } + + // tell the thread that it is a detached one + void Detach() + { + wxCriticalSectionLocker lock(m_csJoinFlag); + + m_shouldBeJoined = false; + m_isDetached = true; + } private: - wxThreadState m_state; // state, see wxThreadState enum - unsigned int m_priority; // thread priority in "wx" units - ThreadID m_tid; // thread id - void* m_result; - static ThreadEntryUPP s_threadEntry ; -}; + // the thread we're associated with + wxThread * m_thread; -static wxArrayPtrVoid s_threads ; + MPTaskID m_tid; // thread id + MPQueueID m_notifyQueueId; // its notification queue + + wxThreadState m_state; // see wxThreadState enum + int m_prio; // in wxWidgets units: from 0 to 100 + + // this flag is set when the thread should terminate + bool m_cancelled; + + // this flag is set when the thread is blocking on m_semSuspend + bool m_isPaused; + + // the thread exit code - only used for joinable (!detached) threads and + // is only valid after the thread termination + wxThread::ExitCode m_exitcode; + + // many threads may call Wait(), but only one of them should call + // pthread_join(), so we have to keep track of this + wxCriticalSection m_csJoinFlag; + bool m_shouldBeJoined; + bool m_isDetached; + + // this semaphore is posted by Run() and the threads Entry() is not + // called before it is done + wxSemaphore m_semRun; + + // this one is signaled when the thread should resume after having been + // Pause()d + wxSemaphore m_semSuspend; +}; -ThreadEntryUPP wxThreadInternal::s_threadEntry = NULL ; -pascal void* wxThreadInternal::MacThreadStart(wxThread *thread) +OSStatus wxThreadInternal::MacThreadStart(void *parameter) { - // first of all, check whether we hadn't been cancelled already - if ( thread->m_internal->GetState() == STATE_EXITED ) + wxThread* thread = (wxThread*) parameter ; + wxThreadInternal *pthread = thread->m_internal; + + // add to TLS so that This() will work + verify_noerr( MPSetTaskStorageValue( gs_tlsForWXThread , (TaskStorageValue) thread ) ) ; + + // have to declare this before pthread_cleanup_push() which defines a + // block! + bool dontRunAtAll; + + // wait for the semaphore to be posted from Run() + pthread->m_semRun.Wait(); + + // test whether we should run the run at all - may be it was deleted + // before it started to Run()? { - return (void*)-1; - } + wxCriticalSectionLocker lock(thread->m_critsect); - void* rc = thread->Entry(); + dontRunAtAll = pthread->GetState() == STATE_NEW && + pthread->WasCancelled(); + } - // enter m_critsect before changing the thread state - thread->m_critsect.Enter(); - bool wasCancelled = thread->m_internal->GetState() == STATE_CANCELED; - thread->m_internal->SetState(STATE_EXITED); - thread->m_critsect.Leave(); + if ( !dontRunAtAll ) + { + pthread->m_exitcode = thread->Entry(); - thread->OnExit(); + { + wxCriticalSectionLocker lock(thread->m_critsect); + pthread->SetState( STATE_EXITED ); + } + } - // if the thread was cancelled (from Delete()), then it the handle is still - // needed there - if ( thread->IsDetached() && !wasCancelled ) + if ( dontRunAtAll ) { - // auto delete - delete thread; + if ( pthread->m_isDetached ) + delete thread; + + return -1; } - //else: the joinable threads handle will be closed when Wait() is done + else + { + // on Mac for the running code, + // the correct thread termination is to return - return rc; -} -void wxThreadInternal::SetPriority(unsigned int priority) -{ - // Priorities don't exist on Mac + // terminate the thread + thread->Exit( pthread->m_exitcode ); + + return (OSStatus) NULL; // pthread->m_exitcode; + } } -bool wxThreadInternal::Create(wxThread *thread, unsigned int stackSize) +bool wxThreadInternal::Create( wxThread *thread, unsigned int stackSize ) { - if ( s_threadEntry == NULL ) - { - s_threadEntry = NewThreadEntryUPP( (ThreadEntryProcPtr) MacThreadStart ) ; - } - OSErr err = NewThread( kCooperativeThread, - s_threadEntry, - (void*) thread, - stackSize, - kNewSuspend, - &m_result, - &m_tid ); + wxASSERT_MSG( m_state == STATE_NEW && !m_tid, + wxT("Create()ing thread twice?") ); + + OSStatus err = noErr; + m_thread = thread; - if ( err != noErr ) + if ( m_notifyQueueId == kInvalidID ) { - wxLogSysError(_("Can't create thread")); - return FALSE; + OSStatus err = MPCreateQueue( &m_notifyQueueId ); + if (err != noErr) + { + wxLogSysError( wxT("Cant create the thread event queue") ); + + return false; + } } - if ( m_priority != WXTHREAD_DEFAULT_PRIORITY ) + m_state = STATE_NEW; + + err = MPCreateTask( + MacThreadStart, (void*)m_thread, stackSize, + m_notifyQueueId, &m_exitcode, 0, 0, &m_tid ); + + if (err != noErr) { - SetPriority(m_priority); + wxLogSysError( wxT("Can't create thread") ); + + return false; } - return TRUE; + if ( m_prio != WXTHREAD_DEFAULT_PRIORITY ) + SetPriority( m_prio ); + + return true; } -bool wxThreadInternal::Suspend() +void wxThreadInternal::SetPriority( int priority ) { - OSErr err ; - - ::ThreadBeginCritical(); + m_prio = priority; - if ( m_state != STATE_RUNNING ) + if (m_tid) { - ::ThreadEndCritical() ; - wxLogSysError(_("Can not suspend thread %x"), m_tid); - return FALSE; + // Mac priorities range from 1 to 10,000, with a default of 100. + // wxWidgets priorities range from 0 to 100 with a default of 50. + // We can map wxWidgets to Mac priorities easily by assuming + // the former uses a logarithmic scale. + const unsigned int macPriority = (int)( exp( priority / 25.0 * log( 10.0)) + 0.5); + + MPSetTaskWeight( m_tid, macPriority ); } +} + +wxThreadError wxThreadInternal::Run() +{ + wxCHECK_MSG( GetState() == STATE_NEW, wxTHREAD_RUNNING, + wxT("thread may only be started once after Create()") ); - m_state = STATE_PAUSED; + SetState( STATE_RUNNING ); - err = ::SetThreadStateEndCritical(m_tid, kStoppedThreadState, kNoThreadID); + // wake up threads waiting for our start + SignalRun(); - return TRUE; + return wxTHREAD_NO_ERROR; } -bool wxThreadInternal::Resume() +void wxThreadInternal::Wait() { - ThreadID current ; - OSErr err ; - err = MacGetCurrentThread( ¤t ) ; + wxCHECK_RET( !m_isDetached, wxT("can't wait for a detached thread") ); - wxASSERT( err == noErr ) ; - wxASSERT( current != m_tid ) ; + // if the thread we're waiting for is waiting for the GUI mutex, we will + // deadlock so make sure we release it temporarily + if ( wxThread::IsMain() ) + { + // give the thread we're waiting for chance to do the GUI call + // it might be in, we don't do this conditionally as the to be waited on + // thread might have to acquire the mutex later but before terminating + if ( wxGuiOwnedByMainThread() ) + wxMutexGuiLeave(); + } - ::ThreadBeginCritical(); - if ( m_state != STATE_PAUSED && m_state != STATE_NEW ) { - ::ThreadEndCritical() ; - wxLogSysError(_("Can not resume thread %x"), m_tid); - return FALSE; + wxCriticalSectionLocker lock(m_csJoinFlag); + if ( m_shouldBeJoined ) + { + void *param1, *param2, *rc; + + OSStatus err = MPWaitOnQueue( + m_notifyQueueId, + ¶m1, + ¶m2, + &rc, + kDurationForever ); + if (err != noErr) + { + wxLogSysError( wxT( "Cannot wait for thread termination.")); + rc = (void*) -1; + } + + // actually param1 would be the address of m_exitcode + // but we don't need this here + m_exitcode = rc; + + m_shouldBeJoined = false; + } } - err = ::SetThreadStateEndCritical(m_tid, kReadyThreadState, kNoThreadID); - wxASSERT( err == noErr ) ; - - m_state = STATE_RUNNING; - ::ThreadEndCritical() ; - ::YieldToAnyThread() ; - return TRUE; } -// static functions -// ---------------- -wxThread *wxThread::This() +void wxThreadInternal::Pause() { - wxMacStCritical critical ; + // the state is set from the thread which pauses us first, this function + // is called later so the state should have been already set + wxCHECK_RET( m_state == STATE_PAUSED, + wxT("thread must first be paused with wxThread::Pause().") ); - ThreadID current ; - OSErr err ; + // wait until the semaphore is Post()ed from Resume() + m_semSuspend.Wait(); +} - err = MacGetCurrentThread( ¤t ) ; +void wxThreadInternal::Resume() +{ + wxCHECK_RET( m_state == STATE_PAUSED, + wxT("can't resume thread which is not suspended.") ); - for ( int i = 0 ; i < s_threads.Count() ; ++i ) + // the thread might be not actually paused yet - if there were no call to + // TestDestroy() since the last call to Pause() for example + if ( IsReallyPaused() ) { - if ( ( (wxThread*) s_threads[i] )->GetId() == current ) - return (wxThread*) s_threads[i] ; + // wake up Pause() + m_semSuspend.Post(); + + // reset the flag + SetReallyPaused( false ); } - wxLogSysError(_("Couldn't get the current thread pointer")); - return NULL; + SetState( STATE_RUNNING ); } -bool wxThread::IsMain() +// static functions +// ---------------- + +wxThread *wxThread::This() { - ThreadID current ; - OSErr err ; + wxThread* thr = (wxThread*) MPGetTaskStorageValue( gs_tlsForWXThread ) ; + + return thr; +} - err = MacGetCurrentThread( ¤t ) ; - return current == gs_idMainThread; +bool wxThread::IsMain() +{ + return GetCurrentId() == gs_idMainThread || gs_idMainThread == kInvalidID ; } #ifdef Yield @@ -502,76 +1255,80 @@ bool wxThread::IsMain() void wxThread::Yield() { - ::YieldToAnyThread() ; +#if TARGET_API_MAC_OSX + CFRunLoopRunInMode( kCFRunLoopDefaultMode , 0 , true ) ; +#endif + + MPYield(); } -void wxThread::Sleep(unsigned long milliseconds) +void wxThread::Sleep( unsigned long milliseconds ) { - clock_t start = clock() ; - do - { - YieldToAnyThread() ; - } while( clock() - start < milliseconds / CLOCKS_PER_SEC ) ; + AbsoluteTime wakeup = AddDurationToAbsolute( milliseconds, UpTime() ); + MPDelayUntil( &wakeup ); } int wxThread::GetCPUCount() { - // we will use whatever MP API will be used for the new MP Macs - return 1; + return MPProcessors(); } unsigned long wxThread::GetCurrentId() { - ThreadID current ; - MacGetCurrentThread( ¤t ) ; - return (unsigned long)current; + return (unsigned long)MPCurrentTaskID(); } -bool wxThread::SetConcurrency(size_t level) +bool wxThread::SetConcurrency( size_t level ) { - wxASSERT_MSG( IsMain(), _T("should only be called from the main thread") ); - - // ok only for the default one - if ( level == 0 ) - return 0; - - // how many CPUs have we got? - if ( GetCPUCount() == 1 ) - { - // don't bother with all this complicated stuff - on a single - // processor system it doesn't make much sense anyhow - return level == 1; - } - - return TRUE ; + // Cannot be set in MacOS. + return false; } -// ctor and dtor -// ------------- - -wxThread::wxThread(wxThreadKind kind) +wxThread::wxThread( wxThreadKind kind ) { + g_numberOfThreads++; m_internal = new wxThreadInternal(); - m_isDetached = kind == wxTHREAD_DETACHED; - s_threads.Add( (void*) this ) ; + m_isDetached = (kind == wxTHREAD_DETACHED); } wxThread::~wxThread() { - s_threads.Remove( (void*) this ) ; - delete m_internal; -} + wxASSERT_MSG( g_numberOfThreads>0 , wxT("More threads deleted than created.") ) ; + + g_numberOfThreads--; -// create/start thread -// ------------------- +#ifdef __WXDEBUG__ + m_critsect.Enter(); -wxThreadError wxThread::Create(unsigned int stackSize) + // check that the thread either exited or couldn't be created + if ( m_internal->GetState() != STATE_EXITED && + m_internal->GetState() != STATE_NEW ) + { + wxLogDebug( + wxT("The thread %ld is being destroyed although it is still running! The application may crash."), + GetId() ); + } + + m_critsect.Leave(); +#endif + + wxDELETE( m_internal ) ; +} + +wxThreadError wxThread::Create( unsigned int stackSize ) { wxCriticalSectionLocker lock(m_critsect); + if ( m_isDetached ) + m_internal->Detach() ; + if ( !m_internal->Create(this, stackSize) ) + { + m_internal->SetState( STATE_EXITED ); + return wxTHREAD_NO_RESOURCE; + } return wxTHREAD_NO_ERROR; } @@ -580,228 +1337,297 @@ wxThreadError wxThread::Run() { wxCriticalSectionLocker lock(m_critsect); - if ( m_internal->GetState() != STATE_NEW ) - { - // actually, it may be almost any state at all, not only STATE_RUNNING - return wxTHREAD_RUNNING; - } + wxCHECK_MSG( m_internal->GetId(), wxTHREAD_MISC_ERROR, + wxT("must call wxThread::Create() first") ); - // the thread has just been created and is still suspended - let it run - return Resume(); + return m_internal->Run(); } -// suspend/resume thread -// --------------------- +// ----------------------------------------------------------------------------- +// pause/resume +// ----------------------------------------------------------------------------- wxThreadError wxThread::Pause() { - wxCriticalSectionLocker lock(m_critsect); - - return m_internal->Suspend() ? wxTHREAD_NO_ERROR : wxTHREAD_MISC_ERROR; -} + wxCHECK_MSG( This() != this, wxTHREAD_MISC_ERROR, + _T("a thread can't pause itself") ); -wxThreadError wxThread::Resume() -{ wxCriticalSectionLocker lock(m_critsect); - return m_internal->Resume() ? wxTHREAD_NO_ERROR : wxTHREAD_MISC_ERROR; -} - -// stopping thread -// --------------- - -wxThread::ExitCode wxThread::Wait() -{ - // although under MacOS we can wait for any thread, it's an error to - // wait for a detached one in wxWin API - wxCHECK_MSG( !IsDetached(), (ExitCode)-1, - _T("can't wait for detached thread") ); - - ExitCode rc = (ExitCode)-1; + if ( m_internal->GetState() != STATE_RUNNING ) + { + wxLogDebug( wxT("Can't pause thread which is not running.") ); - (void)Delete(&rc); + return wxTHREAD_NOT_RUNNING; + } - m_internal->Free(); + // just set a flag, the thread will be really paused only during the next + // call to TestDestroy() + m_internal->SetState( STATE_PAUSED ); - return rc; + return wxTHREAD_NO_ERROR; } -wxThreadError wxThread::Delete(ExitCode *pRc) +wxThreadError wxThread::Resume() { - ExitCode rc = 0; + wxCHECK_MSG( This() != this, wxTHREAD_MISC_ERROR, + wxT("a thread can't resume itself") ); - // Delete() is always safe to call, so consider all possible states + wxCriticalSectionLocker lock(m_critsect); - // has the thread started to run? - bool shouldResume = FALSE; + wxThreadState state = m_internal->GetState(); + switch ( state ) { - wxCriticalSectionLocker lock(m_critsect); + case STATE_PAUSED: + m_internal->Resume(); + return wxTHREAD_NO_ERROR; - if ( m_internal->GetState() == STATE_NEW ) - { - // WinThreadStart() will see it and terminate immediately - m_internal->SetState(STATE_EXITED); + case STATE_EXITED: + return wxTHREAD_NO_ERROR; - shouldResume = TRUE; - } + default: + wxLogDebug( wxT("Attempt to resume a thread which is not paused.") ); + + return wxTHREAD_MISC_ERROR; } +} - // is the thread paused? - if ( shouldResume || IsPaused() ) - Resume(); +// ----------------------------------------------------------------------------- +// exiting thread +// ----------------------------------------------------------------------------- - // does is still run? - if ( IsRunning() ) - { - if ( IsMain() ) - { - // set flag for wxIsWaitingForThread() - gs_waitingForThread = TRUE; +wxThread::ExitCode wxThread::Wait() +{ + wxCHECK_MSG( This() != this, (ExitCode)-1, + wxT("a thread can't wait for itself") ); -#if wxUSE_GUI - wxBeginBusyCursor(); -#endif // wxUSE_GUI - } + wxCHECK_MSG( !m_isDetached, (ExitCode)-1, + wxT("can't wait for detached thread") ); - // ask the thread to terminate - { - wxCriticalSectionLocker lock(m_critsect); + m_internal->Wait(); - m_internal->Cancel(); - } + return m_internal->GetExitCode(); +} -#if wxUSE_GUI - // simply wait for the thread to terminate - while( TestDestroy() ) - { - ::YieldToAnyThread() ; - } -#else // !wxUSE_GUI - // simply wait for the thread to terminate - while( TestDestroy() ) - { - ::YieldToAnyThread() ; - } -#endif // wxUSE_GUI/!wxUSE_GUI +wxThreadError wxThread::Delete(ExitCode *rc) +{ + wxCHECK_MSG( This() != this, wxTHREAD_MISC_ERROR, + wxT("a thread can't delete itself") ); - if ( IsMain() ) - { - gs_waitingForThread = FALSE; + bool isDetached = m_isDetached; -#if wxUSE_GUI - wxEndBusyCursor(); -#endif // wxUSE_GUI - } - } + m_critsect.Enter(); + wxThreadState state = m_internal->GetState(); - // if ( !::GetExitCodeThread(hThread, (LPDWORD)&rc) ) - { - wxLogLastError("GetExitCodeThread"); + // ask the thread to stop + m_internal->SetCancelFlag(); - rc = (ExitCode)-1; - } + m_critsect.Leave(); - if ( IsDetached() ) + switch ( state ) { - // if the thread exits normally, this is done in WinThreadStart, but in - // this case it would have been too early because - // MsgWaitForMultipleObject() would fail if the therad handle was - // closed while we were waiting on it, so we must do it here - delete this; + case STATE_NEW: + // we need to wake up the thread so that PthreadStart() will + // terminate - right now it's blocking on run semaphore in + // PthreadStart() + m_internal->SignalRun(); + + // fall through + + case STATE_EXITED: + // nothing to do + break; + + case STATE_PAUSED: + // resume the thread first + m_internal->Resume(); + + // fall through + + default: + if ( !isDetached ) + { + // wait until the thread stops + m_internal->Wait(); + + if ( rc ) + { + // return the exit code of the thread + *rc = m_internal->GetExitCode(); + } + } } - // wxASSERT_MSG( (DWORD)rc != STILL_ACTIVE, - // wxT("thread must be already terminated.") ); - - if ( pRc ) - *pRc = rc; - - return rc == (ExitCode)-1 ? wxTHREAD_MISC_ERROR : wxTHREAD_NO_ERROR; + return wxTHREAD_NO_ERROR; } wxThreadError wxThread::Kill() { - if ( !IsRunning() ) - return wxTHREAD_NOT_RUNNING; + wxCHECK_MSG( This() != this, wxTHREAD_MISC_ERROR, + wxT("a thread can't kill itself") ); -// if ( !::TerminateThread(m_internal->GetHandle(), (DWORD)-1) ) + switch ( m_internal->GetState() ) { - wxLogSysError(_("Couldn't terminate thread")); - - return wxTHREAD_MISC_ERROR; + case STATE_NEW: + case STATE_EXITED: + return wxTHREAD_NOT_RUNNING; + + case STATE_PAUSED: + // resume the thread first + Resume(); + + // fall through + + default: + OSStatus err = MPTerminateTask( m_internal->GetId() , -1 ) ; + if (err != noErr) + { + wxLogError( wxT("Failed to terminate a thread.") ); + + return wxTHREAD_MISC_ERROR; + } + + if ( m_isDetached ) + { + delete this ; + } + else + { + // this should be retrieved by Wait actually + m_internal->SetExitCode( (void*)-1 ); + } + + return wxTHREAD_NO_ERROR; } +} + +void wxThread::Exit( ExitCode status ) +{ + wxASSERT_MSG( This() == this, + wxT("wxThread::Exit() can only be called in the context of the same thread") ); + + // don't enter m_critsect before calling OnExit() because the user code + // might deadlock if, for example, it signals a condition in OnExit() (a + // common case) while the main thread calls any of functions entering + // m_critsect on us (almost all of them do) + OnExit(); - m_internal->Free(); + MPTaskID threadid = m_internal->GetId(); if ( IsDetached() ) { delete this; } + else // joinable + { + // update the status of the joinable thread + wxCriticalSectionLocker lock( m_critsect ); + m_internal->SetState( STATE_EXITED ); + } - return wxTHREAD_NO_ERROR; + MPTerminateTask( threadid, (long)status ); } -void wxThread::Exit(ExitCode status) +// also test whether we were paused +bool wxThread::TestDestroy() { - m_internal->Free(); + wxASSERT_MSG( This() == this, + wxT("wxThread::TestDestroy() can only be called in the context of the same thread") ); - if ( IsDetached() ) + m_critsect.Enter(); + + if ( m_internal->GetState() == STATE_PAUSED ) { - delete this; - } + m_internal->SetReallyPaused( true ); - m_internal->SetResult( status ) ; + // leave the crit section or the other threads will stop too if they attempt + // to call any of (seemingly harmless) IsXXX() functions while we sleep + m_critsect.Leave(); -/* -#if defined(__VISUALC__) || (defined(__BORLANDC__) && (__BORLANDC__ >= 0x500)) - _endthreadex((unsigned)status); -#else // !VC++ - ::ExitThread((DWORD)status); -#endif // VC++/!VC++ -*/ - wxFAIL_MSG(wxT("Couldn't return from ExitThread()!")); + m_internal->Pause(); + } + else + { + // thread wasn't requested to pause, nothing to do + m_critsect.Leave(); + } + + return m_internal->WasCancelled(); } +// ----------------------------------------------------------------------------- // priority setting -// ---------------- - -// since all these calls are execute cooperatively we don't have to use the critical section +// ----------------------------------------------------------------------------- void wxThread::SetPriority(unsigned int prio) { - m_internal->SetPriority(prio); + wxCHECK_RET( ((int)WXTHREAD_MIN_PRIORITY <= (int)prio) && + ((int)prio <= (int)WXTHREAD_MAX_PRIORITY), + wxT("invalid thread priority") ); + + wxCriticalSectionLocker lock(m_critsect); + + switch ( m_internal->GetState() ) + { + case STATE_RUNNING: + case STATE_PAUSED: + case STATE_NEW: + // thread not yet started, priority will be set when it is + m_internal->SetPriority( prio ); + break; + + case STATE_EXITED: + default: + wxFAIL_MSG( wxT("impossible to set thread priority in this state") ); + } } unsigned int wxThread::GetPriority() const { + wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect); // const_cast + return m_internal->GetPriority(); } unsigned long wxThread::GetId() const { + wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect); // const_cast + return (unsigned long)m_internal->GetId(); } +// ----------------------------------------------------------------------------- +// state tests +// ----------------------------------------------------------------------------- + bool wxThread::IsRunning() const { + wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect); + return m_internal->GetState() == STATE_RUNNING; } bool wxThread::IsAlive() const { - return (m_internal->GetState() == STATE_RUNNING) || - (m_internal->GetState() == STATE_PAUSED); + wxCriticalSectionLocker lock((wxCriticalSection&)m_critsect); + + switch ( m_internal->GetState() ) + { + case STATE_RUNNING: + case STATE_PAUSED: + return true; + + default: + return false; + } } bool wxThread::IsPaused() const { - return m_internal->GetState() == STATE_PAUSED; -} + wxCriticalSectionLocker lock((wxCriticalSection&)m_critsect); -bool wxThread::TestDestroy() -{ - return m_internal->GetState() == STATE_CANCELED; + return (m_internal->GetState() == STATE_PAUSED); } // ---------------------------------------------------------------------------- @@ -822,64 +1648,146 @@ IMPLEMENT_DYNAMIC_CLASS(wxThreadModule, wxModule) bool wxThreadModule::OnInit() { - long response; - bool hasThreadManager ; - hasThreadManager = Gestalt( gestaltThreadMgrAttr, &response) == noErr && response & 1; -#if !TARGET_CARBON -#if GENERATINGCFM - // verify presence of shared library - hasThreadManager = hasThreadManager && ((Ptr)NewThread != (Ptr)kUnresolvedCFragSymbolAddress); -#endif + bool hasThreadManager = +#ifdef __LP64__ + true ; // TODO VERIFY IN NEXT BUILD +#else + MPLibraryIsLoaded(); #endif + if ( !hasThreadManager ) { - wxMessageBox( "Error" , "Thread Support is not available on this System" , wxOK ) ; - return FALSE ; + wxLogError( wxT("MP thread support is not available on this system" ) ) ; + + return false; } - // no error return for GetCurrentThreadId() - MacGetCurrentThread( &gs_idMainThread ) ; + // main thread's This() is NULL + verify_noerr( MPAllocateTaskStorageIndex( &gs_tlsForWXThread ) ) ; + verify_noerr( MPSetTaskStorageValue( gs_tlsForWXThread, 0 ) ) ; + + gs_idMainThread = wxThread::GetCurrentId(); + gs_critsectWaitingForGui = new wxCriticalSection(); + + gs_critsectGui = new wxCriticalSection(); + gs_critsectGui->Enter(); - return TRUE; + return true; } void wxThreadModule::OnExit() { + if ( gs_critsectGui ) + { + if ( !wxGuiOwnedByMainThread() ) + { + gs_critsectGui->Enter(); + gs_bGuiOwnedByMainThread = true; + } + + gs_critsectGui->Leave(); + delete gs_critsectGui; + gs_critsectGui = NULL; + } + + delete gs_critsectWaitingForGui; + gs_critsectWaitingForGui = NULL; } // ---------------------------------------------------------------------------- -// under MacOS we don't have currently preemptive threads, so any thread may access -// the GUI at any time +// GUI Serialization copied from MSW implementation // ---------------------------------------------------------------------------- -void WXDLLEXPORT wxMutexGuiEnter() +void WXDLLIMPEXP_BASE wxMutexGuiEnter() { + // this would dead lock everything... + wxASSERT_MSG( !wxThread::IsMain(), + wxT("main thread doesn't want to block in wxMutexGuiEnter()!") ); + + // the order in which we enter the critical sections here is crucial!! + + // set the flag telling to the main thread that we want to do some GUI + { + wxCriticalSectionLocker enter(*gs_critsectWaitingForGui); + + gs_nWaitingForGui++; + } + + wxWakeUpMainThread(); + + // now we may block here because the main thread will soon let us in + // (during the next iteration of OnIdle()) + gs_critsectGui->Enter(); } -void WXDLLEXPORT wxMutexGuiLeave() +void WXDLLIMPEXP_BASE wxMutexGuiLeave() { + wxCriticalSectionLocker enter(*gs_critsectWaitingForGui); + + if ( wxThread::IsMain() ) + { + gs_bGuiOwnedByMainThread = false; + } + else + { + // decrement the number of threads waiting for GUI access now + wxASSERT_MSG( gs_nWaitingForGui > 0, + wxT("calling wxMutexGuiLeave() without entering it first?") ); + + gs_nWaitingForGui--; + + wxWakeUpMainThread(); + } + + gs_critsectGui->Leave(); } -void WXDLLEXPORT wxMutexGuiLeaveOrEnter() +void WXDLLIMPEXP_BASE wxMutexGuiLeaveOrEnter() { + wxASSERT_MSG( wxThread::IsMain(), + wxT("only main thread may call wxMutexGuiLeaveOrEnter()!") ); + + if ( !gs_critsectWaitingForGui ) + return; + + wxCriticalSectionLocker enter(*gs_critsectWaitingForGui); + + if ( gs_nWaitingForGui == 0 ) + { + // no threads are waiting for GUI - so we may acquire the lock without + // any danger (but only if we don't already have it) + if ( !wxGuiOwnedByMainThread() ) + { + gs_critsectGui->Enter(); + + gs_bGuiOwnedByMainThread = true; + } + //else: already have it, nothing to do + } + else + { + // some threads are waiting, release the GUI lock if we have it + if ( wxGuiOwnedByMainThread() ) + wxMutexGuiLeave(); + //else: some other worker thread is doing GUI + } } -bool WXDLLEXPORT wxGuiOwnedByMainThread() +bool WXDLLIMPEXP_BASE wxGuiOwnedByMainThread() { - return false ; + return gs_bGuiOwnedByMainThread; } // wake up the main thread void WXDLLEXPORT wxWakeUpMainThread() { - wxMacWakeUp() ; + wxMacWakeUp(); } -bool WXDLLEXPORT wxIsWaitingForThread() -{ - return false ; -} +// ---------------------------------------------------------------------------- +// include common implementation code +// ---------------------------------------------------------------------------- -#endif // wxUSE_THREADS +#include "wx/thrimpl.cpp" -// vi:sts=4:sw=4:et +#endif // wxUSE_THREADS