GMain, ThreadPool: embed GCond in struct
authorRyan Lortie <desrt@desrt.ca>
Tue, 4 Oct 2011 03:52:13 +0000 (23:52 -0400)
committerRyan Lortie <desrt@desrt.ca>
Tue, 4 Oct 2011 15:13:46 +0000 (11:13 -0400)
Use an embedded GCond and g_cond_init()/clear() instead of a pointer
with g_cond_new() and _free().

https://bugzilla.gnome.org/show_bug.cgi?id=660739

glib/gmain.c
glib/gthreadpool.c

index 797aea9..84c5477 100644 (file)
@@ -221,7 +221,7 @@ struct _GMainContext
    * and the list of poll records
    */
   GMutex mutex;
-  GCond *cond;
+  GCond cond;
   GThread *owner;
   guint owner_count;
   GSList *waiters;
@@ -496,9 +496,7 @@ g_main_context_unref (GMainContext *context)
   poll_rec_list_free (context, context->poll_records);
 
   g_wakeup_free (context->wakeup);
-
-  if (context->cond != NULL)
-    g_cond_free (context->cond);
+  g_cond_clear (&context->cond);
 
   g_free (context);
 }
@@ -543,6 +541,7 @@ g_main_context_new (void)
   context = g_new0 (GMainContext, 1);
 
   g_mutex_init (&context->mutex);
+  g_cond_init (&context->cond);
 
   context->owner = NULL;
   context->waiters = NULL;
@@ -2952,12 +2951,9 @@ g_main_context_iterate (GMainContext *context,
       if (!block)
        return FALSE;
 
-      if (!context->cond)
-       context->cond = g_cond_new ();
-
       got_ownership = g_main_context_wait (context,
-                                          context->cond,
-                                          &context->mutex);
+                                           &context->cond,
+                                           &context->mutex);
 
       if (!got_ownership)
        return FALSE;
@@ -3162,13 +3158,10 @@ g_main_loop_run (GMainLoop *loop)
       if (!loop->is_running)
        loop->is_running = TRUE;
 
-      if (!loop->context->cond)
-       loop->context->cond = g_cond_new ();
-          
       while (loop->is_running && !got_ownership)
        got_ownership = g_main_context_wait (loop->context,
-                                            loop->context->cond,
-                                            &loop->context->mutex);
+                                             &loop->context->cond,
+                                             &loop->context->mutex);
       
       if (!loop->is_running)
        {
@@ -3223,8 +3216,7 @@ g_main_loop_quit (GMainLoop *loop)
   loop->is_running = FALSE;
   g_wakeup_signal (loop->context->wakeup);
 
-  if (loop->context->cond)
-    g_cond_broadcast (loop->context->cond);
+  g_cond_broadcast (&loop->context->cond);
 
   UNLOCK_CONTEXT (loop->context);
 }
index ca3c4f9..685cd17 100644 (file)
@@ -90,7 +90,7 @@ struct _GRealThreadPool
 {
   GThreadPool pool;
   GAsyncQueue *queue;
-  GCond *cond;
+  GCond cond;
   gint max_threads;
   gint num_threads;
   gboolean running;
@@ -362,7 +362,7 @@ g_thread_pool_thread_proxy (gpointer data)
                    * immediately, inform the waiting thread of a change
                    * of the thread pool state.
                    */
-                  g_cond_broadcast (pool->cond);
+                  g_cond_broadcast (&pool->cond);
                 }
             }
 
@@ -485,7 +485,7 @@ g_thread_pool_new (GFunc      func,
   retval->pool.user_data = user_data;
   retval->pool.exclusive = exclusive;
   retval->queue = g_async_queue_new ();
-  retval->cond = NULL;
+  g_cond_init (&retval->cond);
   retval->max_threads = max_threads;
   retval->num_threads = 0;
   retval->running = TRUE;
@@ -776,11 +776,9 @@ g_thread_pool_free (GThreadPool *pool,
 
   if (wait_)
     {
-      real->cond = g_cond_new ();
-
       while (g_async_queue_length_unlocked (real->queue) != -real->num_threads &&
              !(immediate && real->num_threads == 0))
-        g_cond_wait (real->cond, _g_async_queue_get_mutex (real->queue));
+        g_cond_wait (&real->cond, _g_async_queue_get_mutex (real->queue));
     }
 
   if (immediate || g_async_queue_length_unlocked (real->queue) == -real->num_threads)
@@ -812,9 +810,7 @@ g_thread_pool_free_internal (GRealThreadPool* pool)
   g_return_if_fail (pool->num_threads == 0);
 
   g_async_queue_unref (pool->queue);
-
-  if (pool->cond)
-    g_cond_free (pool->cond);
+  g_cond_clear (&pool->cond);
 
   g_free (pool);
 }