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(__VISUALC__) || \
49 (defined(__BORLANDC__) && (__BORLANDC__ >= 0x500)) || \
50 (defined(__GNUG__) && defined(__MSVCRT__))
52 #if defined(__BORLANDC__) && !defined(__MT__)
53 // I can't set -tWM in the IDE (anyone?) so have to do this
57 #if defined(__BORLANDC__) && !defined(__MFC_COMPAT__)
58 // Needed to know about _beginthreadex etc..
59 #define __MFC_COMPAT__
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 // the possible states of the thread ("=>" shows all possible transitions from
73 STATE_NEW
, // didn't start execution yet (=> RUNNING)
74 STATE_RUNNING
, // thread is running (=> PAUSED, CANCELED)
75 STATE_PAUSED
, // thread is temporarily suspended (=> RUNNING)
76 STATE_CANCELED
, // thread should terminate a.s.a.p. (=> EXITED)
77 STATE_EXITED
// thread is terminating
80 // ----------------------------------------------------------------------------
81 // this module globals
82 // ----------------------------------------------------------------------------
84 // TLS index of the slot where we store the pointer to the current thread
85 static DWORD gs_tlsThisThread
= 0xFFFFFFFF;
87 // id of the main thread - the one which can call GUI functions without first
88 // calling wxMutexGuiEnter()
89 static DWORD gs_idMainThread
= 0;
91 // if it's FALSE, some secondary thread is holding the GUI lock
92 static bool gs_bGuiOwnedByMainThread
= TRUE
;
94 // critical section which controls access to all GUI functions: any secondary
95 // thread (i.e. except the main one) must enter this crit section before doing
97 static wxCriticalSection
*gs_critsectGui
= NULL
;
99 // critical section which protects gs_nWaitingForGui variable
100 static wxCriticalSection
*gs_critsectWaitingForGui
= NULL
;
102 // number of threads waiting for GUI in wxMutexGuiEnter()
103 static size_t gs_nWaitingForGui
= 0;
105 // are we waiting for a thread termination?
106 static bool gs_waitingForThread
= FALSE
;
108 // ============================================================================
109 // Windows implementation of thread classes
110 // ============================================================================
112 // ----------------------------------------------------------------------------
113 // wxMutex implementation
114 // ----------------------------------------------------------------------------
116 class wxMutexInternal
121 m_mutex
= ::CreateMutex(NULL
, FALSE
, NULL
);
124 wxLogSysError(_("Can not create mutex"));
128 ~wxMutexInternal() { if ( m_mutex
) CloseHandle(m_mutex
); }
136 m_internal
= new wxMutexInternal
;
145 wxLogDebug(_T("Warning: freeing a locked mutex (%d locks)."), m_locked
);
151 wxMutexError
wxMutex::Lock()
155 ret
= WaitForSingleObject(m_internal
->m_mutex
, INFINITE
);
166 wxLogSysError(_("Couldn't acquire a mutex lock"));
167 return wxMUTEX_MISC_ERROR
;
171 wxFAIL_MSG(wxT("impossible return value in wxMutex::Lock"));
175 return wxMUTEX_NO_ERROR
;
178 wxMutexError
wxMutex::TryLock()
182 ret
= WaitForSingleObject(m_internal
->m_mutex
, 0);
183 if (ret
== WAIT_TIMEOUT
|| ret
== WAIT_ABANDONED
)
187 return wxMUTEX_NO_ERROR
;
190 wxMutexError
wxMutex::Unlock()
195 BOOL ret
= ReleaseMutex(m_internal
->m_mutex
);
198 wxLogSysError(_("Couldn't release a mutex"));
199 return wxMUTEX_MISC_ERROR
;
202 return wxMUTEX_NO_ERROR
;
205 // ----------------------------------------------------------------------------
206 // wxCondition implementation
207 // ----------------------------------------------------------------------------
209 class wxConditionInternal
212 wxConditionInternal()
214 m_hEvent
= ::CreateEvent(
215 NULL
, // default secutiry
216 FALSE
, // not manual reset
217 FALSE
, // nonsignaled initially
218 NULL
// nameless event
222 wxLogSysError(_("Can not create event object."));
225 // nobody waits for us yet
229 bool Wait(DWORD timeout
)
231 // as m_nWaiters variable is accessed from multiple waiting threads
232 // (and possibly from the broadcasting thread), we need to change its
234 ::InterlockedIncrement(&m_nWaiters
);
236 // FIXME this should be MsgWaitForMultipleObjects() as we want to keep
237 // processing Windows messages while waiting (or don't we?)
238 DWORD rc
= ::WaitForSingleObject(m_hEvent
, timeout
);
240 ::InterlockedDecrement(&m_nWaiters
);
242 return rc
!= WAIT_TIMEOUT
;
247 // set the event to signaled: if a thread is already waiting on it, it
248 // will be woken up, otherwise the event will remain in the signaled
249 // state until someone waits on it. In any case, the system will return
250 // it to a non signalled state afterwards. If multiple threads are
251 // waiting, only one will be woken up.
252 if ( !::SetEvent(m_hEvent
) )
254 wxLogLastError(wxT("SetEvent"));
260 // we need to save the original value as m_nWaiters is goign to be
261 // decreased by the signalled thread resulting in the loop being
262 // executed less times than needed
263 LONG nWaiters
= m_nWaiters
;
265 // this works because all these threads are already waiting and so each
266 // SetEvent() inside Signal() is really a PulseEvent() because the
267 // event state is immediately returned to non-signaled
268 for ( LONG n
= 0; n
< nWaiters
; n
++ )
274 ~wxConditionInternal()
278 if ( !::CloseHandle(m_hEvent
) )
280 wxLogLastError(wxT("CloseHandle(event)"));
286 // the Win32 synchronization object corresponding to this event
289 // number of threads waiting for this condition
293 wxCondition::wxCondition()
295 m_internal
= new wxConditionInternal
;
298 wxCondition::~wxCondition()
303 void wxCondition::Wait()
305 (void)m_internal
->Wait(INFINITE
);
308 bool wxCondition::Wait(unsigned long sec
,
311 return m_internal
->Wait(sec
*1000 + nsec
/1000000);
314 void wxCondition::Signal()
316 m_internal
->Signal();
319 void wxCondition::Broadcast()
321 m_internal
->Broadcast();
324 // ----------------------------------------------------------------------------
325 // wxCriticalSection implementation
326 // ----------------------------------------------------------------------------
328 wxCriticalSection::wxCriticalSection()
330 wxASSERT_MSG( sizeof(CRITICAL_SECTION
) <= sizeof(m_buffer
),
331 _T("must increase buffer size in wx/thread.h") );
333 ::InitializeCriticalSection((CRITICAL_SECTION
*)m_buffer
);
336 wxCriticalSection::~wxCriticalSection()
338 ::DeleteCriticalSection((CRITICAL_SECTION
*)m_buffer
);
341 void wxCriticalSection::Enter()
343 ::EnterCriticalSection((CRITICAL_SECTION
*)m_buffer
);
346 void wxCriticalSection::Leave()
348 ::LeaveCriticalSection((CRITICAL_SECTION
*)m_buffer
);
351 // ----------------------------------------------------------------------------
352 // wxThread implementation
353 // ----------------------------------------------------------------------------
355 // wxThreadInternal class
356 // ----------------------
358 class wxThreadInternal
365 m_priority
= WXTHREAD_DEFAULT_PRIORITY
;
377 if ( !::CloseHandle(m_hThread
) )
379 wxLogLastError(wxT("CloseHandle(thread)"));
386 // create a new (suspended) thread (for the given thread object)
387 bool Create(wxThread
*thread
);
389 // suspend/resume/terminate
392 void Cancel() { m_state
= STATE_CANCELED
; }
395 void SetState(wxThreadState state
) { m_state
= state
; }
396 wxThreadState
GetState() const { return m_state
; }
399 void SetPriority(unsigned int priority
);
400 unsigned int GetPriority() const { return m_priority
; }
402 // thread handle and id
403 HANDLE
GetHandle() const { return m_hThread
; }
404 DWORD
GetId() const { return m_tid
; }
407 static DWORD
WinThreadStart(wxThread
*thread
);
410 HANDLE m_hThread
; // handle of the thread
411 wxThreadState m_state
; // state, see wxThreadState enum
412 unsigned int m_priority
; // thread priority in "wx" units
413 DWORD m_tid
; // thread id
416 DWORD
wxThreadInternal::WinThreadStart(wxThread
*thread
)
421 // first of all, check whether we hadn't been cancelled already and don't
422 // start the user code at all then
423 if ( thread
->m_internal
->GetState() == STATE_EXITED
)
428 else // do run thread
430 // store the thread object in the TLS
431 if ( !::TlsSetValue(gs_tlsThisThread
, thread
) )
433 wxLogSysError(_("Can not start thread: error writing TLS."));
438 rc
= (DWORD
)thread
->Entry();
440 // enter m_critsect before changing the thread state
441 thread
->m_critsect
.Enter();
442 wasCancelled
= thread
->m_internal
->GetState() == STATE_CANCELED
;
443 thread
->m_internal
->SetState(STATE_EXITED
);
444 thread
->m_critsect
.Leave();
449 // if the thread was cancelled (from Delete()), then its handle is still
451 if ( thread
->IsDetached() && !wasCancelled
)
456 //else: the joinable threads handle will be closed when Wait() is done
461 void wxThreadInternal::SetPriority(unsigned int priority
)
463 m_priority
= priority
;
465 // translate wxWindows priority to the Windows one
467 if (m_priority
<= 20)
468 win_priority
= THREAD_PRIORITY_LOWEST
;
469 else if (m_priority
<= 40)
470 win_priority
= THREAD_PRIORITY_BELOW_NORMAL
;
471 else if (m_priority
<= 60)
472 win_priority
= THREAD_PRIORITY_NORMAL
;
473 else if (m_priority
<= 80)
474 win_priority
= THREAD_PRIORITY_ABOVE_NORMAL
;
475 else if (m_priority
<= 100)
476 win_priority
= THREAD_PRIORITY_HIGHEST
;
479 wxFAIL_MSG(wxT("invalid value of thread priority parameter"));
480 win_priority
= THREAD_PRIORITY_NORMAL
;
483 if ( !::SetThreadPriority(m_hThread
, win_priority
) )
485 wxLogSysError(_("Can't set thread priority"));
489 bool wxThreadInternal::Create(wxThread
*thread
)
491 // for compilers which have it, we should use C RTL function for thread
492 // creation instead of Win32 API one because otherwise we will have memory
493 // leaks if the thread uses C RTL (and most threads do)
494 #if defined(__VISUALC__) || \
495 (defined(__BORLANDC__) && (__BORLANDC__ >= 0x500)) || \
496 (defined(__GNUG__) && defined(__MSVCRT__))
497 typedef unsigned (__stdcall
*RtlThreadStart
)(void *);
499 m_hThread
= (HANDLE
)_beginthreadex(NULL
, 0,
501 wxThreadInternal::WinThreadStart
,
502 thread
, CREATE_SUSPENDED
,
503 (unsigned int *)&m_tid
);
504 #else // compiler doesn't have _beginthreadex
505 m_hThread
= ::CreateThread
507 NULL
, // default security
508 0, // default stack size
509 (LPTHREAD_START_ROUTINE
) // thread entry point
510 wxThreadInternal::WinThreadStart
, //
511 (LPVOID
)thread
, // parameter
512 CREATE_SUSPENDED
, // flags
513 &m_tid
// [out] thread id
515 #endif // _beginthreadex/CreateThread
517 if ( m_hThread
== NULL
)
519 wxLogSysError(_("Can't create thread"));
524 if ( m_priority
!= WXTHREAD_DEFAULT_PRIORITY
)
526 SetPriority(m_priority
);
532 bool wxThreadInternal::Suspend()
534 DWORD nSuspendCount
= ::SuspendThread(m_hThread
);
535 if ( nSuspendCount
== (DWORD
)-1 )
537 wxLogSysError(_("Can not suspend thread %x"), m_hThread
);
542 m_state
= STATE_PAUSED
;
547 bool wxThreadInternal::Resume()
549 DWORD nSuspendCount
= ::ResumeThread(m_hThread
);
550 if ( nSuspendCount
== (DWORD
)-1 )
552 wxLogSysError(_("Can not resume thread %x"), m_hThread
);
557 // don't change the state from STATE_EXITED because it's special and means
558 // we are going to terminate without running any user code - if we did it,
559 // the codei n Delete() wouldn't work
560 if ( m_state
!= STATE_EXITED
)
562 m_state
= STATE_RUNNING
;
571 wxThread
*wxThread::This()
573 wxThread
*thread
= (wxThread
*)::TlsGetValue(gs_tlsThisThread
);
575 // be careful, 0 may be a valid return value as well
576 if ( !thread
&& (::GetLastError() != NO_ERROR
) )
578 wxLogSysError(_("Couldn't get the current thread pointer"));
586 bool wxThread::IsMain()
588 return ::GetCurrentThreadId() == gs_idMainThread
;
595 void wxThread::Yield()
597 // 0 argument to Sleep() is special and means to just give away the rest of
602 void wxThread::Sleep(unsigned long milliseconds
)
604 ::Sleep(milliseconds
);
607 int wxThread::GetCPUCount()
612 return si
.dwNumberOfProcessors
;
615 bool wxThread::SetConcurrency(size_t level
)
617 wxASSERT_MSG( IsMain(), _T("should only be called from the main thread") );
619 // ok only for the default one
623 // get system affinity mask first
624 HANDLE hProcess
= ::GetCurrentProcess();
625 DWORD dwProcMask
, dwSysMask
;
626 if ( ::GetProcessAffinityMask(hProcess
, &dwProcMask
, &dwSysMask
) == 0 )
628 wxLogLastError(_T("GetProcessAffinityMask"));
633 // how many CPUs have we got?
634 if ( dwSysMask
== 1 )
636 // don't bother with all this complicated stuff - on a single
637 // processor system it doesn't make much sense anyhow
641 // calculate the process mask: it's a bit vector with one bit per
642 // processor; we want to schedule the process to run on first level
647 if ( dwSysMask
& bit
)
649 // ok, we can set this bit
652 // another process added
664 // could we set all bits?
667 wxLogDebug(_T("bad level %u in wxThread::SetConcurrency()"), level
);
672 // set it: we can't link to SetProcessAffinityMask() because it doesn't
673 // exist in Win9x, use RT binding instead
675 typedef BOOL (*SETPROCESSAFFINITYMASK
)(HANDLE
, DWORD
);
677 // can use static var because we're always in the main thread here
678 static SETPROCESSAFFINITYMASK pfnSetProcessAffinityMask
= NULL
;
680 if ( !pfnSetProcessAffinityMask
)
682 HMODULE hModKernel
= ::LoadLibrary(_T("kernel32"));
685 pfnSetProcessAffinityMask
= (SETPROCESSAFFINITYMASK
)
686 ::GetProcAddress(hModKernel
, "SetProcessAffinityMask");
689 // we've discovered a MT version of Win9x!
690 wxASSERT_MSG( pfnSetProcessAffinityMask
,
691 _T("this system has several CPUs but no SetProcessAffinityMask function?") );
694 if ( !pfnSetProcessAffinityMask
)
696 // msg given above - do it only once
700 if ( pfnSetProcessAffinityMask(hProcess
, dwProcMask
) == 0 )
702 wxLogLastError(_T("SetProcessAffinityMask"));
713 wxThread::wxThread(wxThreadKind kind
)
715 m_internal
= new wxThreadInternal();
717 m_isDetached
= kind
== wxTHREAD_DETACHED
;
720 wxThread::~wxThread()
725 // create/start thread
726 // -------------------
728 wxThreadError
wxThread::Create()
730 wxCriticalSectionLocker
lock(m_critsect
);
732 if ( !m_internal
->Create(this) )
733 return wxTHREAD_NO_RESOURCE
;
735 return wxTHREAD_NO_ERROR
;
738 wxThreadError
wxThread::Run()
740 wxCriticalSectionLocker
lock(m_critsect
);
742 if ( m_internal
->GetState() != STATE_NEW
)
744 // actually, it may be almost any state at all, not only STATE_RUNNING
745 return wxTHREAD_RUNNING
;
748 // the thread has just been created and is still suspended - let it run
752 // suspend/resume thread
753 // ---------------------
755 wxThreadError
wxThread::Pause()
757 wxCriticalSectionLocker
lock(m_critsect
);
759 return m_internal
->Suspend() ? wxTHREAD_NO_ERROR
: wxTHREAD_MISC_ERROR
;
762 wxThreadError
wxThread::Resume()
764 wxCriticalSectionLocker
lock(m_critsect
);
766 return m_internal
->Resume() ? wxTHREAD_NO_ERROR
: wxTHREAD_MISC_ERROR
;
772 wxThread::ExitCode
wxThread::Wait()
774 // although under Windows we can wait for any thread, it's an error to
775 // wait for a detached one in wxWin API
776 wxCHECK_MSG( !IsDetached(), (ExitCode
)-1,
777 _T("can't wait for detached thread") );
779 ExitCode rc
= (ExitCode
)-1;
788 wxThreadError
wxThread::Delete(ExitCode
*pRc
)
792 // Delete() is always safe to call, so consider all possible states
794 // we might need to resume the thread, but we might also not need to cancel
795 // it if it doesn't run yet
796 bool shouldResume
= FALSE
,
800 // check if the thread already started to run
802 wxCriticalSectionLocker
lock(m_critsect
);
804 if ( m_internal
->GetState() == STATE_NEW
)
806 // WinThreadStart() will see it and terminate immediately, no need
807 // to cancel the thread - but we still need to resume it to let it
809 m_internal
->SetState(STATE_EXITED
);
811 Resume(); // it knows about STATE_EXITED special case
813 shouldCancel
= FALSE
;
816 // shouldResume is correctly set to FALSE here
820 shouldResume
= IsPaused();
824 // resume the thread if it is paused
828 HANDLE hThread
= m_internal
->GetHandle();
830 // does is still run?
831 if ( isRunning
|| IsRunning() )
835 // set flag for wxIsWaitingForThread()
836 gs_waitingForThread
= TRUE
;
843 // ask the thread to terminate
846 wxCriticalSectionLocker
lock(m_critsect
);
848 m_internal
->Cancel();
852 // we can't just wait for the thread to terminate because it might be
853 // calling some GUI functions and so it will never terminate before we
854 // process the Windows messages that result from these functions
858 result
= ::MsgWaitForMultipleObjects
860 1, // number of objects to wait for
861 &hThread
, // the objects
862 FALSE
, // don't wait for all objects
863 INFINITE
, // no timeout
864 QS_ALLEVENTS
// return as soon as there are any events
871 wxLogSysError(_("Can not wait for thread termination"));
873 return wxTHREAD_KILLED
;
876 // thread we're waiting for terminated
879 case WAIT_OBJECT_0
+ 1:
880 // new message arrived, process it
881 if ( !wxTheApp
->DoMessage() )
883 // WM_QUIT received: kill the thread
886 return wxTHREAD_KILLED
;
891 // give the thread we're waiting for chance to exit
892 // from the GUI call it might have been in
893 if ( (gs_nWaitingForGui
> 0) && wxGuiOwnedByMainThread() )
902 wxFAIL_MSG(wxT("unexpected result of MsgWaitForMultipleObject"));
904 } while ( result
!= WAIT_OBJECT_0
);
906 // simply wait for the thread to terminate
908 // OTOH, even console apps create windows (in wxExecute, for WinSock
909 // &c), so may be use MsgWaitForMultipleObject() too here?
910 if ( WaitForSingleObject(hThread
, INFINITE
) != WAIT_OBJECT_0
)
912 wxFAIL_MSG(wxT("unexpected result of WaitForSingleObject"));
914 #endif // wxUSE_GUI/!wxUSE_GUI
918 gs_waitingForThread
= FALSE
;
926 if ( !::GetExitCodeThread(hThread
, (LPDWORD
)&rc
) )
928 wxLogLastError(wxT("GetExitCodeThread"));
935 // if the thread exits normally, this is done in WinThreadStart, but in
936 // this case it would have been too early because
937 // MsgWaitForMultipleObject() would fail if the thread handle was
938 // closed while we were waiting on it, so we must do it here
942 wxASSERT_MSG( (DWORD
)rc
!= STILL_ACTIVE
,
943 wxT("thread must be already terminated.") );
948 return rc
== (ExitCode
)-1 ? wxTHREAD_MISC_ERROR
: wxTHREAD_NO_ERROR
;
951 wxThreadError
wxThread::Kill()
954 return wxTHREAD_NOT_RUNNING
;
956 if ( !::TerminateThread(m_internal
->GetHandle(), (DWORD
)-1) )
958 wxLogSysError(_("Couldn't terminate thread"));
960 return wxTHREAD_MISC_ERROR
;
970 return wxTHREAD_NO_ERROR
;
973 void wxThread::Exit(ExitCode status
)
982 #if defined(__VISUALC__) || \
983 (defined(__BORLANDC__) && (__BORLANDC__ >= 0x500)) || \
984 (defined(__GNUG__) && defined(__MSVCRT__))
985 _endthreadex((unsigned)status
);
987 ::ExitThread((DWORD
)status
);
990 wxFAIL_MSG(wxT("Couldn't return from ExitThread()!"));
996 void wxThread::SetPriority(unsigned int prio
)
998 wxCriticalSectionLocker
lock(m_critsect
);
1000 m_internal
->SetPriority(prio
);
1003 unsigned int wxThread::GetPriority() const
1005 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
1007 return m_internal
->GetPriority();
1010 unsigned long wxThread::GetId() const
1012 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
1014 return (unsigned long)m_internal
->GetId();
1017 bool wxThread::IsRunning() const
1019 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
1021 return m_internal
->GetState() == STATE_RUNNING
;
1024 bool wxThread::IsAlive() const
1026 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
1028 return (m_internal
->GetState() == STATE_RUNNING
) ||
1029 (m_internal
->GetState() == STATE_PAUSED
);
1032 bool wxThread::IsPaused() const
1034 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
1036 return m_internal
->GetState() == STATE_PAUSED
;
1039 bool wxThread::TestDestroy()
1041 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
1043 return m_internal
->GetState() == STATE_CANCELED
;
1046 // ----------------------------------------------------------------------------
1047 // Automatic initialization for thread module
1048 // ----------------------------------------------------------------------------
1050 class wxThreadModule
: public wxModule
1053 virtual bool OnInit();
1054 virtual void OnExit();
1057 DECLARE_DYNAMIC_CLASS(wxThreadModule
)
1060 IMPLEMENT_DYNAMIC_CLASS(wxThreadModule
, wxModule
)
1062 bool wxThreadModule::OnInit()
1064 // allocate TLS index for storing the pointer to the current thread
1065 gs_tlsThisThread
= ::TlsAlloc();
1066 if ( gs_tlsThisThread
== 0xFFFFFFFF )
1068 // in normal circumstances it will only happen if all other
1069 // TLS_MINIMUM_AVAILABLE (>= 64) indices are already taken - in other
1070 // words, this should never happen
1071 wxLogSysError(_("Thread module initialization failed: impossible to allocate index in thread local storage"));
1076 // main thread doesn't have associated wxThread object, so store 0 in the
1078 if ( !::TlsSetValue(gs_tlsThisThread
, (LPVOID
)0) )
1080 ::TlsFree(gs_tlsThisThread
);
1081 gs_tlsThisThread
= 0xFFFFFFFF;
1083 wxLogSysError(_("Thread module initialization failed: can not store value in thread local storage"));
1088 gs_critsectWaitingForGui
= new wxCriticalSection();
1090 gs_critsectGui
= new wxCriticalSection();
1091 gs_critsectGui
->Enter();
1093 // no error return for GetCurrentThreadId()
1094 gs_idMainThread
= ::GetCurrentThreadId();
1099 void wxThreadModule::OnExit()
1101 if ( !::TlsFree(gs_tlsThisThread
) )
1103 wxLogLastError(wxT("TlsFree failed."));
1106 if ( gs_critsectGui
)
1108 gs_critsectGui
->Leave();
1109 delete gs_critsectGui
;
1110 gs_critsectGui
= NULL
;
1113 delete gs_critsectWaitingForGui
;
1114 gs_critsectWaitingForGui
= NULL
;
1117 // ----------------------------------------------------------------------------
1118 // under Windows, these functions are implemented using a critical section and
1119 // not a mutex, so the names are a bit confusing
1120 // ----------------------------------------------------------------------------
1122 void WXDLLEXPORT
wxMutexGuiEnter()
1124 // this would dead lock everything...
1125 wxASSERT_MSG( !wxThread::IsMain(),
1126 wxT("main thread doesn't want to block in wxMutexGuiEnter()!") );
1128 // the order in which we enter the critical sections here is crucial!!
1130 // set the flag telling to the main thread that we want to do some GUI
1132 wxCriticalSectionLocker
enter(*gs_critsectWaitingForGui
);
1134 gs_nWaitingForGui
++;
1137 wxWakeUpMainThread();
1139 // now we may block here because the main thread will soon let us in
1140 // (during the next iteration of OnIdle())
1141 gs_critsectGui
->Enter();
1144 void WXDLLEXPORT
wxMutexGuiLeave()
1146 wxCriticalSectionLocker
enter(*gs_critsectWaitingForGui
);
1148 if ( wxThread::IsMain() )
1150 gs_bGuiOwnedByMainThread
= FALSE
;
1154 // decrement the number of threads waiting for GUI access now
1155 wxASSERT_MSG( gs_nWaitingForGui
> 0,
1156 wxT("calling wxMutexGuiLeave() without entering it first?") );
1158 gs_nWaitingForGui
--;
1160 wxWakeUpMainThread();
1163 gs_critsectGui
->Leave();
1166 void WXDLLEXPORT
wxMutexGuiLeaveOrEnter()
1168 wxASSERT_MSG( wxThread::IsMain(),
1169 wxT("only main thread may call wxMutexGuiLeaveOrEnter()!") );
1171 wxCriticalSectionLocker
enter(*gs_critsectWaitingForGui
);
1173 if ( gs_nWaitingForGui
== 0 )
1175 // no threads are waiting for GUI - so we may acquire the lock without
1176 // any danger (but only if we don't already have it)
1177 if ( !wxGuiOwnedByMainThread() )
1179 gs_critsectGui
->Enter();
1181 gs_bGuiOwnedByMainThread
= TRUE
;
1183 //else: already have it, nothing to do
1187 // some threads are waiting, release the GUI lock if we have it
1188 if ( wxGuiOwnedByMainThread() )
1192 //else: some other worker thread is doing GUI
1196 bool WXDLLEXPORT
wxGuiOwnedByMainThread()
1198 return gs_bGuiOwnedByMainThread
;
1201 // wake up the main thread if it's in ::GetMessage()
1202 void WXDLLEXPORT
wxWakeUpMainThread()
1204 // sending any message would do - hopefully WM_NULL is harmless enough
1205 if ( !::PostThreadMessage(gs_idMainThread
, WM_NULL
, 0, 0) )
1207 // should never happen
1208 wxLogLastError(wxT("PostThreadMessage(WM_NULL)"));
1212 bool WXDLLEXPORT
wxIsWaitingForThread()
1214 return gs_waitingForThread
;
1217 #endif // wxUSE_THREADS