diff options
-rw-r--r-- | source/blender/blenlib/BLI_task.h | 20 | ||||
-rw-r--r-- | source/blender/blenlib/intern/task_pool.cc | 7 |
2 files changed, 23 insertions, 4 deletions
diff --git a/source/blender/blenlib/BLI_task.h b/source/blender/blenlib/BLI_task.h index 3ef693ac62a..64dfdc2ad25 100644 --- a/source/blender/blenlib/BLI_task.h +++ b/source/blender/blenlib/BLI_task.h @@ -49,7 +49,7 @@ int BLI_task_scheduler_num_threads(void); /* Task Pool * - * Pool of tasks that will be executed by the central TaskScheduler. For each + * Pool of tasks that will be executed by the central task scheduler. For each * pool, we can wait for all tasks to be done, or cancel them before they are * done. * @@ -70,11 +70,27 @@ typedef struct TaskPool TaskPool; typedef void (*TaskRunFunction)(TaskPool *__restrict pool, void *taskdata); typedef void (*TaskFreeFunction)(TaskPool *__restrict pool, void *taskdata); +/* Regular task pool that immediately starts executing tasks as soon as they + * are pushed, either on the current or another thread. */ TaskPool *BLI_task_pool_create(void *userdata, TaskPriority priority); + +/* Background: always run tasks in a background thread, never immediately + * execute them. For running background jobs. */ TaskPool *BLI_task_pool_create_background(void *userdata, TaskPriority priority); + +/* Background Serial: run tasks one after the other in the background, + * without parallelization between the tasks. */ +TaskPool *BLI_task_pool_create_background_serial(void *userdata, TaskPriority priority); + +/* Suspended: don't execute tasks until work_and_wait is called. This is slower + * as threads can't immediately start working. But it can be used if the data + * structures the threads operate on are not fully initialized until all tasks + * are created. */ TaskPool *BLI_task_pool_create_suspended(void *userdata, TaskPriority priority); + +/* No threads: immediately executes tasks on the same thread. For debugging. */ TaskPool *BLI_task_pool_create_no_threads(void *userdata); -TaskPool *BLI_task_pool_create_background_serial(void *userdata, TaskPriority priority); + void BLI_task_pool_free(TaskPool *pool); void BLI_task_pool_push(TaskPool *pool, diff --git a/source/blender/blenlib/intern/task_pool.cc b/source/blender/blenlib/intern/task_pool.cc index d8e90af551a..c4d60673492 100644 --- a/source/blender/blenlib/intern/task_pool.cc +++ b/source/blender/blenlib/intern/task_pool.cc @@ -305,7 +305,6 @@ static void background_task_pool_create(TaskPool *pool) { pool->background_queue = BLI_thread_queue_init(); BLI_threadpool_init(&pool->background_threads, background_task_run, 1); - BLI_threadpool_insert(&pool->background_threads, pool); } static void background_task_pool_run(TaskPool *pool, Task &&task) @@ -313,6 +312,10 @@ static void background_task_pool_run(TaskPool *pool, Task &&task) Task *task_mem = (Task *)MEM_mallocN(sizeof(Task), __func__); new (task_mem) Task(std::move(task)); BLI_thread_queue_push(pool->background_queue, task_mem); + + if (BLI_available_threads(&pool->background_threads)) { + BLI_threadpool_insert(&pool->background_threads, pool); + } } static void background_task_pool_work_and_wait(TaskPool *pool) @@ -321,7 +324,7 @@ static void background_task_pool_work_and_wait(TaskPool *pool) * left, and wait for tasks and thread to finish. */ BLI_thread_queue_nowait(pool->background_queue); BLI_thread_queue_wait_finish(pool->background_queue); - BLI_threadpool_remove(&pool->background_threads, pool); + BLI_threadpool_clear(&pool->background_threads); } static void background_task_pool_cancel(TaskPool *pool) |