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)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ----------------------------------------------------------------------------
14 // ----------------------------------------------------------------------------
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
21 #include "wx/thread.h"
27 #include "wx/module.h"
30 #include "wx/apptrait.h"
35 #define INCL_DOSSEMAPHORES
36 #define INCL_DOSPROCESS
43 // the possible states of the thread ("=>" shows all possible transitions from
47 STATE_NEW
, // didn't start execution yet (=> RUNNING)
48 STATE_RUNNING
, // thread is running (=> PAUSED, CANCELED)
49 STATE_PAUSED
, // thread is temporarily suspended (=> RUNNING)
50 STATE_CANCELED
, // thread should terminate a.s.a.p. (=> EXITED)
51 STATE_EXITED
// thread is terminating
54 // ----------------------------------------------------------------------------
55 // this module's globals
56 // ----------------------------------------------------------------------------
58 // id of the main thread - the one which can call GUI functions without first
59 // calling wxMutexGuiEnter()
60 wxThreadIdType
wxThread::ms_idMainThread
= 0;
61 wxMutex
* p_wxMainMutex
;
63 // OS2 substitute for Tls pointer the current parent thread object
64 wxThread
* m_pThread
; // pointer to the wxWidgets thread object
66 // if it's false, some secondary thread is holding the GUI lock
67 static bool gs_bGuiOwnedByMainThread
= true;
69 // critical section which controls access to all GUI functions: any secondary
70 // thread (i.e. except the main one) must enter this crit section before doing
72 static wxCriticalSection
*gs_pCritsectGui
= NULL
;
74 // critical section which protects s_nWaitingForGui variable
75 static wxCriticalSection
*gs_pCritsectWaitingForGui
= NULL
;
77 // number of threads waiting for GUI in wxMutexGuiEnter()
78 static size_t gs_nWaitingForGui
= 0;
80 // are we waiting for a thread termination?
81 static bool gs_bWaitingForThread
= false;
83 // ============================================================================
84 // OS/2 implementation of thread and related classes
85 // ============================================================================
87 // ----------------------------------------------------------------------------
88 // wxMutex implementation
89 // ----------------------------------------------------------------------------
93 wxMutexInternal(wxMutexType mutexType
);
96 bool IsOk() const { return m_vMutex
!= NULL
; }
98 wxMutexError
Lock() { return LockTimeout(SEM_INDEFINITE_WAIT
); }
99 wxMutexError
Lock(unsigned long ms
) { return LockTimeout(ms
); }
100 wxMutexError
TryLock();
101 wxMutexError
Unlock();
104 wxMutexError
LockTimeout(ULONG ulMilliseconds
);
108 // all mutexes are "pseudo-"recursive under OS2 so we don't use mutexType
109 // (Calls to DosRequestMutexSem and DosReleaseMutexSem can be nested, but
110 // the request count for a semaphore cannot exceed 65535. If an attempt is
111 // made to exceed this number, ERROR_TOO_MANY_SEM_REQUESTS is returned.)
112 wxMutexInternal::wxMutexInternal(wxMutexType
WXUNUSED(eMutexType
))
114 APIRET ulrc
= ::DosCreateMutexSem(NULL
, &m_vMutex
, 0L, FALSE
);
117 wxLogSysError(_("Can not create mutex."));
122 wxMutexInternal::~wxMutexInternal()
126 if (::DosCloseMutexSem(m_vMutex
))
127 wxLogLastError(_T("DosCloseMutexSem(mutex)"));
131 wxMutexError
wxMutexInternal::TryLock()
133 const wxMutexError rc
= LockTimeout( SEM_IMMEDIATE_RETURN
);
135 // we have a special return code for timeout in this case
136 return rc
== wxMUTEX_TIMEOUT
? wxMUTEX_BUSY
: rc
;
139 wxMutexError
wxMutexInternal::LockTimeout(ULONG ulMilliseconds
)
143 ulrc
= ::DosRequestMutexSem(m_vMutex
, ulMilliseconds
);
148 return wxMUTEX_TIMEOUT
;
149 case ERROR_TOO_MANY_SEM_REQUESTS
:
156 case ERROR_INVALID_HANDLE
:
157 case ERROR_INTERRUPT
:
158 case ERROR_SEM_OWNER_DIED
:
159 wxLogSysError(_("Couldn't acquire a mutex lock"));
160 return wxMUTEX_MISC_ERROR
;
163 wxFAIL_MSG(wxT("impossible return value in wxMutex::Lock"));
164 return wxMUTEX_MISC_ERROR
;
166 return wxMUTEX_NO_ERROR
;
169 wxMutexError
wxMutexInternal::Unlock()
173 ulrc
= ::DosReleaseMutexSem(m_vMutex
);
176 wxLogSysError(_("Couldn't release a mutex"));
177 return wxMUTEX_MISC_ERROR
;
179 return wxMUTEX_NO_ERROR
;
182 // --------------------------------------------------------------------------
184 // --------------------------------------------------------------------------
186 // a trivial wrapper around OS2 event semaphore
187 class wxSemaphoreInternal
190 wxSemaphoreInternal(int initialcount
, int maxcount
);
191 ~wxSemaphoreInternal();
193 bool IsOk() const { return m_vEvent
!= NULL
; }
195 wxSemaError
Wait() { return WaitTimeout(SEM_INDEFINITE_WAIT
); }
196 wxSemaError
TryWait() { return WaitTimeout(SEM_IMMEDIATE_RETURN
); }
197 wxSemaError
WaitTimeout(unsigned long milliseconds
);
208 wxSemaphoreInternal::wxSemaphoreInternal(int initialcount
, int maxcount
)
213 // make it practically infinite
217 m_count
= initialcount
;
218 m_maxcount
= maxcount
;
219 ulrc
= ::DosCreateMutexSem(NULL
, &m_vMutex
, 0L, FALSE
);
222 wxLogLastError(_T("DosCreateMutexSem()"));
227 ulrc
= ::DosCreateEventSem(NULL
, &m_vEvent
, 0L, FALSE
);
230 wxLogLastError(_T("DosCreateEventSem()"));
231 ::DosCloseMutexSem(m_vMutex
);
236 ::DosPostEventSem(m_vEvent
);
239 wxSemaphoreInternal::~wxSemaphoreInternal()
243 if ( ::DosCloseEventSem(m_vEvent
) )
245 wxLogLastError(_T("DosCloseEventSem(semaphore)"));
247 if ( ::DosCloseMutexSem(m_vMutex
) )
249 wxLogLastError(_T("DosCloseMutexSem(semaphore)"));
256 wxSemaError
wxSemaphoreInternal::WaitTimeout(unsigned long ulMilliseconds
)
260 ulrc
= ::DosWaitEventSem(m_vEvent
, ulMilliseconds
);
267 if (ulMilliseconds
== SEM_IMMEDIATE_RETURN
)
270 return wxSEMA_TIMEOUT
;
273 wxLogLastError(_T("DosWaitEventSem(semaphore)"));
274 return wxSEMA_MISC_ERROR
;
276 ulrc
= :: DosRequestMutexSem(m_vMutex
, ulMilliseconds
);
284 case ERROR_TOO_MANY_SEM_REQUESTS
:
285 if (ulMilliseconds
== SEM_IMMEDIATE_RETURN
)
288 return wxSEMA_TIMEOUT
;
291 wxFAIL_MSG(wxT("DosRequestMutexSem(mutex)"));
292 return wxSEMA_MISC_ERROR
;
303 ::DosResetEventSem(m_vEvent
, &ulPostCount
);
305 ::DosReleaseMutexSem(m_vMutex
);
307 return wxSEMA_NO_ERROR
;
308 } while (ulMilliseconds
== SEM_INDEFINITE_WAIT
);
310 if (ulMilliseconds
== SEM_IMMEDIATE_RETURN
)
312 return wxSEMA_TIMEOUT
;
315 wxSemaError
wxSemaphoreInternal::Post()
318 ulrc
= ::DosRequestMutexSem(m_vMutex
, SEM_INDEFINITE_WAIT
);
319 if (ulrc
!= NO_ERROR
)
320 return wxSEMA_MISC_ERROR
;
322 if (m_count
< m_maxcount
)
325 ulrc
= ::DosPostEventSem(m_vEvent
);
328 ::DosReleaseMutexSem(m_vMutex
);
330 return wxSEMA_OVERFLOW
;
331 if ( ulrc
!= NO_ERROR
&& ulrc
!= ERROR_ALREADY_POSTED
)
333 wxLogLastError(_T("DosPostEventSem(semaphore)"));
335 return wxSEMA_MISC_ERROR
;
338 return wxSEMA_NO_ERROR
;
341 // ----------------------------------------------------------------------------
342 // wxThread implementation
343 // ----------------------------------------------------------------------------
345 // wxThreadInternal class
346 // ----------------------
348 class wxThreadInternal
351 inline wxThreadInternal()
354 m_eState
= STATE_NEW
;
355 m_nPriority
= WXTHREAD_DEFAULT_PRIORITY
;
363 // create a new (suspended) thread (for the given thread object)
364 bool Create( wxThread
* pThread
365 ,unsigned int uStackSize
368 // suspend/resume/terminate
371 inline void Cancel() { m_eState
= STATE_CANCELED
; }
374 inline void SetState(wxThreadState eState
) { m_eState
= eState
; }
375 inline wxThreadState
GetState() const { return m_eState
; }
378 void SetPriority(unsigned int nPriority
);
379 inline unsigned int GetPriority() const { return m_nPriority
; }
381 // thread handle and id
382 inline TID
GetHandle() const { return m_hThread
; }
383 TID
GetId() const { return m_hThread
; }
386 static void OS2ThreadStart(void* pParam
);
389 // Threads in OS/2 have only an ID, so m_hThread is both it's handle and ID
390 // PM also has no real Tls mechanism to index pointers by so we'll just
391 // keep track of the wxWidgets parent object here.
392 TID m_hThread
; // handle and ID of the thread
393 wxThreadState m_eState
; // state, see wxThreadState enum
394 unsigned int m_nPriority
; // thread priority in "wx" units
397 void wxThreadInternal::OS2ThreadStart( void * pParam
)
402 wxThread
*pThread
= (wxThread
*)pParam
;
404 // first of all, wait for the thread to be started.
405 pThread
->m_critsect
.Enter();
406 pThread
->m_critsect
.Leave();
407 // Now check whether we hadn't been cancelled already and don't
408 // start the user code at all in this case.
409 if ( pThread
->m_internal
->GetState() == STATE_EXITED
)
412 bWasCancelled
= true;
414 else // do run thread
416 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
419 traits
->InitializeGui(ulHab
);
420 dwRet
= (DWORD
)pThread
->Entry();
422 traits
->TerminateGui(ulHab
);
424 // enter m_critsect before changing the thread state
425 pThread
->m_critsect
.Enter();
427 bWasCancelled
= pThread
->m_internal
->GetState() == STATE_CANCELED
;
429 pThread
->m_internal
->SetState(STATE_EXITED
);
430 pThread
->m_critsect
.Leave();
434 // if the thread was cancelled (from Delete()), then it the handle is still
436 if (pThread
->IsDetached() && !bWasCancelled
)
441 //else: the joinable threads handle will be closed when Wait() is done
445 void wxThreadInternal::SetPriority(
446 unsigned int nPriority
449 // translate wxWidgets priority to the PM one
450 ULONG ulOS2_PriorityClass
;
451 ULONG ulOS2_SubPriority
;
454 m_nPriority
= nPriority
;
455 if (m_nPriority
<= 25)
456 ulOS2_PriorityClass
= PRTYC_IDLETIME
;
457 else if (m_nPriority
<= 50)
458 ulOS2_PriorityClass
= PRTYC_REGULAR
;
459 else if (m_nPriority
<= 75)
460 ulOS2_PriorityClass
= PRTYC_TIMECRITICAL
;
461 else if (m_nPriority
<= 100)
462 ulOS2_PriorityClass
= PRTYC_FOREGROUNDSERVER
;
465 wxFAIL_MSG(wxT("invalid value of thread priority parameter"));
466 ulOS2_PriorityClass
= PRTYC_REGULAR
;
468 ulOS2_SubPriority
= (ULONG
) (((m_nPriority
- 1) % 25 + 1) * 31.0 / 25);
469 ulrc
= ::DosSetPriority( PRTYS_THREAD
476 wxLogSysError(_("Can't set thread priority"));
480 bool wxThreadInternal::Create( wxThread
* pThread
,
481 unsigned int uStackSize
)
488 pThread
->m_critsect
.Enter();
489 tid
= _beginthread(wxThreadInternal::OS2ThreadStart
,
490 NULL
, uStackSize
, pThread
);
493 wxLogSysError(_("Can't create thread"));
498 if (m_nPriority
!= WXTHREAD_DEFAULT_PRIORITY
)
500 SetPriority(m_nPriority
);
506 bool wxThreadInternal::Suspend()
508 ULONG ulrc
= ::DosSuspendThread(m_hThread
);
512 wxLogSysError(_("Can not suspend thread %lu"), m_hThread
);
515 m_eState
= STATE_PAUSED
;
520 bool wxThreadInternal::Resume()
522 ULONG ulrc
= ::DosResumeThread(m_hThread
);
526 wxLogSysError(_("Can not resume thread %lu"), m_hThread
);
530 // don't change the state from STATE_EXITED because it's special and means
531 // we are going to terminate without running any user code - if we did it,
532 // the codei n Delete() wouldn't work
533 if ( m_eState
!= STATE_EXITED
)
535 m_eState
= STATE_RUNNING
;
544 wxThread
*wxThread::This()
546 wxThread
* pThread
= m_pThread
;
554 void wxThread::Yield()
559 int wxThread::GetCPUCount()
563 ulrc
= ::DosQuerySysInfo(26, 26, (void *)&CPUCount
, sizeof(ULONG
));
564 // QSV_NUMPROCESSORS(26) is typically not defined in header files
572 wxThreadIdType
wxThread::GetCurrentId()
577 ::DosGetInfoBlocks(&ptib
, &ppib
);
578 return (wxThreadIdType
) ptib
->tib_ptib2
->tib2_ultid
;
581 bool wxThread::SetConcurrency(size_t level
)
583 wxASSERT_MSG( IsMain(), _T("should only be called from the main thread") );
585 // ok only for the default one
589 // Don't know how to realize this on OS/2.
596 wxThread::wxThread(wxThreadKind kind
)
598 m_internal
= new wxThreadInternal();
600 m_isDetached
= kind
== wxTHREAD_DETACHED
;
603 wxThread::~wxThread()
608 // create/start thread
609 // -------------------
611 wxThreadError
wxThread::Create(
612 unsigned int uStackSize
615 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
617 if ( !m_internal
->Create(this, uStackSize
) )
618 return wxTHREAD_NO_RESOURCE
;
620 return wxTHREAD_NO_ERROR
;
623 wxThreadError
wxThread::Run()
625 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
627 if ( m_internal
->GetState() != STATE_NEW
)
629 // actually, it may be almost any state at all, not only STATE_RUNNING
630 return wxTHREAD_RUNNING
;
635 // suspend/resume thread
636 // ---------------------
638 wxThreadError
wxThread::Pause()
640 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
642 return m_internal
->Suspend() ? wxTHREAD_NO_ERROR
: wxTHREAD_MISC_ERROR
;
645 wxThreadError
wxThread::Resume()
647 if (m_internal
->GetState() == STATE_NEW
)
649 m_internal
->SetState(STATE_RUNNING
);
651 return wxTHREAD_NO_ERROR
;
654 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
656 return m_internal
->Resume() ? wxTHREAD_NO_ERROR
: wxTHREAD_MISC_ERROR
;
662 wxThread::ExitCode
wxThread::Wait()
664 // although under Windows we can wait for any thread, it's an error to
665 // wait for a detached one in wxWin API
666 wxCHECK_MSG( !IsDetached(), (ExitCode
)-1,
667 _T("can't wait for detached thread") );
668 ExitCode rc
= (ExitCode
)-1;
673 wxThreadError
wxThread::Delete(ExitCode
*pRc
)
677 // Delete() is always safe to call, so consider all possible states
679 // we might need to resume the thread, but we might also not need to cancel
680 // it if it doesn't run yet
681 bool shouldResume
= false,
685 // check if the thread already started to run
687 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
689 if ( m_internal
->GetState() == STATE_NEW
)
691 // WinThreadStart() will see it and terminate immediately, no need
692 // to cancel the thread - but we still need to resume it to let it
694 m_internal
->SetState(STATE_EXITED
);
696 Resume(); // it knows about STATE_EXITED special case
698 shouldCancel
= false;
701 // shouldResume is correctly set to false here
705 shouldResume
= IsPaused();
709 // resume the thread if it is paused
713 TID hThread
= m_internal
->GetHandle();
715 if ( isRunning
|| IsRunning())
719 // set flag for wxIsWaitingForThread()
720 gs_bWaitingForThread
= true;
723 // ask the thread to terminate
726 wxCriticalSectionLocker
lock(m_critsect
);
728 m_internal
->Cancel();
732 // we can't just wait for the thread to terminate because it might be
733 // calling some GUI functions and so it will never terminate before we
734 // process the Windows messages that result from these functions
735 DWORD result
= 0; // suppress warnings from broken compilers
740 // give the thread we're waiting for chance to do the GUI call
742 if ( (gs_nWaitingForGui
> 0) && wxGuiOwnedByMainThread() )
748 result
= ::DosWaitThread(&hThread
, DCWW_NOWAIT
);
749 // FIXME: We ought to have a message processing loop here!!
753 case ERROR_INTERRUPT
:
754 case ERROR_THREAD_NOT_TERMINATED
:
756 case ERROR_INVALID_THREADID
:
758 // thread we're waiting for just terminated
759 // or even does not exist any more.
763 wxFAIL_MSG(wxT("unexpected result of DosWaitThread"));
767 // event processing - needed if we are the main thread
768 // to give other threads a chance to do remaining GUI
769 // processing and terminate cleanly.
770 wxTheApp
->HandleSockets();
771 if (wxTheApp
->Pending())
772 if ( !wxTheApp
->DoMessage() )
774 // WM_QUIT received: kill the thread
777 return wxTHREAD_KILLED
;
784 } while ( result
!= NO_ERROR
);
786 // simply wait for the thread to terminate
788 // OTOH, even console apps create windows (in wxExecute, for WinSock
789 // &c), so may be use MsgWaitForMultipleObject() too here?
790 if ( ::DosWaitThread(&hThread
, DCWW_WAIT
) != NO_ERROR
)
792 wxFAIL_MSG(wxT("unexpected result of DosWaitThread"));
794 #endif // wxUSE_GUI/!wxUSE_GUI
798 gs_bWaitingForThread
= false;
803 // although the thread might be already in the EXITED state it might not
804 // have terminated yet and so we are not sure that it has actually
805 // terminated if the "if" above hadn't been taken
808 if ( !::GetExitCodeThread(hThread
, (LPDWORD
)&rc
) )
810 wxLogLastError(wxT("GetExitCodeThread"));
814 } while ( (DWORD
)rc
== STILL_ACTIVE
);
819 // if the thread exits normally, this is done in WinThreadStart, but in
820 // this case it would have been too early because
821 // MsgWaitForMultipleObject() would fail if the thread handle was
822 // closed while we were waiting on it, so we must do it here
829 return rc
== (ExitCode
)-1 ? wxTHREAD_MISC_ERROR
: wxTHREAD_NO_ERROR
;
832 wxThreadError
wxThread::Kill()
835 return wxTHREAD_NOT_RUNNING
;
837 ::DosKillThread(m_internal
->GetHandle());
842 return wxTHREAD_NO_ERROR
;
845 void wxThread::Exit(ExitCode
WXUNUSED(pStatus
))
849 wxFAIL_MSG(wxT("Couldn't return from DosExit()!"));
852 void wxThread::SetPriority(
856 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
858 m_internal
->SetPriority(nPrio
);
861 unsigned int wxThread::GetPriority() const
863 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
865 return m_internal
->GetPriority();
868 unsigned long wxThread::GetId() const
870 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
); // const_cast
872 return (unsigned long)m_internal
->GetId();
875 bool wxThread::IsRunning() const
877 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
879 return(m_internal
->GetState() == STATE_RUNNING
);
882 bool wxThread::IsAlive() const
884 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
886 return (m_internal
->GetState() == STATE_RUNNING
) ||
887 (m_internal
->GetState() == STATE_PAUSED
);
890 bool wxThread::IsPaused() const
892 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
894 return (m_internal
->GetState() == STATE_PAUSED
);
897 bool wxThread::TestDestroy()
899 wxCriticalSectionLocker
lock((wxCriticalSection
&)m_critsect
);
901 return m_internal
->GetState() == STATE_CANCELED
;
904 // ----------------------------------------------------------------------------
905 // Automatic initialization for thread module
906 // ----------------------------------------------------------------------------
908 class wxThreadModule
: public wxModule
911 virtual bool OnInit();
912 virtual void OnExit();
915 DECLARE_DYNAMIC_CLASS(wxThreadModule
)
918 IMPLEMENT_DYNAMIC_CLASS(wxThreadModule
, wxModule
)
920 bool wxThreadModule::OnInit()
922 gs_pCritsectWaitingForGui
= new wxCriticalSection();
924 gs_pCritsectGui
= new wxCriticalSection();
925 gs_pCritsectGui
->Enter();
927 wxThread::ms_idMainThread
= wxThread::GetCurrentId();
932 void wxThreadModule::OnExit()
936 gs_pCritsectGui
->Leave();
937 #if (!(defined(__VISAGECPP__) && (__IBMCPP__ < 400 || __IBMC__ < 400 )))
938 delete gs_pCritsectGui
;
940 gs_pCritsectGui
= NULL
;
943 #if (!(defined(__VISAGECPP__) && (__IBMCPP__ < 400 || __IBMC__ < 400 )))
944 wxDELETE(gs_pCritsectWaitingForGui
);
948 // ----------------------------------------------------------------------------
950 // ----------------------------------------------------------------------------
952 // wake up the main thread if it's in ::GetMessage()
953 void WXDLLEXPORT
wxWakeUpMainThread()
956 if ( !::WinPostQueueMsg(wxTheApp
->m_hMq
, WM_NULL
, 0, 0) )
958 // should never happen
959 wxLogLastError(wxT("WinPostMessage(WM_NULL)"));
964 void wxMutexGuiEnterImpl()
966 // this would dead lock everything...
967 wxASSERT_MSG( !wxThread::IsMain(),
968 wxT("main thread doesn't want to block in wxMutexGuiEnter()!") );
970 // the order in which we enter the critical sections here is crucial!!
972 // set the flag telling to the main thread that we want to do some GUI
974 wxCriticalSectionLocker
enter(*gs_pCritsectWaitingForGui
);
979 wxWakeUpMainThread();
981 // now we may block here because the main thread will soon let us in
982 // (during the next iteration of OnIdle())
983 gs_pCritsectGui
->Enter();
986 void wxMutexGuiLeaveImpl()
988 wxCriticalSectionLocker
enter(*gs_pCritsectWaitingForGui
);
990 if ( wxThread::IsMain() )
992 gs_bGuiOwnedByMainThread
= false;
996 // decrement the number of waiters now
997 wxASSERT_MSG(gs_nWaitingForGui
> 0,
998 wxT("calling wxMutexGuiLeave() without entering it first?") );
1000 gs_nWaitingForGui
--;
1002 wxWakeUpMainThread();
1005 gs_pCritsectGui
->Leave();
1008 void WXDLLEXPORT
wxMutexGuiLeaveOrEnter()
1010 wxASSERT_MSG( wxThread::IsMain(),
1011 wxT("only main thread may call wxMutexGuiLeaveOrEnter()!") );
1013 wxCriticalSectionLocker
enter(*gs_pCritsectWaitingForGui
);
1015 if (gs_nWaitingForGui
== 0)
1017 // no threads are waiting for GUI - so we may acquire the lock without
1018 // any danger (but only if we don't already have it)
1019 if (!wxGuiOwnedByMainThread())
1021 gs_pCritsectGui
->Enter();
1023 gs_bGuiOwnedByMainThread
= true;
1025 //else: already have it, nothing to do
1029 // some threads are waiting, release the GUI lock if we have it
1030 if (wxGuiOwnedByMainThread())
1034 //else: some other worker thread is doing GUI
1038 bool WXDLLEXPORT
wxGuiOwnedByMainThread()
1040 return gs_bGuiOwnedByMainThread
;
1043 bool WXDLLEXPORT
wxIsWaitingForThread()
1045 return gs_bWaitingForThread
;
1048 // ----------------------------------------------------------------------------
1049 // include common implementation code
1050 // ----------------------------------------------------------------------------
1052 #include "wx/thrimpl.cpp"