From b00776b09e3a56a439ff17ee7f259d5d3d283af6 Mon Sep 17 00:00:00 2001 From: aliaspider Date: Mon, 22 May 2017 11:18:42 +0100 Subject: [PATCH] allow easier scheduling of tasks in single threaded mode by adding a condition callback to task_queue_wait. --- libretro-common/include/queues/task_queue.h | 9 +++++++-- libretro-common/queues/task_queue.c | 15 ++++++++------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/libretro-common/include/queues/task_queue.h b/libretro-common/include/queues/task_queue.h index 5910c15dd6..28322ad6be 100644 --- a/libretro-common/include/queues/task_queue.h +++ b/libretro-common/include/queues/task_queue.h @@ -57,6 +57,8 @@ typedef void (*retro_task_queue_msg_t)(const char *msg, typedef bool (*retro_task_retriever_t)(retro_task_t *task, void *data); +typedef bool (*retro_task_condition_fn_t)(void *data); + typedef struct { char *source_file; @@ -200,9 +202,12 @@ void task_queue_check(void); * The task will start as soon as possible. */ void task_queue_push(retro_task_t *task); -/* Blocks until all tasks have finished. +/* Blocks until all tasks have finished + * will return early if cond is not NULL + * and cond(data) returns false. * This must only be called from the main thread. */ -void task_queue_wait(void); +void task_queue_wait(retro_task_condition_fn_t cond, void* data); + /* Sends a signal to terminate all the tasks. * diff --git a/libretro-common/queues/task_queue.c b/libretro-common/queues/task_queue.c index b8782e8399..bc92e7b3f9 100644 --- a/libretro-common/queues/task_queue.c +++ b/libretro-common/queues/task_queue.c @@ -47,7 +47,7 @@ struct retro_task_impl void (*push_running)(retro_task_t *); void (*cancel)(void *); void (*reset)(void); - void (*wait)(void); + void (*wait)(retro_task_condition_fn_t, void *); void (*gather)(void); bool (*find)(retro_task_finder_t, void*); void (*retrieve)(task_retriever_data_t *data); @@ -189,9 +189,9 @@ static void retro_task_regular_gather(void) retro_task_internal_gather(); } -static void retro_task_regular_wait(void) +static void retro_task_regular_wait(retro_task_condition_fn_t cond, void* data) { - while (tasks_running.front) + while (tasks_running.front && (!cond || cond(data))) retro_task_regular_gather(); } @@ -373,7 +373,7 @@ static void retro_task_threaded_gather(void) slock_unlock(property_lock); } -static void retro_task_threaded_wait(void) +static void retro_task_threaded_wait(retro_task_condition_fn_t cond, void* data) { bool wait = false; @@ -382,7 +382,8 @@ static void retro_task_threaded_wait(void) retro_task_threaded_gather(); slock_lock(running_lock); - wait = (tasks_running.front != NULL); + wait = (tasks_running.front != NULL) && + (!cond || cond(data)); slock_unlock(running_lock); } while (wait); } @@ -637,9 +638,9 @@ void task_queue_push(retro_task_t *task) impl_current->push_running(task); } -void task_queue_wait(void) +void task_queue_wait(retro_task_condition_fn_t cond, void* data) { - impl_current->wait(); + impl_current->wait(cond, data); } void task_queue_reset(void)