1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/thread.cpp
3 // Purpose: wxThread Implementation
4 // Author: Original from Wolfram Gloger/Guilhem Lavaux/David Webster
5 // Modified by: Stefan Neis
8 // Copyright: (c) Stefan Neis (2003)
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // ----------------------------------------------------------------------------
15 // ----------------------------------------------------------------------------
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
25 #include "wx/apptrait.h"
26 #include "wx/module.h"
30 #include "wx/thread.h"
32 #define INCL_DOSSEMAPHORES
33 #define INCL_DOSPROCESS
40 // the possible states of the thread ("=>" shows all possible transitions from
44 STATE_NEW
, // didn't start execution yet (=> RUNNING)
45 STATE_RUNNING
, // thread is running (=> PAUSED, CANCELED)
46 STATE_PAUSED
, // thread is temporarily suspended (=> RUNNING)
47 STATE_CANCELED
, // thread should terminate a.s.a.p. (=> EXITED)
48 STATE_EXITED
// thread is terminating
51 // ----------------------------------------------------------------------------
52 // this module's globals
53 // ----------------------------------------------------------------------------
55 // id of the main thread - the one which can call GUI functions without first
56 // calling wxMutexGuiEnter()
57 static ULONG s_ulIdMainThread
= 1;
58 wxMutex
* p_wxMainMutex
;
60 // OS2 substitute for Tls pointer the current parent thread object
61 wxThread
* m_pThread
; // pointer to the wxWidgets thread object
63 // if it's false, some secondary thread is holding the GUI lock
64 static bool gs_bGuiOwnedByMainThread
= true;
66 // critical section which controls access to all GUI functions: any secondary
67 // thread (i.e. except the main one) must enter this crit section before doing
69 static wxCriticalSection
*gs_pCritsectGui
= NULL
;
71 // critical section which protects s_nWaitingForGui variable
72 static wxCriticalSection
*gs_pCritsectWaitingForGui
= NULL
;
74 // number of threads waiting for GUI in wxMutexGuiEnter()
75 static size_t gs_nWaitingForGui
= 0;
77 // are we waiting for a thread termination?
78 static bool gs_bWaitingForThread
= false;
80 // ============================================================================
81 // OS/2 implementation of thread and related classes
82 // ============================================================================
84 // ----------------------------------------------------------------------------
85 // wxMutex implementation
86 // ----------------------------------------------------------------------------
90 wxMutexInternal(wxMutexType mutexType
);
93 bool IsOk() const { return m_vMutex
!= NULL
; }
95 wxMutexError
Lock() { return LockTimeout(SEM_INDEFINITE_WAIT
); }
96 wxMutexError
TryLock() { return LockTimeout(SEM_IMMEDIATE_RETURN
); }
97 wxMutexError
Unlock();
100 wxMutexError
LockTimeout(ULONG ulMilliseconds
);
104 // all mutexes are "pseudo-"recursive under OS2 so we don't use mutexType
105 // (Calls to DosRequestMutexSem and DosReleaseMutexSem can be nested, but
106 // the request count for a semaphore cannot exceed 65535. If an attempt is
107 // made to exceed this number, ERROR_TOO_MANY_SEM_REQUESTS is returned.)
108 wxMutexInternal::wxMutexInternal(wxMutexType
WXUNUSED(eMutexType
))
110 APIRET ulrc
= ::DosCreateMutexSem(NULL
, &m_vMutex
, 0L, FALSE
);
113 wxLogSysError(_("Can not create mutex."));
118 wxMutexInternal::~wxMutexInternal()
122 if (::DosCloseMutexSem(m_vMutex
))
123 wxLogLastError(_T("DosCloseMutexSem(mutex)"));
127 wxMutexError
wxMutexInternal::LockTimeout(ULONG ulMilliseconds
)
131 ulrc
= ::DosRequestMutexSem(m_vMutex
, ulMilliseconds
);
136 case ERROR_TOO_MANY_SEM_REQUESTS
:
143 case ERROR_INVALID_HANDLE
:
144 case ERROR_INTERRUPT
:
145 case ERROR_SEM_OWNER_DIED
:
146 wxLogSysError(_("Couldn't acquire a mutex lock"));
147 return wxMUTEX_MISC_ERROR
;
150 wxFAIL_MSG(wxT("impossible return value in wxMutex::Lock"));
151 return wxMUTEX_MISC_ERROR
;
153 return wxMUTEX_NO_ERROR
;
156 wxMutexError
wxMutexInternal::Unlock()
160 ulrc
= ::DosReleaseMutexSem(m_vMutex
);
163 wxLogSysError(_("Couldn't release a mutex"));
164 return wxMUTEX_MISC_ERROR
;
166 return wxMUTEX_NO_ERROR
;
169 // --------------------------------------------------------------------------
171 // --------------------------------------------------------------------------
173 // a trivial wrapper around OS2 event semaphore
174 class wxSemaphoreInternal
177 wxSemaphoreInternal(int initialcount
, int maxcount
);
178 ~wxSemaphoreInternal();
180 bool IsOk() const { return m_vEvent
!= NULL
; }
182 wxSemaError
Wait() { return WaitTimeout(SEM_INDEFINITE_WAIT
); }
183 wxSemaError
TryWait() { return WaitTimeout(SEM_IMMEDIATE_RETURN
); }
184 wxSemaError
WaitTimeout(unsigned long milliseconds
);
195 wxSemaphoreInternal::wxSemaphoreInternal(int initialcount
, int maxcount
)
200 // make it practically infinite
204 m_count
= initialcount
;
205 m_maxcount
= maxcount
;
206 ulrc
= ::DosCreateMutexSem(NULL
, &m_vMutex
, 0L, FALSE
);
209 wxLogLastError(_T("DosCreateMutexSem()"));
214 ulrc
= ::DosCreateEventSem(NULL
, &m_vEvent
, 0L, FALSE
);
217 wxLogLastError(_T("DosCreateEventSem()"));
218 ::DosCloseMutexSem(m_vMutex
);
223 ::DosPostEventSem(m_vEvent
);
226 wxSemaphoreInternal::~wxSemaphoreInternal()
230 if ( ::DosCloseEventSem(m_vEvent
) )
232 wxLogLastError(_T("DosCloseEventSem(semaphore)"));
234 if ( ::DosCloseMutexSem(m_vMutex
) )
236 wxLogLastError(_T("DosCloseMutexSem(semaphore)"));
243 wxSemaError
wxSemaphoreInternal::WaitTimeout(unsigned long ulMilliseconds
)
247 ulrc
= ::DosWaitEventSem(m_vEvent
, ulMilliseconds
);
254 if (ulMilliseconds
== SEM_IMMEDIATE_RETURN
)
257 return wxSEMA_TIMEOUT
;
260 wxLogLastError(_T("DosWaitEventSem(semaphore)"));
261 return wxSEMA_MISC_ERROR
;
263 ulrc
= :: DosRequestMutexSem(m_vMutex
, ulMilliseconds
);
271 case ERROR_TOO_MANY_SEM_REQUESTS
:
272 if (ulMilliseconds
== SEM_IMMEDIATE_RETURN
)
275 return wxSEMA_TIMEOUT
;
278 wxFAIL_MSG(wxT("DosRequestMutexSem(mutex)"));
279 return wxSEMA_MISC_ERROR
;
290 ::DosResetEventSem(m_vEvent
, &ulPostCount
);
292 ::DosReleaseMutexSem(m_vMutex
);
294 return wxSEMA_NO_ERROR
;
295 } while (ulMilliseconds
== SEM_INDEFINITE_WAIT
);
297 if (ulMilliseconds
== SEM_IMMEDIATE_RETURN
)
299 return wxSEMA_TIMEOUT
;
302 wxSemaError
wxSemaphoreInternal::Post()
305 ulrc
= ::DosRequestMutexSem(m_vMutex
, SEM_INDEFINITE_WAIT
);
306 if (ulrc
!= NO_ERROR
)
307 return wxSEMA_MISC_ERROR
;
309 if (m_count
< m_maxcount
)
312 ulrc
= ::DosPostEventSem(m_vEvent
);
315 ::DosReleaseMutexSem(m_vMutex
);
317 return wxSEMA_OVERFLOW
;
318 if ( ulrc
!= NO_ERROR
&& ulrc
!= ERROR_ALREADY_POSTED
)
320 wxLogLastError(_T("DosPostEventSem(semaphore)"));
322 return wxSEMA_MISC_ERROR
;
325 return wxSEMA_NO_ERROR
;
328 // ----------------------------------------------------------------------------
329 // wxThread implementation
330 // ----------------------------------------------------------------------------
332 // wxThreadInternal class
333 // ----------------------
335 class wxThreadInternal
338 inline wxThreadInternal()
341 m_eState
= STATE_NEW
;
342 m_nPriority
= WXTHREAD_DEFAULT_PRIORITY
;
350 // create a new (suspended) thread (for the given thread object)
351 bool Create( wxThread
* pThread
352 ,unsigned int uStackSize
355 // suspend/resume/terminate
358 inline void Cancel() { m_eState
= STATE_CANCELED
; }
361 inline void SetState(wxThreadState eState
) { m_eState
= eState
; }
362 inline wxThreadState
GetState() const { return m_eState
; }
365 void SetPriority(unsigned int nPriority
);
366 inline unsigned int GetPriority() const { return m_nPriority
; }
368 // thread handle and id
369 inline TID
GetHandle() const { return m_hThread
; }
370 TID
GetId() const { return m_hThread
; }
373 static void OS2ThreadStart(void* pParam
);
376 // Threads in OS/2 have only an ID, so m_hThread is both it's handle and ID
377 // PM also has no real Tls mechanism to index pointers by so we'll just
378 // keep track of the wxWidgets parent object here.
379 TID m_hThread
; // handle and ID of the thread
380 wxThreadState m_eState
; // state, see wxThreadState enum
381 unsigned int m_nPriority
; // thread priority in "wx" units
384 void wxThreadInternal::OS2ThreadStart( void * pParam
)
389 wxThread
*pThread
= (wxThread
*)pParam
;
391 // first of all, wait for the thread to be started.
392 pThread
->m_critsect
.Enter();
393 pThread
->m_critsect
.Leave();
394 // Now check whether we hadn't been cancelled already and don't
395 // start the user code at all in this case.
396 if ( pThread
->m_internal
->GetState() == STATE_EXITED
)
399 bWasCancelled
= true;
401 else // do run thread
403 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
406 traits
->InitializeGui(ulHab
);
407 dwRet
= (DWORD
)pThread
->Entry();
409 traits
->TerminateGui(ulHab
);
411 // enter m_critsect before changing the thread state
412 pThread
->m_critsect
.Enter();
414 bWasCancelled
= pThread
->m_internal
->GetState() == STATE_CANCELED
;
416 pThread
->m_internal
->SetState(STATE_EXITED
);
417 pThread
->m_critsect
.Leave();
421 // if the thread was cancelled (from Delete()), then it the handle is still
423 if (pThread
->IsDetached() && !bWasCancelled
)
428 //else: the joinable threads handle will be closed when Wait() is done
432 void wxThreadInternal::SetPriority(
433 unsigned int nPriority
436 // translate wxWidgets priority to the PM one
437 ULONG ulOS2_PriorityClass
;
438 ULONG ulOS2_SubPriority
;
441 m_nPriority
= nPriority
;
442 if (m_nPriority
<= 25)
443 ulOS2_PriorityClass
= PRTYC_IDLETIME
;
444 else if (m_nPriority
<= 50)
445 ulOS2_PriorityClass
= PRTYC_REGULAR
;
446 else if (m_nPriority
<= 75)
447 ulOS2_PriorityClass
= PRTYC_TIMECRITICAL
;
448 else if (m_nPriority
<= 100)
449 ulOS2_PriorityClass
= PRTYC_FOREGROUNDSERVER
;
452 wxFAIL_MSG(wxT("invalid value of thread priority parameter"));
453 ulOS2_PriorityClass
= PRTYC_REGULAR
;
455 ulOS2_SubPriority
= (ULONG
) (((m_nPriority
- 1) % 25 + 1) * 31.0 / 25);
456 ulrc
= ::DosSetPriority( PRTYS_THREAD
463 wxLogSysError(_("Can't set thread priority"));
467 bool wxThreadInternal::Create( wxThread
* pThread
,
468 unsigned int uStackSize
)
475 pThread
->m_critsect
.Enter();
476 tid
= _beginthread(wxThreadInternal::OS2ThreadStart
,
477 NULL
, uStackSize
, pThread
);
480 wxLogSysError(_("Can't create thread"));
485 if (m_nPriority
!= WXTHREAD_DEFAULT_PRIORITY
)
487 SetPriority(m_nPriority
);
493 bool wxThreadInternal::Suspend()
495 ULONG ulrc
= ::DosSuspendThread(m_hThread
);
499 wxLogSysError(_("Can not suspend thread %lu"), m_hThread
);
502 m_eState
= STATE_PAUSED
;
507 bool wxThreadInternal::Resume()
509 ULONG ulrc
= ::DosResumeThread(m_hThread
);
513 wxLogSysError(_("Can not resume thread %lu"), m_hThread
);
517 // don't change the state from STATE_EXITED because it's special and means
518 // we are going to terminate without running any user code - if we did it,
519 // the codei n Delete() wouldn't work
520 if ( m_eState
!= STATE_EXITED
)
522 m_eState
= STATE_RUNNING
;
531 wxThread
*wxThread::This()
533 wxThread
* pThread
= m_pThread
;
537 bool wxThread::IsMain()
542 ::DosGetInfoBlocks(&ptib
, &ppib
);
544 if (ptib
->tib_ptib2
->tib2_ultid
== s_ulIdMainThread
)
554 void wxThread::Yield()
559 void wxThread::Sleep(
560 unsigned long ulMilliseconds
563 ::DosSleep(ulMilliseconds
);
566 int wxThread::GetCPUCount()
570 ulrc
= ::DosQuerySysInfo(26, 26, (void *)&CPUCount
, sizeof(ULONG
));
571 // QSV_NUMPROCESSORS(26) is typically not defined in header files
579 unsigned long wxThread::GetCurrentId()
584 ::DosGetInfoBlocks(&ptib
, &ppib
);
585 return (unsigned long) ptib
->tib_ptib2
->tib2_ultid
;
588 bool wxThread::SetConcurrency(size_t level
)
590 wxASSERT_MSG( IsMain(), _T("should only be called from the main thread") );
592 // ok only for the default one
596 // Don't know how to realize this on OS/2.
603 wxThread::wxThread(wxThreadKind kind
)
605 m_internal
= new wxThreadInternal();
607 m_isDetached
= kind
== wxTHREAD_DETACHED
;
610 wxThread::~wxThread()
615 // create/start thread
616 // -------------------
618 wxThreadError
wxThread::Create(
619 unsigned int uStackSize
622 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
624 if ( !m_internal
->Create(this, uStackSize
) )
625 return wxTHREAD_NO_RESOURCE
;
627 return wxTHREAD_NO_ERROR
;
630 wxThreadError
wxThread::Run()
632 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
634 if ( m_internal
->GetState() != STATE_NEW
)
636 // actually, it may be almost any state at all, not only STATE_RUNNING
637 return wxTHREAD_RUNNING
;
642 // suspend/resume thread
643 // ---------------------
645 wxThreadError
wxThread::Pause()
647 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
649 return m_internal
->Suspend() ? wxTHREAD_NO_ERROR
: wxTHREAD_MISC_ERROR
;
652 wxThreadError
wxThread::Resume()
654 if (m_internal
->GetState() == STATE_NEW
)
656 m_internal
->SetState(STATE_RUNNING
);
658 return wxTHREAD_NO_ERROR
;
661 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
663 return m_internal
->Resume() ? wxTHREAD_NO_ERROR
: wxTHREAD_MISC_ERROR
;
669 wxThread::ExitCode
wxThread::Wait()
671 // although under Windows we can wait for any thread, it's an error to
672 // wait for a detached one in wxWin API
673 wxCHECK_MSG( !IsDetached(), (ExitCode
)-1,
674 _T("can't wait for detached thread") );
675 ExitCode rc
= (ExitCode
)-1;
680 wxThreadError
wxThread::Delete(ExitCode
*pRc
)
684 // Delete() is always safe to call, so consider all possible states
686 // we might need to resume the thread, but we might also not need to cancel
687 // it if it doesn't run yet
688 bool shouldResume
= false,
692 // check if the thread already started to run
694 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
696 if ( m_internal
->GetState() == STATE_NEW
)
698 // WinThreadStart() will see it and terminate immediately, no need
699 // to cancel the thread - but we still need to resume it to let it
701 m_internal
->SetState(STATE_EXITED
);
703 Resume(); // it knows about STATE_EXITED special case
705 shouldCancel
= false;
708 // shouldResume is correctly set to false here
712 shouldResume
= IsPaused();
716 // resume the thread if it is paused
720 TID hThread
= m_internal
->GetHandle();
722 if ( isRunning
|| IsRunning())
726 // set flag for wxIsWaitingForThread()
727 gs_bWaitingForThread
= true;
730 // ask the thread to terminate
733 wxCriticalSectionLocker
lock(m_critsect
);
735 m_internal
->Cancel();
739 // we can't just wait for the thread to terminate because it might be
740 // calling some GUI functions and so it will never terminate before we
741 // process the Windows messages that result from these functions
742 DWORD result
= 0; // suppress warnings from broken compilers
747 // give the thread we're waiting for chance to do the GUI call
749 if ( (gs_nWaitingForGui
> 0) && wxGuiOwnedByMainThread() )
755 result
= ::DosWaitThread(&hThread
, DCWW_NOWAIT
);
756 // FIXME: We ought to have a message processing loop here!!
760 case ERROR_INTERRUPT
:
761 case ERROR_THREAD_NOT_TERMINATED
:
763 case ERROR_INVALID_THREADID
:
765 // thread we're waiting for just terminated
766 // or even does not exist any more.
770 wxFAIL_MSG(wxT("unexpected result of DosWaitThread"));
774 // event processing - needed if we are the main thread
775 // to give other threads a chance to do remaining GUI
776 // processing and terminate cleanly.
777 wxTheApp
->HandleSockets();
778 if (wxTheApp
->Pending())
779 if ( !wxTheApp
->DoMessage() )
781 // WM_QUIT received: kill the thread
784 return wxTHREAD_KILLED
;
791 } while ( result
!= NO_ERROR
);
793 // simply wait for the thread to terminate
795 // OTOH, even console apps create windows (in wxExecute, for WinSock
796 // &c), so may be use MsgWaitForMultipleObject() too here?
797 if ( ::DosWaitThread(&hThread
, DCWW_WAIT
) != NO_ERROR
)
799 wxFAIL_MSG(wxT("unexpected result of DosWaitThread"));
801 #endif // wxUSE_GUI/!wxUSE_GUI
805 gs_bWaitingForThread
= false;
810 // although the thread might be already in the EXITED state it might not
811 // have terminated yet and so we are not sure that it has actually
812 // terminated if the "if" above hadn't been taken
815 if ( !::GetExitCodeThread(hThread
, (LPDWORD
)&rc
) )
817 wxLogLastError(wxT("GetExitCodeThread"));
821 } while ( (DWORD
)rc
== STILL_ACTIVE
);
826 // if the thread exits normally, this is done in WinThreadStart, but in
827 // this case it would have been too early because
828 // MsgWaitForMultipleObject() would fail if the thread handle was
829 // closed while we were waiting on it, so we must do it here
836 return rc
== (ExitCode
)-1 ? wxTHREAD_MISC_ERROR
: wxTHREAD_NO_ERROR
;
839 wxThreadError
wxThread::Kill()
842 return wxTHREAD_NOT_RUNNING
;
844 ::DosKillThread(m_internal
->GetHandle());
849 return wxTHREAD_NO_ERROR
;
852 void wxThread::Exit(ExitCode
WXUNUSED(pStatus
))
856 wxFAIL_MSG(wxT("Couldn't return from DosExit()!"));
859 void wxThread::SetPriority(
863 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
865 m_internal
->SetPriority(nPrio
);
868 unsigned int wxThread::GetPriority() const
870 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
872 return m_internal
->GetPriority();
875 unsigned long wxThread::GetId() const
877 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
879 return (unsigned long)m_internal
->GetId();
882 bool wxThread::IsRunning() const
884 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
886 return(m_internal
->GetState() == STATE_RUNNING
);
889 bool wxThread::IsAlive() const
891 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
893 return (m_internal
->GetState() == STATE_RUNNING
) ||
894 (m_internal
->GetState() == STATE_PAUSED
);
897 bool wxThread::IsPaused() const
899 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
901 return (m_internal
->GetState() == STATE_PAUSED
);
904 bool wxThread::TestDestroy()
906 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
908 return m_internal
->GetState() == STATE_CANCELED
;
911 // ----------------------------------------------------------------------------
912 // Automatic initialization for thread module
913 // ----------------------------------------------------------------------------
915 class wxThreadModule
: public wxModule
918 virtual bool OnInit();
919 virtual void OnExit();
922 DECLARE_DYNAMIC_CLASS(wxThreadModule
)
925 IMPLEMENT_DYNAMIC_CLASS(wxThreadModule
, wxModule
)
927 bool wxThreadModule::OnInit()
929 gs_pCritsectWaitingForGui
= new wxCriticalSection();
931 gs_pCritsectGui
= new wxCriticalSection();
932 gs_pCritsectGui
->Enter();
937 ::DosGetInfoBlocks(&ptib
, &ppib
);
939 s_ulIdMainThread
= ptib
->tib_ptib2
->tib2_ultid
;
943 void wxThreadModule::OnExit()
947 gs_pCritsectGui
->Leave();
948 #if (!(defined(__VISAGECPP__) && (__IBMCPP__ < 400 || __IBMC__ < 400 )))
949 delete gs_pCritsectGui
;
951 gs_pCritsectGui
= NULL
;
954 #if (!(defined(__VISAGECPP__) && (__IBMCPP__ < 400 || __IBMC__ < 400 )))
955 wxDELETE(gs_pCritsectWaitingForGui
);
959 // ----------------------------------------------------------------------------
961 // ----------------------------------------------------------------------------
963 // wake up the main thread if it's in ::GetMessage()
964 void WXDLLEXPORT
wxWakeUpMainThread()
967 if ( !::WinPostQueueMsg(wxTheApp
->m_hMq
, WM_NULL
, 0, 0) )
969 // should never happen
970 wxLogLastError(wxT("WinPostMessage(WM_NULL)"));
975 void WXDLLEXPORT
wxMutexGuiEnter()
977 // this would dead lock everything...
978 wxASSERT_MSG( !wxThread::IsMain(),
979 wxT("main thread doesn't want to block in wxMutexGuiEnter()!") );
981 // the order in which we enter the critical sections here is crucial!!
983 // set the flag telling to the main thread that we want to do some GUI
985 wxCriticalSectionLocker
enter(*gs_pCritsectWaitingForGui
);
990 wxWakeUpMainThread();
992 // now we may block here because the main thread will soon let us in
993 // (during the next iteration of OnIdle())
994 gs_pCritsectGui
->Enter();
997 void WXDLLEXPORT
wxMutexGuiLeave()
999 wxCriticalSectionLocker
enter(*gs_pCritsectWaitingForGui
);
1001 if ( wxThread::IsMain() )
1003 gs_bGuiOwnedByMainThread
= false;
1007 // decrement the number of waiters now
1008 wxASSERT_MSG(gs_nWaitingForGui
> 0,
1009 wxT("calling wxMutexGuiLeave() without entering it first?") );
1011 gs_nWaitingForGui
--;
1013 wxWakeUpMainThread();
1016 gs_pCritsectGui
->Leave();
1019 void WXDLLEXPORT
wxMutexGuiLeaveOrEnter()
1021 wxASSERT_MSG( wxThread::IsMain(),
1022 wxT("only main thread may call wxMutexGuiLeaveOrEnter()!") );
1024 wxCriticalSectionLocker
enter(*gs_pCritsectWaitingForGui
);
1026 if (gs_nWaitingForGui
== 0)
1028 // no threads are waiting for GUI - so we may acquire the lock without
1029 // any danger (but only if we don't already have it)
1030 if (!wxGuiOwnedByMainThread())
1032 gs_pCritsectGui
->Enter();
1034 gs_bGuiOwnedByMainThread
= true;
1036 //else: already have it, nothing to do
1040 // some threads are waiting, release the GUI lock if we have it
1041 if (wxGuiOwnedByMainThread())
1045 //else: some other worker thread is doing GUI
1049 bool WXDLLEXPORT
wxGuiOwnedByMainThread()
1051 return gs_bGuiOwnedByMainThread
;
1054 bool WXDLLEXPORT
wxIsWaitingForThread()
1056 return gs_bWaitingForThread
;
1059 // ----------------------------------------------------------------------------
1060 // include common implementation code
1061 // ----------------------------------------------------------------------------
1063 #include "wx/thrimpl.cpp"