X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/881b5a06e24012a31a9e535475a2f652d7256780..d109c5849ed117966311a44875c97ae3d77b44e5:/src/mac/carbon/thread.cpp diff --git a/src/mac/carbon/thread.cpp b/src/mac/carbon/thread.cpp index c4fc7e90f9..6c477b3685 100644 --- a/src/mac/carbon/thread.cpp +++ b/src/mac/carbon/thread.cpp @@ -114,24 +114,48 @@ MPCriticalRegionID gs_guiCritical = kInvalidID; to use two indices one for each 32 bit part as the MP implementation is limited to longs. - I have two implementations for mutexes : + 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 + 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() +{ + 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 0 -#define wxUSE_MAC_PTHREADS_MUTEX 1 +#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 @@ -347,6 +371,7 @@ wxMutexInternal::~wxMutexInternal() { if ( m_semaphore != kInvalidID ) MPDeleteSemaphore( m_semaphore); + MPYield() ; } wxMutexError wxMutexInternal::Lock() @@ -383,12 +408,12 @@ wxMutexError wxMutexInternal::Unlock() { wxCHECK_MSG( m_isOk , wxMUTEX_MISC_ERROR , wxT("Invalid Mutex") ) ; OSStatus err = MPSignalSemaphore( m_semaphore); + MPYield() ; if ( err) { wxLogSysError(_("Could not unlock mutex")); return wxMUTEX_MISC_ERROR; } - return wxMUTEX_NO_ERROR; } @@ -427,6 +452,7 @@ wxMutexInternal::~wxMutexInternal() { if ( m_critRegion != kInvalidID ) MPDeleteCriticalRegion( m_critRegion); + MPYield() ; } wxMutexError wxMutexInternal::Lock() @@ -463,6 +489,7 @@ wxMutexError wxMutexInternal::Unlock() { wxCHECK_MSG( m_isOk , wxMUTEX_MISC_ERROR , wxT("Invalid Mutex") ) ; OSStatus err = MPExitCriticalRegion( m_critRegion); + MPYield() ; if ( err) { wxLogSysError(_("Could not unlock mutex")); @@ -524,6 +551,7 @@ wxSemaphoreInternal::~wxSemaphoreInternal() { if( m_semaphore != kInvalidID ) MPDeleteSemaphore( m_semaphore); + MPYield() ; } wxSemaError wxSemaphoreInternal::WaitTimeout(unsigned long milliseconds) @@ -543,6 +571,7 @@ wxSemaError wxSemaphoreInternal::WaitTimeout(unsigned long milliseconds) wxSemaError wxSemaphoreInternal::Post() { OSStatus err = MPSignalSemaphore( m_semaphore); + MPYield() ; if ( err) { return wxSEMA_MISC_ERROR; @@ -957,7 +986,7 @@ private: MPQueueID m_notifyQueueId; // its notification queue wxThreadState m_state; // see wxThreadState enum - int m_prio; // in wxWindows units: from 0 to 100 + int m_prio; // in wxWidgets units: from 0 to 100 // this flag is set when the thread should terminate bool m_cancelled; @@ -1087,8 +1116,8 @@ void wxThreadInternal::SetPriority( int priority) if ( m_tid) { // Mac priorities range from 1 to 10,000, with a default of 100. - // wxWindows priorities range from 0 to 100 with a default of 50. - // We can map wxWindows to Mac priorities easily by assuming + // 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); @@ -1116,7 +1145,15 @@ void wxThreadInternal::Wait() // 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() ) - wxMutexGuiLeave(); + { + // 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(); + } + } { wxCriticalSectionLocker lock(m_csJoinFlag); @@ -1145,10 +1182,6 @@ void wxThreadInternal::Wait() m_shouldBeJoined = FALSE; } } - - // reacquire GUI mutex - if ( wxThread::IsMain() ) - wxMutexGuiEnter(); } void wxThreadInternal::Pause() @@ -1192,7 +1225,7 @@ wxThread *wxThread::This() bool wxThread::IsMain() { - return GetCurrentId() == gs_idMainThread; + return GetCurrentId() == gs_idMainThread || gs_idMainThread == kInvalidID ; } #ifdef Yield @@ -1403,7 +1436,6 @@ wxThreadError wxThread::Delete(ExitCode *rc) *rc = m_internal->GetExitCode(); } } - //else: can't wait for detached threads } return wxTHREAD_NO_ERROR; @@ -1460,7 +1492,7 @@ void wxThread::Exit(ExitCode status) // m_critsect on us (almost all of them do) OnExit(); - MPTerminateTask( m_internal->GetId() , (long) status) ; + MPTaskID threadid = m_internal->GetId() ; if ( IsDetached() ) { @@ -1472,6 +1504,7 @@ void wxThread::Exit(ExitCode status) wxCriticalSectionLocker lock(m_critsect); m_internal->SetState(STATE_EXITED); } + MPTerminateTask( threadid , (long) status) ; } // also test whether we were paused @@ -1621,6 +1654,11 @@ void wxThreadModule::OnExit() { if ( gs_critsectGui ) { + if ( !wxGuiOwnedByMainThread() ) + { + gs_critsectGui->Enter(); + gs_bGuiOwnedByMainThread = true; + } gs_critsectGui->Leave(); delete gs_critsectGui; gs_critsectGui = NULL;