+bool wxThreadModule::OnInit()
+{
+ // allocate TLS index for storing the pointer to the current thread
+ gs_tlsThisThread = ::TlsAlloc();
+ if ( gs_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(gs_tlsThisThread, (LPVOID)0) )
+ {
+ ::TlsFree(gs_tlsThisThread);
+ gs_tlsThisThread = 0xFFFFFFFF;
+
+ wxLogSysError(_("Thread module initialization failed: can not store value in thread local storage"));
+
+ return FALSE;
+ }
+
+ gs_critsectWaitingForGui = new wxCriticalSection();
+
+ gs_critsectGui = new wxCriticalSection();
+ gs_critsectGui->Enter();
+
+ // no error return for GetCurrentThreadId()
+ gs_idMainThread = ::GetCurrentThreadId();
+
+ return TRUE;
+}
+
+void wxThreadModule::OnExit()
+{
+ if ( !::TlsFree(gs_tlsThisThread) )
+ {
+ wxLogLastError(wxT("TlsFree failed."));
+ }
+
+ if ( gs_critsectGui )
+ {
+ gs_critsectGui->Leave();
+ delete gs_critsectGui;
+ gs_critsectGui = NULL;
+ }
+
+ delete gs_critsectWaitingForGui;
+ gs_critsectWaitingForGui = NULL;
+}
+
+// ----------------------------------------------------------------------------
+// under Windows, these functions are implemented using 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(),
+ wxT("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(*gs_critsectWaitingForGui);
+
+ gs_nWaitingForGui++;
+ }
+
+ wxWakeUpMainThread();
+
+ // now we may block here because the main thread will soon let us in
+ // (during the next iteration of OnIdle())
+ gs_critsectGui->Enter();
+}
+
+void WXDLLEXPORT wxMutexGuiLeave()
+{
+ wxCriticalSectionLocker enter(*gs_critsectWaitingForGui);
+
+ if ( wxThread::IsMain() )
+ {
+ gs_bGuiOwnedByMainThread = FALSE;
+ }
+ else
+ {
+ // decrement the number of threads waiting for GUI access now
+ wxASSERT_MSG( gs_nWaitingForGui > 0,
+ wxT("calling wxMutexGuiLeave() without entering it first?") );
+
+ gs_nWaitingForGui--;
+
+ wxWakeUpMainThread();
+ }
+
+ gs_critsectGui->Leave();
+}
+
+void WXDLLEXPORT wxMutexGuiLeaveOrEnter()
+{
+ wxASSERT_MSG( wxThread::IsMain(),
+ wxT("only main thread may call wxMutexGuiLeaveOrEnter()!") );
+
+ wxCriticalSectionLocker enter(*gs_critsectWaitingForGui);
+
+ if ( gs_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() )
+ {
+ gs_critsectGui->Enter();
+
+ gs_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 gs_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(gs_idMainThread, WM_NULL, 0, 0) )
+ {
+ // should never happen
+ wxLogLastError(wxT("PostThreadMessage(WM_NULL)"));
+ }
+}
+
+bool WXDLLEXPORT wxIsWaitingForThread()
+{
+ return gs_waitingForThread;
+}
+
+// ----------------------------------------------------------------------------
+// include common implementation code
+// ----------------------------------------------------------------------------
+
+#include "wx/thrimpl.cpp"
+
+#endif // wxUSE_THREADS
+