Blender V5.3
task_scheduler.cc File Reference
#include "MEM_guardedalloc.h"
#include "BLI_lazy_threading.hh"
#include "BLI_simd.hh"
#include "BLI_task_c.hh"
#include "BLI_threads.hh"

Go to the source code of this file.

Namespaces

namespace  blender
 BMesh Iterators.

Functions

Task Scheduler

Central scheduler that holds running threads ready to execute tasks. A single queue holds the task from all pools.

Initialize/exit must be called before/after any task pools are created/freed, and must be called from the main threads. All other scheduler and pool functions are thread-safe.

void blender::BLI_task_scheduler_init (bool use_flush_denormals_to_zero=true)
void blender::BLI_task_scheduler_exit ()
int blender::BLI_task_scheduler_num_threads ()
Task Isolation

Task isolation helps avoid unexpected task scheduling decisions that can lead to bugs if wrong assumptions were made. Typically that happens when doing "nested threading", i.e. one thread schedules a bunch of main-tasks and those spawn new sub-tasks.

What can happen is that when a main-task waits for its sub-tasks to complete on other threads, another main-task is scheduled within the already running main-task. Generally, this is good, because it leads to better performance. However, sometimes code (often unintentionally) makes the assumption that at most one main-task runs on a thread at a time.

The bugs often show themselves in two ways:

  • Deadlock, when a main-task holds a mutex while waiting for its sub-tasks to complete.
  • Data corruption, when a main-task makes wrong assumptions about a thread-local variable.

Task isolation can avoid these bugs by making sure that a main-task does not start executing another main-task while waiting for its sub-tasks. More precisely, a function that runs in an isolated region is only allowed to run sub-tasks that were spawned in the same isolated region.

Unfortunately, incorrect use of task isolation can lead to deadlocks itself. This can happen when threading primitives are used that separate spawning tasks from executing them. The problem occurs when a task is spawned in one isolated region while the tasks are waited for in another isolated region. In this setup, the thread that is waiting for the spawned tasks to complete cannot run the tasks itself. On a single thread, that causes a deadlock already. When there are multiple threads, another thread will typically run the task and avoid the deadlock. However, if this situation happens on all threads at the same time, all threads will deadlock. This happened in #88598.

void blender::BLI_task_isolate (void(*func)(void *userdata), void *userdata)

Variables

static int blender::task_scheduler_num_threads = 1

Detailed Description

Task scheduler initialization.

Definition in file task_scheduler.cc.