wxThread::GetCurrentId for wxMac

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@13597 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robin Dunn
2002-01-16 06:32:40 +00:00
parent 1fcfb40de3
commit ea736fec49
2 changed files with 60 additions and 46 deletions

View File

@@ -70,7 +70,7 @@ static bool gs_waitingForThread = FALSE ;
class wxMacStCritical class wxMacStCritical
{ {
public : public :
wxMacStCritical() wxMacStCritical()
{ {
if ( UMASystemIsInitialized() ) if ( UMASystemIsInitialized() )
ThreadBeginCritical() ; ThreadBeginCritical() ;
@@ -94,7 +94,7 @@ public:
m_owner = kNoThreadID ; m_owner = kNoThreadID ;
} }
~wxMutexInternal() ~wxMutexInternal()
{ {
} }
@@ -130,7 +130,7 @@ wxMutexError wxMutex::Lock()
err = ::MacGetCurrentThread(&current); err = ::MacGetCurrentThread(&current);
// if we are not the owner, add this thread to the list of waiting threads, stop this thread // 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 // and invoke the scheduler to continue executing the owner's thread
while ( m_internal->m_owner != kNoThreadID && m_internal->m_owner != current) while ( m_internal->m_owner != kNoThreadID && m_internal->m_owner != current)
{ {
m_internal->m_waiters.Add(current); m_internal->m_waiters.Add(current);
err = ::SetThreadStateEndCritical(kCurrentThreadID, kStoppedThreadState, m_internal->m_owner); err = ::SetThreadStateEndCritical(kCurrentThreadID, kStoppedThreadState, m_internal->m_owner);
@@ -147,14 +147,14 @@ wxMutexError wxMutex::TryLock()
{ {
wxMacStCritical critical ; wxMacStCritical critical ;
if ( UMASystemIsInitialized() ) if ( UMASystemIsInitialized() )
{ {
OSErr err ; OSErr err ;
ThreadID current = kNoThreadID; ThreadID current = kNoThreadID;
::MacGetCurrentThread(&current); ::MacGetCurrentThread(&current);
// if we are not the owner, give an error back // if we are not the owner, give an error back
if ( m_internal->m_owner != kNoThreadID && m_internal->m_owner != current ) if ( m_internal->m_owner != kNoThreadID && m_internal->m_owner != current )
return wxMUTEX_BUSY; return wxMUTEX_BUSY;
m_internal->m_owner = current; m_internal->m_owner = current;
} }
m_locked++; m_locked++;
@@ -165,10 +165,10 @@ wxMutexError wxMutex::TryLock()
wxMutexError wxMutex::Unlock() wxMutexError wxMutex::Unlock()
{ {
if ( UMASystemIsInitialized() ) if ( UMASystemIsInitialized() )
{ {
OSErr err; OSErr err;
err = ::ThreadBeginCritical(); err = ::ThreadBeginCritical();
if (m_locked > 0) if (m_locked > 0)
m_locked--; m_locked--;
@@ -178,12 +178,12 @@ wxMutexError wxMutex::Unlock()
// now pass on to the first waiting thread // now pass on to the first waiting thread
ThreadID firstWaiting = kNoThreadID; ThreadID firstWaiting = kNoThreadID;
bool found = false; bool found = false;
while (!m_internal->m_waiters.IsEmpty() && !found) while (!m_internal->m_waiters.IsEmpty() && !found)
{ {
firstWaiting = m_internal->m_waiters[0]; firstWaiting = m_internal->m_waiters[0];
err = ::SetThreadState(firstWaiting, kReadyThreadState, kNoThreadID); err = ::SetThreadState(firstWaiting, kReadyThreadState, kNoThreadID);
// in case this was not successful (dead thread), we just loop on and reset the id // in case this was not successful (dead thread), we just loop on and reset the id
found = (err != threadNotFoundErr); found = (err != threadNotFoundErr);
if ( !found ) if ( !found )
firstWaiting = kNoThreadID ; firstWaiting = kNoThreadID ;
m_internal->m_waiters.RemoveAt(0) ; m_internal->m_waiters.RemoveAt(0) ;
@@ -339,7 +339,7 @@ public:
// thread priority // thread priority
void SetPriority(unsigned int priority); void SetPriority(unsigned int priority);
unsigned int GetPriority() const { return m_priority; } unsigned int GetPriority() const { return m_priority; }
void SetResult( void *res ) { m_result = res ; } void SetResult( void *res ) { m_result = res ; }
void *GetResult() { return m_result ; } void *GetResult() { return m_result ; }
@@ -425,7 +425,7 @@ bool wxThreadInternal::Create(wxThread *thread, unsigned int stackSize)
bool wxThreadInternal::Suspend() bool wxThreadInternal::Suspend()
{ {
OSErr err ; OSErr err ;
::ThreadBeginCritical(); ::ThreadBeginCritical();
if ( m_state != STATE_RUNNING ) if ( m_state != STATE_RUNNING )
@@ -450,18 +450,18 @@ bool wxThreadInternal::Resume()
wxASSERT( err == noErr ) ; wxASSERT( err == noErr ) ;
wxASSERT( current != m_tid ) ; wxASSERT( current != m_tid ) ;
::ThreadBeginCritical(); ::ThreadBeginCritical();
if ( m_state != STATE_PAUSED && m_state != STATE_NEW ) if ( m_state != STATE_PAUSED && m_state != STATE_NEW )
{ {
::ThreadEndCritical() ; ::ThreadEndCritical() ;
wxLogSysError(_("Can not resume thread %x"), m_tid); wxLogSysError(_("Can not resume thread %x"), m_tid);
return FALSE; return FALSE;
} }
err = ::SetThreadStateEndCritical(m_tid, kReadyThreadState, kNoThreadID); err = ::SetThreadStateEndCritical(m_tid, kReadyThreadState, kNoThreadID);
wxASSERT( err == noErr ) ; wxASSERT( err == noErr ) ;
m_state = STATE_RUNNING; m_state = STATE_RUNNING;
::ThreadEndCritical() ; ::ThreadEndCritical() ;
::YieldToAnyThread() ; ::YieldToAnyThread() ;
@@ -473,12 +473,12 @@ bool wxThreadInternal::Resume()
wxThread *wxThread::This() wxThread *wxThread::This()
{ {
wxMacStCritical critical ; wxMacStCritical critical ;
ThreadID current ; ThreadID current ;
OSErr err ; OSErr err ;
err = MacGetCurrentThread( &current ) ; err = MacGetCurrentThread( &current ) ;
for ( int i = 0 ; i < s_threads.Count() ; ++i ) for ( int i = 0 ; i < s_threads.Count() ; ++i )
{ {
if ( ( (wxThread*) s_threads[i] )->GetId() == current ) if ( ( (wxThread*) s_threads[i] )->GetId() == current )
@@ -493,7 +493,7 @@ bool wxThread::IsMain()
{ {
ThreadID current ; ThreadID current ;
OSErr err ; OSErr err ;
err = MacGetCurrentThread( &current ) ; err = MacGetCurrentThread( &current ) ;
return current == gs_idMainThread; return current == gs_idMainThread;
} }
@@ -510,7 +510,7 @@ void wxThread::Yield()
void wxThread::Sleep(unsigned long milliseconds) void wxThread::Sleep(unsigned long milliseconds)
{ {
clock_t start = clock() ; clock_t start = clock() ;
do do
{ {
YieldToAnyThread() ; YieldToAnyThread() ;
} while( clock() - start < milliseconds / CLOCKS_PER_SEC ) ; } while( clock() - start < milliseconds / CLOCKS_PER_SEC ) ;
@@ -522,6 +522,13 @@ int wxThread::GetCPUCount()
return 1; return 1;
} }
unsigned long wxThread::GetCurrentId()
{
ThreadID current ;
MacGetCurrentThread( &current ) ;
return (unsigned long)current;
}
bool wxThread::SetConcurrency(size_t level) bool wxThread::SetConcurrency(size_t level)
{ {
wxASSERT_MSG( IsMain(), _T("should only be called from the main thread") ); wxASSERT_MSG( IsMain(), _T("should only be called from the main thread") );
@@ -537,7 +544,7 @@ bool wxThread::SetConcurrency(size_t level)
// processor system it doesn't make much sense anyhow // processor system it doesn't make much sense anyhow
return level == 1; return level == 1;
} }
return TRUE ; return TRUE ;
} }
@@ -748,7 +755,7 @@ void wxThread::Exit(ExitCode status)
m_internal->SetResult( status ) ; m_internal->SetResult( status ) ;
/* /*
#if defined(__VISUALC__) || (defined(__BORLANDC__) && (__BORLANDC__ >= 0x500)) #if defined(__VISUALC__) || (defined(__BORLANDC__) && (__BORLANDC__ >= 0x500))
_endthreadex((unsigned)status); _endthreadex((unsigned)status);
#else // !VC++ #else // !VC++
@@ -864,7 +871,7 @@ bool WXDLLEXPORT wxGuiOwnedByMainThread()
return false ; return false ;
} }
// wake up the main thread // wake up the main thread
void WXDLLEXPORT wxWakeUpMainThread() void WXDLLEXPORT wxWakeUpMainThread()
{ {
wxMacWakeUp() ; wxMacWakeUp() ;

View File

@@ -70,7 +70,7 @@ static bool gs_waitingForThread = FALSE ;
class wxMacStCritical class wxMacStCritical
{ {
public : public :
wxMacStCritical() wxMacStCritical()
{ {
if ( UMASystemIsInitialized() ) if ( UMASystemIsInitialized() )
ThreadBeginCritical() ; ThreadBeginCritical() ;
@@ -94,7 +94,7 @@ public:
m_owner = kNoThreadID ; m_owner = kNoThreadID ;
} }
~wxMutexInternal() ~wxMutexInternal()
{ {
} }
@@ -130,7 +130,7 @@ wxMutexError wxMutex::Lock()
err = ::MacGetCurrentThread(&current); err = ::MacGetCurrentThread(&current);
// if we are not the owner, add this thread to the list of waiting threads, stop this thread // 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 // and invoke the scheduler to continue executing the owner's thread
while ( m_internal->m_owner != kNoThreadID && m_internal->m_owner != current) while ( m_internal->m_owner != kNoThreadID && m_internal->m_owner != current)
{ {
m_internal->m_waiters.Add(current); m_internal->m_waiters.Add(current);
err = ::SetThreadStateEndCritical(kCurrentThreadID, kStoppedThreadState, m_internal->m_owner); err = ::SetThreadStateEndCritical(kCurrentThreadID, kStoppedThreadState, m_internal->m_owner);
@@ -147,14 +147,14 @@ wxMutexError wxMutex::TryLock()
{ {
wxMacStCritical critical ; wxMacStCritical critical ;
if ( UMASystemIsInitialized() ) if ( UMASystemIsInitialized() )
{ {
OSErr err ; OSErr err ;
ThreadID current = kNoThreadID; ThreadID current = kNoThreadID;
::MacGetCurrentThread(&current); ::MacGetCurrentThread(&current);
// if we are not the owner, give an error back // if we are not the owner, give an error back
if ( m_internal->m_owner != kNoThreadID && m_internal->m_owner != current ) if ( m_internal->m_owner != kNoThreadID && m_internal->m_owner != current )
return wxMUTEX_BUSY; return wxMUTEX_BUSY;
m_internal->m_owner = current; m_internal->m_owner = current;
} }
m_locked++; m_locked++;
@@ -165,10 +165,10 @@ wxMutexError wxMutex::TryLock()
wxMutexError wxMutex::Unlock() wxMutexError wxMutex::Unlock()
{ {
if ( UMASystemIsInitialized() ) if ( UMASystemIsInitialized() )
{ {
OSErr err; OSErr err;
err = ::ThreadBeginCritical(); err = ::ThreadBeginCritical();
if (m_locked > 0) if (m_locked > 0)
m_locked--; m_locked--;
@@ -178,12 +178,12 @@ wxMutexError wxMutex::Unlock()
// now pass on to the first waiting thread // now pass on to the first waiting thread
ThreadID firstWaiting = kNoThreadID; ThreadID firstWaiting = kNoThreadID;
bool found = false; bool found = false;
while (!m_internal->m_waiters.IsEmpty() && !found) while (!m_internal->m_waiters.IsEmpty() && !found)
{ {
firstWaiting = m_internal->m_waiters[0]; firstWaiting = m_internal->m_waiters[0];
err = ::SetThreadState(firstWaiting, kReadyThreadState, kNoThreadID); err = ::SetThreadState(firstWaiting, kReadyThreadState, kNoThreadID);
// in case this was not successful (dead thread), we just loop on and reset the id // in case this was not successful (dead thread), we just loop on and reset the id
found = (err != threadNotFoundErr); found = (err != threadNotFoundErr);
if ( !found ) if ( !found )
firstWaiting = kNoThreadID ; firstWaiting = kNoThreadID ;
m_internal->m_waiters.RemoveAt(0) ; m_internal->m_waiters.RemoveAt(0) ;
@@ -339,7 +339,7 @@ public:
// thread priority // thread priority
void SetPriority(unsigned int priority); void SetPriority(unsigned int priority);
unsigned int GetPriority() const { return m_priority; } unsigned int GetPriority() const { return m_priority; }
void SetResult( void *res ) { m_result = res ; } void SetResult( void *res ) { m_result = res ; }
void *GetResult() { return m_result ; } void *GetResult() { return m_result ; }
@@ -425,7 +425,7 @@ bool wxThreadInternal::Create(wxThread *thread, unsigned int stackSize)
bool wxThreadInternal::Suspend() bool wxThreadInternal::Suspend()
{ {
OSErr err ; OSErr err ;
::ThreadBeginCritical(); ::ThreadBeginCritical();
if ( m_state != STATE_RUNNING ) if ( m_state != STATE_RUNNING )
@@ -450,18 +450,18 @@ bool wxThreadInternal::Resume()
wxASSERT( err == noErr ) ; wxASSERT( err == noErr ) ;
wxASSERT( current != m_tid ) ; wxASSERT( current != m_tid ) ;
::ThreadBeginCritical(); ::ThreadBeginCritical();
if ( m_state != STATE_PAUSED && m_state != STATE_NEW ) if ( m_state != STATE_PAUSED && m_state != STATE_NEW )
{ {
::ThreadEndCritical() ; ::ThreadEndCritical() ;
wxLogSysError(_("Can not resume thread %x"), m_tid); wxLogSysError(_("Can not resume thread %x"), m_tid);
return FALSE; return FALSE;
} }
err = ::SetThreadStateEndCritical(m_tid, kReadyThreadState, kNoThreadID); err = ::SetThreadStateEndCritical(m_tid, kReadyThreadState, kNoThreadID);
wxASSERT( err == noErr ) ; wxASSERT( err == noErr ) ;
m_state = STATE_RUNNING; m_state = STATE_RUNNING;
::ThreadEndCritical() ; ::ThreadEndCritical() ;
::YieldToAnyThread() ; ::YieldToAnyThread() ;
@@ -473,12 +473,12 @@ bool wxThreadInternal::Resume()
wxThread *wxThread::This() wxThread *wxThread::This()
{ {
wxMacStCritical critical ; wxMacStCritical critical ;
ThreadID current ; ThreadID current ;
OSErr err ; OSErr err ;
err = MacGetCurrentThread( &current ) ; err = MacGetCurrentThread( &current ) ;
for ( int i = 0 ; i < s_threads.Count() ; ++i ) for ( int i = 0 ; i < s_threads.Count() ; ++i )
{ {
if ( ( (wxThread*) s_threads[i] )->GetId() == current ) if ( ( (wxThread*) s_threads[i] )->GetId() == current )
@@ -493,7 +493,7 @@ bool wxThread::IsMain()
{ {
ThreadID current ; ThreadID current ;
OSErr err ; OSErr err ;
err = MacGetCurrentThread( &current ) ; err = MacGetCurrentThread( &current ) ;
return current == gs_idMainThread; return current == gs_idMainThread;
} }
@@ -510,7 +510,7 @@ void wxThread::Yield()
void wxThread::Sleep(unsigned long milliseconds) void wxThread::Sleep(unsigned long milliseconds)
{ {
clock_t start = clock() ; clock_t start = clock() ;
do do
{ {
YieldToAnyThread() ; YieldToAnyThread() ;
} while( clock() - start < milliseconds / CLOCKS_PER_SEC ) ; } while( clock() - start < milliseconds / CLOCKS_PER_SEC ) ;
@@ -522,6 +522,13 @@ int wxThread::GetCPUCount()
return 1; return 1;
} }
unsigned long wxThread::GetCurrentId()
{
ThreadID current ;
MacGetCurrentThread( &current ) ;
return (unsigned long)current;
}
bool wxThread::SetConcurrency(size_t level) bool wxThread::SetConcurrency(size_t level)
{ {
wxASSERT_MSG( IsMain(), _T("should only be called from the main thread") ); wxASSERT_MSG( IsMain(), _T("should only be called from the main thread") );
@@ -537,7 +544,7 @@ bool wxThread::SetConcurrency(size_t level)
// processor system it doesn't make much sense anyhow // processor system it doesn't make much sense anyhow
return level == 1; return level == 1;
} }
return TRUE ; return TRUE ;
} }
@@ -748,7 +755,7 @@ void wxThread::Exit(ExitCode status)
m_internal->SetResult( status ) ; m_internal->SetResult( status ) ;
/* /*
#if defined(__VISUALC__) || (defined(__BORLANDC__) && (__BORLANDC__ >= 0x500)) #if defined(__VISUALC__) || (defined(__BORLANDC__) && (__BORLANDC__ >= 0x500))
_endthreadex((unsigned)status); _endthreadex((unsigned)status);
#else // !VC++ #else // !VC++
@@ -864,7 +871,7 @@ bool WXDLLEXPORT wxGuiOwnedByMainThread()
return false ; return false ;
} }
// wake up the main thread // wake up the main thread
void WXDLLEXPORT wxWakeUpMainThread() void WXDLLEXPORT wxWakeUpMainThread()
{ {
wxMacWakeUp() ; wxMacWakeUp() ;