X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/76a5e5d21ee1a6230d777ce0209b2df4c6075f0f..74698d3a22d5e611bbcd731a3ade616c66cfaca6:/src/mac/thread.cpp?ds=sidebyside diff --git a/src/mac/thread.cpp b/src/mac/thread.cpp index c68fd396de..5ea6f21f32 100644 --- a/src/mac/thread.cpp +++ b/src/mac/thread.cpp @@ -35,9 +35,12 @@ #include "wx/thread.h" #ifdef __WXMAC__ -#include "wx/mac/private.h" +#include +#include "wx/mac/uma.h" #endif +#define INFINITE 0xFFFFFFFF + // ---------------------------------------------------------------------------- // constants // ---------------------------------------------------------------------------- @@ -67,13 +70,15 @@ static bool gs_waitingForThread = FALSE ; class wxMacStCritical { public : - wxMacStCritical() + wxMacStCritical() { - ThreadBeginCritical() ; + if ( UMASystemIsInitialized() ) + ThreadBeginCritical() ; } ~wxMacStCritical() { - ThreadEndCritical() ; + if ( UMASystemIsInitialized() ) + ThreadEndCritical() ; } }; @@ -84,114 +89,166 @@ public : class wxMutexInternal { public: - wxMutexInternal() + wxMutexInternal(wxMutexType WXUNUSED(mutexType)) { m_owner = kNoThreadID ; + m_locked = 0; } - ~wxMutexInternal() + ~wxMutexInternal() { + if ( m_locked > 0 ) + { + wxLogDebug(_T("Warning: freeing a locked mutex (%ld locks)."), m_locked); + } } + bool IsOk() const { return true; } + + wxMutexError Lock() ; + wxMutexError TryLock() ; + wxMutexError Unlock(); public: ThreadID m_owner ; wxArrayLong m_waiters ; + long m_locked ; }; -wxMutex::wxMutex() +wxMutexError wxMutexInternal::Lock() { - m_internal = new wxMutexInternal; + wxMacStCritical critical ; + if ( UMASystemIsInitialized() ) + { + 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_owner != kNoThreadID && m_owner != current) + { + m_waiters.Add(current); + err = ::SetThreadStateEndCritical(kCurrentThreadID, kStoppedThreadState, m_owner); + err = ::ThreadBeginCritical(); + } + m_owner = current; + } + m_locked++; - m_locked = 0; + return wxMUTEX_NO_ERROR; } -wxMutex::~wxMutex() +wxMutexError wxMutexInternal::TryLock() { - if ( m_locked > 0 ) + wxMacStCritical critical ; + if ( UMASystemIsInitialized() ) { - wxLogDebug(_T("Warning: freeing a locked mutex (%d locks)."), m_locked); + ThreadID current = kNoThreadID; + ::MacGetCurrentThread(¤t); + // if we are not the owner, give an error back + if ( m_owner != kNoThreadID && m_owner != current ) + return wxMUTEX_BUSY; + + m_owner = current; } + m_locked++; - delete m_internal; + return wxMUTEX_NO_ERROR; } -wxMutexError wxMutex::Lock() +wxMutexError wxMutexInternal::Unlock() { - wxMacStCritical critical ; - - 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); + if ( UMASystemIsInitialized() ) + { + OSErr err; err = ::ThreadBeginCritical(); - } - m_internal->m_owner = current; - m_locked++; + if (m_locked > 0) + m_locked--; + + // this mutex is not owned by anybody anmore + m_owner = kNoThreadID; + + // now pass on to the first waiting thread + ThreadID firstWaiting = kNoThreadID; + bool found = false; + while (!m_waiters.IsEmpty() && !found) + { + firstWaiting = 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_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); + } + else + { + if (m_locked > 0) + m_locked--; + } return wxMUTEX_NO_ERROR; } -wxMutexError wxMutex::TryLock() -{ - wxMacStCritical critical ; - - 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; - - m_internal->m_owner = current; - m_locked++; +// -------------------------------------------------------------------------- +// wxSemaphore +// -------------------------------------------------------------------------- - return wxMUTEX_NO_ERROR; -} +// TODO not yet implemented -wxMutexError wxMutex::Unlock() +class wxSemaphoreInternal { - OSErr err; - err = ::ThreadBeginCritical(); - - if (m_locked > 0) - m_locked--; +public: + wxSemaphoreInternal(int initialcount, int maxcount); + ~wxSemaphoreInternal(); + + bool IsOk() const { return true ; } + + wxSemaError Wait() { return WaitTimeout(INFINITE); } + wxSemaError TryWait() { return WaitTimeout(0); } + wxSemaError WaitTimeout(unsigned long milliseconds); + + wxSemaError Post(); - // this mutex is not owned by anybody anmore - m_internal->m_owner = kNoThreadID; +private: +}; - // now pass on to the first waiting thread - ThreadID firstWaiting = kNoThreadID; - bool found = false; - while (!m_internal->m_waiters.IsEmpty() && !found) +wxSemaphoreInternal::wxSemaphoreInternal(int initialcount, int maxcount) +{ + if ( maxcount == 0 ) { - 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) ; + // make it practically infinite + maxcount = INT_MAX; } - // 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); +} - return wxMUTEX_NO_ERROR; +wxSemaphoreInternal::~wxSemaphoreInternal() +{ +} + +wxSemaError wxSemaphoreInternal::WaitTimeout(unsigned long milliseconds) +{ + return wxSEMA_MISC_ERROR; +} + +wxSemaError wxSemaphoreInternal::Post() +{ + return wxSEMA_MISC_ERROR; } // ---------------------------------------------------------------------------- // wxCondition implementation // ---------------------------------------------------------------------------- +// TODO this is not yet completed + class wxConditionInternal { public: - wxConditionInternal() + wxConditionInternal(wxMutex& mutex) : m_mutex(mutex) { m_excessSignals = 0 ; } @@ -199,17 +256,24 @@ public: { } - bool Wait(unsigned long msectimeout) + bool IsOk() const { return m_mutex.IsOk() ; } + + wxCondError Wait() + { + return WaitTimeout(0xFFFFFFFF ); + } + + wxCondError WaitTimeout(unsigned long msectimeout) { wxMacStCritical critical ; if ( m_excessSignals > 0 ) { --m_excessSignals ; - return TRUE ; + return wxCOND_NO_ERROR ; } else if ( msectimeout == 0 ) { - return FALSE ; + return wxCOND_MISC_ERROR ; } else { @@ -224,59 +288,25 @@ public: return rc != WAIT_TIMEOUT; */ - return TRUE ; + return wxCOND_NO_ERROR ; } - void Signal() + wxCondError Signal() { wxMacStCritical critical ; + return wxCOND_NO_ERROR; } + wxCondError Broadcast() + { + wxMacStCritical critical ; + return wxCOND_NO_ERROR; + } + wxArrayLong m_waiters ; wxInt32 m_excessSignals ; + wxMutex& m_mutex; }; -wxCondition::wxCondition() -{ - m_internal = new wxConditionInternal; -} - -wxCondition::~wxCondition() -{ - delete m_internal; -} - -void wxCondition::Wait() -{ - (void)m_internal->Wait(0xFFFFFFFFL); -} - -bool wxCondition::Wait(unsigned long sec, - unsigned long nsec) -{ - return m_internal->Wait(sec*1000 + nsec/1000000); -} - -void wxCondition::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() ; -} - -void wxCondition::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++ ) - { - Signal(); - } -} - // ---------------------------------------------------------------------------- // wxCriticalSection implementation // ---------------------------------------------------------------------------- @@ -323,7 +353,7 @@ public: // thread priority void SetPriority(unsigned int priority); unsigned int GetPriority() const { return m_priority; } - + void SetResult( void *res ) { m_result = res ; } void *GetResult() { return m_result ; } @@ -409,7 +439,7 @@ bool wxThreadInternal::Create(wxThread *thread, unsigned int stackSize) bool wxThreadInternal::Suspend() { OSErr err ; - + ::ThreadBeginCritical(); if ( m_state != STATE_RUNNING ) @@ -434,18 +464,18 @@ bool wxThreadInternal::Resume() wxASSERT( err == noErr ) ; wxASSERT( current != m_tid ) ; - + ::ThreadBeginCritical(); if ( m_state != STATE_PAUSED && m_state != STATE_NEW ) { ::ThreadEndCritical() ; wxLogSysError(_("Can not resume thread %x"), m_tid); return FALSE; - + } err = ::SetThreadStateEndCritical(m_tid, kReadyThreadState, kNoThreadID); wxASSERT( err == noErr ) ; - + m_state = STATE_RUNNING; ::ThreadEndCritical() ; ::YieldToAnyThread() ; @@ -457,13 +487,13 @@ bool wxThreadInternal::Resume() wxThread *wxThread::This() { wxMacStCritical critical ; - + ThreadID current ; OSErr err ; - + err = MacGetCurrentThread( ¤t ) ; - - for ( int i = 0 ; i < s_threads.Count() ; ++i ) + + for ( size_t i = 0 ; i < s_threads.Count() ; ++i ) { if ( ( (wxThread*) s_threads[i] )->GetId() == current ) return (wxThread*) s_threads[i] ; @@ -477,7 +507,7 @@ bool wxThread::IsMain() { ThreadID current ; OSErr err ; - + err = MacGetCurrentThread( ¤t ) ; return current == gs_idMainThread; } @@ -494,7 +524,7 @@ void wxThread::Yield() void wxThread::Sleep(unsigned long milliseconds) { clock_t start = clock() ; - do + do { YieldToAnyThread() ; } while( clock() - start < milliseconds / CLOCKS_PER_SEC ) ; @@ -506,6 +536,13 @@ int wxThread::GetCPUCount() return 1; } +unsigned long wxThread::GetCurrentId() +{ + ThreadID current ; + MacGetCurrentThread( ¤t ) ; + return (unsigned long)current; +} + bool wxThread::SetConcurrency(size_t level) { wxASSERT_MSG( IsMain(), _T("should only be called from the main thread") ); @@ -521,7 +558,7 @@ bool wxThread::SetConcurrency(size_t level) // processor system it doesn't make much sense anyhow return level == 1; } - + return TRUE ; } @@ -539,7 +576,10 @@ wxThread::wxThread(wxThreadKind kind) wxThread::~wxThread() { s_threads.Remove( (void*) this ) ; - delete m_internal; + if (m_internal != NULL) { + delete m_internal; + m_internal = NULL; + } } // create/start thread @@ -732,7 +772,7 @@ void wxThread::Exit(ExitCode status) m_internal->SetResult( status ) ; -/* +/* #if defined(__VISUALC__) || (defined(__BORLANDC__) && (__BORLANDC__ >= 0x500)) _endthreadex((unsigned)status); #else // !VC++ @@ -848,7 +888,7 @@ bool WXDLLEXPORT wxGuiOwnedByMainThread() return false ; } -// wake up the main thread +// wake up the main thread void WXDLLEXPORT wxWakeUpMainThread() { wxMacWakeUp() ; @@ -859,6 +899,8 @@ bool WXDLLEXPORT wxIsWaitingForThread() return false ; } +#include "wx/thrimpl.cpp" + #endif // wxUSE_THREADS // vi:sts=4:sw=4:et