+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: cannot store value in thread local storage"));
+
+ return false;
+ }
+
+ gs_critsectWaitingForGui = new wxCriticalSection();
+
+ gs_critsectGui = new wxCriticalSection();
+ gs_critsectGui->Enter();
+
+ gs_critsectThreadDelete = new wxCriticalSection;
+
+ wxThread::ms_idMainThread = wxThread::GetCurrentId();
+
+ return true;
+}
+
+void wxThreadModule::OnExit()
+{
+ if ( !::TlsFree(gs_tlsThisThread) )
+ {
+ wxLogLastError(wxT("TlsFree failed."));
+ }
+
+ wxDELETE(gs_critsectThreadDelete);
+
+ if ( gs_critsectGui )
+ {
+ gs_critsectGui->Leave();
+ wxDELETE(gs_critsectGui);
+ }
+
+ wxDELETE(gs_critsectWaitingForGui);
+}
+
+// ----------------------------------------------------------------------------
+// under Windows, these functions are implemented using a critical section and
+// not a mutex, so the names are a bit confusing
+// ----------------------------------------------------------------------------
+
+void wxMutexGuiEnterImpl()
+{
+ // 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 wxMutexGuiLeaveImpl()
+{
+ 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 WXDLLIMPEXP_BASE 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 WXDLLIMPEXP_BASE wxGuiOwnedByMainThread()
+{
+ return gs_bGuiOwnedByMainThread;
+}
+
+// wake up the main thread if it's in ::GetMessage()
+void WXDLLIMPEXP_BASE wxWakeUpMainThread()
+{
+ // sending any message would do - hopefully WM_NULL is harmless enough
+ if ( !::PostThreadMessage(wxThread::GetMainId(), WM_NULL, 0, 0) )
+ {
+ // should never happen
+ wxLogLastError(wxT("PostThreadMessage(WM_NULL)"));
+ }
+}
+
+bool WXDLLIMPEXP_BASE wxIsWaitingForThread()
+{
+ return gs_waitingForThread;
+}
+
+// ----------------------------------------------------------------------------
+// include common implementation code
+// ----------------------------------------------------------------------------
+
+#include "wx/thrimpl.cpp"
+
+#endif // wxUSE_THREADS