Skip to content

Commit

Permalink
Fix control variable initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
SoilRos committed Apr 15, 2024
1 parent 224eca3 commit ebe97c5
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/tbb/global_control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

#include <atomic>
#include <set>
#include <memory>
#include <mutex>

namespace tbb {
namespace detail {
Expand Down Expand Up @@ -139,13 +141,20 @@ class alignas(max_nfs_size) lifetime_control : public control_storage {
}
};

static allowed_parallelism_control allowed_parallelism_ctl;
static stack_size_control stack_size_ctl;
static terminate_on_exception_control terminate_on_exception_ctl;
static lifetime_control lifetime_ctl;
static control_storage *controls[] = {&allowed_parallelism_ctl, &stack_size_ctl, &terminate_on_exception_ctl, &lifetime_ctl};
static std::array<std::unique_ptr<control_storage>, 4> controls;
static std::once_flag control_storage_flag;

void global_control_init() {
controls[0] = std::unique_ptr<control_storage>(new allowed_parallelism_control{});
controls[1] = std::unique_ptr<control_storage>(new stack_size_control{});
controls[2] = std::unique_ptr<control_storage>(new terminate_on_exception_control{});
controls[3] = std::unique_ptr<control_storage>(new lifetime_control{});
}

void global_control_lock() {
std::call_once(control_storage_flag, []() {
global_control_init();
});
for (auto& ctl : controls) {
ctl->my_list_mutex.lock();
}
Expand Down Expand Up @@ -188,7 +197,7 @@ struct global_control_impl {

static void create(d1::global_control& gc) {
__TBB_ASSERT_RELEASE(gc.my_param < d1::global_control::parameter_max, nullptr);
control_storage* const c = controls[gc.my_param];
control_storage* const c = controls[gc.my_param].get();

spin_mutex::scoped_lock lock(c->my_list_mutex);
if (c->my_list.empty() || c->is_first_arg_preferred(gc.my_value, c->my_active_value)) {
Expand All @@ -201,7 +210,7 @@ struct global_control_impl {

static void destroy(d1::global_control& gc) {
__TBB_ASSERT_RELEASE(gc.my_param < d1::global_control::parameter_max, nullptr);
control_storage* const c = controls[gc.my_param];
control_storage* const c = controls[gc.my_param].get();
// Concurrent reading and changing global parameter is possible.
spin_mutex::scoped_lock lock(c->my_list_mutex);
__TBB_ASSERT(gc.my_param == d1::global_control::scheduler_handle || !c->my_list.empty(), nullptr);
Expand All @@ -224,7 +233,7 @@ struct global_control_impl {

static bool remove_and_check_if_empty(d1::global_control& gc) {
__TBB_ASSERT_RELEASE(gc.my_param < d1::global_control::parameter_max, nullptr);
control_storage* const c = controls[gc.my_param];
control_storage* const c = controls[gc.my_param].get();

spin_mutex::scoped_lock lock(c->my_list_mutex);
__TBB_ASSERT(!c->my_list.empty(), nullptr);
Expand All @@ -234,7 +243,7 @@ struct global_control_impl {
#if TBB_USE_ASSERT
static bool is_present(d1::global_control& gc) {
__TBB_ASSERT_RELEASE(gc.my_param < d1::global_control::parameter_max, nullptr);
control_storage* const c = controls[gc.my_param];
control_storage* const c = controls[gc.my_param].get();

spin_mutex::scoped_lock lock(c->my_list_mutex);
auto it = c->my_list.find(&gc);
Expand Down

0 comments on commit ebe97c5

Please sign in to comment.