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
This commit is contained in:
Ryan Lortie 2011-10-03 23:52:13 -04:00
parent 19cd57d4f3
commit 518feb45eb
2 changed files with 13 additions and 25 deletions

View File

@ -221,7 +221,7 @@ struct _GMainContext
* and the list of poll records * and the list of poll records
*/ */
GMutex mutex; GMutex mutex;
GCond *cond; GCond cond;
GThread *owner; GThread *owner;
guint owner_count; guint owner_count;
GSList *waiters; GSList *waiters;
@ -496,9 +496,7 @@ g_main_context_unref (GMainContext *context)
poll_rec_list_free (context, context->poll_records); poll_rec_list_free (context, context->poll_records);
g_wakeup_free (context->wakeup); g_wakeup_free (context->wakeup);
g_cond_clear (&context->cond);
if (context->cond != NULL)
g_cond_free (context->cond);
g_free (context); g_free (context);
} }
@ -543,6 +541,7 @@ g_main_context_new (void)
context = g_new0 (GMainContext, 1); context = g_new0 (GMainContext, 1);
g_mutex_init (&context->mutex); g_mutex_init (&context->mutex);
g_cond_init (&context->cond);
context->owner = NULL; context->owner = NULL;
context->waiters = NULL; context->waiters = NULL;
@ -2952,11 +2951,8 @@ g_main_context_iterate (GMainContext *context,
if (!block) if (!block)
return FALSE; return FALSE;
if (!context->cond)
context->cond = g_cond_new ();
got_ownership = g_main_context_wait (context, got_ownership = g_main_context_wait (context,
context->cond, &context->cond,
&context->mutex); &context->mutex);
if (!got_ownership) if (!got_ownership)
@ -3162,12 +3158,9 @@ g_main_loop_run (GMainLoop *loop)
if (!loop->is_running) if (!loop->is_running)
loop->is_running = TRUE; loop->is_running = TRUE;
if (!loop->context->cond)
loop->context->cond = g_cond_new ();
while (loop->is_running && !got_ownership) while (loop->is_running && !got_ownership)
got_ownership = g_main_context_wait (loop->context, got_ownership = g_main_context_wait (loop->context,
loop->context->cond, &loop->context->cond,
&loop->context->mutex); &loop->context->mutex);
if (!loop->is_running) if (!loop->is_running)
@ -3223,8 +3216,7 @@ g_main_loop_quit (GMainLoop *loop)
loop->is_running = FALSE; loop->is_running = FALSE;
g_wakeup_signal (loop->context->wakeup); 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); UNLOCK_CONTEXT (loop->context);
} }

View File

@ -90,7 +90,7 @@ struct _GRealThreadPool
{ {
GThreadPool pool; GThreadPool pool;
GAsyncQueue *queue; GAsyncQueue *queue;
GCond *cond; GCond cond;
gint max_threads; gint max_threads;
gint num_threads; gint num_threads;
gboolean running; gboolean running;
@ -362,7 +362,7 @@ g_thread_pool_thread_proxy (gpointer data)
* immediately, inform the waiting thread of a change * immediately, inform the waiting thread of a change
* of the thread pool state. * 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.user_data = user_data;
retval->pool.exclusive = exclusive; retval->pool.exclusive = exclusive;
retval->queue = g_async_queue_new (); retval->queue = g_async_queue_new ();
retval->cond = NULL; g_cond_init (&retval->cond);
retval->max_threads = max_threads; retval->max_threads = max_threads;
retval->num_threads = 0; retval->num_threads = 0;
retval->running = TRUE; retval->running = TRUE;
@ -776,11 +776,9 @@ g_thread_pool_free (GThreadPool *pool,
if (wait_) if (wait_)
{ {
real->cond = g_cond_new ();
while (g_async_queue_length_unlocked (real->queue) != -real->num_threads && while (g_async_queue_length_unlocked (real->queue) != -real->num_threads &&
!(immediate && real->num_threads == 0)) !(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) 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_return_if_fail (pool->num_threads == 0);
g_async_queue_unref (pool->queue); g_async_queue_unref (pool->queue);
g_cond_clear (&pool->cond);
if (pool->cond)
g_cond_free (pool->cond);
g_free (pool); g_free (pool);
} }