+ wxCriticalSectionLocker enter(*s_critsectWaitingForGui);
+
+ if ( wxThread::IsMain() )
+ {
+ s_bGuiOwnedByMainThread = FALSE;
+ }
+ else
+ {
+ // decrement the number of waiters now
+ wxASSERT_MSG( s_nWaitingForGui > 0,
+ "calling wxMutexGuiLeave() without entering it first?" );
+
+ s_nWaitingForGui--;
+
+ wxWakeUpMainThread();
+ }
+
+ s_critsectGui->Leave();
+}
+
+void WXDLLEXPORT wxMutexGuiLeaveOrEnter()
+{
+ wxASSERT_MSG( wxThread::IsMain(),
+ "only main thread may call wxMutexGuiLeaveOrEnter()!" );
+
+ wxCriticalSectionLocker enter(*s_critsectWaitingForGui);
+
+ if ( s_nWaitingForGui == 0 )
+ {
+ // no threads are waiting for GUI - so we may acquire the lock without
+ // any danger (but only if we don't already have it)
+ if ( !wxGuiOwnedByMainThread() )
+ {
+ s_critsectGui->Enter();
+
+ s_bGuiOwnedByMainThread = TRUE;
+ }
+ //else: already have it, nothing to do
+ }
+ else
+ {
+ // some threads are waiting, release the GUI lock if we have it
+ if ( wxGuiOwnedByMainThread() )
+ {
+ wxMutexGuiLeave();
+ }
+ //else: some other worker thread is doing GUI
+ }
+}
+
+bool WXDLLEXPORT wxGuiOwnedByMainThread()
+{
+ return s_bGuiOwnedByMainThread;