Index: glib/gmain.c
===================================================================
RCS file: /cvs/gnome/glib/glib/gmain.c,v
retrieving revision 1.133
diff -u -B -b -p -r1.133 gmain.c
--- glib/gmain.c	4 Nov 2005 20:33:34 -0000	1.133
+++ glib/gmain.c	7 Nov 2005 15:48:29 -0000
@@ -139,7 +139,7 @@ struct _GMainContext
 #endif /* G_OS_WIN32 */
 
   GPollFD wake_up_rec;
-  gboolean poll_waiting;
+  gint poll_waiting;
 
 /* Flag indicating whether the set of fd's changed during a poll */
   gboolean poll_changed;
@@ -2170,14 +2170,13 @@ g_main_context_prepare (GMainContext *co
     }
 
 #ifdef G_THREADS_ENABLED
-  if (context->poll_waiting)
+  if (!g_atomic_int_compare_and_exchange (&context->poll_waiting, 0, 1))
     {
       g_warning("g_main_context_prepare(): main loop already active in another thread");
       UNLOCK_CONTEXT (context);
       return FALSE;
     }
   
-  context->poll_waiting = TRUE;
 #endif /* G_THREADS_ENABLED */
 
 #if 0
@@ -2363,15 +2362,13 @@ g_main_context_check (GMainContext *cont
     }
   
 #ifdef G_THREADS_ENABLED
-  if (!context->poll_waiting)
+  if (!g_atomic_int_compare_and_exchange (&context->poll_waiting, 1, 0))
     {
 #ifndef G_OS_WIN32
       gchar a;
       read (context->wake_up_pipe[0], &a, 1);
 #endif
     }
-  else
-    context->poll_waiting = FALSE;
 
   /* If the set of poll file descriptors changed, bail out
    * and let the main loop rerun
@@ -3124,15 +3121,17 @@ static void
 g_main_context_wakeup_unlocked (GMainContext *context)
 {
 #ifdef G_THREADS_ENABLED
-  if (g_thread_supported() && context->poll_waiting)
+  if (g_thread_supported())
+    {
+      if (g_atomic_int_compare_and_exchange (&context->poll_waiting, 1, 0)) 
     {
-      context->poll_waiting = FALSE;
 #ifndef G_OS_WIN32
       write (context->wake_up_pipe[1], "A", 1);
 #else
       ReleaseSemaphore (context->wake_up_semaphore, 1, NULL);
 #endif
     }
+    }
 #endif
 }
 
@@ -3151,9 +3150,7 @@ g_main_context_wakeup (GMainContext *con
   
   g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
 
-  LOCK_CONTEXT (context);
   g_main_context_wakeup_unlocked (context);
-  UNLOCK_CONTEXT (context);
 }
 
 /* Timeouts */
