Using the `MEM_*` API from C++ code was a bit annoying: * When converting C to C++ code, one often has to add a type cast on returned `void *`. That leads to having the same type name three times in the same line. This patch reduces the amount to two and removes the `sizeof(...)` from the line. * The existing alternative of using `OBJECT_GUARDED_NEW` looks a out of place compared to other allocation methods. Sometimes `MEM_CXX_CLASS_ALLOC_FUNCS` can be used when structs are defined in C++ code. It doesn't look great but it's definitely better. The downside is that it makes the name of the allocation less useful. That's because the same name is used for all allocations of a type, independend of where it is allocated. This patch introduces three new functions: `MEM_new`, `MEM_cnew` and `MEM_delete`. These cover the majority of use cases (array allocation is not covered). The `OBJECT_GUARDED_*` macros are removed because they are not needed anymore. Differential Revision: https://developer.blender.org/D13502
89 lines
2.6 KiB
C++
89 lines
2.6 KiB
C++
/*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation; either version 2
|
|
* of the License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
*/
|
|
|
|
/** \file
|
|
* \ingroup bli
|
|
*
|
|
* Task scheduler initialization.
|
|
*/
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
#include "BLI_task.h"
|
|
#include "BLI_threads.h"
|
|
|
|
#ifdef WITH_TBB
|
|
/* Need to include at least one header to get the version define. */
|
|
# include <tbb/blocked_range.h>
|
|
# include <tbb/task_arena.h>
|
|
# if TBB_INTERFACE_VERSION_MAJOR >= 10
|
|
# include <tbb/global_control.h>
|
|
# define WITH_TBB_GLOBAL_CONTROL
|
|
# endif
|
|
#endif
|
|
|
|
/* Task Scheduler */
|
|
|
|
static int task_scheduler_num_threads = 1;
|
|
#ifdef WITH_TBB_GLOBAL_CONTROL
|
|
static tbb::global_control *task_scheduler_global_control = nullptr;
|
|
#endif
|
|
|
|
void BLI_task_scheduler_init()
|
|
{
|
|
#ifdef WITH_TBB_GLOBAL_CONTROL
|
|
const int num_threads_override = BLI_system_num_threads_override_get();
|
|
|
|
if (num_threads_override > 0) {
|
|
/* Override number of threads. This settings is used within the lifetime
|
|
* of tbb::global_control, so we allocate it on the heap. */
|
|
task_scheduler_global_control = MEM_new<tbb::global_control>(
|
|
__func__, tbb::global_control::max_allowed_parallelism, num_threads_override);
|
|
task_scheduler_num_threads = num_threads_override;
|
|
}
|
|
else {
|
|
/* Let TBB choose the number of threads. For (legacy) code that calls
|
|
* BLI_task_scheduler_num_threads() we provide the system thread count.
|
|
* Ideally such code should be rewritten not to use the number of threads
|
|
* at all. */
|
|
task_scheduler_num_threads = BLI_system_thread_count();
|
|
}
|
|
#else
|
|
task_scheduler_num_threads = BLI_system_thread_count();
|
|
#endif
|
|
}
|
|
|
|
void BLI_task_scheduler_exit()
|
|
{
|
|
#ifdef WITH_TBB_GLOBAL_CONTROL
|
|
MEM_delete(task_scheduler_global_control);
|
|
#endif
|
|
}
|
|
|
|
int BLI_task_scheduler_num_threads()
|
|
{
|
|
return task_scheduler_num_threads;
|
|
}
|
|
|
|
void BLI_task_isolate(void (*func)(void *userdata), void *userdata)
|
|
{
|
|
#ifdef WITH_TBB
|
|
tbb::this_task_arena::isolate([&] { func(userdata); });
|
|
#else
|
|
func(userdata);
|
|
#endif
|
|
}
|