Skip to content

Commit

Permalink
Addressing code review.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandr-Konovalov committed Jul 1, 2024
1 parent 99b5be0 commit f7c6435
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
25 changes: 13 additions & 12 deletions src/tbb/arena.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,20 +155,24 @@ std::size_t arena::occupy_free_slot(thread_data& tls) {
std::size_t locked_slot = out_of_arena;
if ( index >= my_num_slots ) index = tls.my_random.get() % my_num_slots;
// Find a free slot
for ( std::size_t i = index; i < my_num_slots; ++i )
for (std::size_t i = index; i < my_num_slots; ++i) {
if (my_slots[i].try_occupy()) {
locked_slot = i;
break;
}
if ( locked_slot == out_of_arena )
for ( std::size_t i = 0; i < index; ++i )
}
if (locked_slot == out_of_arena) {
for (std::size_t i = 0; i < index; ++i) {
if (my_slots[i].try_occupy()) {
locked_slot = i;
break;
}
}
}

if ( locked_slot != out_of_arena )
if (locked_slot != out_of_arena) {
atomic_update( my_limit, (unsigned)(locked_slot + 1), std::less<unsigned>() );
}
return locked_slot;
}

Expand Down Expand Up @@ -390,8 +394,7 @@ bool arena::is_top_priority() const {
bool arena::try_join() {
if (is_joinable()) {
// check quota for number of workers in arena
unsigned curr = my_references.fetch_add(arena::ref_worker);
curr += arena::ref_worker;
unsigned curr = my_references.fetch_add(arena::ref_worker) + arena::ref_worker;
unsigned workers = curr >> arena::ref_external_bits;
if (workers > my_num_slots - my_num_reserved_slots) {
my_references -= arena::ref_worker;
Expand Down Expand Up @@ -632,8 +635,7 @@ class nested_arena_context : no_copy {
if (td.my_arena != &nested_arena) {
m_orig_arena = td.my_arena;
m_orig_slot_index = td.my_arena_index;
__TBB_ASSERT(td.my_worker_slot_type != thread_data::slot_type::undefined, "Invalid slot type");
m_orig_is_worker_slot = td.my_worker_slot_type == thread_data::slot_type::worker;
m_orig_is_worker_slot = td.my_is_workers_slot_occupied;
m_orig_last_observer = td.my_last_observer;

td.detach_task_dispatcher();
Expand All @@ -645,7 +647,7 @@ class nested_arena_context : no_copy {

// If the calling thread occupies the slots out of external thread reserve we need to notify the
// market that this arena requires one worker less.
if (td.my_worker_slot_type == thread_data::slot_type::worker) {
if (td.my_is_workers_slot_occupied) {
td.my_arena->request_workers(/* mandatory_delta = */ 0, /* workers_delta = */ -1);
}

Expand Down Expand Up @@ -681,7 +683,7 @@ class nested_arena_context : no_copy {

// Notify the market that this thread releasing a one slot
// that can be used by a worker thread.
if (td.my_worker_slot_type == thread_data::slot_type::worker) {
if (td.my_is_workers_slot_occupied) {
td.my_arena->request_workers(/* mandatory_delta = */ 0, /* workers_delta = */ 1);
}

Expand Down Expand Up @@ -760,8 +762,7 @@ void task_arena_impl::execute(d1::task_arena_base& ta, d1::delegate_base& d) {

bool same_arena = td->my_arena == a;
std::size_t index1 = td->my_arena_index;
__TBB_ASSERT(td->my_worker_slot_type != thread_data::slot_type::undefined, "Invalid slot type");
bool is_worker_slot = td->my_worker_slot_type == thread_data::slot_type::worker;
bool is_worker_slot = td->my_is_workers_slot_occupied;
if (!same_arena) {
index1 = a->occupy_free_slot(*td);
is_worker_slot = false;
Expand Down
14 changes: 4 additions & 10 deletions src/tbb/thread_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,10 @@ class thread_data : public ::rml::job
, public d1::intrusive_list_node
, no_copy {
public:
enum class slot_type {
worker,
master,
undefined
};

thread_data(unsigned short index, bool is_worker)
: my_arena_index{ index }
, my_is_worker{ is_worker }
, my_worker_slot_type{ slot_type::undefined }
, my_is_workers_slot_occupied{ false }
, my_task_dispatcher{ nullptr }
, my_arena{ nullptr }
, my_last_client{ nullptr }
Expand Down Expand Up @@ -152,8 +146,8 @@ class thread_data : public ::rml::job
//! Indicates if the thread is created by RML
const bool my_is_worker;

//! Is the slot occupied in arena begongs to workers' quota?
slot_type my_worker_slot_type;
//! Is the slot occupied in arena belongs to workers' quota?
bool my_is_workers_slot_occupied;

//! The current task dipsatcher
task_dispatcher* my_task_dispatcher;
Expand Down Expand Up @@ -215,7 +209,7 @@ class thread_data : public ::rml::job
inline void thread_data::attach_arena(arena& a, std::size_t index, bool is_worker_slot) {
my_arena = &a;
my_arena_index = static_cast<unsigned short>(index);
my_worker_slot_type = is_worker_slot? slot_type::worker : slot_type::master;
my_is_workers_slot_occupied = is_worker_slot;
my_arena_slot = a.my_slots + index;
// Read the current slot mail_outbox and attach it to the mail_inbox (remove inbox later maybe)
my_inbox.attach(my_arena->mailbox(index));
Expand Down

0 comments on commit f7c6435

Please sign in to comment.