 ---
  configure.ac     |   2 +-
  glib/Makefile.am |   6 +-
- glib/gmain.c     | 982 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
- 3 files changed, 988 insertions(+), 2 deletions(-)
+ glib/gmain.c     | 985 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ 3 files changed, 991 insertions(+), 2 deletions(-)
 
 diff --git a/configure.ac b/configure.ac
-index 17c1509..5194f69 100644
+index 60e4938..51de9f2 100644
 --- a/configure.ac
 +++ b/configure.ac
 @@ -244,7 +244,7 @@ AM_CONDITIONAL(OS_COCOA, [test "$glib_have_cocoa" = "yes"])

+  COCOA_POLLING_DESCRIPTORS,
+} CocoaSelectThreadState;
+
+static const char *const cocoa_state_names[]  = {
+  "BEFORE_START",
+  "WAITING",

+          }
+      }
+#endif
+
       TRACE (GLIB_MAIN_CONTEXT_ACQUIRE (context, TRUE  /* success */));
     }
 
@@ -3323,6 +3435,24 @@ g_main_context_release (GMainContext *context)
 	  if (!loop_internal_waiter)
 	    g_mutex_unlock (waiter->mutex);
 	}
+
+#ifdef HAVE_COCOA
+      if (g_atomic_pointer_compare_and_exchange (&cocoa_main_context, context, NULL))
+        {
+#ifdef G_MAIN_POLL_DEBUG
+          g_print ("%p stopped being the cocoa default main context\n", context);
+#endif
+          context->poll_func = cocoa_old_poll_func;
+          cocoa_old_poll_func = NULL;
+
+          CFRunLoopRemoveObserver (CFRunLoopGetMain (), cocoa_observer, kCFRunLoopCommonModes);
+          CFRelease (cocoa_observer);
+          cocoa_observer = NULL;
+
+          [cocoa_autorelease_pool release];
+          cocoa_autorelease_pool = NULL;
+        }
+#endif
     }
 
   UNLOCK_CONTEXT (context); 
@@ -5816,3 +5946,858 @@ g_get_worker_context (void)
 
   return glib_worker_context;
 }
