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"
29 #include "wx/msw/missing.h"
32 #include "wx/module.h"
35 #include "wx/apptrait.h"
36 #include "wx/scopeguard.h"
38 #include "wx/msw/private.h"
39 #include "wx/msw/seh.h"
41 #include "wx/except.h"
43 // must have this symbol defined to get _beginthread/_endthread declarations
48 #if defined(__BORLANDC__)
50 // I can't set -tWM in the IDE (anyone?) so have to do this
54 #if !defined(__MFC_COMPAT__)
55 // Needed to know about _beginthreadex etc..
56 #define __MFC_COMPAT__
60 // define wxUSE_BEGIN_THREAD if the compiler has _beginthreadex() function
61 // which should be used instead of Win32 ::CreateThread() if possible
62 #if defined(__VISUALC__) || \
63 (defined(__BORLANDC__) && (__BORLANDC__ >= 0x500)) || \
64 (defined(__GNUG__) && defined(__MSVCRT__)) || \
65 defined(__WATCOMC__) || defined(__MWERKS__)
68 #undef wxUSE_BEGIN_THREAD
69 #define wxUSE_BEGIN_THREAD
74 #ifdef wxUSE_BEGIN_THREAD
75 // this is where _beginthreadex() is declared
78 // the return type of the thread function entry point
79 typedef unsigned THREAD_RETVAL
;
81 // the calling convention of the thread function entry point
82 #define THREAD_CALLCONV __stdcall
84 // the settings for CreateThread()
85 typedef DWORD THREAD_RETVAL
;
86 #define THREAD_CALLCONV WINAPI
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
93 // the possible states of the thread ("=>" shows all possible transitions from
97 STATE_NEW
, // didn't start execution yet (=> RUNNING)
98 STATE_RUNNING
, // thread is running (=> PAUSED, CANCELED)
99 STATE_PAUSED
, // thread is temporarily suspended (=> RUNNING)
100 STATE_CANCELED
, // thread should terminate a.s.a.p. (=> EXITED)
101 STATE_EXITED
// thread is terminating
104 // ----------------------------------------------------------------------------
105 // this module globals
106 // ----------------------------------------------------------------------------
108 // TLS index of the slot where we store the pointer to the current thread
109 static DWORD gs_tlsThisThread
= 0xFFFFFFFF;
111 // id of the main thread - the one which can call GUI functions without first
112 // calling wxMutexGuiEnter()
113 static DWORD gs_idMainThread
= 0;
115 // if it's false, some secondary thread is holding the GUI lock
116 static bool gs_bGuiOwnedByMainThread
= true;
118 // critical section which controls access to all GUI functions: any secondary
119 // thread (i.e. except the main one) must enter this crit section before doing
121 static wxCriticalSection
*gs_critsectGui
= NULL
;
123 // critical section which protects gs_nWaitingForGui variable
124 static wxCriticalSection
*gs_critsectWaitingForGui
= NULL
;
126 // critical section which serializes WinThreadStart() and WaitForTerminate()
127 // (this is a potential bottleneck, we use a single crit sect for all threads
128 // in the system, but normally time spent inside it should be quite short)
129 static wxCriticalSection
*gs_critsectThreadDelete
= NULL
;
131 // number of threads waiting for GUI in wxMutexGuiEnter()
132 static size_t gs_nWaitingForGui
= 0;
134 // are we waiting for a thread termination?
135 static bool gs_waitingForThread
= false;
137 // ============================================================================
138 // Windows implementation of thread and related classes
139 // ============================================================================
141 // ----------------------------------------------------------------------------
143 // ----------------------------------------------------------------------------
145 wxCriticalSection::wxCriticalSection()
147 wxCOMPILE_TIME_ASSERT( sizeof(CRITICAL_SECTION
) <= sizeof(wxCritSectBuffer
),
148 wxCriticalSectionBufferTooSmall
);
150 ::InitializeCriticalSection((CRITICAL_SECTION
*)m_buffer
);
153 wxCriticalSection::~wxCriticalSection()
155 ::DeleteCriticalSection((CRITICAL_SECTION
*)m_buffer
);
158 void wxCriticalSection::Enter()
160 ::EnterCriticalSection((CRITICAL_SECTION
*)m_buffer
);
163 void wxCriticalSection::Leave()
165 ::LeaveCriticalSection((CRITICAL_SECTION
*)m_buffer
);
168 // ----------------------------------------------------------------------------
170 // ----------------------------------------------------------------------------
172 class wxMutexInternal
175 wxMutexInternal(wxMutexType mutexType
);
178 bool IsOk() const { return m_mutex
!= NULL
; }
180 wxMutexError
Lock() { return LockTimeout(INFINITE
); }
181 wxMutexError
Lock(unsigned long ms
) { return LockTimeout(ms
); }
182 wxMutexError
TryLock();
183 wxMutexError
Unlock();
186 wxMutexError
LockTimeout(DWORD milliseconds
);
190 DECLARE_NO_COPY_CLASS(wxMutexInternal
)
193 // all mutexes are recursive under Win32 so we don't use mutexType
194 wxMutexInternal::wxMutexInternal(wxMutexType
WXUNUSED(mutexType
))
196 // create a nameless (hence intra process and always private) mutex
197 m_mutex
= ::CreateMutex
199 NULL
, // default secutiry attributes
200 FALSE
, // not initially locked
206 wxLogLastError(_T("CreateMutex()"));
210 wxMutexInternal::~wxMutexInternal()
214 if ( !::CloseHandle(m_mutex
) )
216 wxLogLastError(_T("CloseHandle(mutex)"));
221 wxMutexError
wxMutexInternal::TryLock()
223 const wxMutexError rc
= LockTimeout(0);
225 // we have a special return code for timeout in this case
226 return rc
== wxMUTEX_TIMEOUT
? wxMUTEX_BUSY
: rc
;
229 wxMutexError
wxMutexInternal::LockTimeout(DWORD milliseconds
)
231 DWORD rc
= ::WaitForSingleObject(m_mutex
, milliseconds
);
232 if ( rc
== WAIT_ABANDONED
)
234 // the previous caller died without releasing the mutex, but now we can
236 wxLogDebug(_T("WaitForSingleObject() returned WAIT_ABANDONED"));
238 // use 0 timeout, normally we should always get it
239 rc
= ::WaitForSingleObject(m_mutex
, 0);
249 return wxMUTEX_TIMEOUT
;
251 case WAIT_ABANDONED
: // checked for above
253 wxFAIL_MSG(wxT("impossible return value in wxMutex::Lock"));
257 wxLogLastError(_T("WaitForSingleObject(mutex)"));
258 return wxMUTEX_MISC_ERROR
;
261 return wxMUTEX_NO_ERROR
;
264 wxMutexError
wxMutexInternal::Unlock()
266 if ( !::ReleaseMutex(m_mutex
) )
268 wxLogLastError(_T("ReleaseMutex()"));
270 return wxMUTEX_MISC_ERROR
;
273 return wxMUTEX_NO_ERROR
;
276 // --------------------------------------------------------------------------
278 // --------------------------------------------------------------------------
280 // a trivial wrapper around Win32 semaphore
281 class wxSemaphoreInternal
284 wxSemaphoreInternal(int initialcount
, int maxcount
);
285 ~wxSemaphoreInternal();
287 bool IsOk() const { return m_semaphore
!= NULL
; }
289 wxSemaError
Wait() { return WaitTimeout(INFINITE
); }
291 wxSemaError
TryWait()
293 wxSemaError rc
= WaitTimeout(0);
294 if ( rc
== wxSEMA_TIMEOUT
)
300 wxSemaError
WaitTimeout(unsigned long milliseconds
);
307 DECLARE_NO_COPY_CLASS(wxSemaphoreInternal
)
310 wxSemaphoreInternal::wxSemaphoreInternal(int initialcount
, int maxcount
)
312 #if !defined(_WIN32_WCE) || (_WIN32_WCE >= 300)
315 // make it practically infinite
319 m_semaphore
= ::CreateSemaphore
321 NULL
, // default security attributes
329 wxLogLastError(_T("CreateSemaphore()"));
333 wxSemaphoreInternal::~wxSemaphoreInternal()
337 if ( !::CloseHandle(m_semaphore
) )
339 wxLogLastError(_T("CloseHandle(semaphore)"));
344 wxSemaError
wxSemaphoreInternal::WaitTimeout(unsigned long milliseconds
)
346 DWORD rc
= ::WaitForSingleObject( m_semaphore
, milliseconds
);
351 return wxSEMA_NO_ERROR
;
354 return wxSEMA_TIMEOUT
;
357 wxLogLastError(_T("WaitForSingleObject(semaphore)"));
360 return wxSEMA_MISC_ERROR
;
363 wxSemaError
wxSemaphoreInternal::Post()
365 #if !defined(_WIN32_WCE) || (_WIN32_WCE >= 300)
366 if ( !::ReleaseSemaphore(m_semaphore
, 1, NULL
/* ptr to previous count */) )
368 if ( GetLastError() == ERROR_TOO_MANY_POSTS
)
370 return wxSEMA_OVERFLOW
;
374 wxLogLastError(_T("ReleaseSemaphore"));
375 return wxSEMA_MISC_ERROR
;
379 return wxSEMA_NO_ERROR
;
381 return wxSEMA_MISC_ERROR
;
385 // ----------------------------------------------------------------------------
386 // wxThread implementation
387 // ----------------------------------------------------------------------------
389 // wxThreadInternal class
390 // ----------------------
392 class wxThreadInternal
395 wxThreadInternal(wxThread
*thread
)
400 m_priority
= WXTHREAD_DEFAULT_PRIORITY
;
413 if ( !::CloseHandle(m_hThread
) )
415 wxLogLastError(wxT("CloseHandle(thread)"));
422 // create a new (suspended) thread (for the given thread object)
423 bool Create(wxThread
*thread
, unsigned int stackSize
);
425 // wait for the thread to terminate, either by itself, or by asking it
426 // (politely, this is not Kill()!) to do it
427 wxThreadError
WaitForTerminate(wxCriticalSection
& cs
,
428 wxThread::ExitCode
*pRc
,
429 wxThread
*threadToDelete
= NULL
);
431 // kill the thread unconditionally
432 wxThreadError
Kill();
434 // suspend/resume/terminate
437 void Cancel() { m_state
= STATE_CANCELED
; }
440 void SetState(wxThreadState state
) { m_state
= state
; }
441 wxThreadState
GetState() const { return m_state
; }
444 void SetPriority(unsigned int priority
);
445 unsigned int GetPriority() const { return m_priority
; }
447 // thread handle and id
448 HANDLE
GetHandle() const { return m_hThread
; }
449 DWORD
GetId() const { return m_tid
; }
451 // the thread function forwarding to DoThreadStart
452 static THREAD_RETVAL THREAD_CALLCONV
WinThreadStart(void *thread
);
454 // really start the thread (if it's not already dead)
455 static THREAD_RETVAL
DoThreadStart(wxThread
*thread
);
457 // call OnExit() on the thread
458 static void DoThreadOnExit(wxThread
*thread
);
463 if ( m_thread
->IsDetached() )
464 ::InterlockedIncrement(&m_nRef
);
469 if ( m_thread
->IsDetached() && !::InterlockedDecrement(&m_nRef
) )
474 // the thread we're associated with
477 HANDLE m_hThread
; // handle of the thread
478 wxThreadState m_state
; // state, see wxThreadState enum
479 unsigned int m_priority
; // thread priority in "wx" units
480 DWORD m_tid
; // thread id
482 // number of threads which need this thread to remain alive, when the count
483 // reaches 0 we kill the owning wxThread -- and die ourselves with it
486 DECLARE_NO_COPY_CLASS(wxThreadInternal
)
489 // small class which keeps a thread alive during its lifetime
490 class wxThreadKeepAlive
493 wxThreadKeepAlive(wxThreadInternal
& thrImpl
) : m_thrImpl(thrImpl
)
494 { m_thrImpl
.KeepAlive(); }
496 { m_thrImpl
.LetDie(); }
499 wxThreadInternal
& m_thrImpl
;
503 void wxThreadInternal::DoThreadOnExit(wxThread
*thread
)
509 wxCATCH_ALL( wxTheApp
->OnUnhandledException(); )
513 THREAD_RETVAL
wxThreadInternal::DoThreadStart(wxThread
*thread
)
515 wxON_BLOCK_EXIT1(DoThreadOnExit
, thread
);
517 THREAD_RETVAL rc
= (THREAD_RETVAL
)-1;
521 // store the thread object in the TLS
522 if ( !::TlsSetValue(gs_tlsThisThread
, thread
) )
524 wxLogSysError(_("Can not start thread: error writing TLS."));
526 return (THREAD_RETVAL
)-1;
529 rc
= (THREAD_RETVAL
)thread
->Entry();
531 wxCATCH_ALL( wxTheApp
->OnUnhandledException(); )
537 THREAD_RETVAL THREAD_CALLCONV
wxThreadInternal::WinThreadStart(void *param
)
539 THREAD_RETVAL rc
= (THREAD_RETVAL
)-1;
541 wxThread
* const thread
= (wxThread
*)param
;
543 // each thread has its own SEH translator so install our own a.s.a.p.
544 DisableAutomaticSETranslator();
546 // first of all, check whether we hadn't been cancelled already and don't
547 // start the user code at all then
548 const bool hasExited
= thread
->m_internal
->GetState() == STATE_EXITED
;
550 // run the thread function itself inside a SEH try/except block
554 DoThreadOnExit(thread
);
556 rc
= DoThreadStart(thread
);
558 wxSEH_HANDLE((THREAD_RETVAL
)-1)
561 // save IsDetached because thread object can be deleted by joinable
562 // threads after state is changed to STATE_EXITED.
563 const bool isDetached
= thread
->IsDetached();
566 // enter m_critsect before changing the thread state
568 // NB: can't use wxCriticalSectionLocker here as we use SEH and it's
569 // incompatible with C++ object dtors
570 thread
->m_critsect
.Enter();
571 thread
->m_internal
->SetState(STATE_EXITED
);
572 thread
->m_critsect
.Leave();
575 // the thread may delete itself now if it wants, we don't need it any more
577 thread
->m_internal
->LetDie();
582 void wxThreadInternal::SetPriority(unsigned int priority
)
584 m_priority
= priority
;
586 // translate wxWidgets priority to the Windows one
588 if (m_priority
<= 20)
589 win_priority
= THREAD_PRIORITY_LOWEST
;
590 else if (m_priority
<= 40)
591 win_priority
= THREAD_PRIORITY_BELOW_NORMAL
;
592 else if (m_priority
<= 60)
593 win_priority
= THREAD_PRIORITY_NORMAL
;
594 else if (m_priority
<= 80)
595 win_priority
= THREAD_PRIORITY_ABOVE_NORMAL
;
596 else if (m_priority
<= 100)
597 win_priority
= THREAD_PRIORITY_HIGHEST
;
600 wxFAIL_MSG(wxT("invalid value of thread priority parameter"));
601 win_priority
= THREAD_PRIORITY_NORMAL
;
604 if ( !::SetThreadPriority(m_hThread
, win_priority
) )
606 wxLogSysError(_("Can't set thread priority"));
610 bool wxThreadInternal::Create(wxThread
*thread
, unsigned int stackSize
)
612 wxASSERT_MSG( m_state
== STATE_NEW
&& !m_hThread
,
613 _T("Create()ing thread twice?") );
615 // for compilers which have it, we should use C RTL function for thread
616 // creation instead of Win32 API one because otherwise we will have memory
617 // leaks if the thread uses C RTL (and most threads do)
618 #ifdef wxUSE_BEGIN_THREAD
620 // Watcom is reported to not like 0 stack size (which means "use default"
621 // for the other compilers and is also the default value for stackSize)
625 #endif // __WATCOMC__
627 m_hThread
= (HANDLE
)_beginthreadex
629 NULL
, // default security
631 wxThreadInternal::WinThreadStart
, // entry point
634 (unsigned int *)&m_tid
636 #else // compiler doesn't have _beginthreadex
637 m_hThread
= ::CreateThread
639 NULL
, // default security
640 stackSize
, // stack size
641 wxThreadInternal::WinThreadStart
, // thread entry point
642 (LPVOID
)thread
, // parameter
643 CREATE_SUSPENDED
, // flags
644 &m_tid
// [out] thread id
646 #endif // _beginthreadex/CreateThread
648 if ( m_hThread
== NULL
)
650 wxLogSysError(_("Can't create thread"));
655 if ( m_priority
!= WXTHREAD_DEFAULT_PRIORITY
)
657 SetPriority(m_priority
);
663 wxThreadError
wxThreadInternal::Kill()
665 if ( !::TerminateThread(m_hThread
, (DWORD
)-1) )
667 wxLogSysError(_("Couldn't terminate thread"));
669 return wxTHREAD_MISC_ERROR
;
674 return wxTHREAD_NO_ERROR
;
678 wxThreadInternal::WaitForTerminate(wxCriticalSection
& cs
,
679 wxThread::ExitCode
*pRc
,
680 wxThread
*threadToDelete
)
682 // prevent the thread C++ object from disappearing as long as we are using
684 wxThreadKeepAlive
keepAlive(*this);
687 // we may either wait passively for the thread to terminate (when called
688 // from Wait()) or ask it to terminate (when called from Delete())
689 bool shouldDelete
= threadToDelete
!= NULL
;
691 wxThread::ExitCode rc
= 0;
693 // we might need to resume the thread if it's currently stopped
694 bool shouldResume
= false;
696 // as Delete() (which calls us) is always safe to call we need to consider
697 // all possible states
699 wxCriticalSectionLocker
lock(cs
);
701 if ( m_state
== STATE_NEW
)
705 // WinThreadStart() will see it and terminate immediately, no
706 // need to cancel the thread -- but we still need to resume it
708 m_state
= STATE_EXITED
;
710 // we must call Resume() as the thread hasn't been initially
711 // resumed yet (and as Resume() it knows about STATE_EXITED
712 // special case, it won't touch it and WinThreadStart() will
713 // just exit immediately)
715 shouldDelete
= false;
717 //else: shouldResume is correctly set to false here, wait until
718 // someone else runs the thread and it finishes
720 else // running, paused, cancelled or even exited
722 shouldResume
= m_state
== STATE_PAUSED
;
726 // resume the thread if it is paused
730 // ask the thread to terminate
733 wxCriticalSectionLocker
lock(cs
);
739 // now wait for thread to finish
740 if ( wxThread::IsMain() )
742 // set flag for wxIsWaitingForThread()
743 gs_waitingForThread
= true;
746 // we can't just wait for the thread to terminate because it might be
747 // calling some GUI functions and so it will never terminate before we
748 // process the Windows messages that result from these functions
749 // (note that even in console applications we might have to process
750 // messages if we use wxExecute() or timers or ...)
751 DWORD result
wxDUMMY_INITIALIZE(0);
754 if ( wxThread::IsMain() )
756 // give the thread we're waiting for chance to do the GUI call
758 if ( (gs_nWaitingForGui
> 0) && wxGuiOwnedByMainThread() )
764 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
767 result
= traits
->WaitForThread(m_hThread
);
769 else // can't wait for the thread
779 wxLogSysError(_("Can not wait for thread termination"));
781 return wxTHREAD_KILLED
;
784 // thread we're waiting for terminated
787 case WAIT_OBJECT_0
+ 1:
788 // new message arrived, process it -- but only if we're the
789 // main thread as we don't support processing messages in
792 // NB: we still must include QS_ALLINPUT even when waiting
793 // in a secondary thread because if it had created some
794 // window somehow (possible not even using wxWidgets)
795 // the system might dead lock then
796 if ( wxThread::IsMain() )
798 if ( traits
&& !traits
->DoMessageFromThreadWait() )
800 // WM_QUIT received: kill the thread
803 return wxTHREAD_KILLED
;
809 wxFAIL_MSG(wxT("unexpected result of MsgWaitForMultipleObject"));
811 } while ( result
!= WAIT_OBJECT_0
);
813 if ( wxThread::IsMain() )
815 gs_waitingForThread
= false;
819 // although the thread might be already in the EXITED state it might not
820 // have terminated yet and so we are not sure that it has actually
821 // terminated if the "if" above hadn't been taken
824 if ( !::GetExitCodeThread(m_hThread
, (LPDWORD
)&rc
) )
826 wxLogLastError(wxT("GetExitCodeThread"));
828 rc
= (wxThread::ExitCode
)-1;
833 if ( (DWORD
)rc
!= STILL_ACTIVE
)
836 // give the other thread some time to terminate, otherwise we may be
844 // we don't need the thread handle any more in any case
848 return rc
== (wxThread::ExitCode
)-1 ? wxTHREAD_MISC_ERROR
852 bool wxThreadInternal::Suspend()
854 DWORD nSuspendCount
= ::SuspendThread(m_hThread
);
855 if ( nSuspendCount
== (DWORD
)-1 )
857 wxLogSysError(_("Can not suspend thread %x"), m_hThread
);
862 m_state
= STATE_PAUSED
;
867 bool wxThreadInternal::Resume()
869 DWORD nSuspendCount
= ::ResumeThread(m_hThread
);
870 if ( nSuspendCount
== (DWORD
)-1 )
872 wxLogSysError(_("Can not resume thread %x"), m_hThread
);
877 // don't change the state from STATE_EXITED because it's special and means
878 // we are going to terminate without running any user code - if we did it,
879 // the code in WaitForTerminate() wouldn't work
880 if ( m_state
!= STATE_EXITED
)
882 m_state
= STATE_RUNNING
;
891 wxThread
*wxThread::This()
893 wxThread
*thread
= (wxThread
*)::TlsGetValue(gs_tlsThisThread
);
895 // be careful, 0 may be a valid return value as well
896 if ( !thread
&& (::GetLastError() != NO_ERROR
) )
898 wxLogSysError(_("Couldn't get the current thread pointer"));
906 bool wxThread::IsMain()
908 return ::GetCurrentThreadId() == gs_idMainThread
|| gs_idMainThread
== 0;
911 void wxThread::Yield()
913 // 0 argument to Sleep() is special and means to just give away the rest of
918 void wxThread::Sleep(unsigned long milliseconds
)
920 ::Sleep(milliseconds
);
923 int wxThread::GetCPUCount()
928 return si
.dwNumberOfProcessors
;
931 unsigned long wxThread::GetCurrentId()
933 return (unsigned long)::GetCurrentThreadId();
936 bool wxThread::SetConcurrency(size_t WXUNUSED_IN_WINCE(level
))
941 wxASSERT_MSG( IsMain(), _T("should only be called from the main thread") );
943 // ok only for the default one
947 // get system affinity mask first
948 HANDLE hProcess
= ::GetCurrentProcess();
949 DWORD_PTR dwProcMask
, dwSysMask
;
950 if ( ::GetProcessAffinityMask(hProcess
, &dwProcMask
, &dwSysMask
) == 0 )
952 wxLogLastError(_T("GetProcessAffinityMask"));
957 // how many CPUs have we got?
958 if ( dwSysMask
== 1 )
960 // don't bother with all this complicated stuff - on a single
961 // processor system it doesn't make much sense anyhow
965 // calculate the process mask: it's a bit vector with one bit per
966 // processor; we want to schedule the process to run on first level
971 if ( dwSysMask
& bit
)
973 // ok, we can set this bit
976 // another process added
988 // could we set all bits?
991 wxLogDebug(_T("bad level %u in wxThread::SetConcurrency()"), level
);
996 // set it: we can't link to SetProcessAffinityMask() because it doesn't
997 // exist in Win9x, use RT binding instead
999 typedef BOOL (*SETPROCESSAFFINITYMASK
)(HANDLE
, DWORD
);
1001 // can use static var because we're always in the main thread here
1002 static SETPROCESSAFFINITYMASK pfnSetProcessAffinityMask
= NULL
;
1004 if ( !pfnSetProcessAffinityMask
)
1006 HMODULE hModKernel
= ::LoadLibrary(_T("kernel32"));
1009 pfnSetProcessAffinityMask
= (SETPROCESSAFFINITYMASK
)
1010 ::GetProcAddress(hModKernel
, "SetProcessAffinityMask");
1013 // we've discovered a MT version of Win9x!
1014 wxASSERT_MSG( pfnSetProcessAffinityMask
,
1015 _T("this system has several CPUs but no SetProcessAffinityMask function?") );
1018 if ( !pfnSetProcessAffinityMask
)
1020 // msg given above - do it only once
1024 if ( pfnSetProcessAffinityMask(hProcess
, dwProcMask
) == 0 )
1026 wxLogLastError(_T("SetProcessAffinityMask"));
1032 #endif // __WXWINCE__/!__WXWINCE__
1038 wxThread::wxThread(wxThreadKind kind
)
1040 m_internal
= new wxThreadInternal(this);
1042 m_isDetached
= kind
== wxTHREAD_DETACHED
;
1045 wxThread::~wxThread()
1050 // create/start thread
1051 // -------------------
1053 wxThreadError
wxThread::Create(unsigned int stackSize
)
1055 wxCriticalSectionLocker
lock(m_critsect
);
1057 if ( !m_internal
->Create(this, stackSize
) )
1058 return wxTHREAD_NO_RESOURCE
;
1060 return wxTHREAD_NO_ERROR
;
1063 wxThreadError
wxThread::Run()
1065 wxCriticalSectionLocker
lock(m_critsect
);
1067 if ( m_internal
->GetState() != STATE_NEW
)
1069 // actually, it may be almost any state at all, not only STATE_RUNNING
1070 return wxTHREAD_RUNNING
;
1073 // the thread has just been created and is still suspended - let it run
1077 // suspend/resume thread
1078 // ---------------------
1080 wxThreadError
wxThread::Pause()
1082 wxCriticalSectionLocker
lock(m_critsect
);
1084 return m_internal
->Suspend() ? wxTHREAD_NO_ERROR
: wxTHREAD_MISC_ERROR
;
1087 wxThreadError
wxThread::Resume()
1089 wxCriticalSectionLocker
lock(m_critsect
);
1091 return m_internal
->Resume() ? wxTHREAD_NO_ERROR
: wxTHREAD_MISC_ERROR
;
1097 wxThread::ExitCode
wxThread::Wait()
1099 // although under Windows we can wait for any thread, it's an error to
1100 // wait for a detached one in wxWin API
1101 wxCHECK_MSG( !IsDetached(), (ExitCode
)-1,
1102 _T("wxThread::Wait(): can't wait for detached thread") );
1104 ExitCode rc
= (ExitCode
)-1;
1106 (void)m_internal
->WaitForTerminate(m_critsect
, &rc
);
1111 wxThreadError
wxThread::Delete(ExitCode
*pRc
)
1113 return m_internal
->WaitForTerminate(m_critsect
, pRc
, this);
1116 wxThreadError
wxThread::Kill()
1119 return wxTHREAD_NOT_RUNNING
;
1121 wxThreadError rc
= m_internal
->Kill();
1129 // update the status of the joinable thread
1130 wxCriticalSectionLocker
lock(m_critsect
);
1131 m_internal
->SetState(STATE_EXITED
);
1137 void wxThread::Exit(ExitCode status
)
1147 // update the status of the joinable thread
1148 wxCriticalSectionLocker
lock(m_critsect
);
1149 m_internal
->SetState(STATE_EXITED
);
1152 #ifdef wxUSE_BEGIN_THREAD
1153 _endthreadex((unsigned)status
);
1155 ::ExitThread((DWORD
)status
);
1156 #endif // VC++/!VC++
1158 wxFAIL_MSG(wxT("Couldn't return from ExitThread()!"));
1164 void wxThread::SetPriority(unsigned int prio
)
1166 wxCriticalSectionLocker
lock(m_critsect
);
1168 m_internal
->SetPriority(prio
);
1171 unsigned int wxThread::GetPriority() const
1173 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
1175 return m_internal
->GetPriority();
1178 unsigned long wxThread::GetId() const
1180 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
1182 return (unsigned long)m_internal
->GetId();
1185 bool wxThread::IsRunning() const
1187 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
1189 return m_internal
->GetState() == STATE_RUNNING
;
1192 bool wxThread::IsAlive() const
1194 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
1196 return (m_internal
->GetState() == STATE_RUNNING
) ||
1197 (m_internal
->GetState() == STATE_PAUSED
);
1200 bool wxThread::IsPaused() const
1202 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
1204 return m_internal
->GetState() == STATE_PAUSED
;
1207 bool wxThread::TestDestroy()
1209 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
1211 return m_internal
->GetState() == STATE_CANCELED
;
1214 // ----------------------------------------------------------------------------
1215 // Automatic initialization for thread module
1216 // ----------------------------------------------------------------------------
1218 class wxThreadModule
: public wxModule
1221 virtual bool OnInit();
1222 virtual void OnExit();
1225 DECLARE_DYNAMIC_CLASS(wxThreadModule
)
1228 IMPLEMENT_DYNAMIC_CLASS(wxThreadModule
, wxModule
)
1230 bool wxThreadModule::OnInit()
1232 // allocate TLS index for storing the pointer to the current thread
1233 gs_tlsThisThread
= ::TlsAlloc();
1234 if ( gs_tlsThisThread
== 0xFFFFFFFF )
1236 // in normal circumstances it will only happen if all other
1237 // TLS_MINIMUM_AVAILABLE (>= 64) indices are already taken - in other
1238 // words, this should never happen
1239 wxLogSysError(_("Thread module initialization failed: impossible to allocate index in thread local storage"));
1244 // main thread doesn't have associated wxThread object, so store 0 in the
1246 if ( !::TlsSetValue(gs_tlsThisThread
, (LPVOID
)0) )
1248 ::TlsFree(gs_tlsThisThread
);
1249 gs_tlsThisThread
= 0xFFFFFFFF;
1251 wxLogSysError(_("Thread module initialization failed: can not store value in thread local storage"));
1256 gs_critsectWaitingForGui
= new wxCriticalSection();
1258 gs_critsectGui
= new wxCriticalSection();
1259 gs_critsectGui
->Enter();
1261 gs_critsectThreadDelete
= new wxCriticalSection
;
1263 // no error return for GetCurrentThreadId()
1264 gs_idMainThread
= ::GetCurrentThreadId();
1269 void wxThreadModule::OnExit()
1271 if ( !::TlsFree(gs_tlsThisThread
) )
1273 wxLogLastError(wxT("TlsFree failed."));
1276 delete gs_critsectThreadDelete
;
1277 gs_critsectThreadDelete
= NULL
;
1279 if ( gs_critsectGui
)
1281 gs_critsectGui
->Leave();
1282 delete gs_critsectGui
;
1283 gs_critsectGui
= NULL
;
1286 delete gs_critsectWaitingForGui
;
1287 gs_critsectWaitingForGui
= NULL
;
1290 // ----------------------------------------------------------------------------
1291 // under Windows, these functions are implemented using a critical section and
1292 // not a mutex, so the names are a bit confusing
1293 // ----------------------------------------------------------------------------
1295 void WXDLLIMPEXP_BASE
wxMutexGuiEnter()
1297 // this would dead lock everything...
1298 wxASSERT_MSG( !wxThread::IsMain(),
1299 wxT("main thread doesn't want to block in wxMutexGuiEnter()!") );
1301 // the order in which we enter the critical sections here is crucial!!
1303 // set the flag telling to the main thread that we want to do some GUI
1305 wxCriticalSectionLocker
enter(*gs_critsectWaitingForGui
);
1307 gs_nWaitingForGui
++;
1310 wxWakeUpMainThread();
1312 // now we may block here because the main thread will soon let us in
1313 // (during the next iteration of OnIdle())
1314 gs_critsectGui
->Enter();
1317 void WXDLLIMPEXP_BASE
wxMutexGuiLeave()
1319 wxCriticalSectionLocker
enter(*gs_critsectWaitingForGui
);
1321 if ( wxThread::IsMain() )
1323 gs_bGuiOwnedByMainThread
= false;
1327 // decrement the number of threads waiting for GUI access now
1328 wxASSERT_MSG( gs_nWaitingForGui
> 0,
1329 wxT("calling wxMutexGuiLeave() without entering it first?") );
1331 gs_nWaitingForGui
--;
1333 wxWakeUpMainThread();
1336 gs_critsectGui
->Leave();
1339 void WXDLLIMPEXP_BASE
wxMutexGuiLeaveOrEnter()
1341 wxASSERT_MSG( wxThread::IsMain(),
1342 wxT("only main thread may call wxMutexGuiLeaveOrEnter()!") );
1344 wxCriticalSectionLocker
enter(*gs_critsectWaitingForGui
);
1346 if ( gs_nWaitingForGui
== 0 )
1348 // no threads are waiting for GUI - so we may acquire the lock without
1349 // any danger (but only if we don't already have it)
1350 if ( !wxGuiOwnedByMainThread() )
1352 gs_critsectGui
->Enter();
1354 gs_bGuiOwnedByMainThread
= true;
1356 //else: already have it, nothing to do
1360 // some threads are waiting, release the GUI lock if we have it
1361 if ( wxGuiOwnedByMainThread() )
1365 //else: some other worker thread is doing GUI
1369 bool WXDLLIMPEXP_BASE
wxGuiOwnedByMainThread()
1371 return gs_bGuiOwnedByMainThread
;
1374 // wake up the main thread if it's in ::GetMessage()
1375 void WXDLLIMPEXP_BASE
wxWakeUpMainThread()
1377 // sending any message would do - hopefully WM_NULL is harmless enough
1378 if ( !::PostThreadMessage(gs_idMainThread
, WM_NULL
, 0, 0) )
1380 // should never happen
1381 wxLogLastError(wxT("PostThreadMessage(WM_NULL)"));
1385 bool WXDLLIMPEXP_BASE
wxIsWaitingForThread()
1387 return gs_waitingForThread
;
1390 // ----------------------------------------------------------------------------
1391 // include common implementation code
1392 // ----------------------------------------------------------------------------
1394 #include "wx/thrimpl.cpp"
1396 #endif // wxUSE_THREADS