+// TODO Move to thread_osx.cpp
+
+#if wxUSE_THREADS
+
+// ----------------------------------------------------------------------------
+// GUI Serialization copied from MSW implementation
+// ----------------------------------------------------------------------------
+
+// if it's false, some secondary thread is holding the GUI lock
+static bool gs_bGuiOwnedByMainThread = true;
+
+// critical section which controls access to all GUI functions: any secondary
+// thread (i.e. except the main one) must enter this crit section before doing
+// any GUI calls
+static wxCriticalSection *gs_critsectGui = NULL;
+
+// critical section which protects gs_nWaitingForGui variable
+static wxCriticalSection *gs_critsectWaitingForGui = NULL;
+
+// number of threads waiting for GUI in wxMutexGuiEnter()
+static size_t gs_nWaitingForGui = 0;
+
+void wxOSXThreadModuleOnInit()
+{
+ gs_critsectWaitingForGui = new wxCriticalSection();
+ gs_critsectGui = new wxCriticalSection();
+ gs_critsectGui->Enter();
+}
+
+
+void wxOSXThreadModuleOnExit()
+{
+ if ( gs_critsectGui )
+ {
+ if ( !wxGuiOwnedByMainThread() )
+ {
+ gs_critsectGui->Enter();
+ gs_bGuiOwnedByMainThread = true;
+ }
+
+ gs_critsectGui->Leave();
+ wxDELETE(gs_critsectGui);
+ }
+
+ wxDELETE(gs_critsectWaitingForGui);
+}