From 4958ea8f7b27a4213e092234a13d151b7b08fbea Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Wed, 16 Jan 2002 06:28:11 +0000 Subject: [PATCH] Added wxThread::GetCurrentId(), which returns an ID for the current thread, even if it is not a wxThread. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@13595 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/latex/wx/thread.tex | 28 ++++++++++++++++++---------- include/wx/thread.h | 7 ++++++- src/msw/thread.cpp | 5 +++++ wxPython/src/helpers.cpp | 20 ++------------------ 4 files changed, 31 insertions(+), 29 deletions(-) diff --git a/docs/latex/wx/thread.tex b/docs/latex/wx/thread.tex index ada3f901b9..33328eef99 100644 --- a/docs/latex/wx/thread.tex +++ b/docs/latex/wx/thread.tex @@ -63,7 +63,7 @@ The possible values for {\it kind} parameters are: \func{}{\destruct{wxThread}}{\void} The destructor frees the resources associated with the thread. Notice that you -should never delete a detached thread - you may only call +should never delete a detached thread - you may only call \helpref{Delete}{wxthreaddelete} on it or wait until it terminates (and auto destructs) itself. Because the detached threads delete themselves, they can only be allocated on the heap. @@ -98,17 +98,17 @@ One of: Calling \helpref{Delete}{wxthreaddelete} is a graceful way to terminate the thread. It asks the thread to terminate and, if the thread code is well -written, the thread will terminate after the next call to +written, the thread will terminate after the next call to \helpref{TestDestroy}{wxthreadtestdestroy} which should happen quite soon. -However, if the thread doesn't call \helpref{TestDestroy}{wxthreadtestdestroy} +However, if the thread doesn't call \helpref{TestDestroy}{wxthreadtestdestroy} often enough (or at all), the function will not return immediately, but wait until the thread terminates. As it may take a long time, and the message processing is not stopped during this function execution, message handlers may be called from inside it! Delete() may be called for thread in any state: running, paused or even not yet -created. Moreover, it must be called if \helpref{Create}{wxthreadcreate} or +created. Moreover, it must be called if \helpref{Create}{wxthreadcreate} or \helpref{Run}{wxthreadrun} failed for a detached thread to free the memory occupied by the thread object. This cleanup will be done in the destructor for joinable threads. @@ -146,7 +146,7 @@ from a derived class. It also can only be called in the context of this thread, i.e. a thread can only exit from itself, not from another thread. This function will terminate the OS thread (i.e. stop the associated path of -execution) and also delete the associated C++ object for detached threads. +execution) and also delete the associated C++ object for detached threads. \helpref{wxThread::OnExit}{wxthreadonexit} will be called just before exiting. \membersection{wxThread::GetCPUCount}\label{wxthreadgetcpucount} @@ -159,6 +159,14 @@ Returns the number of system CPUs or -1 if the value is unknown. \helpref{SetConcurrency}{wxthreadsetconcurrency} +\membersection{wxThread::GetCurrentId}\label{wxthreadgetcurrentid} + +\func{static unsigned long}{GetCurrentId}{\void} + +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. + \membersection{wxThread::GetId}\label{wxthreadgetid} \constfunc{unsigned long}{GetId}{\void} @@ -230,7 +238,7 @@ This function can only be called from another thread context, i.e. a thread cannot kill itself. It is also an error to call this function for a thread which is not running or -paused (in the latter case, the thread will be resumed first) - if you do it, +paused (in the latter case, the thread will be resumed first) - if you do it, a {\tt wxTHREAD\_NOT\_RUNNING} error will be returned. \membersection{wxThread::OnExit}\label{wxthreadonexit} @@ -239,7 +247,7 @@ a {\tt wxTHREAD\_NOT\_RUNNING} error will be returned. Called when the thread exits. This function is called in the context of the thread associated with the wxThread object, not in the context of the main -thread. This function will not be called if the thread was +thread. This function will not be called if the thread was \helpref{killed}{wxthreadkill}. This function should never be called directly. @@ -249,7 +257,7 @@ This function should never be called directly. \func{wxThreadError}{Pause}{\void} Suspends the thread. Under some implementations (Win32), the thread is -suspended immediately, under others it will only be suspended when it calls +suspended immediately, under others it will only be suspended when it calls \helpref{TestDestroy}{wxthreadtestdestroy} for the next time (hence, if the thread doesn't call it at all, it won't be suspended). @@ -259,7 +267,7 @@ This function can only be called from another thread context. \func{wxThreadError}{Run}{\void} -Starts the thread execution. Should be called after +Starts the thread execution. Should be called after \helpref{Create}{wxthreadcreate}. This function can only be called from another thread context. @@ -269,7 +277,7 @@ This function can only be called from another thread context. \func{void}{SetPriority}{\param{int}{ priority}} Sets the priority of the thread, between $0$ and $100$. It can only be set -after calling \helpref{Create()}{wxthreadcreate} but before calling +after calling \helpref{Create()}{wxthreadcreate} but before calling \helpref{Run()}{wxthreadrun}. The following priorities are already defined: diff --git a/include/wx/thread.h b/include/wx/thread.h index f80aa89bf5..ff86535a78 100644 --- a/include/wx/thread.h +++ b/include/wx/thread.h @@ -286,6 +286,11 @@ public: // Returns -1 if unknown, number of CPUs otherwise static int GetCPUCount(); + // Get the platform specific thread ID and return as a long. This + // can be used to uniquely identify threads, even if they are not + // wxThreads. This is used by wxPython. + static unsigned long GetCurrentId(); + // sets the concurrency level: this is, roughly, the number of threads // the system tries to schedule to run in parallel. 0 means the // default value (usually acceptable, but may not yield the best @@ -376,7 +381,7 @@ public: #else unsigned long GetId() const; #endif - + // called when the thread exits - in the context of this thread // // NB: this function will not be called if the thread is Kill()ed diff --git a/src/msw/thread.cpp b/src/msw/thread.cpp index 8320440d20..b654353bfc 100644 --- a/src/msw/thread.cpp +++ b/src/msw/thread.cpp @@ -647,6 +647,11 @@ int wxThread::GetCPUCount() return si.dwNumberOfProcessors; } +unsigned long wxThread::GetCurrentId() +{ + return (unsigned long)::GetCurrentThreadId(); +} + bool wxThread::SetConcurrency(size_t level) { wxASSERT_MSG( IsMain(), _T("should only be called from the main thread") ); diff --git a/wxPython/src/helpers.cpp b/wxPython/src/helpers.cpp index c8cf22a045..0943cabc00 100644 --- a/wxPython/src/helpers.cpp +++ b/wxPython/src/helpers.cpp @@ -441,30 +441,14 @@ PyObject* wxPyConstructObject(void* ptr, //--------------------------------------------------------------------------- -// TODO: This should really be wxThread::GetCurrentId(), and I will do so -// after I make a quick 2.3.2.1 release. + #ifdef WXP_WITH_THREAD -#ifdef __WXGTK__ // does wxGTK always use pthreads? -#include -#include -#endif inline unsigned long wxPyGetCurrentThreadId() { -#ifdef __WXMSW__ - return (unsigned long)::GetCurrentThreadId(); -#endif -#ifdef __WXGTK__ // does wxGTK always use pthreads? - return (unsigned long)pthread_self(); -#endif -#ifdef __WXMAC__ - ThreadID current ; - MacGetCurrentThread( ¤t ) ; - return (unsigned long)current; -#endif + return wxThread::GetCurrentId(); } - static PyThreadState* wxPyGetThreadState() { unsigned long ctid = wxPyGetCurrentThreadId(); -- 2.45.2