Blender V4.3
task_scheduler.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11#include "MEM_guardedalloc.h"
12
13#include "BLI_lazy_threading.hh"
14#include "BLI_task.h"
15#include "BLI_threads.h"
16
17#ifdef WITH_TBB
18/* Need to include at least one header to get the version define. */
19# include <tbb/blocked_range.h>
20# include <tbb/task_arena.h>
21# if TBB_INTERFACE_VERSION_MAJOR >= 10
22# include <tbb/global_control.h>
23# define WITH_TBB_GLOBAL_CONTROL
24# endif
25#endif
26
27/* Task Scheduler */
28
30#ifdef WITH_TBB_GLOBAL_CONTROL
31static tbb::global_control *task_scheduler_global_control = nullptr;
32#endif
33
35{
36#ifdef WITH_TBB_GLOBAL_CONTROL
38
39 if (threads_override_num > 0) {
40 /* Override number of threads. This settings is used within the lifetime
41 * of tbb::global_control, so we allocate it on the heap. */
42 task_scheduler_global_control = MEM_new<tbb::global_control>(
43 __func__, tbb::global_control::max_allowed_parallelism, threads_override_num);
45 }
46 else {
47 /* Let TBB choose the number of threads. For (legacy) code that calls
48 * BLI_task_scheduler_num_threads() we provide the system thread count.
49 * Ideally such code should be rewritten not to use the number of threads
50 * at all. */
52 }
53#else
55#endif
56}
57
59{
60#ifdef WITH_TBB_GLOBAL_CONTROL
61 MEM_delete(task_scheduler_global_control);
62#endif
63}
64
69
70void BLI_task_isolate(void (*func)(void *userdata), void *userdata)
71{
72#ifdef WITH_TBB
74 tbb::this_task_arena::isolate([&] { func(userdata); });
75#else
76 func(userdata);
77#endif
78}
int BLI_system_thread_count(void)
Definition threads.cc:253
int BLI_system_num_threads_override_get(void)
Definition threads.cc:294
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:101