From 3d41d1d2db13cf9f2131c51307f27368d9cd669d Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Fri, 21 Dec 2012 09:35:27 -0500 Subject: [PATCH] OtWorkerQueue: Don't exit until we've fulfilled initial requests This counts as an implicit "hold" on the first items, which we must complete. Otherwise there are race conditions where the queue may signal idle when in fact the main process is still working. --- src/libotutil/ot-worker-queue.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/libotutil/ot-worker-queue.c b/src/libotutil/ot-worker-queue.c index 8bf71bee..79d48b71 100644 --- a/src/libotutil/ot-worker-queue.c +++ b/src/libotutil/ot-worker-queue.c @@ -35,6 +35,7 @@ struct OtWorkerQueue { char *thread_name; + gboolean complete; gboolean destroyed; GThread *worker; @@ -71,6 +72,7 @@ void ot_worker_queue_start (OtWorkerQueue *queue) { queue->worker = g_thread_new (queue->thread_name, ot_worker_queue_thread_main, queue); + ot_worker_queue_push (queue, queue); /* Self marks end of (initial) queue */ } void @@ -129,7 +131,8 @@ ot_worker_queue_thread_main (gpointer user_data) while (!g_queue_peek_tail_link (&queue->queue)) { - if (queue->idle_callback && g_atomic_int_get (&queue->holds) == 0) + if (queue->idle_callback && queue->complete && + g_atomic_int_get (&queue->holds) == 0) g_main_context_invoke (queue->idle_context, invoke_idle_callback, queue); @@ -143,7 +146,10 @@ ot_worker_queue_thread_main (gpointer user_data) if (!item) break; - queue->work_func (item, queue->work_data); + if (item == queue) + queue->complete = TRUE; + else + queue->work_func (item, queue->work_data); } return NULL;