+bool wxThreadModule::OnInit()
+{
+ // allocate TLS index for storing the pointer to the current thread
+ s_tlsThisThread = ::TlsAlloc();
+ if ( s_tlsThisThread == 0xFFFFFFFF )
+ {
+ // in normal circumstances it will only happen if all other
+ // TLS_MINIMUM_AVAILABLE (>= 64) indices are already taken - in other
+ // words, this should never happen
+ wxLogSysError(_("Thread module initialization failed: "
+ "impossible to allocate index in thread "
+ "local storage"));
+
+ return FALSE;
+ }
+
+ // main thread doesn't have associated wxThread object, so store 0 in the
+ // TLS instead
+ if ( !::TlsSetValue(s_tlsThisThread, (LPVOID)0) )
+ {
+ ::TlsFree(s_tlsThisThread);
+ s_tlsThisThread = 0xFFFFFFFF;
+
+ wxLogSysError(_("Thread module initialization failed: "
+ "can not store value in thread local storage"));
+
+ return FALSE;
+ }
+
+ s_critsectWaitingForGui = new wxCriticalSection();
+
+ s_critsectGui = new wxCriticalSection();
+ s_critsectGui->Enter();
+
+ // no error return for GetCurrentThreadId()
+ s_idMainThread = ::GetCurrentThreadId();
+
+ return TRUE;
+}
+
+void wxThreadModule::OnExit()
+{
+ if ( !::TlsFree(s_tlsThisThread) )
+ {
+ wxLogLastError("TlsFree failed.");
+ }
+
+ if ( s_critsectGui )
+ {
+ s_critsectGui->Leave();
+ delete s_critsectGui;
+ s_critsectGui = NULL;
+ }
+
+ wxDELETE(s_critsectWaitingForGui);
+}
+
+// ----------------------------------------------------------------------------
+// under Windows, these functions are implemented usign a critical section and
+// not a mutex, so the names are a bit confusing
+// ----------------------------------------------------------------------------
+
+void WXDLLEXPORT wxMutexGuiEnter()
+{
+ // this would dead lock everything...
+ wxASSERT_MSG( !wxThread::IsMain(),
+ "main thread doesn't want to block in wxMutexGuiEnter()!" );
+
+ // the order in which we enter the critical sections here is crucial!!
+
+ // set the flag telling to the main thread that we want to do some GUI
+ {
+ wxCriticalSectionLocker enter(*s_critsectWaitingForGui);
+
+ s_nWaitingForGui++;
+ }
+
+ wxWakeUpMainThread();
+
+ // now we may block here because the main thread will soon let us in
+ // (during the next iteration of OnIdle())
+ s_critsectGui->Enter();
+}
+
+void WXDLLEXPORT wxMutexGuiLeave()
+{
+ 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;
+}
+
+// wake up the main thread if it's in ::GetMessage()
+void WXDLLEXPORT wxWakeUpMainThread()
+{
+ // sending any message would do - hopefully WM_NULL is harmless enough
+ if ( !::PostThreadMessage(s_idMainThread, WM_NULL, 0, 0) )
+ {
+ // should never happen
+ wxLogLastError("PostThreadMessage(WM_NULL)");
+ }
+}
+
+bool WXDLLEXPORT wxIsWaitingForThread()
+{
+ return s_waitingForThread;
+}
+
+#endif // wxUSE_THREADS