diff --git a/core/idle.c b/core/idle.c index 9c405384..5c6986a4 100644 --- a/core/idle.c +++ b/core/idle.c @@ -10,8 +10,9 @@ struct idle_task { void *idle_data; }; -static GThreadPool *idle_pool = NULL; -static GQueue idle_queue = G_QUEUE_INIT; +static GThreadPool *idle_pool = NULL; +static GQueue idle_queue = G_QUEUE_INIT; +static enum idle_sync_t idle_mode = IDLE_SYNC; static unsigned int queued = 0; static unsigned int serviced = 0; @@ -37,10 +38,9 @@ void __idle_thread(gpointer task, gpointer data) } -void idle_init(enum idle_sync_t idle_sync) +void idle_init(enum idle_sync_t sync) { - if (idle_sync == IDLE_ASYNC) - idle_pool = g_thread_pool_new(__idle_thread, NULL, 1, true, NULL); + idle_mode = sync; } void idle_deinit() @@ -52,8 +52,10 @@ void idle_deinit() g_free(task); } - if (idle_pool) - g_thread_pool_free(idle_pool, true, false); + if (idle_pool) { + g_thread_pool_free(idle_pool, true, true); + idle_pool = NULL; + } queued = 0; serviced = 0; @@ -63,7 +65,7 @@ void idle_schedule(enum idle_sync_t sync, bool (*func)(void *), void *data) { struct idle_task *task; - if (sync == IDLE_ASYNC && !idle_pool) + if (sync == IDLE_ASYNC && idle_mode == IDLE_SYNC) return; task = g_malloc(sizeof(struct idle_task)); @@ -72,8 +74,12 @@ void idle_schedule(enum idle_sync_t sync, bool (*func)(void *), void *data) if (sync == IDLE_SYNC) g_queue_push_tail(&idle_queue, task); - else + else { + if (!idle_pool) + idle_pool = g_thread_pool_new(__idle_thread, NULL, 1, + false, NULL); g_thread_pool_push(idle_pool, task, NULL); + } g_atomic_int_inc(&queued); }