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 event
= ::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."));
227 bool Wait(DWORD timeout
)
231 // FIXME this should be MsgWaitForMultipleObjects() as well probably
232 DWORD rc
= ::WaitForSingleObject(event
, timeout
);
236 return rc
!= WAIT_TIMEOUT
;
239 ~wxConditionInternal()
243 if ( !::CloseHandle(event
) )
245 wxLogLastError("CloseHandle(event)");
254 wxCondition::wxCondition()
256 m_internal
= new wxConditionInternal
;
259 wxCondition::~wxCondition()
264 void wxCondition::Wait()
266 (void)m_internal
->Wait(INFINITE
);
269 bool wxCondition::Wait(unsigned long sec
,
272 return m_internal
->Wait(sec
*1000 + nsec
/1000000);
275 void wxCondition::Signal()
277 // set the event to signaled: if a thread is already waiting on it, it will
278 // be woken up, otherwise the event will remain in the signaled state until
279 // someone waits on it. In any case, the system will return it to a non
280 // signalled state afterwards. If multiple threads are waiting, only one
282 if ( !::SetEvent(m_internal
->event
) )
284 wxLogLastError("SetEvent");
288 void wxCondition::Broadcast()
290 // this works because all these threads are already waiting and so each
291 // SetEvent() inside Signal() is really a PulseEvent() because the event
292 // state is immediately returned to non-signaled
293 for ( int i
= 0; i
< m_internal
->waiters
; i
++ )
299 // ----------------------------------------------------------------------------
300 // wxCriticalSection implementation
301 // ----------------------------------------------------------------------------
303 wxCriticalSection::wxCriticalSection()
305 wxASSERT_MSG( sizeof(CRITICAL_SECTION
) <= sizeof(m_buffer
),
306 _T("must increase buffer size in wx/thread.h") );
308 ::InitializeCriticalSection((CRITICAL_SECTION
*)m_buffer
);
311 wxCriticalSection::~wxCriticalSection()
313 ::DeleteCriticalSection((CRITICAL_SECTION
*)m_buffer
);
316 void wxCriticalSection::Enter()
318 ::EnterCriticalSection((CRITICAL_SECTION
*)m_buffer
);
321 void wxCriticalSection::Leave()
323 ::LeaveCriticalSection((CRITICAL_SECTION
*)m_buffer
);
326 // ----------------------------------------------------------------------------
327 // wxThread implementation
328 // ----------------------------------------------------------------------------
330 // wxThreadInternal class
331 // ----------------------
333 class wxThreadInternal
340 m_priority
= WXTHREAD_DEFAULT_PRIORITY
;
352 if ( !::CloseHandle(m_hThread
) )
354 wxLogLastError("CloseHandle(thread)");
361 // create a new (suspended) thread (for the given thread object)
362 bool Create(wxThread
*thread
);
364 // suspend/resume/terminate
367 void Cancel() { m_state
= STATE_CANCELED
; }
370 void SetState(wxThreadState state
) { m_state
= state
; }
371 wxThreadState
GetState() const { return m_state
; }
374 void SetPriority(unsigned int priority
);
375 unsigned int GetPriority() const { return m_priority
; }
377 // thread handle and id
378 HANDLE
GetHandle() const { return m_hThread
; }
379 DWORD
GetId() const { return m_tid
; }
382 static DWORD
WinThreadStart(wxThread
*thread
);
385 HANDLE m_hThread
; // handle of the thread
386 wxThreadState m_state
; // state, see wxThreadState enum
387 unsigned int m_priority
; // thread priority in "wx" units
388 DWORD m_tid
; // thread id
391 DWORD
wxThreadInternal::WinThreadStart(wxThread
*thread
)
393 // first of all, check whether we hadn't been cancelled already
394 if ( thread
->m_internal
->GetState() == STATE_EXITED
)
399 // store the thread object in the TLS
400 if ( !::TlsSetValue(gs_tlsThisThread
, thread
) )
402 wxLogSysError(_("Can not start thread: error writing TLS."));
407 DWORD rc
= (DWORD
)thread
->Entry();
409 // enter m_critsect before changing the thread state
410 thread
->m_critsect
.Enter();
411 bool wasCancelled
= thread
->m_internal
->GetState() == STATE_CANCELED
;
412 thread
->m_internal
->SetState(STATE_EXITED
);
413 thread
->m_critsect
.Leave();
417 // if the thread was cancelled (from Delete()), then it the handle is still
419 if ( thread
->IsDetached() && !wasCancelled
)
424 //else: the joinable threads handle will be closed when Wait() is done
429 void wxThreadInternal::SetPriority(unsigned int priority
)
431 m_priority
= priority
;
433 // translate wxWindows priority to the Windows one
435 if (m_priority
<= 20)
436 win_priority
= THREAD_PRIORITY_LOWEST
;
437 else if (m_priority
<= 40)
438 win_priority
= THREAD_PRIORITY_BELOW_NORMAL
;
439 else if (m_priority
<= 60)
440 win_priority
= THREAD_PRIORITY_NORMAL
;
441 else if (m_priority
<= 80)
442 win_priority
= THREAD_PRIORITY_ABOVE_NORMAL
;
443 else if (m_priority
<= 100)
444 win_priority
= THREAD_PRIORITY_HIGHEST
;
447 wxFAIL_MSG(wxT("invalid value of thread priority parameter"));
448 win_priority
= THREAD_PRIORITY_NORMAL
;
451 if ( !::SetThreadPriority(m_hThread
, win_priority
) )
453 wxLogSysError(_("Can't set thread priority"));
457 bool wxThreadInternal::Create(wxThread
*thread
)
459 // for compilers which have it, we should use C RTL function for thread
460 // creation instead of Win32 API one because otherwise we will have memory
461 // leaks if the thread uses C RTL (and most threads do)
462 #if defined(__VISUALC__) || \
463 (defined(__BORLANDC__) && (__BORLANDC__ >= 0x500)) || \
464 (defined(__GNUG__) && defined(__MSVCRT__))
465 typedef unsigned (__stdcall
*RtlThreadStart
)(void *);
467 m_hThread
= (HANDLE
)_beginthreadex(NULL
, 0,
469 wxThreadInternal::WinThreadStart
,
470 thread
, CREATE_SUSPENDED
,
471 (unsigned int *)&m_tid
);
472 #else // compiler doesn't have _beginthreadex
473 m_hThread
= ::CreateThread
475 NULL
, // default security
476 0, // default stack size
477 (LPTHREAD_START_ROUTINE
) // thread entry point
478 wxThreadInternal::WinThreadStart
, //
479 (LPVOID
)thread
, // parameter
480 CREATE_SUSPENDED
, // flags
481 &m_tid
// [out] thread id
483 #endif // _beginthreadex/CreateThread
485 if ( m_hThread
== NULL
)
487 wxLogSysError(_("Can't create thread"));
492 if ( m_priority
!= WXTHREAD_DEFAULT_PRIORITY
)
494 SetPriority(m_priority
);
500 bool wxThreadInternal::Suspend()
502 DWORD nSuspendCount
= ::SuspendThread(m_hThread
);
503 if ( nSuspendCount
== (DWORD
)-1 )
505 wxLogSysError(_("Can not suspend thread %x"), m_hThread
);
510 m_state
= STATE_PAUSED
;
515 bool wxThreadInternal::Resume()
517 DWORD nSuspendCount
= ::ResumeThread(m_hThread
);
518 if ( nSuspendCount
== (DWORD
)-1 )
520 wxLogSysError(_("Can not resume thread %x"), m_hThread
);
525 m_state
= STATE_RUNNING
;
533 wxThread
*wxThread::This()
535 wxThread
*thread
= (wxThread
*)::TlsGetValue(gs_tlsThisThread
);
537 // be careful, 0 may be a valid return value as well
538 if ( !thread
&& (::GetLastError() != NO_ERROR
) )
540 wxLogSysError(_("Couldn't get the current thread pointer"));
548 bool wxThread::IsMain()
550 return ::GetCurrentThreadId() == gs_idMainThread
;
557 void wxThread::Yield()
559 // 0 argument to Sleep() is special and means to just give away the rest of
564 void wxThread::Sleep(unsigned long milliseconds
)
566 ::Sleep(milliseconds
);
569 int wxThread::GetCPUCount()
574 return si
.dwNumberOfProcessors
;
577 bool wxThread::SetConcurrency(size_t level
)
579 wxASSERT_MSG( IsMain(), _T("should only be called from the main thread") );
581 // ok only for the default one
585 // get system affinity mask first
586 HANDLE hProcess
= ::GetCurrentProcess();
587 DWORD dwProcMask
, dwSysMask
;
588 if ( ::GetProcessAffinityMask(hProcess
, &dwProcMask
, &dwSysMask
) == 0 )
590 wxLogLastError(_T("GetProcessAffinityMask"));
595 // how many CPUs have we got?
596 if ( dwSysMask
== 1 )
598 // don't bother with all this complicated stuff - on a single
599 // processor system it doesn't make much sense anyhow
603 // calculate the process mask: it's a bit vector with one bit per
604 // processor; we want to schedule the process to run on first level
609 if ( dwSysMask
& bit
)
611 // ok, we can set this bit
614 // another process added
626 // could we set all bits?
629 wxLogDebug(_T("bad level %u in wxThread::SetConcurrency()"), level
);
634 // set it: we can't link to SetProcessAffinityMask() because it doesn't
635 // exist in Win9x, use RT binding instead
637 typedef BOOL (*SETPROCESSAFFINITYMASK
)(HANDLE
, DWORD
);
639 // can use static var because we're always in the main thread here
640 static SETPROCESSAFFINITYMASK pfnSetProcessAffinityMask
= NULL
;
642 if ( !pfnSetProcessAffinityMask
)
644 HMODULE hModKernel
= ::LoadLibrary(_T("kernel32"));
647 pfnSetProcessAffinityMask
= (SETPROCESSAFFINITYMASK
)
648 ::GetProcAddress(hModKernel
,
649 #if defined(__BORLANDC__) && (__BORLANDC__ <= 0x520)
650 "SetProcessAffinityMask");
652 _T("SetProcessAffinityMask"));
656 // we've discovered a MT version of Win9x!
657 wxASSERT_MSG( pfnSetProcessAffinityMask
,
658 _T("this system has several CPUs but no "
659 "SetProcessAffinityMask function?") );
662 if ( !pfnSetProcessAffinityMask
)
664 // msg given above - do it only once
668 if ( pfnSetProcessAffinityMask(hProcess
, dwProcMask
) == 0 )
670 wxLogLastError(_T("SetProcessAffinityMask"));
681 wxThread::wxThread(wxThreadKind kind
)
683 m_internal
= new wxThreadInternal();
685 m_isDetached
= kind
== wxTHREAD_DETACHED
;
688 wxThread::~wxThread()
693 // create/start thread
694 // -------------------
696 wxThreadError
wxThread::Create()
698 wxCriticalSectionLocker
lock(m_critsect
);
700 if ( !m_internal
->Create(this) )
701 return wxTHREAD_NO_RESOURCE
;
703 return wxTHREAD_NO_ERROR
;
706 wxThreadError
wxThread::Run()
708 wxCriticalSectionLocker
lock(m_critsect
);
710 if ( m_internal
->GetState() != STATE_NEW
)
712 // actually, it may be almost any state at all, not only STATE_RUNNING
713 return wxTHREAD_RUNNING
;
716 // the thread has just been created and is still suspended - let it run
720 // suspend/resume thread
721 // ---------------------
723 wxThreadError
wxThread::Pause()
725 wxCriticalSectionLocker
lock(m_critsect
);
727 return m_internal
->Suspend() ? wxTHREAD_NO_ERROR
: wxTHREAD_MISC_ERROR
;
730 wxThreadError
wxThread::Resume()
732 wxCriticalSectionLocker
lock(m_critsect
);
734 return m_internal
->Resume() ? wxTHREAD_NO_ERROR
: wxTHREAD_MISC_ERROR
;
740 wxThread::ExitCode
wxThread::Wait()
742 // although under Windows we can wait for any thread, it's an error to
743 // wait for a detached one in wxWin API
744 wxCHECK_MSG( !IsDetached(), (ExitCode
)-1,
745 _T("can't wait for detached thread") );
747 ExitCode rc
= (ExitCode
)-1;
756 wxThreadError
wxThread::Delete(ExitCode
*pRc
)
760 // Delete() is always safe to call, so consider all possible states
762 // has the thread started to run?
763 bool shouldResume
= FALSE
;
766 wxCriticalSectionLocker
lock(m_critsect
);
768 if ( m_internal
->GetState() == STATE_NEW
)
770 // WinThreadStart() will see it and terminate immediately
771 m_internal
->SetState(STATE_EXITED
);
777 // is the thread paused?
778 if ( shouldResume
|| IsPaused() )
781 HANDLE hThread
= m_internal
->GetHandle();
783 // does is still run?
788 // set flag for wxIsWaitingForThread()
789 gs_waitingForThread
= TRUE
;
796 // ask the thread to terminate
798 wxCriticalSectionLocker
lock(m_critsect
);
800 m_internal
->Cancel();
804 // we can't just wait for the thread to terminate because it might be
805 // calling some GUI functions and so it will never terminate before we
806 // process the Windows messages that result from these functions
810 result
= ::MsgWaitForMultipleObjects
812 1, // number of objects to wait for
813 &hThread
, // the objects
814 FALSE
, // don't wait for all objects
815 INFINITE
, // no timeout
816 QS_ALLEVENTS
// return as soon as there are any events
823 wxLogSysError(_("Can not wait for thread termination"));
825 return wxTHREAD_KILLED
;
828 // thread we're waiting for terminated
831 case WAIT_OBJECT_0
+ 1:
832 // new message arrived, process it
833 if ( !wxTheApp
->DoMessage() )
835 // WM_QUIT received: kill the thread
838 return wxTHREAD_KILLED
;
843 // give the thread we're waiting for chance to exit
844 // from the GUI call it might have been in
845 if ( (gs_nWaitingForGui
> 0) && wxGuiOwnedByMainThread() )
854 wxFAIL_MSG(wxT("unexpected result of MsgWaitForMultipleObject"));
856 } while ( result
!= WAIT_OBJECT_0
);
858 // simply wait for the thread to terminate
860 // OTOH, even console apps create windows (in wxExecute, for WinSock
861 // &c), so may be use MsgWaitForMultipleObject() too here?
862 if ( WaitForSingleObject(hThread
, INFINITE
) != WAIT_OBJECT_0
)
864 wxFAIL_MSG(wxT("unexpected result of WaitForSingleObject"));
866 #endif // wxUSE_GUI/!wxUSE_GUI
870 gs_waitingForThread
= FALSE
;
878 if ( !::GetExitCodeThread(hThread
, (LPDWORD
)&rc
) )
880 wxLogLastError("GetExitCodeThread");
887 // if the thread exits normally, this is done in WinThreadStart, but in
888 // this case it would have been too early because
889 // MsgWaitForMultipleObject() would fail if the therad handle was
890 // closed while we were waiting on it, so we must do it here
894 wxASSERT_MSG( (DWORD
)rc
!= STILL_ACTIVE
,
895 wxT("thread must be already terminated.") );
900 return rc
== (ExitCode
)-1 ? wxTHREAD_MISC_ERROR
: wxTHREAD_NO_ERROR
;
903 wxThreadError
wxThread::Kill()
906 return wxTHREAD_NOT_RUNNING
;
908 if ( !::TerminateThread(m_internal
->GetHandle(), (DWORD
)-1) )
910 wxLogSysError(_("Couldn't terminate thread"));
912 return wxTHREAD_MISC_ERROR
;
922 return wxTHREAD_NO_ERROR
;
925 void wxThread::Exit(ExitCode status
)
934 #if defined(__VISUALC__) || \
935 (defined(__BORLANDC__) && (__BORLANDC__ >= 0x500)) || \
936 (defined(__GNUG__) && defined(__MSVCRT__))
937 _endthreadex((unsigned)status
);
939 ::ExitThread((DWORD
)status
);
942 wxFAIL_MSG(wxT("Couldn't return from ExitThread()!"));
948 void wxThread::SetPriority(unsigned int prio
)
950 wxCriticalSectionLocker
lock(m_critsect
);
952 m_internal
->SetPriority(prio
);
955 unsigned int wxThread::GetPriority() const
957 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
959 return m_internal
->GetPriority();
962 unsigned long wxThread::GetId() const
964 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
966 return (unsigned long)m_internal
->GetId();
969 bool wxThread::IsRunning() const
971 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
973 return m_internal
->GetState() == STATE_RUNNING
;
976 bool wxThread::IsAlive() const
978 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
980 return (m_internal
->GetState() == STATE_RUNNING
) ||
981 (m_internal
->GetState() == STATE_PAUSED
);
984 bool wxThread::IsPaused() const
986 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
988 return m_internal
->GetState() == STATE_PAUSED
;
991 bool wxThread::TestDestroy()
993 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
995 return m_internal
->GetState() == STATE_CANCELED
;
998 // ----------------------------------------------------------------------------
999 // Automatic initialization for thread module
1000 // ----------------------------------------------------------------------------
1002 class wxThreadModule
: public wxModule
1005 virtual bool OnInit();
1006 virtual void OnExit();
1009 DECLARE_DYNAMIC_CLASS(wxThreadModule
)
1012 IMPLEMENT_DYNAMIC_CLASS(wxThreadModule
, wxModule
)
1014 bool wxThreadModule::OnInit()
1016 // allocate TLS index for storing the pointer to the current thread
1017 gs_tlsThisThread
= ::TlsAlloc();
1018 if ( gs_tlsThisThread
== 0xFFFFFFFF )
1020 // in normal circumstances it will only happen if all other
1021 // TLS_MINIMUM_AVAILABLE (>= 64) indices are already taken - in other
1022 // words, this should never happen
1023 wxLogSysError(_("Thread module initialization failed: "
1024 "impossible to allocate index in thread "
1030 // main thread doesn't have associated wxThread object, so store 0 in the
1032 if ( !::TlsSetValue(gs_tlsThisThread
, (LPVOID
)0) )
1034 ::TlsFree(gs_tlsThisThread
);
1035 gs_tlsThisThread
= 0xFFFFFFFF;
1037 wxLogSysError(_("Thread module initialization failed: "
1038 "can not store value in thread local storage"));
1043 gs_critsectWaitingForGui
= new wxCriticalSection();
1045 gs_critsectGui
= new wxCriticalSection();
1046 gs_critsectGui
->Enter();
1048 // no error return for GetCurrentThreadId()
1049 gs_idMainThread
= ::GetCurrentThreadId();
1054 void wxThreadModule::OnExit()
1056 if ( !::TlsFree(gs_tlsThisThread
) )
1058 wxLogLastError("TlsFree failed.");
1061 if ( gs_critsectGui
)
1063 gs_critsectGui
->Leave();
1064 delete gs_critsectGui
;
1065 gs_critsectGui
= NULL
;
1068 delete gs_critsectWaitingForGui
;
1069 gs_critsectWaitingForGui
= NULL
;
1072 // ----------------------------------------------------------------------------
1073 // under Windows, these functions are implemented using a critical section and
1074 // not a mutex, so the names are a bit confusing
1075 // ----------------------------------------------------------------------------
1077 void WXDLLEXPORT
wxMutexGuiEnter()
1079 // this would dead lock everything...
1080 wxASSERT_MSG( !wxThread::IsMain(),
1081 wxT("main thread doesn't want to block in wxMutexGuiEnter()!") );
1083 // the order in which we enter the critical sections here is crucial!!
1085 // set the flag telling to the main thread that we want to do some GUI
1087 wxCriticalSectionLocker
enter(*gs_critsectWaitingForGui
);
1089 gs_nWaitingForGui
++;
1092 wxWakeUpMainThread();
1094 // now we may block here because the main thread will soon let us in
1095 // (during the next iteration of OnIdle())
1096 gs_critsectGui
->Enter();
1099 void WXDLLEXPORT
wxMutexGuiLeave()
1101 wxCriticalSectionLocker
enter(*gs_critsectWaitingForGui
);
1103 if ( wxThread::IsMain() )
1105 gs_bGuiOwnedByMainThread
= FALSE
;
1109 // decrement the number of waiters now
1110 wxASSERT_MSG( gs_nWaitingForGui
> 0,
1111 wxT("calling wxMutexGuiLeave() without entering it first?") );
1113 gs_nWaitingForGui
--;
1115 wxWakeUpMainThread();
1118 gs_critsectGui
->Leave();
1121 void WXDLLEXPORT
wxMutexGuiLeaveOrEnter()
1123 wxASSERT_MSG( wxThread::IsMain(),
1124 wxT("only main thread may call wxMutexGuiLeaveOrEnter()!") );
1126 wxCriticalSectionLocker
enter(*gs_critsectWaitingForGui
);
1128 if ( gs_nWaitingForGui
== 0 )
1130 // no threads are waiting for GUI - so we may acquire the lock without
1131 // any danger (but only if we don't already have it)
1132 if ( !wxGuiOwnedByMainThread() )
1134 gs_critsectGui
->Enter();
1136 gs_bGuiOwnedByMainThread
= TRUE
;
1138 //else: already have it, nothing to do
1142 // some threads are waiting, release the GUI lock if we have it
1143 if ( wxGuiOwnedByMainThread() )
1147 //else: some other worker thread is doing GUI
1151 bool WXDLLEXPORT
wxGuiOwnedByMainThread()
1153 return gs_bGuiOwnedByMainThread
;
1156 // wake up the main thread if it's in ::GetMessage()
1157 void WXDLLEXPORT
wxWakeUpMainThread()
1159 // sending any message would do - hopefully WM_NULL is harmless enough
1160 if ( !::PostThreadMessage(gs_idMainThread
, WM_NULL
, 0, 0) )
1162 // should never happen
1163 wxLogLastError("PostThreadMessage(WM_NULL)");
1167 bool WXDLLEXPORT
wxIsWaitingForThread()
1169 return gs_waitingForThread
;
1172 #endif // wxUSE_THREADS