Blender  V2.93
task_scheduler.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
23 #include "MEM_guardedalloc.h"
24 
25 #include "BLI_task.h"
26 #include "BLI_threads.h"
27 
28 #ifdef WITH_TBB
29 /* Need to include at least one header to get the version define. */
30 # include <tbb/blocked_range.h>
31 # if TBB_INTERFACE_VERSION_MAJOR >= 10
32 # include <tbb/global_control.h>
33 # define WITH_TBB_GLOBAL_CONTROL
34 # endif
35 #endif
36 
37 /* Task Scheduler */
38 
40 #ifdef WITH_TBB_GLOBAL_CONTROL
41 static tbb::global_control *task_scheduler_global_control = nullptr;
42 #endif
43 
45 {
46 #ifdef WITH_TBB_GLOBAL_CONTROL
48 
49  if (num_threads_override > 0) {
50  /* Override number of threads. This settings is used within the lifetime
51  * of tbb::global_control, so we allocate it on the heap. */
52  task_scheduler_global_control = OBJECT_GUARDED_NEW(
53  tbb::global_control, tbb::global_control::max_allowed_parallelism, num_threads_override);
55  }
56  else {
57  /* Let TBB choose the number of threads. For (legacy) code that calls
58  * BLI_task_scheduler_num_threads() we provide the system thread count.
59  * Ideally such code should be rewritten not to use the number of threads
60  * at all. */
62  }
63 #else
65 #endif
66 }
67 
69 {
70 #ifdef WITH_TBB_GLOBAL_CONTROL
71  OBJECT_GUARDED_DELETE(task_scheduler_global_control, tbb::global_control);
72 #endif
73 }
74 
76 {
78 }
int BLI_system_thread_count(void)
Definition: threads.cc:309
int BLI_system_num_threads_override_get(void)
Definition: threads.cc:350
Read Guarded memory(de)allocation.
void BLI_task_scheduler_exit()
static int task_scheduler_num_threads
int BLI_task_scheduler_num_threads()
void BLI_task_scheduler_init()
static int num_threads_override
Definition: threads.cc:130