Added wxThread::GetMainId().
This is useful for checking if a message was logged from the main thread or not and also allows us to implement IsMain() by comparing GetCurrentId() with GetMainId() in all ports and avoid repetition. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@61406 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -447,7 +447,17 @@ public:
|
|||||||
static wxThread *This();
|
static wxThread *This();
|
||||||
|
|
||||||
// Returns true if current thread is the main thread.
|
// Returns true if current thread is the main thread.
|
||||||
static bool IsMain();
|
//
|
||||||
|
// Notice that it also returns true if main thread id hadn't been
|
||||||
|
// initialized yet on the assumption that it's too early in wx startup
|
||||||
|
// process for any other threads to have been created in this case.
|
||||||
|
static bool IsMain()
|
||||||
|
{
|
||||||
|
return !ms_idMainThread || GetCurrentId() == ms_idMainThread;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the main thread id
|
||||||
|
static wxThreadIdType GetMainId() { return ms_idMainThread; }
|
||||||
|
|
||||||
// Release the rest of our time slice letting the other threads run
|
// Release the rest of our time slice letting the other threads run
|
||||||
static void Yield();
|
static void Yield();
|
||||||
@@ -466,7 +476,7 @@ public:
|
|||||||
// Get the platform specific thread ID and return as a long. This
|
// Get the platform specific thread ID and return as a long. This
|
||||||
// can be used to uniquely identify threads, even if they are not
|
// can be used to uniquely identify threads, even if they are not
|
||||||
// wxThreads. This is used by wxPython.
|
// wxThreads. This is used by wxPython.
|
||||||
static wxThreadIdType GetCurrentId();
|
static wxThreadIdType GetCurrentId();
|
||||||
|
|
||||||
// sets the concurrency level: this is, roughly, the number of threads
|
// sets the concurrency level: this is, roughly, the number of threads
|
||||||
// the system tries to schedule to run in parallel. 0 means the
|
// the system tries to schedule to run in parallel. 0 means the
|
||||||
@@ -586,6 +596,11 @@ private:
|
|||||||
virtual void OnExit() { }
|
virtual void OnExit() { }
|
||||||
|
|
||||||
friend class wxThreadInternal;
|
friend class wxThreadInternal;
|
||||||
|
friend class wxThreadModule;
|
||||||
|
|
||||||
|
|
||||||
|
// the main thread identifier, should be set on startup
|
||||||
|
static wxThreadIdType ms_idMainThread;
|
||||||
|
|
||||||
// the (platform-dependent) thread class implementation
|
// the (platform-dependent) thread class implementation
|
||||||
wxThreadInternal *m_internal;
|
wxThreadInternal *m_internal;
|
||||||
|
@@ -989,7 +989,10 @@ public:
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
Returns the platform specific thread ID of the current thread as a long.
|
Returns the platform specific thread ID of the current thread as a long.
|
||||||
|
|
||||||
This can be used to uniquely identify threads, even if they are not wxThreads.
|
This can be used to uniquely identify threads, even if they are not wxThreads.
|
||||||
|
|
||||||
|
@see GetMainId()
|
||||||
*/
|
*/
|
||||||
static wxThreadIdType GetCurrentId();
|
static wxThreadIdType GetCurrentId();
|
||||||
|
|
||||||
@@ -1007,6 +1010,15 @@ public:
|
|||||||
*/
|
*/
|
||||||
wxThreadKind GetKind() const;
|
wxThreadKind GetKind() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns the thread ID of the main thread.
|
||||||
|
|
||||||
|
@see IsMain()
|
||||||
|
|
||||||
|
@since 2.9.1
|
||||||
|
*/
|
||||||
|
static wxThreadIdType GetMainId();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Gets the priority of the thread, between zero and 100.
|
Gets the priority of the thread, between zero and 100.
|
||||||
|
|
||||||
@@ -1035,6 +1047,11 @@ public:
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
Returns @true if the calling thread is the main application thread.
|
Returns @true if the calling thread is the main application thread.
|
||||||
|
|
||||||
|
Main thread in the context of wxWidgets is the one which initialized
|
||||||
|
the library.
|
||||||
|
|
||||||
|
@see GetMainId(), GetCurrentId()
|
||||||
*/
|
*/
|
||||||
static bool IsMain();
|
static bool IsMain();
|
||||||
|
|
||||||
|
@@ -113,7 +113,7 @@ static DWORD gs_tlsThisThread = 0xFFFFFFFF;
|
|||||||
|
|
||||||
// id of the main thread - the one which can call GUI functions without first
|
// id of the main thread - the one which can call GUI functions without first
|
||||||
// calling wxMutexGuiEnter()
|
// calling wxMutexGuiEnter()
|
||||||
static DWORD gs_idMainThread = 0;
|
wxThreadIdType wxThread::ms_idMainThread = 0;
|
||||||
|
|
||||||
// if it's false, some secondary thread is holding the GUI lock
|
// if it's false, some secondary thread is holding the GUI lock
|
||||||
static bool gs_bGuiOwnedByMainThread = true;
|
static bool gs_bGuiOwnedByMainThread = true;
|
||||||
@@ -926,11 +926,6 @@ wxThread *wxThread::This()
|
|||||||
return thread;
|
return thread;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxThread::IsMain()
|
|
||||||
{
|
|
||||||
return ::GetCurrentThreadId() == gs_idMainThread || gs_idMainThread == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void wxThread::Yield()
|
void wxThread::Yield()
|
||||||
{
|
{
|
||||||
// 0 argument to Sleep() is special and means to just give away the rest of
|
// 0 argument to Sleep() is special and means to just give away the rest of
|
||||||
@@ -1278,8 +1273,7 @@ bool wxThreadModule::OnInit()
|
|||||||
|
|
||||||
gs_critsectThreadDelete = new wxCriticalSection;
|
gs_critsectThreadDelete = new wxCriticalSection;
|
||||||
|
|
||||||
// no error return for GetCurrentThreadId()
|
wxThread::ms_idMainThread = wxThread::GetCurrentId();
|
||||||
gs_idMainThread = ::GetCurrentThreadId();
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -1393,7 +1387,7 @@ bool WXDLLIMPEXP_BASE wxGuiOwnedByMainThread()
|
|||||||
void WXDLLIMPEXP_BASE wxWakeUpMainThread()
|
void WXDLLIMPEXP_BASE wxWakeUpMainThread()
|
||||||
{
|
{
|
||||||
// sending any message would do - hopefully WM_NULL is harmless enough
|
// sending any message would do - hopefully WM_NULL is harmless enough
|
||||||
if ( !::PostThreadMessage(gs_idMainThread, WM_NULL, 0, 0) )
|
if ( !::PostThreadMessage(ms_idMainThread, WM_NULL, 0, 0) )
|
||||||
{
|
{
|
||||||
// should never happen
|
// should never happen
|
||||||
wxLogLastError(wxT("PostThreadMessage(WM_NULL)"));
|
wxLogLastError(wxT("PostThreadMessage(WM_NULL)"));
|
||||||
|
@@ -57,7 +57,7 @@ enum wxThreadState
|
|||||||
|
|
||||||
// id of the main thread - the one which can call GUI functions without first
|
// id of the main thread - the one which can call GUI functions without first
|
||||||
// calling wxMutexGuiEnter()
|
// calling wxMutexGuiEnter()
|
||||||
static ULONG s_ulIdMainThread = 1;
|
wxThreadIdType wxThread::ms_idMainThread = 0;
|
||||||
wxMutex* p_wxMainMutex;
|
wxMutex* p_wxMainMutex;
|
||||||
|
|
||||||
// OS2 substitute for Tls pointer the current parent thread object
|
// OS2 substitute for Tls pointer the current parent thread object
|
||||||
@@ -547,19 +547,6 @@ wxThread *wxThread::This()
|
|||||||
return pThread;
|
return pThread;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxThread::IsMain()
|
|
||||||
{
|
|
||||||
PTIB ptib;
|
|
||||||
PPIB ppib;
|
|
||||||
|
|
||||||
::DosGetInfoBlocks(&ptib, &ppib);
|
|
||||||
|
|
||||||
if (ptib->tib_ptib2->tib2_ultid == s_ulIdMainThread)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef Yield
|
#ifdef Yield
|
||||||
#undef Yield
|
#undef Yield
|
||||||
#endif
|
#endif
|
||||||
@@ -582,13 +569,13 @@ int wxThread::GetCPUCount()
|
|||||||
return CPUCount;
|
return CPUCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long wxThread::GetCurrentId()
|
wxThreadIdType wxThread::GetCurrentId()
|
||||||
{
|
{
|
||||||
PTIB ptib;
|
PTIB ptib;
|
||||||
PPIB ppib;
|
PPIB ppib;
|
||||||
|
|
||||||
::DosGetInfoBlocks(&ptib, &ppib);
|
::DosGetInfoBlocks(&ptib, &ppib);
|
||||||
return (unsigned long) ptib->tib_ptib2->tib2_ultid;
|
return (wxThreadIdType) ptib->tib_ptib2->tib2_ultid;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxThread::SetConcurrency(size_t level)
|
bool wxThread::SetConcurrency(size_t level)
|
||||||
@@ -937,12 +924,8 @@ bool wxThreadModule::OnInit()
|
|||||||
gs_pCritsectGui = new wxCriticalSection();
|
gs_pCritsectGui = new wxCriticalSection();
|
||||||
gs_pCritsectGui->Enter();
|
gs_pCritsectGui->Enter();
|
||||||
|
|
||||||
PTIB ptib;
|
wxThread::ms_idMainThread = wxThread::GetCurrentId();
|
||||||
PPIB ppib;
|
|
||||||
|
|
||||||
::DosGetInfoBlocks(&ptib, &ppib);
|
|
||||||
|
|
||||||
s_ulIdMainThread = ptib->tib_ptib2->tib2_ultid;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -49,7 +49,7 @@ enum wxThreadState
|
|||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
// the task ID of the main thread
|
// the task ID of the main thread
|
||||||
static wxThreadIdType gs_idMainThread = kInvalidID;
|
wxThreadIdType wxThread::ms_idMainThread = kInvalidID;
|
||||||
|
|
||||||
// this is the Per-Task Storage for the pointer to the appropriate wxThread
|
// this is the Per-Task Storage for the pointer to the appropriate wxThread
|
||||||
TaskStorageIndex gs_tlsForWXThread = 0;
|
TaskStorageIndex gs_tlsForWXThread = 0;
|
||||||
@@ -796,11 +796,6 @@ wxThread *wxThread::This()
|
|||||||
return thr;
|
return thr;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxThread::IsMain()
|
|
||||||
{
|
|
||||||
return GetCurrentId() == gs_idMainThread || gs_idMainThread == kInvalidID ;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef Yield
|
#ifdef Yield
|
||||||
#undef Yield
|
#undef Yield
|
||||||
#endif
|
#endif
|
||||||
@@ -1214,7 +1209,7 @@ bool wxThreadModule::OnInit()
|
|||||||
verify_noerr( MPAllocateTaskStorageIndex( &gs_tlsForWXThread ) ) ;
|
verify_noerr( MPAllocateTaskStorageIndex( &gs_tlsForWXThread ) ) ;
|
||||||
verify_noerr( MPSetTaskStorageValue( gs_tlsForWXThread, 0 ) ) ;
|
verify_noerr( MPSetTaskStorageValue( gs_tlsForWXThread, 0 ) ) ;
|
||||||
|
|
||||||
gs_idMainThread = wxThread::GetCurrentId();
|
wxThread::ms_idMainThread = wxThread::GetCurrentId();
|
||||||
gs_critsectWaitingForGui = new wxCriticalSection();
|
gs_critsectWaitingForGui = new wxCriticalSection();
|
||||||
|
|
||||||
gs_critsectGui = new wxCriticalSection();
|
gs_critsectGui = new wxCriticalSection();
|
||||||
|
@@ -118,7 +118,11 @@ static wxArrayThread gs_allThreads;
|
|||||||
static wxMutex *gs_mutexAllThreads = NULL;
|
static wxMutex *gs_mutexAllThreads = NULL;
|
||||||
|
|
||||||
// the id of the main thread
|
// the id of the main thread
|
||||||
static pthread_t gs_tidMain = (pthread_t)-1;
|
//
|
||||||
|
// we suppose that 0 is not a valid pthread_t value but in principle this might
|
||||||
|
// be false (e.g. if it's a selector-like value), wxThread::IsMain() would need
|
||||||
|
// to be updated in such case
|
||||||
|
wxThreadIdType wxThread::ms_idMainThread = 0;
|
||||||
|
|
||||||
// the key for the pointer to the associated wxThread object
|
// the key for the pointer to the associated wxThread object
|
||||||
static pthread_key_t gs_keySelf;
|
static pthread_key_t gs_keySelf;
|
||||||
@@ -1057,11 +1061,6 @@ wxThread *wxThread::This()
|
|||||||
return (wxThread *)pthread_getspecific(gs_keySelf);
|
return (wxThread *)pthread_getspecific(gs_keySelf);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxThread::IsMain()
|
|
||||||
{
|
|
||||||
return (bool)pthread_equal(pthread_self(), gs_tidMain) || gs_tidMain == (pthread_t)-1;
|
|
||||||
}
|
|
||||||
|
|
||||||
void wxThread::Yield()
|
void wxThread::Yield()
|
||||||
{
|
{
|
||||||
#ifdef HAVE_SCHED_YIELD
|
#ifdef HAVE_SCHED_YIELD
|
||||||
@@ -1110,23 +1109,11 @@ int wxThread::GetCPUCount()
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// VMS is a 64 bit system and threads have 64 bit pointers.
|
wxThreadIdType wxThread::GetCurrentId()
|
||||||
// FIXME: also needed for other systems????
|
|
||||||
#ifdef __VMS
|
|
||||||
unsigned long long wxThread::GetCurrentId()
|
|
||||||
{
|
{
|
||||||
return (unsigned long long)pthread_self();
|
return (wxThreadIdType)pthread_self();
|
||||||
}
|
}
|
||||||
|
|
||||||
#else // !__VMS
|
|
||||||
|
|
||||||
unsigned long wxThread::GetCurrentId()
|
|
||||||
{
|
|
||||||
return (unsigned long)pthread_self();
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // __VMS/!__VMS
|
|
||||||
|
|
||||||
|
|
||||||
bool wxThread::SetConcurrency(size_t level)
|
bool wxThread::SetConcurrency(size_t level)
|
||||||
{
|
{
|
||||||
@@ -1705,7 +1692,7 @@ bool wxThreadModule::OnInit()
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
gs_tidMain = pthread_self();
|
wxThread::ms_idMainThread = wxThread::GetCurrentId();
|
||||||
|
|
||||||
gs_mutexAllThreads = new wxMutex();
|
gs_mutexAllThreads = new wxMutex();
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user