+bool wxThreadModule::OnInit()
+{
+ s_pCritsectWaitingForGui = new wxCriticalSection();
+
+ s_pCritsectGui = new wxCriticalSection();
+ s_pCritsectGui->Enter();
+
+ PTIB ptib;
+ PPIB ppib;
+
+ ::DosGetInfoBlocks(&ptib, &ppib);
+
+ s_ulIdMainThread = ptib->tib_ptib2->tib2_ultid;
+ return TRUE;
+}
+
+void wxThreadModule::OnExit()
+{
+ if (s_pCritsectGui)
+ {
+ s_pCritsectGui->Leave();
+ delete s_pCritsectGui;
+ s_pCritsectGui = NULL;
+ }
+
+ wxDELETE(s_pCritsectWaitingForGui);
+}
+
+// ----------------------------------------------------------------------------
+// Helper functions
+// ----------------------------------------------------------------------------
+
+// Does nothing under OS/2 [for now]
+void WXDLLEXPORT wxWakeUpMainThread()
+{
+}
+
+void WXDLLEXPORT wxMutexGuiLeave()
+{
+ wxCriticalSectionLocker enter(*s_pCritsectWaitingForGui);
+
+ if ( wxThread::IsMain() )
+ {
+ s_bGuiOwnedByMainThread = FALSE;
+ }
+ else
+ {
+ // decrement the number of waiters now
+ wxASSERT_MSG( s_nWaitingForGui > 0,
+ wxT("calling wxMutexGuiLeave() without entering it first?") );
+
+ s_nWaitingForGui--;
+
+ wxWakeUpMainThread();
+ }
+
+ s_pCritsectGui->Leave();
+}
+
+void WXDLLEXPORT wxMutexGuiLeaveOrEnter()
+{
+ wxASSERT_MSG( wxThread::IsMain(),
+ wxT("only main thread may call wxMutexGuiLeaveOrEnter()!") );
+
+ wxCriticalSectionLocker enter(*s_pCritsectWaitingForGui);
+
+ 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_pCritsectGui->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;
+}
+