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"
39 // must have this symbol defined to get _beginthread/_endthread declarations
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 // the possible states of the thread ("=>" shows all possible transitions from
56 STATE_NEW
, // didn't start execution yet (=> RUNNING)
57 STATE_RUNNING
, // thread is running (=> PAUSED, CANCELED)
58 STATE_PAUSED
, // thread is temporarily suspended (=> RUNNING)
59 STATE_CANCELED
, // thread should terminate a.s.a.p. (=> EXITED)
60 STATE_EXITED
// thread is terminating
63 // ----------------------------------------------------------------------------
64 // this module globals
65 // ----------------------------------------------------------------------------
67 // TLS index of the slot where we store the pointer to the current thread
68 static DWORD gs_tlsThisThread
= 0xFFFFFFFF;
70 // id of the main thread - the one which can call GUI functions without first
71 // calling wxMutexGuiEnter()
72 static DWORD gs_idMainThread
= 0;
74 // if it's FALSE, some secondary thread is holding the GUI lock
75 static bool gs_bGuiOwnedByMainThread
= TRUE
;
77 // critical section which controls access to all GUI functions: any secondary
78 // thread (i.e. except the main one) must enter this crit section before doing
80 static wxCriticalSection
*gs_critsectGui
= NULL
;
82 // critical section which protects gs_nWaitingForGui variable
83 static wxCriticalSection
*gs_critsectWaitingForGui
= NULL
;
85 // number of threads waiting for GUI in wxMutexGuiEnter()
86 static size_t gs_nWaitingForGui
= 0;
88 // are we waiting for a thread termination?
89 static bool gs_waitingForThread
= FALSE
;
91 // ============================================================================
92 // Windows implementation of thread classes
93 // ============================================================================
95 // ----------------------------------------------------------------------------
96 // wxMutex implementation
97 // ----------------------------------------------------------------------------
107 m_internal
= new wxMutexInternal
;
108 m_internal
->p_mutex
= CreateMutex(NULL
, FALSE
, NULL
);
109 if ( !m_internal
->p_mutex
)
111 wxLogSysError(_("Can not create mutex."));
120 wxLogDebug(wxT("Warning: freeing a locked mutex (%d locks)."), m_locked
);
121 CloseHandle(m_internal
->p_mutex
);
124 wxMutexError
wxMutex::Lock()
128 ret
= WaitForSingleObject(m_internal
->p_mutex
, INFINITE
);
139 wxLogSysError(_("Couldn't acquire a mutex lock"));
140 return wxMUTEX_MISC_ERROR
;
144 wxFAIL_MSG(wxT("impossible return value in wxMutex::Lock"));
148 return wxMUTEX_NO_ERROR
;
151 wxMutexError
wxMutex::TryLock()
155 ret
= WaitForSingleObject(m_internal
->p_mutex
, 0);
156 if (ret
== WAIT_TIMEOUT
|| ret
== WAIT_ABANDONED
)
160 return wxMUTEX_NO_ERROR
;
163 wxMutexError
wxMutex::Unlock()
168 BOOL ret
= ReleaseMutex(m_internal
->p_mutex
);
171 wxLogSysError(_("Couldn't release a mutex"));
172 return wxMUTEX_MISC_ERROR
;
175 return wxMUTEX_NO_ERROR
;
178 // ----------------------------------------------------------------------------
179 // wxCondition implementation
180 // ----------------------------------------------------------------------------
182 class wxConditionInternal
185 wxConditionInternal()
187 event
= ::CreateEvent(
188 NULL
, // default secutiry
189 FALSE
, // not manual reset
190 FALSE
, // nonsignaled initially
191 NULL
// nameless event
195 wxLogSysError(_("Can not create event object."));
200 bool Wait(DWORD timeout
)
204 // FIXME this should be MsgWaitForMultipleObjects() as well probably
205 DWORD rc
= ::WaitForSingleObject(event
, timeout
);
209 return rc
!= WAIT_TIMEOUT
;
212 ~wxConditionInternal()
216 if ( !::CloseHandle(event
) )
218 wxLogLastError("CloseHandle(event)");
227 wxCondition::wxCondition()
229 m_internal
= new wxConditionInternal
;
232 wxCondition::~wxCondition()
237 void wxCondition::Wait()
239 (void)m_internal
->Wait(INFINITE
);
242 bool wxCondition::Wait(unsigned long sec
,
245 return m_internal
->Wait(sec
*1000 + nsec
/1000000);
248 void wxCondition::Signal()
250 // set the event to signaled: if a thread is already waiting on it, it will
251 // be woken up, otherwise the event will remain in the signaled state until
252 // someone waits on it. In any case, the system will return it to a non
253 // signalled state afterwards. If multiple threads are waiting, only one
255 if ( !::SetEvent(m_internal
->event
) )
257 wxLogLastError("SetEvent");
261 void wxCondition::Broadcast()
263 // this works because all these threads are already waiting and so each
264 // SetEvent() inside Signal() is really a PulseEvent() because the event
265 // state is immediately returned to non-signaled
266 for ( int i
= 0; i
< m_internal
->waiters
; i
++ )
272 // ----------------------------------------------------------------------------
273 // wxCriticalSection implementation
274 // ----------------------------------------------------------------------------
276 wxCriticalSection::wxCriticalSection()
278 wxASSERT_MSG( sizeof(CRITICAL_SECTION
) <= sizeof(m_buffer
),
279 _T("must increase buffer size in wx/thread.h") );
281 ::InitializeCriticalSection((CRITICAL_SECTION
*)m_buffer
);
284 wxCriticalSection::~wxCriticalSection()
286 ::DeleteCriticalSection((CRITICAL_SECTION
*)m_buffer
);
289 void wxCriticalSection::Enter()
291 ::EnterCriticalSection((CRITICAL_SECTION
*)m_buffer
);
294 void wxCriticalSection::Leave()
296 ::LeaveCriticalSection((CRITICAL_SECTION
*)m_buffer
);
299 // ----------------------------------------------------------------------------
300 // wxThread implementation
301 // ----------------------------------------------------------------------------
303 // wxThreadInternal class
304 // ----------------------
306 class wxThreadInternal
313 m_priority
= WXTHREAD_DEFAULT_PRIORITY
;
325 if ( !::CloseHandle(m_hThread
) )
327 wxLogLastError("CloseHandle(thread)");
334 // create a new (suspended) thread (for the given thread object)
335 bool Create(wxThread
*thread
);
337 // suspend/resume/terminate
340 void Cancel() { m_state
= STATE_CANCELED
; }
343 void SetState(wxThreadState state
) { m_state
= state
; }
344 wxThreadState
GetState() const { return m_state
; }
347 void SetPriority(unsigned int priority
);
348 unsigned int GetPriority() const { return m_priority
; }
350 // thread handle and id
351 HANDLE
GetHandle() const { return m_hThread
; }
352 DWORD
GetId() const { return m_tid
; }
355 static DWORD
WinThreadStart(wxThread
*thread
);
358 HANDLE m_hThread
; // handle of the thread
359 wxThreadState m_state
; // state, see wxThreadState enum
360 unsigned int m_priority
; // thread priority in "wx" units
361 DWORD m_tid
; // thread id
364 DWORD
wxThreadInternal::WinThreadStart(wxThread
*thread
)
366 // first of all, check whether we hadn't been cancelled already
367 if ( thread
->m_internal
->GetState() == STATE_EXITED
)
372 // store the thread object in the TLS
373 if ( !::TlsSetValue(gs_tlsThisThread
, thread
) )
375 wxLogSysError(_("Can not start thread: error writing TLS."));
380 DWORD rc
= (DWORD
)thread
->Entry();
382 // enter m_critsect before changing the thread state
383 thread
->m_critsect
.Enter();
384 bool wasCancelled
= thread
->m_internal
->GetState() == STATE_CANCELED
;
385 thread
->m_internal
->SetState(STATE_EXITED
);
386 thread
->m_critsect
.Leave();
390 // if the thread was cancelled (from Delete()), then it the handle is still
392 if ( thread
->IsDetached() && !wasCancelled
)
397 //else: the joinable threads handle will be closed when Wait() is done
402 void wxThreadInternal::SetPriority(unsigned int priority
)
404 m_priority
= priority
;
406 // translate wxWindows priority to the Windows one
408 if (m_priority
<= 20)
409 win_priority
= THREAD_PRIORITY_LOWEST
;
410 else if (m_priority
<= 40)
411 win_priority
= THREAD_PRIORITY_BELOW_NORMAL
;
412 else if (m_priority
<= 60)
413 win_priority
= THREAD_PRIORITY_NORMAL
;
414 else if (m_priority
<= 80)
415 win_priority
= THREAD_PRIORITY_ABOVE_NORMAL
;
416 else if (m_priority
<= 100)
417 win_priority
= THREAD_PRIORITY_HIGHEST
;
420 wxFAIL_MSG(wxT("invalid value of thread priority parameter"));
421 win_priority
= THREAD_PRIORITY_NORMAL
;
424 if ( !::SetThreadPriority(m_hThread
, win_priority
) )
426 wxLogSysError(_("Can't set thread priority"));
430 bool wxThreadInternal::Create(wxThread
*thread
)
432 // for compilers which have it, we should use C RTL function for thread
433 // creation instead of Win32 API one because otherwise we will have memory
434 // leaks if the thread uses C RTL (and most threads do)
436 typedef unsigned (__stdcall
*RtlThreadStart
)(void *);
438 m_hThread
= (HANDLE
)_beginthreadex(NULL
, 0,
440 wxThreadInternal::WinThreadStart
,
441 thread
, CREATE_SUSPENDED
,
442 (unsigned int *)&m_tid
);
444 m_hThread
= ::CreateThread
446 NULL
, // default security
447 0, // default stack size
448 (LPTHREAD_START_ROUTINE
) // thread entry point
449 wxThreadInternal::WinThreadStart
, //
450 (LPVOID
)thread
, // parameter
451 CREATE_SUSPENDED
, // flags
452 &m_tid
// [out] thread id
456 if ( m_hThread
== NULL
)
458 wxLogSysError(_("Can't create thread"));
463 if ( m_priority
!= WXTHREAD_DEFAULT_PRIORITY
)
465 SetPriority(m_priority
);
471 bool wxThreadInternal::Suspend()
473 DWORD nSuspendCount
= ::SuspendThread(m_hThread
);
474 if ( nSuspendCount
== (DWORD
)-1 )
476 wxLogSysError(_("Can not suspend thread %x"), m_hThread
);
481 m_state
= STATE_PAUSED
;
486 bool wxThreadInternal::Resume()
488 DWORD nSuspendCount
= ::ResumeThread(m_hThread
);
489 if ( nSuspendCount
== (DWORD
)-1 )
491 wxLogSysError(_("Can not resume thread %x"), m_hThread
);
496 m_state
= STATE_RUNNING
;
504 wxThread
*wxThread::This()
506 wxThread
*thread
= (wxThread
*)::TlsGetValue(gs_tlsThisThread
);
508 // be careful, 0 may be a valid return value as well
509 if ( !thread
&& (::GetLastError() != NO_ERROR
) )
511 wxLogSysError(_("Couldn't get the current thread pointer"));
519 bool wxThread::IsMain()
521 return ::GetCurrentThreadId() == gs_idMainThread
;
524 void wxThread::Yield()
526 // 0 argument to Sleep() is special and means to just give away the rest of
531 void wxThread::Sleep(unsigned long milliseconds
)
533 ::Sleep(milliseconds
);
536 int wxThread::GetCPUCount()
541 return si
.dwNumberOfProcessors
;
544 bool wxThread::SetConcurrency(size_t level
)
546 wxASSERT_MSG( IsMain(), _T("should only be called from the main thread") );
548 // ok only for the default one
552 // get system affinity mask first
553 HANDLE hProcess
= ::GetCurrentProcess();
554 DWORD dwProcMask
, dwSysMask
;
555 if ( ::GetProcessAffinityMask(hProcess
, &dwProcMask
, &dwSysMask
) == 0 )
557 wxLogLastError(_T("GetProcessAffinityMask"));
562 // how many CPUs have we got?
563 if ( dwSysMask
== 1 )
565 // don't bother with all this complicated stuff - on a single
566 // processor system it doesn't make much sense anyhow
570 // calculate the process mask: it's a bit vector with one bit per
571 // processor; we want to schedule the process to run on first level
576 if ( dwSysMask
& bit
)
578 // ok, we can set this bit
581 // another process added
593 // could we set all bits?
596 wxLogDebug(_T("bad level %u in wxThread::SetConcurrency()"), level
);
601 // set it: we can't link to SetProcessAffinityMask() because it doesn't
602 // exist in Win9x, use RT binding instead
604 typedef BOOL (*SETPROCESSAFFINITYMASK
)(HANDLE
, DWORD
);
606 // can use static var because we're always in the main thread here
607 static SETPROCESSAFFINITYMASK pfnSetProcessAffinityMask
= NULL
;
609 if ( !pfnSetProcessAffinityMask
)
611 HMODULE hModKernel
= ::LoadLibrary(_T("kernel32"));
614 pfnSetProcessAffinityMask
= (SETPROCESSAFFINITYMASK
)
615 ::GetProcAddress(hModKernel
, _T("SetProcessAffinityMask"));
618 // we've discovered a MT version of Win9x!
619 wxASSERT_MSG( pfnSetProcessAffinityMask
,
620 _T("this system has several CPUs but no "
621 "SetProcessAffinityMask function?") );
624 if ( !pfnSetProcessAffinityMask
)
626 // msg given above - do it only once
630 if ( pfnSetProcessAffinityMask(hProcess
, dwProcMask
) == 0 )
632 wxLogLastError(_T("SetProcessAffinityMask"));
643 wxThread::wxThread(wxThreadKind kind
)
645 m_internal
= new wxThreadInternal();
647 m_isDetached
= kind
== wxTHREAD_DETACHED
;
650 wxThread::~wxThread()
655 // create/start thread
656 // -------------------
658 wxThreadError
wxThread::Create()
660 wxCriticalSectionLocker
lock(m_critsect
);
662 if ( !m_internal
->Create(this) )
663 return wxTHREAD_NO_RESOURCE
;
665 return wxTHREAD_NO_ERROR
;
668 wxThreadError
wxThread::Run()
670 wxCriticalSectionLocker
lock(m_critsect
);
672 if ( m_internal
->GetState() != STATE_NEW
)
674 // actually, it may be almost any state at all, not only STATE_RUNNING
675 return wxTHREAD_RUNNING
;
678 // the thread has just been created and is still suspended - let it run
682 // suspend/resume thread
683 // ---------------------
685 wxThreadError
wxThread::Pause()
687 wxCriticalSectionLocker
lock(m_critsect
);
689 return m_internal
->Suspend() ? wxTHREAD_NO_ERROR
: wxTHREAD_MISC_ERROR
;
692 wxThreadError
wxThread::Resume()
694 wxCriticalSectionLocker
lock(m_critsect
);
696 return m_internal
->Resume() ? wxTHREAD_NO_ERROR
: wxTHREAD_MISC_ERROR
;
702 wxThread::ExitCode
wxThread::Wait()
704 // although under Windows we can wait for any thread, it's an error to
705 // wait for a detached one in wxWin API
706 wxCHECK_MSG( !IsDetached(), (ExitCode
)-1,
707 _T("can't wait for detached thread") );
709 ExitCode rc
= (ExitCode
)-1;
718 wxThreadError
wxThread::Delete(ExitCode
*pRc
)
722 // Delete() is always safe to call, so consider all possible states
724 // has the thread started to run?
725 bool shouldResume
= FALSE
;
728 wxCriticalSectionLocker
lock(m_critsect
);
730 if ( m_internal
->GetState() == STATE_NEW
)
732 // WinThreadStart() will see it and terminate immediately
733 m_internal
->SetState(STATE_EXITED
);
739 // is the thread paused?
740 if ( shouldResume
|| IsPaused() )
743 HANDLE hThread
= m_internal
->GetHandle();
745 // does is still run?
750 // set flag for wxIsWaitingForThread()
751 gs_waitingForThread
= TRUE
;
758 // ask the thread to terminate
760 wxCriticalSectionLocker
lock(m_critsect
);
762 m_internal
->Cancel();
766 // we can't just wait for the thread to terminate because it might be
767 // calling some GUI functions and so it will never terminate before we
768 // process the Windows messages that result from these functions
772 result
= ::MsgWaitForMultipleObjects
774 1, // number of objects to wait for
775 &hThread
, // the objects
776 FALSE
, // don't wait for all objects
777 INFINITE
, // no timeout
778 QS_ALLEVENTS
// return as soon as there are any events
785 wxLogSysError(_("Can not wait for thread termination"));
787 return wxTHREAD_KILLED
;
790 // thread we're waiting for terminated
793 case WAIT_OBJECT_0
+ 1:
794 // new message arrived, process it
795 if ( !wxTheApp
->DoMessage() )
797 // WM_QUIT received: kill the thread
800 return wxTHREAD_KILLED
;
805 // give the thread we're waiting for chance to exit
806 // from the GUI call it might have been in
807 if ( (gs_nWaitingForGui
> 0) && wxGuiOwnedByMainThread() )
816 wxFAIL_MSG(wxT("unexpected result of MsgWaitForMultipleObject"));
818 } while ( result
!= WAIT_OBJECT_0
);
820 // simply wait for the thread to terminate
822 // OTOH, even console apps create windows (in wxExecute, for WinSock
823 // &c), so may be use MsgWaitForMultipleObject() too here?
824 if ( WaitForSingleObject(hThread
, INFINITE
) != WAIT_OBJECT_0
)
826 wxFAIL_MSG(wxT("unexpected result of WaitForSingleObject"));
828 #endif // wxUSE_GUI/!wxUSE_GUI
832 gs_waitingForThread
= FALSE
;
840 if ( !::GetExitCodeThread(hThread
, (LPDWORD
)&rc
) )
842 wxLogLastError("GetExitCodeThread");
849 // if the thread exits normally, this is done in WinThreadStart, but in
850 // this case it would have been too early because
851 // MsgWaitForMultipleObject() would fail if the therad handle was
852 // closed while we were waiting on it, so we must do it here
856 wxASSERT_MSG( (DWORD
)rc
!= STILL_ACTIVE
,
857 wxT("thread must be already terminated.") );
862 return rc
== (ExitCode
)-1 ? wxTHREAD_MISC_ERROR
: wxTHREAD_NO_ERROR
;
865 wxThreadError
wxThread::Kill()
868 return wxTHREAD_NOT_RUNNING
;
870 if ( !::TerminateThread(m_internal
->GetHandle(), (DWORD
)-1) )
872 wxLogSysError(_("Couldn't terminate thread"));
874 return wxTHREAD_MISC_ERROR
;
884 return wxTHREAD_NO_ERROR
;
887 void wxThread::Exit(ExitCode status
)
897 _endthreadex((unsigned)status
);
899 ::ExitThread((DWORD
)status
);
902 wxFAIL_MSG(wxT("Couldn't return from ExitThread()!"));
908 void wxThread::SetPriority(unsigned int prio
)
910 wxCriticalSectionLocker
lock(m_critsect
);
912 m_internal
->SetPriority(prio
);
915 unsigned int wxThread::GetPriority() const
917 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
919 return m_internal
->GetPriority();
922 unsigned long wxThread::GetId() const
924 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
926 return (unsigned long)m_internal
->GetId();
929 bool wxThread::IsRunning() const
931 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
933 return m_internal
->GetState() == STATE_RUNNING
;
936 bool wxThread::IsAlive() const
938 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
940 return (m_internal
->GetState() == STATE_RUNNING
) ||
941 (m_internal
->GetState() == STATE_PAUSED
);
944 bool wxThread::IsPaused() const
946 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
948 return m_internal
->GetState() == STATE_PAUSED
;
951 bool wxThread::TestDestroy()
953 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
955 return m_internal
->GetState() == STATE_CANCELED
;
958 // ----------------------------------------------------------------------------
959 // Automatic initialization for thread module
960 // ----------------------------------------------------------------------------
962 class wxThreadModule
: public wxModule
965 virtual bool OnInit();
966 virtual void OnExit();
969 DECLARE_DYNAMIC_CLASS(wxThreadModule
)
972 IMPLEMENT_DYNAMIC_CLASS(wxThreadModule
, wxModule
)
974 bool wxThreadModule::OnInit()
976 // allocate TLS index for storing the pointer to the current thread
977 gs_tlsThisThread
= ::TlsAlloc();
978 if ( gs_tlsThisThread
== 0xFFFFFFFF )
980 // in normal circumstances it will only happen if all other
981 // TLS_MINIMUM_AVAILABLE (>= 64) indices are already taken - in other
982 // words, this should never happen
983 wxLogSysError(_("Thread module initialization failed: "
984 "impossible to allocate index in thread "
990 // main thread doesn't have associated wxThread object, so store 0 in the
992 if ( !::TlsSetValue(gs_tlsThisThread
, (LPVOID
)0) )
994 ::TlsFree(gs_tlsThisThread
);
995 gs_tlsThisThread
= 0xFFFFFFFF;
997 wxLogSysError(_("Thread module initialization failed: "
998 "can not store value in thread local storage"));
1003 gs_critsectWaitingForGui
= new wxCriticalSection();
1005 gs_critsectGui
= new wxCriticalSection();
1006 gs_critsectGui
->Enter();
1008 // no error return for GetCurrentThreadId()
1009 gs_idMainThread
= ::GetCurrentThreadId();
1014 void wxThreadModule::OnExit()
1016 if ( !::TlsFree(gs_tlsThisThread
) )
1018 wxLogLastError("TlsFree failed.");
1021 if ( gs_critsectGui
)
1023 gs_critsectGui
->Leave();
1024 delete gs_critsectGui
;
1025 gs_critsectGui
= NULL
;
1028 delete gs_critsectWaitingForGui
;
1029 gs_critsectWaitingForGui
= NULL
;
1032 // ----------------------------------------------------------------------------
1033 // under Windows, these functions are implemented using a critical section and
1034 // not a mutex, so the names are a bit confusing
1035 // ----------------------------------------------------------------------------
1037 void WXDLLEXPORT
wxMutexGuiEnter()
1039 // this would dead lock everything...
1040 wxASSERT_MSG( !wxThread::IsMain(),
1041 wxT("main thread doesn't want to block in wxMutexGuiEnter()!") );
1043 // the order in which we enter the critical sections here is crucial!!
1045 // set the flag telling to the main thread that we want to do some GUI
1047 wxCriticalSectionLocker
enter(*gs_critsectWaitingForGui
);
1049 gs_nWaitingForGui
++;
1052 wxWakeUpMainThread();
1054 // now we may block here because the main thread will soon let us in
1055 // (during the next iteration of OnIdle())
1056 gs_critsectGui
->Enter();
1059 void WXDLLEXPORT
wxMutexGuiLeave()
1061 wxCriticalSectionLocker
enter(*gs_critsectWaitingForGui
);
1063 if ( wxThread::IsMain() )
1065 gs_bGuiOwnedByMainThread
= FALSE
;
1069 // decrement the number of waiters now
1070 wxASSERT_MSG( gs_nWaitingForGui
> 0,
1071 wxT("calling wxMutexGuiLeave() without entering it first?") );
1073 gs_nWaitingForGui
--;
1075 wxWakeUpMainThread();
1078 gs_critsectGui
->Leave();
1081 void WXDLLEXPORT
wxMutexGuiLeaveOrEnter()
1083 wxASSERT_MSG( wxThread::IsMain(),
1084 wxT("only main thread may call wxMutexGuiLeaveOrEnter()!") );
1086 wxCriticalSectionLocker
enter(*gs_critsectWaitingForGui
);
1088 if ( gs_nWaitingForGui
== 0 )
1090 // no threads are waiting for GUI - so we may acquire the lock without
1091 // any danger (but only if we don't already have it)
1092 if ( !wxGuiOwnedByMainThread() )
1094 gs_critsectGui
->Enter();
1096 gs_bGuiOwnedByMainThread
= TRUE
;
1098 //else: already have it, nothing to do
1102 // some threads are waiting, release the GUI lock if we have it
1103 if ( wxGuiOwnedByMainThread() )
1107 //else: some other worker thread is doing GUI
1111 bool WXDLLEXPORT
wxGuiOwnedByMainThread()
1113 return gs_bGuiOwnedByMainThread
;
1116 // wake up the main thread if it's in ::GetMessage()
1117 void WXDLLEXPORT
wxWakeUpMainThread()
1119 // sending any message would do - hopefully WM_NULL is harmless enough
1120 if ( !::PostThreadMessage(gs_idMainThread
, WM_NULL
, 0, 0) )
1122 // should never happen
1123 wxLogLastError("PostThreadMessage(WM_NULL)");
1127 bool WXDLLEXPORT
wxIsWaitingForThread()
1129 return gs_waitingForThread
;
1132 #endif // wxUSE_THREADS