1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/thread.cpp
3 // Purpose: wxThread Implementation
4 // Author: Original from Wolfram Gloger/Guilhem Lavaux
5 // Modified by: Vadim Zeitlin to make it work :-)
8 // Copyright: (c) Wolfram Gloger (1996, 1997), Guilhem Lavaux (1998);
9 // Vadim Zeitlin (1999-2002)
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // ----------------------------------------------------------------------------
15 // ----------------------------------------------------------------------------
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
20 #if defined(__BORLANDC__)
26 #include "wx/thread.h"
31 #include "wx/module.h"
34 #include "wx/apptrait.h"
35 #include "wx/scopeguard.h"
37 #include "wx/msw/private.h"
38 #include "wx/msw/missing.h"
39 #include "wx/msw/seh.h"
41 #include "wx/except.h"
43 #include "wx/dynlib.h"
45 // must have this symbol defined to get _beginthread/_endthread declarations
50 #if defined(__BORLANDC__)
52 // I can't set -tWM in the IDE (anyone?) so have to do this
56 #if !defined(__MFC_COMPAT__)
57 // Needed to know about _beginthreadex etc..
58 #define __MFC_COMPAT__
62 // define wxUSE_BEGIN_THREAD if the compiler has _beginthreadex() function
63 // which should be used instead of Win32 ::CreateThread() if possible
64 #if defined(__VISUALC__) || \
65 (defined(__BORLANDC__) && (__BORLANDC__ >= 0x500)) || \
66 (defined(__GNUG__) && defined(__MSVCRT__)) || \
70 #undef wxUSE_BEGIN_THREAD
71 #define wxUSE_BEGIN_THREAD
76 #ifdef wxUSE_BEGIN_THREAD
77 // this is where _beginthreadex() is declared
80 // the return type of the thread function entry point: notice that this
81 // type can't hold a pointer under Win64
82 typedef unsigned THREAD_RETVAL
;
84 // the calling convention of the thread function entry point
85 #define THREAD_CALLCONV __stdcall
87 // the settings for CreateThread()
88 typedef DWORD THREAD_RETVAL
;
89 #define THREAD_CALLCONV WINAPI
92 static const THREAD_RETVAL THREAD_ERROR_EXIT
= (THREAD_RETVAL
)-1;
94 // ----------------------------------------------------------------------------
96 // ----------------------------------------------------------------------------
98 // the possible states of the thread ("=>" shows all possible transitions from
102 STATE_NEW
, // didn't start execution yet (=> RUNNING)
103 STATE_RUNNING
, // thread is running (=> PAUSED, CANCELED)
104 STATE_PAUSED
, // thread is temporarily suspended (=> RUNNING)
105 STATE_CANCELED
, // thread should terminate a.s.a.p. (=> EXITED)
106 STATE_EXITED
// thread is terminating
109 // ----------------------------------------------------------------------------
110 // this module globals
111 // ----------------------------------------------------------------------------
113 // TLS index of the slot where we store the pointer to the current thread
114 static DWORD gs_tlsThisThread
= 0xFFFFFFFF;
116 // id of the main thread - the one which can call GUI functions without first
117 // calling wxMutexGuiEnter()
118 wxThreadIdType
wxThread::ms_idMainThread
= 0;
120 // if it's false, some secondary thread is holding the GUI lock
121 static bool gs_bGuiOwnedByMainThread
= true;
123 // critical section which controls access to all GUI functions: any secondary
124 // thread (i.e. except the main one) must enter this crit section before doing
126 static wxCriticalSection
*gs_critsectGui
= NULL
;
128 // critical section which protects gs_nWaitingForGui variable
129 static wxCriticalSection
*gs_critsectWaitingForGui
= NULL
;
131 // critical section which serializes WinThreadStart() and WaitForTerminate()
132 // (this is a potential bottleneck, we use a single crit sect for all threads
133 // in the system, but normally time spent inside it should be quite short)
134 static wxCriticalSection
*gs_critsectThreadDelete
= NULL
;
136 // number of threads waiting for GUI in wxMutexGuiEnter()
137 static size_t gs_nWaitingForGui
= 0;
139 // are we waiting for a thread termination?
140 static bool gs_waitingForThread
= false;
142 // ============================================================================
143 // Windows implementation of thread and related classes
144 // ============================================================================
146 // ----------------------------------------------------------------------------
148 // ----------------------------------------------------------------------------
150 wxCriticalSection::wxCriticalSection( wxCriticalSectionType
WXUNUSED(critSecType
) )
152 wxCOMPILE_TIME_ASSERT( sizeof(CRITICAL_SECTION
) <= sizeof(wxCritSectBuffer
),
153 wxCriticalSectionBufferTooSmall
);
155 ::InitializeCriticalSection((CRITICAL_SECTION
*)m_buffer
);
158 wxCriticalSection::~wxCriticalSection()
160 ::DeleteCriticalSection((CRITICAL_SECTION
*)m_buffer
);
163 void wxCriticalSection::Enter()
165 ::EnterCriticalSection((CRITICAL_SECTION
*)m_buffer
);
168 bool wxCriticalSection::TryEnter()
170 #if wxUSE_DYNLIB_CLASS
172 (WINAPI
*TryEnterCriticalSection_t
)(LPCRITICAL_SECTION lpCriticalSection
);
174 static TryEnterCriticalSection_t
175 pfnTryEnterCriticalSection
= (TryEnterCriticalSection_t
)
176 wxDynamicLibrary(wxT("kernel32.dll")).
177 GetSymbol(wxT("TryEnterCriticalSection"));
179 return pfnTryEnterCriticalSection
180 ? (*pfnTryEnterCriticalSection
)((CRITICAL_SECTION
*)m_buffer
) != 0
187 void wxCriticalSection::Leave()
189 ::LeaveCriticalSection((CRITICAL_SECTION
*)m_buffer
);
192 // ----------------------------------------------------------------------------
194 // ----------------------------------------------------------------------------
196 class wxMutexInternal
199 wxMutexInternal(wxMutexType mutexType
);
202 bool IsOk() const { return m_mutex
!= NULL
; }
204 wxMutexError
Lock() { return LockTimeout(INFINITE
); }
205 wxMutexError
Lock(unsigned long ms
) { return LockTimeout(ms
); }
206 wxMutexError
TryLock();
207 wxMutexError
Unlock();
210 wxMutexError
LockTimeout(DWORD milliseconds
);
214 unsigned long m_owningThread
;
217 wxDECLARE_NO_COPY_CLASS(wxMutexInternal
);
220 // all mutexes are recursive under Win32 so we don't use mutexType
221 wxMutexInternal::wxMutexInternal(wxMutexType mutexType
)
223 // create a nameless (hence intra process and always private) mutex
224 m_mutex
= ::CreateMutex
226 NULL
, // default secutiry attributes
227 FALSE
, // not initially locked
236 wxLogLastError(wxT("CreateMutex()"));
241 wxMutexInternal::~wxMutexInternal()
245 if ( !::CloseHandle(m_mutex
) )
247 wxLogLastError(wxT("CloseHandle(mutex)"));
252 wxMutexError
wxMutexInternal::TryLock()
254 const wxMutexError rc
= LockTimeout(0);
256 // we have a special return code for timeout in this case
257 return rc
== wxMUTEX_TIMEOUT
? wxMUTEX_BUSY
: rc
;
260 wxMutexError
wxMutexInternal::LockTimeout(DWORD milliseconds
)
262 if (m_type
== wxMUTEX_DEFAULT
)
264 // Don't allow recursive
265 if (m_owningThread
!= 0)
267 if (m_owningThread
== wxThread::GetCurrentId())
268 return wxMUTEX_DEAD_LOCK
;
272 DWORD rc
= ::WaitForSingleObject(m_mutex
, milliseconds
);
276 // the previous caller died without releasing the mutex, so even
277 // though we did get it, log a message about this
278 wxLogDebug(wxT("WaitForSingleObject() returned WAIT_ABANDONED"));
286 return wxMUTEX_TIMEOUT
;
289 wxFAIL_MSG(wxT("impossible return value in wxMutex::Lock"));
293 wxLogLastError(wxT("WaitForSingleObject(mutex)"));
294 return wxMUTEX_MISC_ERROR
;
297 if (m_type
== wxMUTEX_DEFAULT
)
299 // required for checking recursiveness
300 m_owningThread
= wxThread::GetCurrentId();
303 return wxMUTEX_NO_ERROR
;
306 wxMutexError
wxMutexInternal::Unlock()
308 // required for checking recursiveness
311 if ( !::ReleaseMutex(m_mutex
) )
313 wxLogLastError(wxT("ReleaseMutex()"));
315 return wxMUTEX_MISC_ERROR
;
318 return wxMUTEX_NO_ERROR
;
321 // --------------------------------------------------------------------------
323 // --------------------------------------------------------------------------
325 // a trivial wrapper around Win32 semaphore
326 class wxSemaphoreInternal
329 wxSemaphoreInternal(int initialcount
, int maxcount
);
330 ~wxSemaphoreInternal();
332 bool IsOk() const { return m_semaphore
!= NULL
; }
334 wxSemaError
Wait() { return WaitTimeout(INFINITE
); }
336 wxSemaError
TryWait()
338 wxSemaError rc
= WaitTimeout(0);
339 if ( rc
== wxSEMA_TIMEOUT
)
345 wxSemaError
WaitTimeout(unsigned long milliseconds
);
352 wxDECLARE_NO_COPY_CLASS(wxSemaphoreInternal
);
355 wxSemaphoreInternal::wxSemaphoreInternal(int initialcount
, int maxcount
)
357 #if !defined(_WIN32_WCE) || (_WIN32_WCE >= 300)
360 // make it practically infinite
364 m_semaphore
= ::CreateSemaphore
366 NULL
, // default security attributes
374 wxLogLastError(wxT("CreateSemaphore()"));
378 wxSemaphoreInternal::~wxSemaphoreInternal()
382 if ( !::CloseHandle(m_semaphore
) )
384 wxLogLastError(wxT("CloseHandle(semaphore)"));
389 wxSemaError
wxSemaphoreInternal::WaitTimeout(unsigned long milliseconds
)
391 DWORD rc
= ::WaitForSingleObject( m_semaphore
, milliseconds
);
396 return wxSEMA_NO_ERROR
;
399 return wxSEMA_TIMEOUT
;
402 wxLogLastError(wxT("WaitForSingleObject(semaphore)"));
405 return wxSEMA_MISC_ERROR
;
408 wxSemaError
wxSemaphoreInternal::Post()
410 #if !defined(_WIN32_WCE) || (_WIN32_WCE >= 300)
411 if ( !::ReleaseSemaphore(m_semaphore
, 1, NULL
/* ptr to previous count */) )
413 if ( GetLastError() == ERROR_TOO_MANY_POSTS
)
415 return wxSEMA_OVERFLOW
;
419 wxLogLastError(wxT("ReleaseSemaphore"));
420 return wxSEMA_MISC_ERROR
;
424 return wxSEMA_NO_ERROR
;
426 return wxSEMA_MISC_ERROR
;
430 // ----------------------------------------------------------------------------
431 // wxThread implementation
432 // ----------------------------------------------------------------------------
434 // wxThreadInternal class
435 // ----------------------
437 class wxThreadInternal
440 wxThreadInternal(wxThread
*thread
)
445 m_priority
= WXTHREAD_DEFAULT_PRIORITY
;
458 if ( !::CloseHandle(m_hThread
) )
460 wxLogLastError(wxT("CloseHandle(thread)"));
467 // create a new (suspended) thread (for the given thread object)
468 bool Create(wxThread
*thread
, unsigned int stackSize
);
470 // wait for the thread to terminate, either by itself, or by asking it
471 // (politely, this is not Kill()!) to do it
472 wxThreadError
WaitForTerminate(wxCriticalSection
& cs
,
473 wxThread::ExitCode
*pRc
,
474 wxThreadWait waitMode
,
475 wxThread
*threadToDelete
= NULL
);
477 // kill the thread unconditionally
478 wxThreadError
Kill();
480 // suspend/resume/terminate
483 void Cancel() { m_state
= STATE_CANCELED
; }
486 void SetState(wxThreadState state
) { m_state
= state
; }
487 wxThreadState
GetState() const { return m_state
; }
490 void SetPriority(unsigned int priority
);
491 unsigned int GetPriority() const { return m_priority
; }
493 // thread handle and id
494 HANDLE
GetHandle() const { return m_hThread
; }
495 DWORD
GetId() const { return m_tid
; }
497 // the thread function forwarding to DoThreadStart
498 static THREAD_RETVAL THREAD_CALLCONV
WinThreadStart(void *thread
);
500 // really start the thread (if it's not already dead)
501 static THREAD_RETVAL
DoThreadStart(wxThread
*thread
);
503 // call OnExit() on the thread
504 static void DoThreadOnExit(wxThread
*thread
);
509 if ( m_thread
->IsDetached() )
510 ::InterlockedIncrement(&m_nRef
);
515 if ( m_thread
->IsDetached() && !::InterlockedDecrement(&m_nRef
) )
520 // the thread we're associated with
523 HANDLE m_hThread
; // handle of the thread
524 wxThreadState m_state
; // state, see wxThreadState enum
525 unsigned int m_priority
; // thread priority in "wx" units
526 DWORD m_tid
; // thread id
528 // number of threads which need this thread to remain alive, when the count
529 // reaches 0 we kill the owning wxThread -- and die ourselves with it
532 wxDECLARE_NO_COPY_CLASS(wxThreadInternal
);
535 // small class which keeps a thread alive during its lifetime
536 class wxThreadKeepAlive
539 wxThreadKeepAlive(wxThreadInternal
& thrImpl
) : m_thrImpl(thrImpl
)
540 { m_thrImpl
.KeepAlive(); }
542 { m_thrImpl
.LetDie(); }
545 wxThreadInternal
& m_thrImpl
;
549 void wxThreadInternal::DoThreadOnExit(wxThread
*thread
)
555 wxCATCH_ALL( wxTheApp
->OnUnhandledException(); )
559 THREAD_RETVAL
wxThreadInternal::DoThreadStart(wxThread
*thread
)
561 wxON_BLOCK_EXIT1(DoThreadOnExit
, thread
);
563 THREAD_RETVAL rc
= THREAD_ERROR_EXIT
;
567 // store the thread object in the TLS
568 if ( !::TlsSetValue(gs_tlsThisThread
, thread
) )
570 wxLogSysError(_("Cannot start thread: error writing TLS."));
572 return THREAD_ERROR_EXIT
;
575 rc
= wxPtrToUInt(thread
->Entry());
577 wxCATCH_ALL( wxTheApp
->OnUnhandledException(); )
583 THREAD_RETVAL THREAD_CALLCONV
wxThreadInternal::WinThreadStart(void *param
)
585 THREAD_RETVAL rc
= THREAD_ERROR_EXIT
;
587 wxThread
* const thread
= (wxThread
*)param
;
589 // each thread has its own SEH translator so install our own a.s.a.p.
590 DisableAutomaticSETranslator();
592 // NB: Notice that we can't use wxCriticalSectionLocker in this function as
593 // we use SEH and it's incompatible with C++ object dtors.
595 // first of all, check whether we hadn't been cancelled already and don't
596 // start the user code at all then
597 thread
->m_critsect
.Enter();
598 const bool hasExited
= thread
->m_internal
->GetState() == STATE_EXITED
;
599 thread
->m_critsect
.Leave();
601 // run the thread function itself inside a SEH try/except block
605 DoThreadOnExit(thread
);
607 rc
= DoThreadStart(thread
);
609 wxSEH_HANDLE(THREAD_ERROR_EXIT
)
612 // save IsDetached because thread object can be deleted by joinable
613 // threads after state is changed to STATE_EXITED.
614 const bool isDetached
= thread
->IsDetached();
617 thread
->m_critsect
.Enter();
618 thread
->m_internal
->SetState(STATE_EXITED
);
619 thread
->m_critsect
.Leave();
622 // the thread may delete itself now if it wants, we don't need it any more
624 thread
->m_internal
->LetDie();
629 void wxThreadInternal::SetPriority(unsigned int priority
)
631 m_priority
= priority
;
633 // translate wxWidgets priority to the Windows one
635 if (m_priority
<= 20)
636 win_priority
= THREAD_PRIORITY_LOWEST
;
637 else if (m_priority
<= 40)
638 win_priority
= THREAD_PRIORITY_BELOW_NORMAL
;
639 else if (m_priority
<= 60)
640 win_priority
= THREAD_PRIORITY_NORMAL
;
641 else if (m_priority
<= 80)
642 win_priority
= THREAD_PRIORITY_ABOVE_NORMAL
;
643 else if (m_priority
<= 100)
644 win_priority
= THREAD_PRIORITY_HIGHEST
;
647 wxFAIL_MSG(wxT("invalid value of thread priority parameter"));
648 win_priority
= THREAD_PRIORITY_NORMAL
;
651 if ( !::SetThreadPriority(m_hThread
, win_priority
) )
653 wxLogSysError(_("Can't set thread priority"));
657 bool wxThreadInternal::Create(wxThread
*thread
, unsigned int stackSize
)
659 wxASSERT_MSG( m_state
== STATE_NEW
&& !m_hThread
,
660 wxT("Create()ing thread twice?") );
662 // for compilers which have it, we should use C RTL function for thread
663 // creation instead of Win32 API one because otherwise we will have memory
664 // leaks if the thread uses C RTL (and most threads do)
665 #ifdef wxUSE_BEGIN_THREAD
667 // Watcom is reported to not like 0 stack size (which means "use default"
668 // for the other compilers and is also the default value for stackSize)
672 #endif // __WATCOMC__
674 m_hThread
= (HANDLE
)_beginthreadex
676 NULL
, // default security
678 wxThreadInternal::WinThreadStart
, // entry point
681 (unsigned int *)&m_tid
683 #else // compiler doesn't have _beginthreadex
684 m_hThread
= ::CreateThread
686 NULL
, // default security
687 stackSize
, // stack size
688 wxThreadInternal::WinThreadStart
, // thread entry point
689 (LPVOID
)thread
, // parameter
690 CREATE_SUSPENDED
, // flags
691 &m_tid
// [out] thread id
693 #endif // _beginthreadex/CreateThread
695 if ( m_hThread
== NULL
)
697 wxLogSysError(_("Can't create thread"));
702 if ( m_priority
!= WXTHREAD_DEFAULT_PRIORITY
)
704 SetPriority(m_priority
);
710 wxThreadError
wxThreadInternal::Kill()
714 if ( !::TerminateThread(m_hThread
, THREAD_ERROR_EXIT
) )
716 wxLogSysError(_("Couldn't terminate thread"));
718 return wxTHREAD_MISC_ERROR
;
723 return wxTHREAD_NO_ERROR
;
727 wxThreadInternal::WaitForTerminate(wxCriticalSection
& cs
,
728 wxThread::ExitCode
*pRc
,
729 wxThreadWait waitMode
,
730 wxThread
*threadToDelete
)
732 // prevent the thread C++ object from disappearing as long as we are using
734 wxThreadKeepAlive
keepAlive(*this);
737 // we may either wait passively for the thread to terminate (when called
738 // from Wait()) or ask it to terminate (when called from Delete())
739 bool shouldDelete
= threadToDelete
!= NULL
;
743 // we might need to resume the thread if it's currently stopped
744 bool shouldResume
= false;
746 // as Delete() (which calls us) is always safe to call we need to consider
747 // all possible states
749 wxCriticalSectionLocker
lock(cs
);
751 if ( m_state
== STATE_NEW
)
755 // WinThreadStart() will see it and terminate immediately, no
756 // need to cancel the thread -- but we still need to resume it
758 m_state
= STATE_EXITED
;
760 // we must call Resume() as the thread hasn't been initially
761 // resumed yet (and as Resume() it knows about STATE_EXITED
762 // special case, it won't touch it and WinThreadStart() will
763 // just exit immediately)
765 shouldDelete
= false;
767 //else: shouldResume is correctly set to false here, wait until
768 // someone else runs the thread and it finishes
770 else // running, paused, cancelled or even exited
772 shouldResume
= m_state
== STATE_PAUSED
;
776 // resume the thread if it is paused
780 // ask the thread to terminate
783 wxCriticalSectionLocker
lock(cs
);
788 if ( threadToDelete
)
789 threadToDelete
->OnDelete();
791 // now wait for thread to finish
792 if ( wxThread::IsMain() )
794 // set flag for wxIsWaitingForThread()
795 gs_waitingForThread
= true;
798 // we can't just wait for the thread to terminate because it might be
799 // calling some GUI functions and so it will never terminate before we
800 // process the Windows messages that result from these functions
801 // (note that even in console applications we might have to process
802 // messages if we use wxExecute() or timers or ...)
803 DWORD result
wxDUMMY_INITIALIZE(0);
806 if ( wxThread::IsMain() )
808 // give the thread we're waiting for chance to do the GUI call
810 if ( (gs_nWaitingForGui
> 0) && wxGuiOwnedByMainThread() )
816 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
819 result
= traits
->WaitForThread(m_hThread
, waitMode
);
821 else // can't wait for the thread
831 wxLogSysError(_("Cannot wait for thread termination"));
833 return wxTHREAD_KILLED
;
836 // thread we're waiting for terminated
839 case WAIT_OBJECT_0
+ 1:
840 // new message arrived, process it -- but only if we're the
841 // main thread as we don't support processing messages in
844 // NB: we still must include QS_ALLINPUT even when waiting
845 // in a secondary thread because if it had created some
846 // window somehow (possible not even using wxWidgets)
847 // the system might dead lock then
848 if ( wxThread::IsMain() )
850 if ( traits
&& !traits
->DoMessageFromThreadWait() )
852 // WM_QUIT received: kill the thread
855 return wxTHREAD_KILLED
;
861 wxFAIL_MSG(wxT("unexpected result of MsgWaitForMultipleObject"));
863 } while ( result
!= WAIT_OBJECT_0
);
865 if ( wxThread::IsMain() )
867 gs_waitingForThread
= false;
871 // although the thread might be already in the EXITED state it might not
872 // have terminated yet and so we are not sure that it has actually
873 // terminated if the "if" above hadn't been taken
876 if ( !::GetExitCodeThread(m_hThread
, &rc
) )
878 wxLogLastError(wxT("GetExitCodeThread"));
880 rc
= THREAD_ERROR_EXIT
;
885 if ( rc
!= STILL_ACTIVE
)
888 // give the other thread some time to terminate, otherwise we may be
894 *pRc
= wxUIntToPtr(rc
);
896 // we don't need the thread handle any more in any case
900 return rc
== THREAD_ERROR_EXIT
? wxTHREAD_MISC_ERROR
: wxTHREAD_NO_ERROR
;
903 bool wxThreadInternal::Suspend()
905 DWORD nSuspendCount
= ::SuspendThread(m_hThread
);
906 if ( nSuspendCount
== (DWORD
)-1 )
908 wxLogSysError(_("Cannot suspend thread %x"), m_hThread
);
913 m_state
= STATE_PAUSED
;
918 bool wxThreadInternal::Resume()
920 DWORD nSuspendCount
= ::ResumeThread(m_hThread
);
921 if ( nSuspendCount
== (DWORD
)-1 )
923 wxLogSysError(_("Cannot resume thread %x"), m_hThread
);
928 // don't change the state from STATE_EXITED because it's special and means
929 // we are going to terminate without running any user code - if we did it,
930 // the code in WaitForTerminate() wouldn't work
931 if ( m_state
!= STATE_EXITED
)
933 m_state
= STATE_RUNNING
;
942 wxThread
*wxThread::This()
944 wxThread
*thread
= (wxThread
*)::TlsGetValue(gs_tlsThisThread
);
946 // be careful, 0 may be a valid return value as well
947 if ( !thread
&& (::GetLastError() != NO_ERROR
) )
949 wxLogSysError(_("Couldn't get the current thread pointer"));
957 void wxThread::Yield()
959 // 0 argument to Sleep() is special and means to just give away the rest of
964 int wxThread::GetCPUCount()
969 return si
.dwNumberOfProcessors
;
972 unsigned long wxThread::GetCurrentId()
974 return (unsigned long)::GetCurrentThreadId();
977 bool wxThread::SetConcurrency(size_t WXUNUSED_IN_WINCE(level
))
982 wxASSERT_MSG( IsMain(), wxT("should only be called from the main thread") );
984 // ok only for the default one
988 // get system affinity mask first
989 HANDLE hProcess
= ::GetCurrentProcess();
990 DWORD_PTR dwProcMask
, dwSysMask
;
991 if ( ::GetProcessAffinityMask(hProcess
, &dwProcMask
, &dwSysMask
) == 0 )
993 wxLogLastError(wxT("GetProcessAffinityMask"));
998 // how many CPUs have we got?
999 if ( dwSysMask
== 1 )
1001 // don't bother with all this complicated stuff - on a single
1002 // processor system it doesn't make much sense anyhow
1006 // calculate the process mask: it's a bit vector with one bit per
1007 // processor; we want to schedule the process to run on first level
1012 if ( dwSysMask
& bit
)
1014 // ok, we can set this bit
1017 // another process added
1020 // and that's enough
1029 // could we set all bits?
1032 wxLogDebug(wxT("bad level %u in wxThread::SetConcurrency()"), level
);
1037 // set it: we can't link to SetProcessAffinityMask() because it doesn't
1038 // exist in Win9x, use RT binding instead
1040 typedef BOOL (WINAPI
*SETPROCESSAFFINITYMASK
)(HANDLE
, DWORD_PTR
);
1042 // can use static var because we're always in the main thread here
1043 static SETPROCESSAFFINITYMASK pfnSetProcessAffinityMask
= NULL
;
1045 if ( !pfnSetProcessAffinityMask
)
1047 HMODULE hModKernel
= ::LoadLibrary(wxT("kernel32"));
1050 pfnSetProcessAffinityMask
= (SETPROCESSAFFINITYMASK
)
1051 ::GetProcAddress(hModKernel
, "SetProcessAffinityMask");
1054 // we've discovered a MT version of Win9x!
1055 wxASSERT_MSG( pfnSetProcessAffinityMask
,
1056 wxT("this system has several CPUs but no SetProcessAffinityMask function?") );
1059 if ( !pfnSetProcessAffinityMask
)
1061 // msg given above - do it only once
1065 if ( pfnSetProcessAffinityMask(hProcess
, dwProcMask
) == 0 )
1067 wxLogLastError(wxT("SetProcessAffinityMask"));
1073 #endif // __WXWINCE__/!__WXWINCE__
1079 wxThread::wxThread(wxThreadKind kind
)
1081 m_internal
= new wxThreadInternal(this);
1083 m_isDetached
= kind
== wxTHREAD_DETACHED
;
1086 wxThread::~wxThread()
1091 // create/start thread
1092 // -------------------
1094 wxThreadError
wxThread::Create(unsigned int stackSize
)
1096 wxCriticalSectionLocker
lock(m_critsect
);
1098 if ( !m_internal
->Create(this, stackSize
) )
1099 return wxTHREAD_NO_RESOURCE
;
1101 return wxTHREAD_NO_ERROR
;
1104 wxThreadError
wxThread::Run()
1106 wxCriticalSectionLocker
lock(m_critsect
);
1108 wxCHECK_MSG( m_internal
->GetState() == STATE_NEW
, wxTHREAD_RUNNING
,
1109 wxT("thread may only be started once after Create()") );
1111 // the thread has just been created and is still suspended - let it run
1115 // suspend/resume thread
1116 // ---------------------
1118 wxThreadError
wxThread::Pause()
1120 wxCriticalSectionLocker
lock(m_critsect
);
1122 return m_internal
->Suspend() ? wxTHREAD_NO_ERROR
: wxTHREAD_MISC_ERROR
;
1125 wxThreadError
wxThread::Resume()
1127 wxCriticalSectionLocker
lock(m_critsect
);
1129 return m_internal
->Resume() ? wxTHREAD_NO_ERROR
: wxTHREAD_MISC_ERROR
;
1135 wxThread::ExitCode
wxThread::Wait(wxThreadWait waitMode
)
1137 ExitCode rc
= wxUIntToPtr(THREAD_ERROR_EXIT
);
1139 // although under Windows we can wait for any thread, it's an error to
1140 // wait for a detached one in wxWin API
1141 wxCHECK_MSG( !IsDetached(), rc
,
1142 wxT("wxThread::Wait(): can't wait for detached thread") );
1144 (void)m_internal
->WaitForTerminate(m_critsect
, &rc
, waitMode
);
1149 wxThreadError
wxThread::Delete(ExitCode
*pRc
, wxThreadWait waitMode
)
1151 return m_internal
->WaitForTerminate(m_critsect
, pRc
, waitMode
, this);
1154 wxThreadError
wxThread::Kill()
1157 return wxTHREAD_NOT_RUNNING
;
1159 wxThreadError rc
= m_internal
->Kill();
1167 // update the status of the joinable thread
1168 wxCriticalSectionLocker
lock(m_critsect
);
1169 m_internal
->SetState(STATE_EXITED
);
1175 void wxThread::Exit(ExitCode status
)
1177 wxThreadInternal::DoThreadOnExit(this);
1187 // update the status of the joinable thread
1188 wxCriticalSectionLocker
lock(m_critsect
);
1189 m_internal
->SetState(STATE_EXITED
);
1192 #ifdef wxUSE_BEGIN_THREAD
1193 _endthreadex(wxPtrToUInt(status
));
1195 ::ExitThread((DWORD
)status
);
1196 #endif // VC++/!VC++
1198 wxFAIL_MSG(wxT("Couldn't return from ExitThread()!"));
1204 void wxThread::SetPriority(unsigned int prio
)
1206 wxCriticalSectionLocker
lock(m_critsect
);
1208 m_internal
->SetPriority(prio
);
1211 unsigned int wxThread::GetPriority() const
1213 wxCriticalSectionLocker
lock(const_cast<wxCriticalSection
&>(m_critsect
));
1215 return m_internal
->GetPriority();
1218 unsigned long wxThread::GetId() const
1220 wxCriticalSectionLocker
lock(const_cast<wxCriticalSection
&>(m_critsect
));
1222 return (unsigned long)m_internal
->GetId();
1225 bool wxThread::IsRunning() const
1227 wxCriticalSectionLocker
lock(const_cast<wxCriticalSection
&>(m_critsect
));
1229 return m_internal
->GetState() == STATE_RUNNING
;
1232 bool wxThread::IsAlive() const
1234 wxCriticalSectionLocker
lock(const_cast<wxCriticalSection
&>(m_critsect
));
1236 return (m_internal
->GetState() == STATE_RUNNING
) ||
1237 (m_internal
->GetState() == STATE_PAUSED
);
1240 bool wxThread::IsPaused() const
1242 wxCriticalSectionLocker
lock(const_cast<wxCriticalSection
&>(m_critsect
));
1244 return m_internal
->GetState() == STATE_PAUSED
;
1247 bool wxThread::TestDestroy()
1249 wxCriticalSectionLocker
lock(const_cast<wxCriticalSection
&>(m_critsect
));
1251 return m_internal
->GetState() == STATE_CANCELED
;
1254 // ----------------------------------------------------------------------------
1255 // Automatic initialization for thread module
1256 // ----------------------------------------------------------------------------
1258 class wxThreadModule
: public wxModule
1261 virtual bool OnInit();
1262 virtual void OnExit();
1265 DECLARE_DYNAMIC_CLASS(wxThreadModule
)
1268 IMPLEMENT_DYNAMIC_CLASS(wxThreadModule
, wxModule
)
1270 bool wxThreadModule::OnInit()
1272 // allocate TLS index for storing the pointer to the current thread
1273 gs_tlsThisThread
= ::TlsAlloc();
1274 if ( gs_tlsThisThread
== 0xFFFFFFFF )
1276 // in normal circumstances it will only happen if all other
1277 // TLS_MINIMUM_AVAILABLE (>= 64) indices are already taken - in other
1278 // words, this should never happen
1279 wxLogSysError(_("Thread module initialization failed: impossible to allocate index in thread local storage"));
1284 // main thread doesn't have associated wxThread object, so store 0 in the
1286 if ( !::TlsSetValue(gs_tlsThisThread
, (LPVOID
)0) )
1288 ::TlsFree(gs_tlsThisThread
);
1289 gs_tlsThisThread
= 0xFFFFFFFF;
1291 wxLogSysError(_("Thread module initialization failed: cannot store value in thread local storage"));
1296 gs_critsectWaitingForGui
= new wxCriticalSection();
1298 gs_critsectGui
= new wxCriticalSection();
1299 gs_critsectGui
->Enter();
1301 gs_critsectThreadDelete
= new wxCriticalSection
;
1303 wxThread::ms_idMainThread
= wxThread::GetCurrentId();
1308 void wxThreadModule::OnExit()
1310 if ( !::TlsFree(gs_tlsThisThread
) )
1312 wxLogLastError(wxT("TlsFree failed."));
1315 wxDELETE(gs_critsectThreadDelete
);
1317 if ( gs_critsectGui
)
1319 gs_critsectGui
->Leave();
1320 wxDELETE(gs_critsectGui
);
1323 wxDELETE(gs_critsectWaitingForGui
);
1326 // ----------------------------------------------------------------------------
1327 // under Windows, these functions are implemented using a critical section and
1328 // not a mutex, so the names are a bit confusing
1329 // ----------------------------------------------------------------------------
1331 void wxMutexGuiEnterImpl()
1333 // this would dead lock everything...
1334 wxASSERT_MSG( !wxThread::IsMain(),
1335 wxT("main thread doesn't want to block in wxMutexGuiEnter()!") );
1337 // the order in which we enter the critical sections here is crucial!!
1339 // set the flag telling to the main thread that we want to do some GUI
1341 wxCriticalSectionLocker
enter(*gs_critsectWaitingForGui
);
1343 gs_nWaitingForGui
++;
1346 wxWakeUpMainThread();
1348 // now we may block here because the main thread will soon let us in
1349 // (during the next iteration of OnIdle())
1350 gs_critsectGui
->Enter();
1353 void wxMutexGuiLeaveImpl()
1355 wxCriticalSectionLocker
enter(*gs_critsectWaitingForGui
);
1357 if ( wxThread::IsMain() )
1359 gs_bGuiOwnedByMainThread
= false;
1363 // decrement the number of threads waiting for GUI access now
1364 wxASSERT_MSG( gs_nWaitingForGui
> 0,
1365 wxT("calling wxMutexGuiLeave() without entering it first?") );
1367 gs_nWaitingForGui
--;
1369 wxWakeUpMainThread();
1372 gs_critsectGui
->Leave();
1375 void WXDLLIMPEXP_BASE
wxMutexGuiLeaveOrEnter()
1377 wxASSERT_MSG( wxThread::IsMain(),
1378 wxT("only main thread may call wxMutexGuiLeaveOrEnter()!") );
1380 wxCriticalSectionLocker
enter(*gs_critsectWaitingForGui
);
1382 if ( gs_nWaitingForGui
== 0 )
1384 // no threads are waiting for GUI - so we may acquire the lock without
1385 // any danger (but only if we don't already have it)
1386 if ( !wxGuiOwnedByMainThread() )
1388 gs_critsectGui
->Enter();
1390 gs_bGuiOwnedByMainThread
= true;
1392 //else: already have it, nothing to do
1396 // some threads are waiting, release the GUI lock if we have it
1397 if ( wxGuiOwnedByMainThread() )
1401 //else: some other worker thread is doing GUI
1405 bool WXDLLIMPEXP_BASE
wxGuiOwnedByMainThread()
1407 return gs_bGuiOwnedByMainThread
;
1410 // wake up the main thread if it's in ::GetMessage()
1411 void WXDLLIMPEXP_BASE
wxWakeUpMainThread()
1413 // sending any message would do - hopefully WM_NULL is harmless enough
1414 if ( !::PostThreadMessage(wxThread::GetMainId(), WM_NULL
, 0, 0) )
1416 // should never happen
1417 wxLogLastError(wxT("PostThreadMessage(WM_NULL)"));
1421 bool WXDLLIMPEXP_BASE
wxIsWaitingForThread()
1423 return gs_waitingForThread
;
1426 // ----------------------------------------------------------------------------
1427 // include common implementation code
1428 // ----------------------------------------------------------------------------
1430 #include "wx/thrimpl.cpp"
1432 #endif // wxUSE_THREADS