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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
14 #pragma implementation "thread.h"
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
24 #if defined(__BORLANDC__)
35 #include "wx/apptrait.h"
37 #include "wx/msw/private.h"
38 #include "wx/msw/missing.h"
40 #include "wx/module.h"
41 #include "wx/thread.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 // number of threads waiting for GUI in wxMutexGuiEnter()
127 static size_t gs_nWaitingForGui
= 0;
129 // are we waiting for a thread termination?
130 static bool gs_waitingForThread
= FALSE
;
132 // ============================================================================
133 // Windows implementation of thread and related classes
134 // ============================================================================
136 // ----------------------------------------------------------------------------
138 // ----------------------------------------------------------------------------
140 wxCriticalSection::wxCriticalSection()
142 wxCOMPILE_TIME_ASSERT( sizeof(CRITICAL_SECTION
) <= sizeof(wxCritSectBuffer
),
143 wxCriticalSectionBufferTooSmall
);
145 ::InitializeCriticalSection((CRITICAL_SECTION
*)m_buffer
);
148 wxCriticalSection::~wxCriticalSection()
150 ::DeleteCriticalSection((CRITICAL_SECTION
*)m_buffer
);
153 void wxCriticalSection::Enter()
155 ::EnterCriticalSection((CRITICAL_SECTION
*)m_buffer
);
158 void wxCriticalSection::Leave()
160 ::LeaveCriticalSection((CRITICAL_SECTION
*)m_buffer
);
163 // ----------------------------------------------------------------------------
165 // ----------------------------------------------------------------------------
167 class wxMutexInternal
170 wxMutexInternal(wxMutexType mutexType
);
173 bool IsOk() const { return m_mutex
!= NULL
; }
175 wxMutexError
Lock() { return LockTimeout(INFINITE
); }
176 wxMutexError
TryLock() { return LockTimeout(0); }
177 wxMutexError
Unlock();
180 wxMutexError
LockTimeout(DWORD milliseconds
);
184 DECLARE_NO_COPY_CLASS(wxMutexInternal
)
187 // all mutexes are recursive under Win32 so we don't use mutexType
188 wxMutexInternal::wxMutexInternal(wxMutexType
WXUNUSED(mutexType
))
190 // create a nameless (hence intra process and always private) mutex
191 m_mutex
= ::CreateMutex
193 NULL
, // default secutiry attributes
194 FALSE
, // not initially locked
200 wxLogLastError(_T("CreateMutex()"));
204 wxMutexInternal::~wxMutexInternal()
208 if ( !::CloseHandle(m_mutex
) )
210 wxLogLastError(_T("CloseHandle(mutex)"));
215 wxMutexError
wxMutexInternal::LockTimeout(DWORD milliseconds
)
217 DWORD rc
= ::WaitForSingleObject(m_mutex
, milliseconds
);
218 if ( rc
== WAIT_ABANDONED
)
220 // the previous caller died without releasing the mutex, but now we can
222 wxLogDebug(_T("WaitForSingleObject() returned WAIT_ABANDONED"));
224 // use 0 timeout, normally we should always get it
225 rc
= ::WaitForSingleObject(m_mutex
, 0);
237 case WAIT_ABANDONED
: // checked for above
239 wxFAIL_MSG(wxT("impossible return value in wxMutex::Lock"));
243 wxLogLastError(_T("WaitForSingleObject(mutex)"));
244 return wxMUTEX_MISC_ERROR
;
247 return wxMUTEX_NO_ERROR
;
250 wxMutexError
wxMutexInternal::Unlock()
252 if ( !::ReleaseMutex(m_mutex
) )
254 wxLogLastError(_T("ReleaseMutex()"));
256 return wxMUTEX_MISC_ERROR
;
259 return wxMUTEX_NO_ERROR
;
262 // --------------------------------------------------------------------------
264 // --------------------------------------------------------------------------
266 // a trivial wrapper around Win32 semaphore
267 class wxSemaphoreInternal
270 wxSemaphoreInternal(int initialcount
, int maxcount
);
271 ~wxSemaphoreInternal();
273 bool IsOk() const { return m_semaphore
!= NULL
; }
275 wxSemaError
Wait() { return WaitTimeout(INFINITE
); }
277 wxSemaError
TryWait()
279 wxSemaError rc
= WaitTimeout(0);
280 if ( rc
== wxSEMA_TIMEOUT
)
286 wxSemaError
WaitTimeout(unsigned long milliseconds
);
293 DECLARE_NO_COPY_CLASS(wxSemaphoreInternal
)
296 wxSemaphoreInternal::wxSemaphoreInternal(int initialcount
, int maxcount
)
301 // make it practically infinite
305 m_semaphore
= ::CreateSemaphore
307 NULL
, // default security attributes
315 wxLogLastError(_T("CreateSemaphore()"));
319 wxSemaphoreInternal::~wxSemaphoreInternal()
323 if ( !::CloseHandle(m_semaphore
) )
325 wxLogLastError(_T("CloseHandle(semaphore)"));
330 wxSemaError
wxSemaphoreInternal::WaitTimeout(unsigned long milliseconds
)
332 DWORD rc
= ::WaitForSingleObject( m_semaphore
, milliseconds
);
337 return wxSEMA_NO_ERROR
;
340 return wxSEMA_TIMEOUT
;
343 wxLogLastError(_T("WaitForSingleObject(semaphore)"));
346 return wxSEMA_MISC_ERROR
;
349 wxSemaError
wxSemaphoreInternal::Post()
352 if ( !::ReleaseSemaphore(m_semaphore
, 1, NULL
/* ptr to previous count */) )
355 wxLogLastError(_T("ReleaseSemaphore"));
357 return wxSEMA_MISC_ERROR
;
360 return wxSEMA_NO_ERROR
;
363 // --------------------------------------------------------------------------
365 // --------------------------------------------------------------------------
367 // Win32 doesn't have explicit support for the POSIX condition variables and
368 // the Win32 events have quite different semantics, so we reimplement the
369 // conditions from scratch using the mutexes and semaphores
370 class wxConditionInternal
373 wxConditionInternal(wxMutex
& mutex
);
375 bool IsOk() const { return m_mutex
.IsOk() && m_semaphore
.IsOk(); }
378 wxCondError
WaitTimeout(unsigned long milliseconds
);
380 wxCondError
Signal();
381 wxCondError
Broadcast();
384 // the number of threads currently waiting for this condition
387 // the critical section protecting m_numWaiters
388 wxCriticalSection m_csWaiters
;
391 wxSemaphore m_semaphore
;
393 DECLARE_NO_COPY_CLASS(wxConditionInternal
)
396 wxConditionInternal::wxConditionInternal(wxMutex
& mutex
)
399 // another thread can't access it until we return from ctor, so no need to
400 // protect access to m_numWaiters here
404 wxCondError
wxConditionInternal::Wait()
406 // increment the number of waiters
407 ::InterlockedIncrement(&m_numWaiters
);
411 // a potential race condition can occur here
413 // after a thread increments nwaiters, and unlocks the mutex and before the
414 // semaphore.Wait() is called, if another thread can cause a signal to be
417 // this race condition is handled by using a semaphore and incrementing the
418 // semaphore only if 'nwaiters' is greater that zero since the semaphore,
419 // can 'remember' signals the race condition will not occur
421 // wait ( if necessary ) and decrement semaphore
422 wxSemaError err
= m_semaphore
.Wait();
425 return err
== wxSEMA_NO_ERROR
? wxCOND_NO_ERROR
: wxCOND_MISC_ERROR
;
428 wxCondError
wxConditionInternal::WaitTimeout(unsigned long milliseconds
)
430 ::InterlockedIncrement(&m_numWaiters
);
434 // a race condition can occur at this point in the code
436 // please see the comments in Wait(), for details
438 wxSemaError err
= m_semaphore
.WaitTimeout(milliseconds
);
440 if ( err
== wxSEMA_BUSY
)
442 // another potential race condition exists here it is caused when a
443 // 'waiting' thread timesout, and returns from WaitForSingleObject, but
444 // has not yet decremented 'nwaiters'.
446 // at this point if another thread calls signal() then the semaphore
447 // will be incremented, but the waiting thread will miss it.
449 // to handle this particular case, the waiting thread calls
450 // WaitForSingleObject again with a timeout of 0, after locking
451 // 'nwaiters_mutex'. this call does not block because of the zero
452 // timeout, but will allow the waiting thread to catch the missed
454 wxCriticalSectionLocker
lock(m_csWaiters
);
456 err
= m_semaphore
.WaitTimeout(0);
458 if ( err
!= wxSEMA_NO_ERROR
)
466 return err
== wxSEMA_NO_ERROR
? wxCOND_NO_ERROR
: wxCOND_MISC_ERROR
;
469 wxCondError
wxConditionInternal::Signal()
471 wxCriticalSectionLocker
lock(m_csWaiters
);
473 if ( m_numWaiters
> 0 )
475 // increment the semaphore by 1
476 if ( m_semaphore
.Post() != wxSEMA_NO_ERROR
)
477 return wxCOND_MISC_ERROR
;
482 return wxCOND_NO_ERROR
;
485 wxCondError
wxConditionInternal::Broadcast()
487 wxCriticalSectionLocker
lock(m_csWaiters
);
489 while ( m_numWaiters
> 0 )
491 if ( m_semaphore
.Post() != wxSEMA_NO_ERROR
)
492 return wxCOND_MISC_ERROR
;
497 return wxCOND_NO_ERROR
;
500 // ----------------------------------------------------------------------------
501 // wxThread implementation
502 // ----------------------------------------------------------------------------
504 // wxThreadInternal class
505 // ----------------------
507 class wxThreadInternal
514 m_priority
= WXTHREAD_DEFAULT_PRIORITY
;
526 if ( !::CloseHandle(m_hThread
) )
528 wxLogLastError(wxT("CloseHandle(thread)"));
535 // create a new (suspended) thread (for the given thread object)
536 bool Create(wxThread
*thread
, unsigned int stackSize
);
538 // wait for the thread to terminate, either by itself, or by asking it
539 // (politely, this is not Kill()!) to do it
540 wxThreadError
WaitForTerminate(bool shouldCancel
,
541 wxCriticalSection
& cs
,
542 wxThread::ExitCode
*pRc
);
544 // kill the thread unconditionally
545 wxThreadError
Kill();
547 // suspend/resume/terminate
550 void Cancel() { m_state
= STATE_CANCELED
; }
553 void SetState(wxThreadState state
) { m_state
= state
; }
554 wxThreadState
GetState() const { return m_state
; }
557 void SetPriority(unsigned int priority
);
558 unsigned int GetPriority() const { return m_priority
; }
560 // thread handle and id
561 HANDLE
GetHandle() const { return m_hThread
; }
562 DWORD
GetId() const { return m_tid
; }
565 static THREAD_RETVAL THREAD_CALLCONV
WinThreadStart(void *thread
);
568 HANDLE m_hThread
; // handle of the thread
569 wxThreadState m_state
; // state, see wxThreadState enum
570 unsigned int m_priority
; // thread priority in "wx" units
571 DWORD m_tid
; // thread id
573 DECLARE_NO_COPY_CLASS(wxThreadInternal
)
576 THREAD_RETVAL THREAD_CALLCONV
wxThreadInternal::WinThreadStart(void *param
)
581 // first of all, check whether we hadn't been cancelled already and don't
582 // start the user code at all then
583 wxThread
*thread
= (wxThread
*)param
;
584 if ( thread
->m_internal
->GetState() == STATE_EXITED
)
586 rc
= (THREAD_RETVAL
)-1;
589 else // do run thread
591 // store the thread object in the TLS
592 if ( !::TlsSetValue(gs_tlsThisThread
, thread
) )
594 wxLogSysError(_("Can not start thread: error writing TLS."));
599 rc
= (THREAD_RETVAL
)thread
->Entry();
601 // enter m_critsect before changing the thread state
602 thread
->m_critsect
.Enter();
603 wasCancelled
= thread
->m_internal
->GetState() == STATE_CANCELED
;
604 thread
->m_internal
->SetState(STATE_EXITED
);
605 thread
->m_critsect
.Leave();
610 // if the thread was cancelled (from Delete()), then its handle is still
612 if ( thread
->IsDetached() && !wasCancelled
)
617 //else: the joinable threads handle will be closed when Wait() is done
622 void wxThreadInternal::SetPriority(unsigned int priority
)
624 m_priority
= priority
;
626 // translate wxWindows priority to the Windows one
628 if (m_priority
<= 20)
629 win_priority
= THREAD_PRIORITY_LOWEST
;
630 else if (m_priority
<= 40)
631 win_priority
= THREAD_PRIORITY_BELOW_NORMAL
;
632 else if (m_priority
<= 60)
633 win_priority
= THREAD_PRIORITY_NORMAL
;
634 else if (m_priority
<= 80)
635 win_priority
= THREAD_PRIORITY_ABOVE_NORMAL
;
636 else if (m_priority
<= 100)
637 win_priority
= THREAD_PRIORITY_HIGHEST
;
640 wxFAIL_MSG(wxT("invalid value of thread priority parameter"));
641 win_priority
= THREAD_PRIORITY_NORMAL
;
644 if ( !::SetThreadPriority(m_hThread
, win_priority
) )
646 wxLogSysError(_("Can't set thread priority"));
650 bool wxThreadInternal::Create(wxThread
*thread
, unsigned int stackSize
)
652 wxASSERT_MSG( m_state
== STATE_NEW
&& !m_hThread
,
653 _T("Create()ing thread twice?") );
655 // for compilers which have it, we should use C RTL function for thread
656 // creation instead of Win32 API one because otherwise we will have memory
657 // leaks if the thread uses C RTL (and most threads do)
658 #ifdef wxUSE_BEGIN_THREAD
660 // Watcom is reported to not like 0 stack size (which means "use default"
661 // for the other compilers and is also the default value for stackSize)
665 #endif // __WATCOMC__
667 m_hThread
= (HANDLE
)_beginthreadex
669 NULL
, // default security
671 wxThreadInternal::WinThreadStart
, // entry point
674 (unsigned int *)&m_tid
676 #else // compiler doesn't have _beginthreadex
677 m_hThread
= ::CreateThread
679 NULL
, // default security
680 stackSize
, // stack size
681 wxThreadInternal::WinThreadStart
, // thread entry point
682 (LPVOID
)thread
, // parameter
683 CREATE_SUSPENDED
, // flags
684 &m_tid
// [out] thread id
686 #endif // _beginthreadex/CreateThread
688 if ( m_hThread
== NULL
)
690 wxLogSysError(_("Can't create thread"));
695 if ( m_priority
!= WXTHREAD_DEFAULT_PRIORITY
)
697 SetPriority(m_priority
);
703 wxThreadError
wxThreadInternal::Kill()
705 if ( !::TerminateThread(m_hThread
, (DWORD
)-1) )
707 wxLogSysError(_("Couldn't terminate thread"));
709 return wxTHREAD_MISC_ERROR
;
714 return wxTHREAD_NO_ERROR
;
718 wxThreadInternal::WaitForTerminate(bool shouldCancel
,
719 wxCriticalSection
& cs
,
720 wxThread::ExitCode
*pRc
)
722 wxThread::ExitCode rc
= 0;
724 // Delete() is always safe to call, so consider all possible states
726 // we might need to resume the thread, but we might also not need to cancel
727 // it if it doesn't run yet
728 bool shouldResume
= FALSE
,
731 // check if the thread already started to run
733 wxCriticalSectionLocker
lock(cs
);
735 if ( m_state
== STATE_NEW
)
739 // WinThreadStart() will see it and terminate immediately, no need
740 // to cancel the thread - but we still need to resume it to let it
742 m_state
= STATE_EXITED
;
744 Resume(); // it knows about STATE_EXITED special case
746 shouldCancel
= FALSE
;
751 // shouldResume is correctly set to FALSE here
755 shouldResume
= m_state
== STATE_PAUSED
;
759 // resume the thread if it is paused
763 // does is still run?
764 if ( isRunning
|| m_state
== STATE_RUNNING
)
766 if ( wxThread::IsMain() )
768 // set flag for wxIsWaitingForThread()
769 gs_waitingForThread
= TRUE
;
772 // ask the thread to terminate
775 wxCriticalSectionLocker
lock(cs
);
780 // we can't just wait for the thread to terminate because it might be
781 // calling some GUI functions and so it will never terminate before we
782 // process the Windows messages that result from these functions
783 // (note that even in console applications we might have to process
784 // messages if we use wxExecute() or timers or ...)
785 DWORD result
= 0; // suppress warnings from broken compilers
788 if ( wxThread::IsMain() )
790 // give the thread we're waiting for chance to do the GUI call
792 if ( (gs_nWaitingForGui
> 0) && wxGuiOwnedByMainThread() )
798 result
= ::MsgWaitForMultipleObjects
800 1, // number of objects to wait for
801 &m_hThread
, // the objects
802 FALSE
, // don't wait for all objects
803 INFINITE
, // no timeout
804 QS_ALLINPUT
| // return as soon as there are any events
812 wxLogSysError(_("Can not wait for thread termination"));
814 return wxTHREAD_KILLED
;
817 // thread we're waiting for terminated
820 case WAIT_OBJECT_0
+ 1:
821 // new message arrived, process it -- but only if we're the
822 // main thread as we don't support processing messages in
825 // NB: we still must include QS_ALLINPUT even when waiting
826 // in a secondary thread because if it had created some
827 // window somehow (possible not even using wxWindows)
828 // the system might dead lock then
829 if ( wxThread::IsMain() )
831 // it looks that sometimes WAIT_OBJECT_0 + 1 is
832 // returned but there are no messages in the thread
833 // queue -- prevent DoMessageFromThreadWait() from
834 // blocking inside ::GetMessage() forever in this case
835 ::PostMessage(NULL
, WM_NULL
, 0, 0);
837 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits()
840 if ( traits
&& !traits
->DoMessageFromThreadWait() )
842 // WM_QUIT received: kill the thread
845 return wxTHREAD_KILLED
;
851 wxFAIL_MSG(wxT("unexpected result of MsgWaitForMultipleObject"));
853 } while ( result
!= WAIT_OBJECT_0
);
855 if ( wxThread::IsMain() )
857 gs_waitingForThread
= FALSE
;
861 // although the thread might be already in the EXITED state it might not
862 // have terminated yet and so we are not sure that it has actually
863 // terminated if the "if" above hadn't been taken
866 if ( !::GetExitCodeThread(m_hThread
, (LPDWORD
)&rc
) )
868 wxLogLastError(wxT("GetExitCodeThread"));
870 rc
= (wxThread::ExitCode
)-1;
872 } while ( (DWORD
)rc
== STILL_ACTIVE
);
877 return rc
== (wxThread::ExitCode
)-1 ? wxTHREAD_MISC_ERROR
881 bool wxThreadInternal::Suspend()
883 DWORD nSuspendCount
= ::SuspendThread(m_hThread
);
884 if ( nSuspendCount
== (DWORD
)-1 )
886 wxLogSysError(_("Can not suspend thread %x"), m_hThread
);
891 m_state
= STATE_PAUSED
;
896 bool wxThreadInternal::Resume()
898 DWORD nSuspendCount
= ::ResumeThread(m_hThread
);
899 if ( nSuspendCount
== (DWORD
)-1 )
901 wxLogSysError(_("Can not resume thread %x"), m_hThread
);
906 // don't change the state from STATE_EXITED because it's special and means
907 // we are going to terminate without running any user code - if we did it,
908 // the codei n Delete() wouldn't work
909 if ( m_state
!= STATE_EXITED
)
911 m_state
= STATE_RUNNING
;
920 wxThread
*wxThread::This()
922 wxThread
*thread
= (wxThread
*)::TlsGetValue(gs_tlsThisThread
);
924 // be careful, 0 may be a valid return value as well
925 if ( !thread
&& (::GetLastError() != NO_ERROR
) )
927 wxLogSysError(_("Couldn't get the current thread pointer"));
935 bool wxThread::IsMain()
937 return ::GetCurrentThreadId() == gs_idMainThread
;
944 void wxThread::Yield()
946 // 0 argument to Sleep() is special and means to just give away the rest of
951 void wxThread::Sleep(unsigned long milliseconds
)
953 ::Sleep(milliseconds
);
956 int wxThread::GetCPUCount()
961 return si
.dwNumberOfProcessors
;
964 unsigned long wxThread::GetCurrentId()
966 return (unsigned long)::GetCurrentThreadId();
969 bool wxThread::SetConcurrency(size_t level
)
972 wxASSERT_MSG( IsMain(), _T("should only be called from the main thread") );
974 // ok only for the default one
978 // get system affinity mask first
979 HANDLE hProcess
= ::GetCurrentProcess();
980 DWORD dwProcMask
, dwSysMask
;
981 if ( ::GetProcessAffinityMask(hProcess
, &dwProcMask
, &dwSysMask
) == 0 )
983 wxLogLastError(_T("GetProcessAffinityMask"));
988 // how many CPUs have we got?
989 if ( dwSysMask
== 1 )
991 // don't bother with all this complicated stuff - on a single
992 // processor system it doesn't make much sense anyhow
996 // calculate the process mask: it's a bit vector with one bit per
997 // processor; we want to schedule the process to run on first level
1002 if ( dwSysMask
& bit
)
1004 // ok, we can set this bit
1007 // another process added
1010 // and that's enough
1019 // could we set all bits?
1022 wxLogDebug(_T("bad level %u in wxThread::SetConcurrency()"), level
);
1027 // set it: we can't link to SetProcessAffinityMask() because it doesn't
1028 // exist in Win9x, use RT binding instead
1030 typedef BOOL (*SETPROCESSAFFINITYMASK
)(HANDLE
, DWORD
);
1032 // can use static var because we're always in the main thread here
1033 static SETPROCESSAFFINITYMASK pfnSetProcessAffinityMask
= NULL
;
1035 if ( !pfnSetProcessAffinityMask
)
1037 HMODULE hModKernel
= ::LoadLibrary(_T("kernel32"));
1040 pfnSetProcessAffinityMask
= (SETPROCESSAFFINITYMASK
)
1041 ::GetProcAddress(hModKernel
, "SetProcessAffinityMask");
1044 // we've discovered a MT version of Win9x!
1045 wxASSERT_MSG( pfnSetProcessAffinityMask
,
1046 _T("this system has several CPUs but no SetProcessAffinityMask function?") );
1049 if ( !pfnSetProcessAffinityMask
)
1051 // msg given above - do it only once
1055 if ( pfnSetProcessAffinityMask(hProcess
, dwProcMask
) == 0 )
1057 wxLogLastError(_T("SetProcessAffinityMask"));
1068 wxThread::wxThread(wxThreadKind kind
)
1070 m_internal
= new wxThreadInternal();
1072 m_isDetached
= kind
== wxTHREAD_DETACHED
;
1075 wxThread::~wxThread()
1080 // create/start thread
1081 // -------------------
1083 wxThreadError
wxThread::Create(unsigned int stackSize
)
1085 wxCriticalSectionLocker
lock(m_critsect
);
1087 if ( !m_internal
->Create(this, stackSize
) )
1088 return wxTHREAD_NO_RESOURCE
;
1090 return wxTHREAD_NO_ERROR
;
1093 wxThreadError
wxThread::Run()
1095 wxCriticalSectionLocker
lock(m_critsect
);
1097 if ( m_internal
->GetState() != STATE_NEW
)
1099 // actually, it may be almost any state at all, not only STATE_RUNNING
1100 return wxTHREAD_RUNNING
;
1103 // the thread has just been created and is still suspended - let it run
1107 // suspend/resume thread
1108 // ---------------------
1110 wxThreadError
wxThread::Pause()
1112 wxCriticalSectionLocker
lock(m_critsect
);
1114 return m_internal
->Suspend() ? wxTHREAD_NO_ERROR
: wxTHREAD_MISC_ERROR
;
1117 wxThreadError
wxThread::Resume()
1119 wxCriticalSectionLocker
lock(m_critsect
);
1121 return m_internal
->Resume() ? wxTHREAD_NO_ERROR
: wxTHREAD_MISC_ERROR
;
1127 wxThread::ExitCode
wxThread::Wait()
1129 // although under Windows we can wait for any thread, it's an error to
1130 // wait for a detached one in wxWin API
1131 wxCHECK_MSG( !IsDetached(), (ExitCode
)-1,
1132 _T("wxThread::Wait(): can't wait for detached thread") );
1134 ExitCode rc
= (ExitCode
)-1;
1136 (void)m_internal
->WaitForTerminate(false, m_critsect
, &rc
);
1140 wxCriticalSectionLocker
lock(m_critsect
);
1141 m_internal
->SetState(STATE_EXITED
);
1146 wxThreadError
wxThread::Delete(ExitCode
*pRc
)
1148 wxThreadError rc
= m_internal
->WaitForTerminate(true, m_critsect
, pRc
);
1156 // update the status of the joinable thread
1157 wxCriticalSectionLocker
lock(m_critsect
);
1158 m_internal
->SetState(STATE_EXITED
);
1164 wxThreadError
wxThread::Kill()
1167 return wxTHREAD_NOT_RUNNING
;
1169 wxThreadError rc
= m_internal
->Kill();
1177 // update the status of the joinable thread
1178 wxCriticalSectionLocker
lock(m_critsect
);
1179 m_internal
->SetState(STATE_EXITED
);
1185 void wxThread::Exit(ExitCode status
)
1195 // update the status of the joinable thread
1196 wxCriticalSectionLocker
lock(m_critsect
);
1197 m_internal
->SetState(STATE_EXITED
);
1200 #ifdef wxUSE_BEGIN_THREAD
1201 _endthreadex((unsigned)status
);
1203 ::ExitThread((DWORD
)status
);
1204 #endif // VC++/!VC++
1206 wxFAIL_MSG(wxT("Couldn't return from ExitThread()!"));
1212 void wxThread::SetPriority(unsigned int prio
)
1214 wxCriticalSectionLocker
lock(m_critsect
);
1216 m_internal
->SetPriority(prio
);
1219 unsigned int wxThread::GetPriority() const
1221 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
1223 return m_internal
->GetPriority();
1226 unsigned long wxThread::GetId() const
1228 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
1230 return (unsigned long)m_internal
->GetId();
1233 bool wxThread::IsRunning() const
1235 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
1237 return m_internal
->GetState() == STATE_RUNNING
;
1240 bool wxThread::IsAlive() const
1242 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
1244 return (m_internal
->GetState() == STATE_RUNNING
) ||
1245 (m_internal
->GetState() == STATE_PAUSED
);
1248 bool wxThread::IsPaused() const
1250 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
1252 return m_internal
->GetState() == STATE_PAUSED
;
1255 bool wxThread::TestDestroy()
1257 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
1259 return m_internal
->GetState() == STATE_CANCELED
;
1262 // ----------------------------------------------------------------------------
1263 // Automatic initialization for thread module
1264 // ----------------------------------------------------------------------------
1266 class wxThreadModule
: public wxModule
1269 virtual bool OnInit();
1270 virtual void OnExit();
1273 DECLARE_DYNAMIC_CLASS(wxThreadModule
)
1276 IMPLEMENT_DYNAMIC_CLASS(wxThreadModule
, wxModule
)
1278 bool wxThreadModule::OnInit()
1280 // allocate TLS index for storing the pointer to the current thread
1281 gs_tlsThisThread
= ::TlsAlloc();
1282 if ( gs_tlsThisThread
== 0xFFFFFFFF )
1284 // in normal circumstances it will only happen if all other
1285 // TLS_MINIMUM_AVAILABLE (>= 64) indices are already taken - in other
1286 // words, this should never happen
1287 wxLogSysError(_("Thread module initialization failed: impossible to allocate index in thread local storage"));
1292 // main thread doesn't have associated wxThread object, so store 0 in the
1294 if ( !::TlsSetValue(gs_tlsThisThread
, (LPVOID
)0) )
1296 ::TlsFree(gs_tlsThisThread
);
1297 gs_tlsThisThread
= 0xFFFFFFFF;
1299 wxLogSysError(_("Thread module initialization failed: can not store value in thread local storage"));
1304 gs_critsectWaitingForGui
= new wxCriticalSection();
1306 gs_critsectGui
= new wxCriticalSection();
1307 gs_critsectGui
->Enter();
1309 // no error return for GetCurrentThreadId()
1310 gs_idMainThread
= ::GetCurrentThreadId();
1315 void wxThreadModule::OnExit()
1317 if ( !::TlsFree(gs_tlsThisThread
) )
1319 wxLogLastError(wxT("TlsFree failed."));
1322 if ( gs_critsectGui
)
1324 gs_critsectGui
->Leave();
1325 delete gs_critsectGui
;
1326 gs_critsectGui
= NULL
;
1329 delete gs_critsectWaitingForGui
;
1330 gs_critsectWaitingForGui
= NULL
;
1333 // ----------------------------------------------------------------------------
1334 // under Windows, these functions are implemented using a critical section and
1335 // not a mutex, so the names are a bit confusing
1336 // ----------------------------------------------------------------------------
1338 void WXDLLIMPEXP_BASE
wxMutexGuiEnter()
1340 // this would dead lock everything...
1341 wxASSERT_MSG( !wxThread::IsMain(),
1342 wxT("main thread doesn't want to block in wxMutexGuiEnter()!") );
1344 // the order in which we enter the critical sections here is crucial!!
1346 // set the flag telling to the main thread that we want to do some GUI
1348 wxCriticalSectionLocker
enter(*gs_critsectWaitingForGui
);
1350 gs_nWaitingForGui
++;
1353 wxWakeUpMainThread();
1355 // now we may block here because the main thread will soon let us in
1356 // (during the next iteration of OnIdle())
1357 gs_critsectGui
->Enter();
1360 void WXDLLIMPEXP_BASE
wxMutexGuiLeave()
1362 wxCriticalSectionLocker
enter(*gs_critsectWaitingForGui
);
1364 if ( wxThread::IsMain() )
1366 gs_bGuiOwnedByMainThread
= FALSE
;
1370 // decrement the number of threads waiting for GUI access now
1371 wxASSERT_MSG( gs_nWaitingForGui
> 0,
1372 wxT("calling wxMutexGuiLeave() without entering it first?") );
1374 gs_nWaitingForGui
--;
1376 wxWakeUpMainThread();
1379 gs_critsectGui
->Leave();
1382 void WXDLLIMPEXP_BASE
wxMutexGuiLeaveOrEnter()
1384 wxASSERT_MSG( wxThread::IsMain(),
1385 wxT("only main thread may call wxMutexGuiLeaveOrEnter()!") );
1387 wxCriticalSectionLocker
enter(*gs_critsectWaitingForGui
);
1389 if ( gs_nWaitingForGui
== 0 )
1391 // no threads are waiting for GUI - so we may acquire the lock without
1392 // any danger (but only if we don't already have it)
1393 if ( !wxGuiOwnedByMainThread() )
1395 gs_critsectGui
->Enter();
1397 gs_bGuiOwnedByMainThread
= TRUE
;
1399 //else: already have it, nothing to do
1403 // some threads are waiting, release the GUI lock if we have it
1404 if ( wxGuiOwnedByMainThread() )
1408 //else: some other worker thread is doing GUI
1412 bool WXDLLIMPEXP_BASE
wxGuiOwnedByMainThread()
1414 return gs_bGuiOwnedByMainThread
;
1417 // wake up the main thread if it's in ::GetMessage()
1418 void WXDLLIMPEXP_BASE
wxWakeUpMainThread()
1420 // sending any message would do - hopefully WM_NULL is harmless enough
1421 if ( !::PostThreadMessage(gs_idMainThread
, WM_NULL
, 0, 0) )
1423 // should never happen
1424 wxLogLastError(wxT("PostThreadMessage(WM_NULL)"));
1428 bool WXDLLIMPEXP_BASE
wxIsWaitingForThread()
1430 return gs_waitingForThread
;
1433 // ----------------------------------------------------------------------------
1434 // include common implementation code
1435 // ----------------------------------------------------------------------------
1437 #include "wx/thrimpl.cpp"
1439 #endif // wxUSE_THREADS