[glib] GMain, ThreadPool: embed GCond in struct



commit 518feb45eb1522f3e80125372d78b225b4e2e6e1
Author: Ryan Lortie <desrt desrt ca>
Date:   Mon Oct 3 23:52:13 2011 -0400

    GMain, ThreadPool: embed GCond in struct
    
    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       |   24 ++++++++----------------
 glib/gthreadpool.c |   14 +++++---------
 2 files changed, 13 insertions(+), 25 deletions(-)
---
diff --git a/glib/gmain.c b/glib/gmain.c
index 797aea9..84c5477 100644
--- a/glib/gmain.c
+++ b/glib/gmain.c
@@ -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);
 }
diff --git a/glib/gthreadpool.c b/glib/gthreadpool.c
index ca3c4f9..685cd17 100644
--- a/glib/gthreadpool.c
+++ b/glib/gthreadpool.c
@@ -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);
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]