1 /////////////////////////////////////////////////////////////////////////////
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)
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
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__)
34 #include "wx/msw/private.h"
36 #include "wx/module.h"
37 #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__)) || \
67 #undef wxUSE_BEGIN_THREAD
68 #define wxUSE_BEGIN_THREAD
71 #ifdef wxUSE_BEGIN_THREAD
72 // this is where _beginthreadex() is declared
75 // the return type of the thread function entry point
76 typedef unsigned THREAD_RETVAL
;
78 // the calling convention of the thread function entry point
79 #define THREAD_CALLCONV __stdcall
81 // the settings for CreateThread()
82 typedef DWORD THREAD_RETVAL
;
83 #define THREAD_CALLCONV WINAPI
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
90 // the possible states of the thread ("=>" shows all possible transitions from
94 STATE_NEW
, // didn't start execution yet (=> RUNNING)
95 STATE_RUNNING
, // thread is running (=> PAUSED, CANCELED)
96 STATE_PAUSED
, // thread is temporarily suspended (=> RUNNING)
97 STATE_CANCELED
, // thread should terminate a.s.a.p. (=> EXITED)
98 STATE_EXITED
// thread is terminating
101 // ----------------------------------------------------------------------------
102 // this module globals
103 // ----------------------------------------------------------------------------
105 // TLS index of the slot where we store the pointer to the current thread
106 static DWORD gs_tlsThisThread
= 0xFFFFFFFF;
108 // id of the main thread - the one which can call GUI functions without first
109 // calling wxMutexGuiEnter()
110 static DWORD gs_idMainThread
= 0;
112 // if it's FALSE, some secondary thread is holding the GUI lock
113 static bool gs_bGuiOwnedByMainThread
= TRUE
;
115 // critical section which controls access to all GUI functions: any secondary
116 // thread (i.e. except the main one) must enter this crit section before doing
118 static wxCriticalSection
*gs_critsectGui
= NULL
;
120 // critical section which protects gs_nWaitingForGui variable
121 static wxCriticalSection
*gs_critsectWaitingForGui
= NULL
;
123 // number of threads waiting for GUI in wxMutexGuiEnter()
124 static size_t gs_nWaitingForGui
= 0;
126 // are we waiting for a thread termination?
127 static bool gs_waitingForThread
= FALSE
;
129 // ============================================================================
130 // Windows implementation of thread classes
131 // ============================================================================
133 // ----------------------------------------------------------------------------
134 // wxMutex implementation
135 // ----------------------------------------------------------------------------
137 class wxMutexInternal
142 m_mutex
= ::CreateMutex(NULL
, FALSE
, NULL
);
145 wxLogSysError(_("Can not create mutex"));
149 ~wxMutexInternal() { if ( m_mutex
) CloseHandle(m_mutex
); }
157 m_internal
= new wxMutexInternal
;
166 wxLogDebug(_T("Warning: freeing a locked mutex (%d locks)."), m_locked
);
172 wxMutexError
wxMutex::Lock()
176 ret
= WaitForSingleObject(m_internal
->m_mutex
, INFINITE
);
187 wxLogSysError(_("Couldn't acquire a mutex lock"));
188 return wxMUTEX_MISC_ERROR
;
192 wxFAIL_MSG(wxT("impossible return value in wxMutex::Lock"));
196 return wxMUTEX_NO_ERROR
;
199 wxMutexError
wxMutex::TryLock()
203 ret
= WaitForSingleObject(m_internal
->m_mutex
, 0);
204 if (ret
== WAIT_TIMEOUT
|| ret
== WAIT_ABANDONED
)
208 return wxMUTEX_NO_ERROR
;
211 wxMutexError
wxMutex::Unlock()
216 BOOL ret
= ReleaseMutex(m_internal
->m_mutex
);
219 wxLogSysError(_("Couldn't release a mutex"));
220 return wxMUTEX_MISC_ERROR
;
223 return wxMUTEX_NO_ERROR
;
226 // ----------------------------------------------------------------------------
227 // wxCondition implementation
228 // ----------------------------------------------------------------------------
230 class wxConditionInternal
233 wxConditionInternal()
235 m_hEvent
= ::CreateEvent(
236 NULL
, // default secutiry
237 FALSE
, // not manual reset
238 FALSE
, // nonsignaled initially
239 NULL
// nameless event
243 wxLogSysError(_("Can not create event object."));
246 // nobody waits for us yet
250 bool Wait(DWORD timeout
)
252 // as m_nWaiters variable is accessed from multiple waiting threads
253 // (and possibly from the broadcasting thread), we need to change its
255 ::InterlockedIncrement(&m_nWaiters
);
257 // FIXME this should be MsgWaitForMultipleObjects() as we want to keep
258 // processing Windows messages while waiting (or don't we?)
259 DWORD rc
= ::WaitForSingleObject(m_hEvent
, timeout
);
261 ::InterlockedDecrement(&m_nWaiters
);
263 return rc
!= WAIT_TIMEOUT
;
268 // set the event to signaled: if a thread is already waiting on it, it
269 // will be woken up, otherwise the event will remain in the signaled
270 // state until someone waits on it. In any case, the system will return
271 // it to a non signalled state afterwards. If multiple threads are
272 // waiting, only one will be woken up.
273 if ( !::SetEvent(m_hEvent
) )
275 wxLogLastError(wxT("SetEvent"));
281 // we need to save the original value as m_nWaiters is goign to be
282 // decreased by the signalled thread resulting in the loop being
283 // executed less times than needed
284 LONG nWaiters
= m_nWaiters
;
286 // this works because all these threads are already waiting and so each
287 // SetEvent() inside Signal() is really a PulseEvent() because the
288 // event state is immediately returned to non-signaled
289 for ( LONG n
= 0; n
< nWaiters
; n
++ )
295 ~wxConditionInternal()
299 if ( !::CloseHandle(m_hEvent
) )
301 wxLogLastError(wxT("CloseHandle(event)"));
307 // the Win32 synchronization object corresponding to this event
310 // number of threads waiting for this condition
314 wxCondition::wxCondition()
316 m_internal
= new wxConditionInternal
;
319 wxCondition::~wxCondition()
324 void wxCondition::Wait()
326 (void)m_internal
->Wait(INFINITE
);
329 bool wxCondition::Wait(unsigned long sec
,
332 return m_internal
->Wait(sec
*1000 + nsec
/1000000);
335 void wxCondition::Signal()
337 m_internal
->Signal();
340 void wxCondition::Broadcast()
342 m_internal
->Broadcast();
345 // ----------------------------------------------------------------------------
346 // wxCriticalSection implementation
347 // ----------------------------------------------------------------------------
349 wxCriticalSection::wxCriticalSection()
352 // Done this way to stop warnings during compilation about statement
353 // always being false
354 int csSize
= sizeof(CRITICAL_SECTION
);
355 int bSize
= sizeof(m_buffer
);
356 wxASSERT_MSG( csSize
<= bSize
,
357 _T("must increase buffer size in wx/thread.h") );
360 ::InitializeCriticalSection((CRITICAL_SECTION
*)m_buffer
);
363 wxCriticalSection::~wxCriticalSection()
365 ::DeleteCriticalSection((CRITICAL_SECTION
*)m_buffer
);
368 void wxCriticalSection::Enter()
370 ::EnterCriticalSection((CRITICAL_SECTION
*)m_buffer
);
373 void wxCriticalSection::Leave()
375 ::LeaveCriticalSection((CRITICAL_SECTION
*)m_buffer
);
378 // ----------------------------------------------------------------------------
379 // wxThread implementation
380 // ----------------------------------------------------------------------------
382 // wxThreadInternal class
383 // ----------------------
385 class wxThreadInternal
392 m_priority
= WXTHREAD_DEFAULT_PRIORITY
;
404 if ( !::CloseHandle(m_hThread
) )
406 wxLogLastError(wxT("CloseHandle(thread)"));
413 // create a new (suspended) thread (for the given thread object)
414 bool Create(wxThread
*thread
, unsigned int stackSize
);
416 // suspend/resume/terminate
419 void Cancel() { m_state
= STATE_CANCELED
; }
422 void SetState(wxThreadState state
) { m_state
= state
; }
423 wxThreadState
GetState() const { return m_state
; }
426 void SetPriority(unsigned int priority
);
427 unsigned int GetPriority() const { return m_priority
; }
429 // thread handle and id
430 HANDLE
GetHandle() const { return m_hThread
; }
431 DWORD
GetId() const { return m_tid
; }
434 static THREAD_RETVAL THREAD_CALLCONV
WinThreadStart(void *thread
);
437 HANDLE m_hThread
; // handle of the thread
438 wxThreadState m_state
; // state, see wxThreadState enum
439 unsigned int m_priority
; // thread priority in "wx" units
440 DWORD m_tid
; // thread id
443 THREAD_RETVAL THREAD_CALLCONV
wxThreadInternal::WinThreadStart(void *param
)
448 // first of all, check whether we hadn't been cancelled already and don't
449 // start the user code at all then
450 wxThread
*thread
= (wxThread
*)param
;
451 if ( thread
->m_internal
->GetState() == STATE_EXITED
)
453 rc
= (THREAD_RETVAL
)-1;
456 else // do run thread
458 // store the thread object in the TLS
459 if ( !::TlsSetValue(gs_tlsThisThread
, thread
) )
461 wxLogSysError(_("Can not start thread: error writing TLS."));
466 rc
= (THREAD_RETVAL
)thread
->Entry();
468 // enter m_critsect before changing the thread state
469 thread
->m_critsect
.Enter();
470 wasCancelled
= thread
->m_internal
->GetState() == STATE_CANCELED
;
471 thread
->m_internal
->SetState(STATE_EXITED
);
472 thread
->m_critsect
.Leave();
477 // if the thread was cancelled (from Delete()), then its handle is still
479 if ( thread
->IsDetached() && !wasCancelled
)
484 //else: the joinable threads handle will be closed when Wait() is done
489 void wxThreadInternal::SetPriority(unsigned int priority
)
491 m_priority
= priority
;
493 // translate wxWindows priority to the Windows one
495 if (m_priority
<= 20)
496 win_priority
= THREAD_PRIORITY_LOWEST
;
497 else if (m_priority
<= 40)
498 win_priority
= THREAD_PRIORITY_BELOW_NORMAL
;
499 else if (m_priority
<= 60)
500 win_priority
= THREAD_PRIORITY_NORMAL
;
501 else if (m_priority
<= 80)
502 win_priority
= THREAD_PRIORITY_ABOVE_NORMAL
;
503 else if (m_priority
<= 100)
504 win_priority
= THREAD_PRIORITY_HIGHEST
;
507 wxFAIL_MSG(wxT("invalid value of thread priority parameter"));
508 win_priority
= THREAD_PRIORITY_NORMAL
;
511 if ( !::SetThreadPriority(m_hThread
, win_priority
) )
513 wxLogSysError(_("Can't set thread priority"));
517 bool wxThreadInternal::Create(wxThread
*thread
, unsigned int stackSize
)
519 // for compilers which have it, we should use C RTL function for thread
520 // creation instead of Win32 API one because otherwise we will have memory
521 // leaks if the thread uses C RTL (and most threads do)
522 #ifdef wxUSE_BEGIN_THREAD
524 // Watcom is reported to not like 0 stack size (which means "use default"
525 // for the other compilers and is also the default value for stackSize)
529 #endif // __WATCOMC__
531 m_hThread
= (HANDLE
)_beginthreadex
533 NULL
, // default security
535 wxThreadInternal::WinThreadStart
, // entry point
538 (unsigned int *)&m_tid
540 #else // compiler doesn't have _beginthreadex
541 m_hThread
= ::CreateThread
543 NULL
, // default security
544 stackSize
, // stack size
545 wxThreadInternal::WinThreadStart
, // thread entry point
546 (LPVOID
)thread
, // parameter
547 CREATE_SUSPENDED
, // flags
548 &m_tid
// [out] thread id
550 #endif // _beginthreadex/CreateThread
552 if ( m_hThread
== NULL
)
554 wxLogSysError(_("Can't create thread"));
559 if ( m_priority
!= WXTHREAD_DEFAULT_PRIORITY
)
561 SetPriority(m_priority
);
567 bool wxThreadInternal::Suspend()
569 DWORD nSuspendCount
= ::SuspendThread(m_hThread
);
570 if ( nSuspendCount
== (DWORD
)-1 )
572 wxLogSysError(_("Can not suspend thread %x"), m_hThread
);
577 m_state
= STATE_PAUSED
;
582 bool wxThreadInternal::Resume()
584 DWORD nSuspendCount
= ::ResumeThread(m_hThread
);
585 if ( nSuspendCount
== (DWORD
)-1 )
587 wxLogSysError(_("Can not resume thread %x"), m_hThread
);
592 // don't change the state from STATE_EXITED because it's special and means
593 // we are going to terminate without running any user code - if we did it,
594 // the codei n Delete() wouldn't work
595 if ( m_state
!= STATE_EXITED
)
597 m_state
= STATE_RUNNING
;
606 wxThread
*wxThread::This()
608 wxThread
*thread
= (wxThread
*)::TlsGetValue(gs_tlsThisThread
);
610 // be careful, 0 may be a valid return value as well
611 if ( !thread
&& (::GetLastError() != NO_ERROR
) )
613 wxLogSysError(_("Couldn't get the current thread pointer"));
621 bool wxThread::IsMain()
623 return ::GetCurrentThreadId() == gs_idMainThread
;
630 void wxThread::Yield()
632 // 0 argument to Sleep() is special and means to just give away the rest of
637 void wxThread::Sleep(unsigned long milliseconds
)
639 ::Sleep(milliseconds
);
642 int wxThread::GetCPUCount()
647 return si
.dwNumberOfProcessors
;
650 unsigned long wxThread::GetCurrentId()
652 return (unsigned long)::GetCurrentThreadId();
655 bool wxThread::SetConcurrency(size_t level
)
657 wxASSERT_MSG( IsMain(), _T("should only be called from the main thread") );
659 // ok only for the default one
663 // get system affinity mask first
664 HANDLE hProcess
= ::GetCurrentProcess();
665 DWORD dwProcMask
, dwSysMask
;
666 if ( ::GetProcessAffinityMask(hProcess
, &dwProcMask
, &dwSysMask
) == 0 )
668 wxLogLastError(_T("GetProcessAffinityMask"));
673 // how many CPUs have we got?
674 if ( dwSysMask
== 1 )
676 // don't bother with all this complicated stuff - on a single
677 // processor system it doesn't make much sense anyhow
681 // calculate the process mask: it's a bit vector with one bit per
682 // processor; we want to schedule the process to run on first level
687 if ( dwSysMask
& bit
)
689 // ok, we can set this bit
692 // another process added
704 // could we set all bits?
707 wxLogDebug(_T("bad level %u in wxThread::SetConcurrency()"), level
);
712 // set it: we can't link to SetProcessAffinityMask() because it doesn't
713 // exist in Win9x, use RT binding instead
715 typedef BOOL (*SETPROCESSAFFINITYMASK
)(HANDLE
, DWORD
);
717 // can use static var because we're always in the main thread here
718 static SETPROCESSAFFINITYMASK pfnSetProcessAffinityMask
= NULL
;
720 if ( !pfnSetProcessAffinityMask
)
722 HMODULE hModKernel
= ::LoadLibrary(_T("kernel32"));
725 pfnSetProcessAffinityMask
= (SETPROCESSAFFINITYMASK
)
726 ::GetProcAddress(hModKernel
, "SetProcessAffinityMask");
729 // we've discovered a MT version of Win9x!
730 wxASSERT_MSG( pfnSetProcessAffinityMask
,
731 _T("this system has several CPUs but no SetProcessAffinityMask function?") );
734 if ( !pfnSetProcessAffinityMask
)
736 // msg given above - do it only once
740 if ( pfnSetProcessAffinityMask(hProcess
, dwProcMask
) == 0 )
742 wxLogLastError(_T("SetProcessAffinityMask"));
753 wxThread::wxThread(wxThreadKind kind
)
755 m_internal
= new wxThreadInternal();
757 m_isDetached
= kind
== wxTHREAD_DETACHED
;
760 wxThread::~wxThread()
765 // create/start thread
766 // -------------------
768 wxThreadError
wxThread::Create(unsigned int stackSize
)
770 wxCriticalSectionLocker
lock(m_critsect
);
772 if ( !m_internal
->Create(this, stackSize
) )
773 return wxTHREAD_NO_RESOURCE
;
775 return wxTHREAD_NO_ERROR
;
778 wxThreadError
wxThread::Run()
780 wxCriticalSectionLocker
lock(m_critsect
);
782 if ( m_internal
->GetState() != STATE_NEW
)
784 // actually, it may be almost any state at all, not only STATE_RUNNING
785 return wxTHREAD_RUNNING
;
788 // the thread has just been created and is still suspended - let it run
792 // suspend/resume thread
793 // ---------------------
795 wxThreadError
wxThread::Pause()
797 wxCriticalSectionLocker
lock(m_critsect
);
799 return m_internal
->Suspend() ? wxTHREAD_NO_ERROR
: wxTHREAD_MISC_ERROR
;
802 wxThreadError
wxThread::Resume()
804 wxCriticalSectionLocker
lock(m_critsect
);
806 return m_internal
->Resume() ? wxTHREAD_NO_ERROR
: wxTHREAD_MISC_ERROR
;
812 wxThread::ExitCode
wxThread::Wait()
814 // although under Windows we can wait for any thread, it's an error to
815 // wait for a detached one in wxWin API
816 wxCHECK_MSG( !IsDetached(), (ExitCode
)-1,
817 _T("can't wait for detached thread") );
819 ExitCode rc
= (ExitCode
)-1;
828 wxThreadError
wxThread::Delete(ExitCode
*pRc
)
832 // Delete() is always safe to call, so consider all possible states
834 // we might need to resume the thread, but we might also not need to cancel
835 // it if it doesn't run yet
836 bool shouldResume
= FALSE
,
840 // check if the thread already started to run
842 wxCriticalSectionLocker
lock(m_critsect
);
844 if ( m_internal
->GetState() == STATE_NEW
)
846 // WinThreadStart() will see it and terminate immediately, no need
847 // to cancel the thread - but we still need to resume it to let it
849 m_internal
->SetState(STATE_EXITED
);
851 Resume(); // it knows about STATE_EXITED special case
853 shouldCancel
= FALSE
;
856 // shouldResume is correctly set to FALSE here
860 shouldResume
= IsPaused();
864 // resume the thread if it is paused
868 HANDLE hThread
= m_internal
->GetHandle();
870 // does is still run?
871 if ( isRunning
|| IsRunning() )
875 // set flag for wxIsWaitingForThread()
876 gs_waitingForThread
= TRUE
;
883 // ask the thread to terminate
886 wxCriticalSectionLocker
lock(m_critsect
);
888 m_internal
->Cancel();
892 // we can't just wait for the thread to terminate because it might be
893 // calling some GUI functions and so it will never terminate before we
894 // process the Windows messages that result from these functions
898 result
= ::MsgWaitForMultipleObjects
900 1, // number of objects to wait for
901 &hThread
, // the objects
902 FALSE
, // don't wait for all objects
903 INFINITE
, // no timeout
904 QS_ALLEVENTS
// return as soon as there are any events
911 wxLogSysError(_("Can not wait for thread termination"));
913 return wxTHREAD_KILLED
;
916 // thread we're waiting for terminated
919 case WAIT_OBJECT_0
+ 1:
920 // new message arrived, process it
921 if ( !wxTheApp
->DoMessage() )
923 // WM_QUIT received: kill the thread
926 return wxTHREAD_KILLED
;
931 // give the thread we're waiting for chance to exit
932 // from the GUI call it might have been in
933 if ( (gs_nWaitingForGui
> 0) && wxGuiOwnedByMainThread() )
942 wxFAIL_MSG(wxT("unexpected result of MsgWaitForMultipleObject"));
944 } while ( result
!= WAIT_OBJECT_0
);
946 // simply wait for the thread to terminate
948 // OTOH, even console apps create windows (in wxExecute, for WinSock
949 // &c), so may be use MsgWaitForMultipleObject() too here?
950 if ( WaitForSingleObject(hThread
, INFINITE
) != WAIT_OBJECT_0
)
952 wxFAIL_MSG(wxT("unexpected result of WaitForSingleObject"));
954 #endif // wxUSE_GUI/!wxUSE_GUI
958 gs_waitingForThread
= FALSE
;
966 if ( !::GetExitCodeThread(hThread
, (LPDWORD
)&rc
) )
968 wxLogLastError(wxT("GetExitCodeThread"));
975 // if the thread exits normally, this is done in WinThreadStart, but in
976 // this case it would have been too early because
977 // MsgWaitForMultipleObject() would fail if the thread handle was
978 // closed while we were waiting on it, so we must do it here
982 wxASSERT_MSG( (DWORD
)rc
!= STILL_ACTIVE
,
983 wxT("thread must be already terminated.") );
988 return rc
== (ExitCode
)-1 ? wxTHREAD_MISC_ERROR
: wxTHREAD_NO_ERROR
;
991 wxThreadError
wxThread::Kill()
994 return wxTHREAD_NOT_RUNNING
;
996 if ( !::TerminateThread(m_internal
->GetHandle(), (DWORD
)-1) )
998 wxLogSysError(_("Couldn't terminate thread"));
1000 return wxTHREAD_MISC_ERROR
;
1010 return wxTHREAD_NO_ERROR
;
1013 void wxThread::Exit(ExitCode status
)
1022 #ifdef wxUSE_BEGIN_THREAD
1023 _endthreadex((unsigned)status
);
1025 ::ExitThread((DWORD
)status
);
1026 #endif // VC++/!VC++
1028 wxFAIL_MSG(wxT("Couldn't return from ExitThread()!"));
1034 void wxThread::SetPriority(unsigned int prio
)
1036 wxCriticalSectionLocker
lock(m_critsect
);
1038 m_internal
->SetPriority(prio
);
1041 unsigned int wxThread::GetPriority() const
1043 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
1045 return m_internal
->GetPriority();
1048 unsigned long wxThread::GetId() const
1050 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
1052 return (unsigned long)m_internal
->GetId();
1055 bool wxThread::IsRunning() const
1057 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
1059 return m_internal
->GetState() == STATE_RUNNING
;
1062 bool wxThread::IsAlive() const
1064 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
1066 return (m_internal
->GetState() == STATE_RUNNING
) ||
1067 (m_internal
->GetState() == STATE_PAUSED
);
1070 bool wxThread::IsPaused() const
1072 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
1074 return m_internal
->GetState() == STATE_PAUSED
;
1077 bool wxThread::TestDestroy()
1079 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
1081 return m_internal
->GetState() == STATE_CANCELED
;
1084 // ----------------------------------------------------------------------------
1085 // Automatic initialization for thread module
1086 // ----------------------------------------------------------------------------
1088 class wxThreadModule
: public wxModule
1091 virtual bool OnInit();
1092 virtual void OnExit();
1095 DECLARE_DYNAMIC_CLASS(wxThreadModule
)
1098 IMPLEMENT_DYNAMIC_CLASS(wxThreadModule
, wxModule
)
1100 bool wxThreadModule::OnInit()
1102 // allocate TLS index for storing the pointer to the current thread
1103 gs_tlsThisThread
= ::TlsAlloc();
1104 if ( gs_tlsThisThread
== 0xFFFFFFFF )
1106 // in normal circumstances it will only happen if all other
1107 // TLS_MINIMUM_AVAILABLE (>= 64) indices are already taken - in other
1108 // words, this should never happen
1109 wxLogSysError(_("Thread module initialization failed: impossible to allocate index in thread local storage"));
1114 // main thread doesn't have associated wxThread object, so store 0 in the
1116 if ( !::TlsSetValue(gs_tlsThisThread
, (LPVOID
)0) )
1118 ::TlsFree(gs_tlsThisThread
);
1119 gs_tlsThisThread
= 0xFFFFFFFF;
1121 wxLogSysError(_("Thread module initialization failed: can not store value in thread local storage"));
1126 gs_critsectWaitingForGui
= new wxCriticalSection();
1128 gs_critsectGui
= new wxCriticalSection();
1129 gs_critsectGui
->Enter();
1131 // no error return for GetCurrentThreadId()
1132 gs_idMainThread
= ::GetCurrentThreadId();
1137 void wxThreadModule::OnExit()
1139 if ( !::TlsFree(gs_tlsThisThread
) )
1141 wxLogLastError(wxT("TlsFree failed."));
1144 if ( gs_critsectGui
)
1146 gs_critsectGui
->Leave();
1147 delete gs_critsectGui
;
1148 gs_critsectGui
= NULL
;
1151 delete gs_critsectWaitingForGui
;
1152 gs_critsectWaitingForGui
= NULL
;
1155 // ----------------------------------------------------------------------------
1156 // under Windows, these functions are implemented using a critical section and
1157 // not a mutex, so the names are a bit confusing
1158 // ----------------------------------------------------------------------------
1160 void WXDLLEXPORT
wxMutexGuiEnter()
1162 // this would dead lock everything...
1163 wxASSERT_MSG( !wxThread::IsMain(),
1164 wxT("main thread doesn't want to block in wxMutexGuiEnter()!") );
1166 // the order in which we enter the critical sections here is crucial!!
1168 // set the flag telling to the main thread that we want to do some GUI
1170 wxCriticalSectionLocker
enter(*gs_critsectWaitingForGui
);
1172 gs_nWaitingForGui
++;
1175 wxWakeUpMainThread();
1177 // now we may block here because the main thread will soon let us in
1178 // (during the next iteration of OnIdle())
1179 gs_critsectGui
->Enter();
1182 void WXDLLEXPORT
wxMutexGuiLeave()
1184 wxCriticalSectionLocker
enter(*gs_critsectWaitingForGui
);
1186 if ( wxThread::IsMain() )
1188 gs_bGuiOwnedByMainThread
= FALSE
;
1192 // decrement the number of threads waiting for GUI access now
1193 wxASSERT_MSG( gs_nWaitingForGui
> 0,
1194 wxT("calling wxMutexGuiLeave() without entering it first?") );
1196 gs_nWaitingForGui
--;
1198 wxWakeUpMainThread();
1201 gs_critsectGui
->Leave();
1204 void WXDLLEXPORT
wxMutexGuiLeaveOrEnter()
1206 wxASSERT_MSG( wxThread::IsMain(),
1207 wxT("only main thread may call wxMutexGuiLeaveOrEnter()!") );
1209 wxCriticalSectionLocker
enter(*gs_critsectWaitingForGui
);
1211 if ( gs_nWaitingForGui
== 0 )
1213 // no threads are waiting for GUI - so we may acquire the lock without
1214 // any danger (but only if we don't already have it)
1215 if ( !wxGuiOwnedByMainThread() )
1217 gs_critsectGui
->Enter();
1219 gs_bGuiOwnedByMainThread
= TRUE
;
1221 //else: already have it, nothing to do
1225 // some threads are waiting, release the GUI lock if we have it
1226 if ( wxGuiOwnedByMainThread() )
1230 //else: some other worker thread is doing GUI
1234 bool WXDLLEXPORT
wxGuiOwnedByMainThread()
1236 return gs_bGuiOwnedByMainThread
;
1239 // wake up the main thread if it's in ::GetMessage()
1240 void WXDLLEXPORT
wxWakeUpMainThread()
1242 // sending any message would do - hopefully WM_NULL is harmless enough
1243 if ( !::PostThreadMessage(gs_idMainThread
, WM_NULL
, 0, 0) )
1245 // should never happen
1246 wxLogLastError(wxT("PostThreadMessage(WM_NULL)"));
1250 bool WXDLLEXPORT
wxIsWaitingForThread()
1252 return gs_waitingForThread
;
1255 #endif // wxUSE_THREADS