Blender  V3.3
task_scheduler.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
9 #include "MEM_guardedalloc.h"
10 
11 #include "BLI_task.h"
12 #include "BLI_threads.h"
13 
14 #ifdef WITH_TBB
15 /* Need to include at least one header to get the version define. */
16 # include <tbb/blocked_range.h>
17 # include <tbb/task_arena.h>
18 # if TBB_INTERFACE_VERSION_MAJOR >= 10
19 # include <tbb/global_control.h>
20 # define WITH_TBB_GLOBAL_CONTROL
21 # endif
22 #endif
23 
24 /* Task Scheduler */
25 
27 #ifdef WITH_TBB_GLOBAL_CONTROL
28 static tbb::global_control *task_scheduler_global_control = nullptr;
29 #endif
30 
32 {
33 #ifdef WITH_TBB_GLOBAL_CONTROL
35 
36  if (threads_override_num > 0) {
37  /* Override number of threads. This settings is used within the lifetime
38  * of tbb::global_control, so we allocate it on the heap. */
39  task_scheduler_global_control = MEM_new<tbb::global_control>(
40  __func__, tbb::global_control::max_allowed_parallelism, threads_override_num);
42  }
43  else {
44  /* Let TBB choose the number of threads. For (legacy) code that calls
45  * BLI_task_scheduler_num_threads() we provide the system thread count.
46  * Ideally such code should be rewritten not to use the number of threads
47  * at all. */
49  }
50 #else
52 #endif
53 }
54 
56 {
57 #ifdef WITH_TBB_GLOBAL_CONTROL
58  MEM_delete(task_scheduler_global_control);
59 #endif
60 }
61 
63 {
65 }
66 
67 void BLI_task_isolate(void (*func)(void *userdata), void *userdata)
68 {
69 #ifdef WITH_TBB
70  tbb::this_task_arena::isolate([&] { func(userdata); });
71 #else
72  func(userdata);
73 #endif
74 }
int BLI_system_thread_count(void)
Definition: threads.cc:281
int BLI_system_num_threads_override_get(void)
Definition: threads.cc:322
Read Guarded memory(de)allocation.
void BLI_task_scheduler_exit()
void BLI_task_isolate(void(*func)(void *userdata), void *userdata)
static int task_scheduler_num_threads
int BLI_task_scheduler_num_threads()
void BLI_task_scheduler_init()
static int threads_override_num
Definition: threads.cc:112