Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
tbb::internal::arena Class Reference

#include <arena.h>

Inheritance diagram for tbb::internal::arena:
Collaboration diagram for tbb::internal::arena:

Public Types

enum  new_work_type { work_spawned, wakeup, work_enqueued }
 Types of work advertised by advertise_new_work() More...
 
typedef padded< arena_basebase_type
 
typedef uintptr_t pool_state_t
 

Public Member Functions

 arena (market &, unsigned max_num_workers, unsigned num_reserved_slots)
 Constructor. More...
 
mail_outboxmailbox (affinity_id id)
 Get reference to mailbox corresponding to given affinity_id. More...
 
void free_arena ()
 Completes arena shutdown, destructs and deallocates it. More...
 
unsigned num_workers_active ()
 The number of workers active in the arena. More...
 
template<arena::new_work_type work_type>
void advertise_new_work ()
 If necessary, raise a flag that there is new job in arena. More...
 
bool is_out_of_work ()
 Check if there is job anywhere in arena. More...
 
void enqueue_task (task &, intptr_t, FastRandom &)
 enqueue a task into starvation-resistance queue More...
 
void process (generic_scheduler &)
 Registers the worker with the arena and enters TBB scheduler dispatch loop. More...
 
template<unsigned ref_param>
void on_thread_leaving ()
 Notification that worker or master leaves its arena. More...
 
bool has_enqueued_tasks ()
 Check for the presence of enqueued tasks at all priority levels. More...
 
template<bool as_worker>
size_t occupy_free_slot (generic_scheduler &s)
 Tries to occupy a slot in the arena. On success, returns the slot index; if no slot is available, returns out_of_arena. More...
 
size_t occupy_free_slot_in_range (generic_scheduler &s, size_t lower, size_t upper)
 Tries to occupy a slot in the specified range. More...
 

Static Public Member Functions

static arenaallocate_arena (market &, unsigned num_slots, unsigned num_reserved_slots)
 Allocate an instance of arena. More...
 
static int unsigned num_arena_slots (unsigned num_slots)
 
static int allocation_size (unsigned num_slots)
 
static bool is_busy_or_empty (pool_state_t s)
 No tasks to steal or snapshot is being taken. More...
 

Public Attributes

arena_slot my_slots [1]
 
- Public Attributes inherited from tbb::internal::padded_base< arena_base, NFS_MaxLineSize, sizeof(arena_base) % NFS_MaxLineSize >
char pad [S - R]
 
- Public Attributes inherited from tbb::internal::arena_base
unsigned my_num_workers_allotted
 The number of workers that have been marked out by the resource manager to service the arena. More...
 
atomic< unsigned > my_references
 Reference counter for the arena. More...
 
atomic< unsigned > my_limit
 The maximal number of currently busy slots. More...
 
task_stream< num_priority_levelsmy_task_stream
 Task pool for the tasks scheduled via task::enqueue() method. More...
 
unsigned my_max_num_workers
 The number of workers requested by the master thread owning the arena. More...
 
int my_num_workers_requested
 The number of workers that are currently requested from the resource manager. More...
 
tbb::atomic< uintptr_t > my_pool_state
 Current task pool state and estimate of available tasks amount. More...
 
marketmy_market
 The market that owns this arena. More...
 
uintptr_t my_aba_epoch
 ABA prevention marker. More...
 
cpu_ctl_env my_cpu_ctl_env
 FPU control settings of arena's master thread captured at the moment of arena instantiation. More...
 
unsigned my_num_slots
 The number of slots in the arena. More...
 
unsigned my_num_reserved_slots
 The number of reserved slots (can be occupied only by masters). More...
 
concurrent_monitor my_exit_monitors
 Waiting object for master threads that cannot join the arena. More...
 
- Public Attributes inherited from tbb::internal::padded_base< intrusive_list_node, NFS_MaxLineSize, sizeof(intrusive_list_node) % NFS_MaxLineSize >
char pad [S - R]
 
- Public Attributes inherited from tbb::internal::intrusive_list_node
intrusive_list_nodemy_prev_node
 
intrusive_list_nodemy_next_node
 

Static Public Attributes

static const pool_state_t SNAPSHOT_EMPTY = 0
 No tasks to steal since last snapshot was taken. More...
 
static const pool_state_t SNAPSHOT_FULL = pool_state_t(-1)
 At least one task has been offered for stealing since the last snapshot started. More...
 
static const unsigned ref_external_bits = 12
 The number of least significant bits for external references. More...
 
static const unsigned ref_external = 1
 Reference increment values for externals and workers. More...
 
static const unsigned ref_worker = 1<<ref_external_bits
 
static const size_t out_of_arena = ~size_t(0)
 

Private Member Functions

void restore_priority_if_need ()
 If enqueued tasks found, restore arena priority and task presence status. More...
 

Detailed Description

Definition at line 162 of file arena.h.

Member Typedef Documentation

◆ base_type

Definition at line 167 of file arena.h.

◆ pool_state_t

Definition at line 201 of file arena.h.

Member Enumeration Documentation

◆ new_work_type

Types of work advertised by advertise_new_work()

Enumerator
work_spawned 
wakeup 
work_enqueued 

Definition at line 170 of file arena.h.

Constructor & Destructor Documentation

◆ arena()

tbb::internal::arena::arena ( market m,
unsigned  max_num_workers,
unsigned  num_reserved_slots 
)

Constructor.

Definition at line 190 of file arena.cpp.

190  {
191  __TBB_ASSERT( !my_guard, "improperly allocated arena?" );
192  __TBB_ASSERT( sizeof(my_slots[0]) % NFS_GetLineSize()==0, "arena::slot size not multiple of cache line size" );
193  __TBB_ASSERT( (uintptr_t)this % NFS_GetLineSize()==0, "arena misaligned" );
194 #if __TBB_TASK_PRIORITY
195  __TBB_ASSERT( !my_reload_epoch && !my_orphaned_tasks && !my_skipped_fifo_priority, "New arena object is not zeroed" );
196 #endif /* __TBB_TASK_PRIORITY */
197  my_market = &m;
198  my_limit = 1;
199  // Two slots are mandatory: for the master, and for 1 worker (required to support starvation resistant tasks).
200  my_num_slots = num_arena_slots(num_slots);
201  my_num_reserved_slots = num_reserved_slots;
202  my_max_num_workers = num_slots-num_reserved_slots;
203  my_references = ref_external; // accounts for the master
204 #if __TBB_TASK_PRIORITY
205  my_bottom_priority = my_top_priority = normalized_normal_priority;
206 #endif /* __TBB_TASK_PRIORITY */
207  my_aba_epoch = m.my_arenas_aba_epoch;
208 #if __TBB_ARENA_OBSERVER
209  my_observers.my_arena = this;
210 #endif
212  // Construct slots. Mark internal synchronization elements for the tools.
213  for( unsigned i = 0; i < my_num_slots; ++i ) {
214  __TBB_ASSERT( !my_slots[i].my_scheduler && !my_slots[i].task_pool, NULL );
215  __TBB_ASSERT( !my_slots[i].task_pool_ptr, NULL );
216  __TBB_ASSERT( !my_slots[i].my_task_pool_size, NULL );
217  ITT_SYNC_CREATE(my_slots + i, SyncType_Scheduler, SyncObj_WorkerTaskPool);
218  mailbox(i+1).construct();
219  ITT_SYNC_CREATE(&mailbox(i+1), SyncType_Scheduler, SyncObj_Mailbox);
220  my_slots[i].hint_for_pop = i;
221 #if __TBB_STATISTICS
222  my_slots[i].my_counters = new ( NFS_Allocate(1, sizeof(statistics_counters), NULL) ) statistics_counters;
223 #endif /* __TBB_STATISTICS */
224  }
226  ITT_SYNC_CREATE(&my_task_stream, SyncType_Scheduler, SyncObj_TaskStream);
227 #if __TBB_ENQUEUE_ENFORCED_CONCURRENCY
228  my_concurrency_mode = cm_normal;
229 #endif
230 #if !__TBB_FP_CONTEXT
232 #endif
233 }
void initialize(unsigned n_lanes)
Definition: task_stream.h:87
unsigned my_max_num_workers
The number of workers requested by the master thread owning the arena.
Definition: arena.h:76
market * my_market
The market that owns this arena.
Definition: arena.h:118
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:169
void construct()
Construct *this as a mailbox from zeroed memory.
Definition: mailbox.h:162
static int unsigned num_arena_slots(unsigned num_slots)
Definition: arena.h:182
size_t __TBB_EXPORTED_FUNC NFS_GetLineSize()
Cache/sector line size.
static const unsigned ref_external
Reference increment values for externals and workers.
Definition: arena.h:213
unsigned hint_for_pop
Hint provided for operations with the container of starvation-resistant tasks.
#define ITT_SYNC_CREATE(obj, type, name)
Definition: itt_notify.h:120
task_stream< num_priority_levels > my_task_stream
Task pool for the tasks scheduled via task::enqueue() method.
Definition: arena.h:73
atomic< unsigned > my_limit
The maximal number of currently busy slots.
Definition: arena.h:65
void *__TBB_EXPORTED_FUNC NFS_Allocate(size_t n_element, size_t element_size, void *hint)
Allocate memory on cache/sector line boundary.
unsigned my_num_reserved_slots
The number of reserved slots (can be occupied only by masters).
Definition: arena.h:139
unsigned my_num_slots
The number of slots in the arena.
Definition: arena.h:136
cpu_ctl_env my_cpu_ctl_env
FPU control settings of arena's master thread captured at the moment of arena instantiation.
Definition: arena.h:125
atomic< unsigned > my_references
Reference counter for the arena.
Definition: arena.h:57
mail_outbox & mailbox(affinity_id id)
Get reference to mailbox corresponding to given affinity_id.
Definition: arena.h:191
uintptr_t my_aba_epoch
ABA prevention marker.
Definition: arena.h:121
arena_slot my_slots[1]
Definition: arena.h:283

References __TBB_ASSERT, tbb::internal::mail_outbox::construct(), tbb::internal::cpu_ctl_env::get_env(), tbb::internal::arena_slot_line2::hint_for_pop, tbb::internal::task_stream< Levels >::initialize(), ITT_SYNC_CREATE, mailbox(), tbb::internal::arena_base::my_aba_epoch, tbb::internal::market::my_arenas_aba_epoch, tbb::internal::arena_base::my_cpu_ctl_env, tbb::internal::arena_base::my_limit, tbb::internal::arena_base::my_market, tbb::internal::arena_base::my_max_num_workers, tbb::internal::arena_base::my_num_reserved_slots, tbb::internal::arena_base::my_num_slots, tbb::internal::arena_base::my_references, my_slots, tbb::internal::arena_base::my_task_stream, tbb::internal::NFS_Allocate(), tbb::internal::NFS_GetLineSize(), num_arena_slots(), and ref_external.

Referenced by allocate_arena().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Function Documentation

◆ advertise_new_work()

template<arena::new_work_type work_type>
void tbb::internal::arena::advertise_new_work ( )

If necessary, raise a flag that there is new job in arena.

Definition at line 376 of file arena.h.

376  {
377  if( work_type == work_enqueued ) {
378 #if __TBB_ENQUEUE_ENFORCED_CONCURRENCY
380  if( my_concurrency_mode!=cm_enforced_global ) {
381  if( my_market->mandatory_concurrency_enable( this ) ) {
383  return;
384  }
385  }
386  } else if( my_max_num_workers==0 && my_num_reserved_slots==1 ) {
387  my_max_num_workers = 1;
388  __TBB_ASSERT(my_concurrency_mode==cm_normal, NULL);
389  my_concurrency_mode = cm_enforced_local;
391  my_market->adjust_demand( *this, 1 );
392  return;
393  }
394 #endif /* __TBB_ENQUEUE_ENFORCED_CONCURRENCY */
395  // Local memory fence here and below is required to avoid missed wakeups; see the comment below.
396  // Starvation resistant tasks require concurrency, so missed wakeups are unacceptable.
397  atomic_fence();
398  }
399  else if( work_type == wakeup ) {
400  __TBB_ASSERT(my_max_num_workers!=0, "Unexpected worker wakeup request");
401  atomic_fence();
402  }
403  // Double-check idiom that, in case of spawning, is deliberately sloppy about memory fences.
404  // Technically, to avoid missed wakeups, there should be a full memory fence between the point we
405  // released the task pool (i.e. spawned task) and read the arena's state. However, adding such a
406  // fence might hurt overall performance more than it helps, because the fence would be executed
407  // on every task pool release, even when stealing does not occur. Since TBB allows parallelism,
408  // but never promises parallelism, the missed wakeup is not a correctness problem.
409  pool_state_t snapshot = my_pool_state;
410  if( is_busy_or_empty(snapshot) ) {
411  // Attempt to mark as full. The compare_and_swap below is a little unusual because the
412  // result is compared to a value that can be different than the comparand argument.
414  if( snapshot!=SNAPSHOT_EMPTY ) {
415  // This thread read "busy" into snapshot, and then another thread transitioned
416  // my_pool_state to "empty" in the meantime, which caused the compare_and_swap above
417  // to fail. Attempt to transition my_pool_state from "empty" to "full".
419  // Some other thread transitioned my_pool_state from "empty", and hence became
420  // responsible for waking up workers.
421  return;
422  }
423  }
424  // This thread transitioned pool from empty to full state, and thus is responsible for
425  // telling the market that there is work to do.
426 #if __TBB_ENQUEUE_ENFORCED_CONCURRENCY
427  if( work_type == work_spawned ) {
428  if( my_concurrency_mode!=cm_normal ) {
429  switch( my_concurrency_mode ) {
430  case cm_enforced_local:
432  __TBB_ASSERT(!governor::local_scheduler()->is_worker(), "");
433  // There was deliberate oversubscription on 1 core for sake of starvation-resistant tasks.
434  // Now a single active thread (must be the master) supposedly starts a new parallel region
435  // with relaxed sequential semantics, and oversubscription should be avoided.
436  // Demand for workers has been decreased to 0 during SNAPSHOT_EMPTY, so just keep it.
437  my_max_num_workers = 0;
438  my_concurrency_mode = cm_normal;
439  break;
440  case cm_enforced_global:
441  my_market->mandatory_concurrency_disable( this );
443  break;
444  default:
445  break;
446  }
447  return;
448  }
449  }
450 #endif /* __TBB_ENQUEUE_ENFORCED_CONCURRENCY */
452  }
453  }
454 }
unsigned my_max_num_workers
The number of workers requested by the master thread owning the arena.
Definition: arena.h:76
market * my_market
The market that owns this arena.
Definition: arena.h:118
static const pool_state_t SNAPSHOT_FULL
At least one task has been offered for stealing since the last snapshot started.
Definition: arena.h:207
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:169
void adjust_demand(arena &, int delta)
Request that arena's need in workers should be adjusted.
Definition: market.cpp:590
static generic_scheduler * local_scheduler()
Obtain the thread-local instance of the TBB scheduler.
Definition: governor.h:126
static const pool_state_t SNAPSHOT_EMPTY
No tasks to steal since last snapshot was taken.
Definition: arena.h:204
uintptr_t pool_state_t
Definition: arena.h:201
unsigned my_num_workers_soft_limit
Current application-imposed limit on the number of workers (see set_active_num_workers())
Definition: market.h:82
value_type compare_and_swap(value_type value, value_type comparand)
Definition: atomic.h:289
void restore_priority_if_need()
If enqueued tasks found, restore arena priority and task presence status.
Definition: arena.cpp:377
unsigned my_num_reserved_slots
The number of reserved slots (can be occupied only by masters).
Definition: arena.h:139
static bool is_busy_or_empty(pool_state_t s)
No tasks to steal or snapshot is being taken.
Definition: arena.h:217
tbb::atomic< uintptr_t > my_pool_state
Current task pool state and estimate of available tasks amount.
Definition: arena.h:86
void atomic_fence()
Sequentially consistent full memory fence.
Definition: tbb_machine.h:343

References __TBB_ASSERT, tbb::internal::market::adjust_demand(), tbb::atomic_fence(), tbb::internal::atomic_impl< T >::compare_and_swap(), is_busy_or_empty(), tbb::internal::governor::local_scheduler(), tbb::internal::arena_base::my_market, tbb::internal::arena_base::my_max_num_workers, tbb::internal::arena_base::my_num_reserved_slots, tbb::internal::market::my_num_workers_soft_limit, tbb::internal::arena_base::my_pool_state, restore_priority_if_need(), SNAPSHOT_EMPTY, SNAPSHOT_FULL, wakeup, work_enqueued, and work_spawned.

Referenced by tbb::internal::generic_scheduler::get_task(), tbb::internal::generic_scheduler::local_spawn(), and tbb::internal::generic_scheduler::steal_task().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ allocate_arena()

arena & tbb::internal::arena::allocate_arena ( market m,
unsigned  num_slots,
unsigned  num_reserved_slots 
)
static

Allocate an instance of arena.

Definition at line 235 of file arena.cpp.

235  {
236  __TBB_ASSERT( sizeof(base_type) + sizeof(arena_slot) == sizeof(arena), "All arena data fields must go to arena_base" );
237  __TBB_ASSERT( sizeof(base_type) % NFS_GetLineSize() == 0, "arena slots area misaligned: wrong padding" );
238  __TBB_ASSERT( sizeof(mail_outbox) == NFS_MaxLineSize, "Mailbox padding is wrong" );
239  size_t n = allocation_size(num_arena_slots(num_slots));
240  unsigned char* storage = (unsigned char*)NFS_Allocate( 1, n, NULL );
241  // Zero all slots to indicate that they are empty
242  memset( storage, 0, n );
243  return *new( storage + num_arena_slots(num_slots) * sizeof(mail_outbox) ) arena(m, num_slots, num_reserved_slots);
244 }
static int allocation_size(unsigned num_slots)
Definition: arena.h:186
const size_t NFS_MaxLineSize
Compile-time constant that is upper bound on cache line/sector size.
Definition: tbb_stddef.h:220
padded< arena_base > base_type
Definition: arena.h:167
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:169
static int unsigned num_arena_slots(unsigned num_slots)
Definition: arena.h:182
size_t __TBB_EXPORTED_FUNC NFS_GetLineSize()
Cache/sector line size.
void *__TBB_EXPORTED_FUNC NFS_Allocate(size_t n_element, size_t element_size, void *hint)
Allocate memory on cache/sector line boundary.
arena(market &, unsigned max_num_workers, unsigned num_reserved_slots)
Constructor.
Definition: arena.cpp:190

References __TBB_ASSERT, allocation_size(), arena(), tbb::internal::NFS_Allocate(), tbb::internal::NFS_GetLineSize(), tbb::internal::NFS_MaxLineSize, and num_arena_slots().

Referenced by tbb::internal::market::create_arena().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ allocation_size()

static int tbb::internal::arena::allocation_size ( unsigned  num_slots)
inlinestatic

Definition at line 186 of file arena.h.

186  {
187  return sizeof(base_type) + num_slots * (sizeof(mail_outbox) + sizeof(arena_slot));
188  }
padded< arena_base > base_type
Definition: arena.h:167

Referenced by allocate_arena(), and free_arena().

Here is the caller graph for this function:

◆ enqueue_task()

void tbb::internal::arena::enqueue_task ( task t,
intptr_t  prio,
FastRandom random 
)

enqueue a task into starvation-resistance queue

Definition at line 544 of file arena.cpp.

545 {
546 #if __TBB_RECYCLE_TO_ENQUEUE
547  __TBB_ASSERT( t.state()==task::allocated || t.state()==task::to_enqueue, "attempt to enqueue task with inappropriate state" );
548 #else
549  __TBB_ASSERT( t.state()==task::allocated, "attempt to enqueue task that is not in 'allocated' state" );
550 #endif
551  t.prefix().state = task::ready;
552  t.prefix().extra_state |= es_task_enqueued; // enqueued task marker
553 
554 #if TBB_USE_ASSERT
555  if( task* parent = t.parent() ) {
556  internal::reference_count ref_count = parent->prefix().ref_count;
557  __TBB_ASSERT( ref_count!=0, "attempt to enqueue task whose parent has a ref_count==0 (forgot to set_ref_count?)" );
558  __TBB_ASSERT( ref_count>0, "attempt to enqueue task whose parent has a ref_count<0" );
559  parent->prefix().extra_state |= es_ref_count_active;
560  }
561  __TBB_ASSERT(t.prefix().affinity==affinity_id(0), "affinity is ignored for enqueued tasks");
562 #endif /* TBB_USE_ASSERT */
563 
565 #if __TBB_TASK_PRIORITY
566  intptr_t p = prio ? normalize_priority(priority_t(prio)) : normalized_normal_priority;
567  assert_priority_valid(p);
568  my_task_stream.push( &t, p, random );
569  if ( p != my_top_priority )
570  my_market->update_arena_priority( *this, p );
571 #else /* !__TBB_TASK_PRIORITY */
572  __TBB_ASSERT_EX(prio == 0, "the library is not configured to respect the task priority");
573  my_task_stream.push( &t, 0, random );
574 #endif /* !__TBB_TASK_PRIORITY */
575  advertise_new_work<work_enqueued>();
576 #if __TBB_TASK_PRIORITY
577  if ( p != my_top_priority )
578  my_market->update_arena_priority( *this, p );
579 #endif /* __TBB_TASK_PRIORITY */
580 }
task object is freshly allocated or recycled.
Definition: task.h:604
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task * task
#define __TBB_ASSERT_EX(predicate, comment)
"Extended" version is useful to suppress warnings if a variable is only used with an assert
Definition: tbb_stddef.h:171
market * my_market
The market that owns this arena.
Definition: arena.h:118
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id parent
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:169
void const char const char int ITT_FORMAT __itt_group_sync p
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p sync_releasing
#define ITT_NOTIFY(name, obj)
Definition: itt_notify.h:117
task is in ready pool, or is going to be put there, or was just taken off.
Definition: task.h:602
task_stream< num_priority_levels > my_task_stream
Task pool for the tasks scheduled via task::enqueue() method.
Definition: arena.h:73
Set if ref_count might be changed by another thread. Used for debugging.
unsigned short affinity_id
An id as used for specifying affinity.
Definition: task.h:122
intptr_t reference_count
A reference count.
Definition: task.h:119
void push(task *source, int level, FastRandom &random)
Push a task into a lane.
Definition: task_stream.h:105
priority_t
Definition: task.h:279

References __TBB_ASSERT, __TBB_ASSERT_EX, tbb::task::allocated, tbb::internal::es_ref_count_active, tbb::internal::es_task_enqueued, ITT_NOTIFY, tbb::internal::arena_base::my_market, tbb::internal::arena_base::my_task_stream, p, parent, tbb::task::parent(), tbb::task::prefix(), tbb::internal::task_stream< Levels >::push(), tbb::task::ready, tbb::task::state(), and sync_releasing.

Referenced by tbb::internal::custom_scheduler< SchedulerTraits >::tally_completion_of_predecessor().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ free_arena()

void tbb::internal::arena::free_arena ( )

Completes arena shutdown, destructs and deallocates it.

Definition at line 246 of file arena.cpp.

246  {
247  __TBB_ASSERT( is_alive(my_guard), NULL );
248  __TBB_ASSERT( !my_references, "There are threads in the dying arena" );
249  __TBB_ASSERT( !my_num_workers_requested && !my_num_workers_allotted, "Dying arena requests workers" );
250  __TBB_ASSERT( my_pool_state == SNAPSHOT_EMPTY || !my_max_num_workers, "Inconsistent state of a dying arena" );
251 #if __TBB_ENQUEUE_ENFORCED_CONCURRENCY
252  __TBB_ASSERT( my_concurrency_mode != cm_enforced_global, NULL );
253 #endif
254 #if !__TBB_STATISTICS_EARLY_DUMP
255  GATHER_STATISTIC( dump_arena_statistics() );
256 #endif
257  poison_value( my_guard );
258  intptr_t drained = 0;
259  for ( unsigned i = 0; i < my_num_slots; ++i ) {
260  __TBB_ASSERT( !my_slots[i].my_scheduler, "arena slot is not empty" );
261  // TODO: understand the assertion and modify
262  // __TBB_ASSERT( my_slots[i].task_pool == EmptyTaskPool, NULL );
263  __TBB_ASSERT( my_slots[i].head == my_slots[i].tail, NULL ); // TODO: replace by is_quiescent_local_task_pool_empty
265 #if __TBB_STATISTICS
266  NFS_Free( my_slots[i].my_counters );
267 #endif /* __TBB_STATISTICS */
268  drained += mailbox(i+1).drain();
269  }
270  __TBB_ASSERT( my_task_stream.drain()==0, "Not all enqueued tasks were executed");
271 #if __TBB_COUNT_TASK_NODES
272  my_market->update_task_node_count( -drained );
273 #endif /* __TBB_COUNT_TASK_NODES */
274  // remove an internal reference
275  my_market->release( /*is_public=*/false, /*blocking_terminate=*/false );
276 #if __TBB_TASK_GROUP_CONTEXT
277  __TBB_ASSERT( my_default_ctx, "Master thread never entered the arena?" );
278  my_default_ctx->~task_group_context();
279  NFS_Free(my_default_ctx);
280 #endif /* __TBB_TASK_GROUP_CONTEXT */
281 #if __TBB_ARENA_OBSERVER
282  if ( !my_observers.empty() )
283  my_observers.clear();
284 #endif /* __TBB_ARENA_OBSERVER */
285  void* storage = &mailbox(my_num_slots);
286  __TBB_ASSERT( my_references == 0, NULL );
288  this->~arena();
289 #if TBB_USE_ASSERT > 1
290  memset( storage, 0, allocation_size(my_num_slots) );
291 #endif /* TBB_USE_ASSERT */
292  NFS_Free( storage );
293 }
unsigned my_max_num_workers
The number of workers requested by the master thread owning the arena.
Definition: arena.h:76
int my_num_workers_requested
The number of workers that are currently requested from the resource manager.
Definition: arena.h:79
static int allocation_size(unsigned num_slots)
Definition: arena.h:186
market * my_market
The market that owns this arena.
Definition: arena.h:118
bool release(bool is_public, bool blocking_terminate)
Decrements market's refcount and destroys it in the end.
Definition: market.cpp:179
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:169
void free_task_pool()
Deallocate task pool that was allocated by means of allocate_task_pool.
static const pool_state_t SNAPSHOT_EMPTY
No tasks to steal since last snapshot was taken.
Definition: arena.h:204
unsigned my_num_workers_allotted
The number of workers that have been marked out by the resource manager to service the arena.
Definition: arena.h:51
intptr_t drain()
Destroys all remaining tasks in every lane. Returns the number of destroyed tasks.
Definition: task_stream.h:149
task_stream< num_priority_levels > my_task_stream
Task pool for the tasks scheduled via task::enqueue() method.
Definition: arena.h:73
unsigned my_num_slots
The number of slots in the arena.
Definition: arena.h:136
atomic< unsigned > my_references
Reference counter for the arena.
Definition: arena.h:57
tbb::atomic< uintptr_t > my_pool_state
Current task pool state and estimate of available tasks amount.
Definition: arena.h:86
arena(market &, unsigned max_num_workers, unsigned num_reserved_slots)
Constructor.
Definition: arena.cpp:190
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain ITT_FORMAT p const __itt_domain __itt_string_handle unsigned long long ITT_FORMAT lu const __itt_domain __itt_id __itt_string_handle __itt_metadata_type size_t void ITT_FORMAT p const __itt_domain __itt_id __itt_string_handle const wchar_t size_t ITT_FORMAT lu const __itt_domain __itt_id head
mail_outbox & mailbox(affinity_id id)
Get reference to mailbox corresponding to given affinity_id.
Definition: arena.h:191
void __TBB_EXPORTED_FUNC NFS_Free(void *)
Free memory allocated by NFS_Allocate.
arena_slot my_slots[1]
Definition: arena.h:283
intptr_t drain()
Drain the mailbox.
Definition: mailbox.h:172
#define GATHER_STATISTIC(x)
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain ITT_FORMAT p const __itt_domain __itt_string_handle unsigned long long ITT_FORMAT lu const __itt_domain __itt_id __itt_string_handle __itt_metadata_type size_t void ITT_FORMAT p const __itt_domain __itt_id __itt_string_handle const wchar_t size_t ITT_FORMAT lu const __itt_domain __itt_id __itt_relation __itt_id tail
#define poison_value(g)

References __TBB_ASSERT, allocation_size(), tbb::internal::task_stream< Levels >::drain(), tbb::internal::mail_outbox::drain(), tbb::internal::arena_slot::free_task_pool(), GATHER_STATISTIC, head, mailbox(), tbb::internal::arena_base::my_market, tbb::internal::arena_base::my_max_num_workers, tbb::internal::arena_base::my_num_slots, tbb::internal::arena_base::my_num_workers_allotted, tbb::internal::arena_base::my_num_workers_requested, tbb::internal::arena_base::my_pool_state, tbb::internal::arena_base::my_references, my_slots, tbb::internal::arena_base::my_task_stream, tbb::internal::NFS_Free(), poison_value, tbb::internal::market::release(), SNAPSHOT_EMPTY, and tail.

Referenced by tbb::internal::market::try_destroy_arena().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ has_enqueued_tasks()

bool tbb::internal::arena::has_enqueued_tasks ( )

Check for the presence of enqueued tasks at all priority levels.

Definition at line 369 of file arena.cpp.

369  {
370  // Look for enqueued tasks at all priority levels
371  for ( int p = 0; p < num_priority_levels; ++p )
372  if ( !my_task_stream.empty(p) )
373  return true;
374  return false;
375 }
bool empty(int level)
Checks existence of a task.
Definition: task_stream.h:142
static const intptr_t num_priority_levels
void const char const char int ITT_FORMAT __itt_group_sync p
task_stream< num_priority_levels > my_task_stream
Task pool for the tasks scheduled via task::enqueue() method.
Definition: arena.h:73

References tbb::internal::task_stream< Levels >::empty(), tbb::internal::arena_base::my_task_stream, tbb::internal::num_priority_levels, and p.

Referenced by restore_priority_if_need().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_busy_or_empty()

static bool tbb::internal::arena::is_busy_or_empty ( pool_state_t  s)
inlinestatic

No tasks to steal or snapshot is being taken.

Definition at line 217 of file arena.h.

217 { return s < SNAPSHOT_FULL; }
void const char const char int ITT_FORMAT __itt_group_sync s
static const pool_state_t SNAPSHOT_FULL
At least one task has been offered for stealing since the last snapshot started.
Definition: arena.h:207

References s, and SNAPSHOT_FULL.

Referenced by advertise_new_work().

Here is the caller graph for this function:

◆ is_out_of_work()

bool tbb::internal::arena::is_out_of_work ( )

Check if there is job anywhere in arena.

Return true if no job or if arena is being cleaned up.

Definition at line 397 of file arena.cpp.

397  {
398  // TODO: rework it to return at least a hint about where a task was found; better if the task itself.
399  for(;;) {
400  pool_state_t snapshot = my_pool_state;
401  switch( snapshot ) {
402  case SNAPSHOT_EMPTY:
403  return true;
404  case SNAPSHOT_FULL: {
405  // Use unique id for "busy" in order to avoid ABA problems.
406  const pool_state_t busy = pool_state_t(&busy);
407  // Request permission to take snapshot
409  // Got permission. Take the snapshot.
410  // NOTE: This is not a lock, as the state can be set to FULL at
411  // any moment by a thread that spawns/enqueues new task.
412  size_t n = my_limit;
413  // Make local copies of volatile parameters. Their change during
414  // snapshot taking procedure invalidates the attempt, and returns
415  // this thread into the dispatch loop.
416 #if __TBB_TASK_PRIORITY
417  uintptr_t reload_epoch = __TBB_load_with_acquire( my_reload_epoch );
418  intptr_t top_priority = my_top_priority;
419  // Inspect primary task pools first
420 #endif /* __TBB_TASK_PRIORITY */
421  size_t k;
422  for( k=0; k<n; ++k ) {
423  if( my_slots[k].task_pool != EmptyTaskPool &&
425  {
426  // k-th primary task pool is nonempty and does contain tasks.
427  break;
428  }
429  if( my_pool_state!=busy )
430  return false; // the work was published
431  }
432  __TBB_ASSERT( k <= n, NULL );
433  bool work_absent = k == n;
434 #if __TBB_TASK_PRIORITY
435  // Variable tasks_present indicates presence of tasks at any priority
436  // level, while work_absent refers only to the current priority.
437  bool tasks_present = !work_absent || my_orphaned_tasks;
438  bool dequeuing_possible = false;
439  if ( work_absent ) {
440  // Check for the possibility that recent priority changes
441  // brought some tasks to the current priority level
442 
443  uintptr_t abandonment_epoch = my_abandonment_epoch;
444  // Master thread's scheduler needs special handling as it
445  // may be destroyed at any moment (workers' schedulers are
446  // guaranteed to be alive while at least one thread is in arena).
447  // The lock below excludes concurrency with task group state change
448  // propagation and guarantees lifetime of the master thread.
449  the_context_state_propagation_mutex.lock();
450  work_absent = !may_have_tasks( my_slots[0].my_scheduler, tasks_present, dequeuing_possible );
451  the_context_state_propagation_mutex.unlock();
452  // The following loop is subject to data races. While k-th slot's
453  // scheduler is being examined, corresponding worker can either
454  // leave to RML or migrate to another arena.
455  // But the races are not prevented because all of them are benign.
456  // First, the code relies on the fact that worker thread's scheduler
457  // object persists until the whole library is deinitialized.
458  // Second, in the worst case the races can only cause another
459  // round of stealing attempts to be undertaken. Introducing complex
460  // synchronization into this coldest part of the scheduler's control
461  // flow does not seem to make sense because it both is unlikely to
462  // ever have any observable performance effect, and will require
463  // additional synchronization code on the hotter paths.
464  for( k = 1; work_absent && k < n; ++k ) {
465  if( my_pool_state!=busy )
466  return false; // the work was published
467  work_absent = !may_have_tasks( my_slots[k].my_scheduler, tasks_present, dequeuing_possible );
468  }
469  // Preclude premature switching arena off because of a race in the previous loop.
470  work_absent = work_absent
471  && !__TBB_load_with_acquire(my_orphaned_tasks)
472  && abandonment_epoch == my_abandonment_epoch;
473  }
474 #endif /* __TBB_TASK_PRIORITY */
475  // Test and test-and-set.
476  if( my_pool_state==busy ) {
477 #if __TBB_TASK_PRIORITY
478  bool no_fifo_tasks = my_task_stream.empty(top_priority);
479  work_absent = work_absent && (!dequeuing_possible || no_fifo_tasks)
480  && top_priority == my_top_priority && reload_epoch == my_reload_epoch;
481 #else
482  bool no_fifo_tasks = my_task_stream.empty(0);
483  work_absent = work_absent && no_fifo_tasks;
484 #endif /* __TBB_TASK_PRIORITY */
485  if( work_absent ) {
486 #if __TBB_TASK_PRIORITY
487  if ( top_priority > my_bottom_priority ) {
488  if ( my_market->lower_arena_priority(*this, top_priority - 1, reload_epoch)
489  && !my_task_stream.empty(top_priority) )
490  {
491  atomic_update( my_skipped_fifo_priority, top_priority, std::less<intptr_t>());
492  }
493  }
494  else if ( !tasks_present && !my_orphaned_tasks && no_fifo_tasks ) {
495 #endif /* __TBB_TASK_PRIORITY */
496  // save current demand value before setting SNAPSHOT_EMPTY,
497  // to avoid race with advertise_new_work.
498  int current_demand = (int)my_max_num_workers;
499  if( my_pool_state.compare_and_swap( SNAPSHOT_EMPTY, busy )==busy ) {
500 #if __TBB_ENQUEUE_ENFORCED_CONCURRENCY
501  if( my_concurrency_mode==cm_enforced_global ) {
502  // adjust_demand() called inside, if needed
503  my_market->mandatory_concurrency_disable( this );
504  } else
505 #endif /* __TBB_ENQUEUE_ENFORCED_CONCURRENCY */
506  {
507  // This thread transitioned pool to empty state, and thus is
508  // responsible for telling the market that there is no work to do.
509  my_market->adjust_demand( *this, -current_demand );
510  }
512  return true;
513  }
514  return false;
515 #if __TBB_TASK_PRIORITY
516  }
517 #endif /* __TBB_TASK_PRIORITY */
518  }
519  // Undo previous transition SNAPSHOT_FULL-->busy, unless another thread undid it.
521  }
522  }
523  return false;
524  }
525  default:
526  // Another thread is taking a snapshot.
527  return false;
528  }
529  }
530 }
unsigned my_max_num_workers
The number of workers requested by the master thread owning the arena.
Definition: arena.h:76
T1 atomic_update(tbb::atomic< T1 > &dst, T2 newValue, Pred compare)
Atomically replaces value of dst with newValue if they satisfy condition of compare predicate.
Definition: tbb_misc.h:185
market * my_market
The market that owns this arena.
Definition: arena.h:118
bool empty(int level)
Checks existence of a task.
Definition: task_stream.h:142
static const pool_state_t SNAPSHOT_FULL
At least one task has been offered for stealing since the last snapshot started.
Definition: arena.h:207
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:169
void adjust_demand(arena &, int delta)
Request that arena's need in workers should be adjusted.
Definition: market.cpp:590
T __TBB_load_with_acquire(const volatile T &location)
Definition: tbb_machine.h:713
static const pool_state_t SNAPSHOT_EMPTY
No tasks to steal since last snapshot was taken.
Definition: arena.h:204
uintptr_t pool_state_t
Definition: arena.h:201
task_stream< num_priority_levels > my_task_stream
Task pool for the tasks scheduled via task::enqueue() method.
Definition: arena.h:73
atomic< unsigned > my_limit
The maximal number of currently busy slots.
Definition: arena.h:65
value_type compare_and_swap(value_type value, value_type comparand)
Definition: atomic.h:289
void restore_priority_if_need()
If enqueued tasks found, restore arena priority and task presence status.
Definition: arena.cpp:377
tbb::atomic< uintptr_t > my_pool_state
Current task pool state and estimate of available tasks amount.
Definition: arena.h:86
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain ITT_FORMAT p const __itt_domain __itt_string_handle unsigned long long ITT_FORMAT lu const __itt_domain __itt_id __itt_string_handle __itt_metadata_type size_t void ITT_FORMAT p const __itt_domain __itt_id __itt_string_handle const wchar_t size_t ITT_FORMAT lu const __itt_domain __itt_id __itt_relation __itt_id ITT_FORMAT p const wchar_t int ITT_FORMAT __itt_group_mark d int
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain ITT_FORMAT p const __itt_domain __itt_string_handle unsigned long long ITT_FORMAT lu const __itt_domain __itt_id __itt_string_handle __itt_metadata_type size_t void ITT_FORMAT p const __itt_domain __itt_id __itt_string_handle const wchar_t size_t ITT_FORMAT lu const __itt_domain __itt_id head
T __TBB_load_relaxed(const volatile T &location)
Definition: tbb_machine.h:739
arena_slot my_slots[1]
Definition: arena.h:283
#define EmptyTaskPool
Definition: scheduler.h:46
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain ITT_FORMAT p const __itt_domain __itt_string_handle unsigned long long ITT_FORMAT lu const __itt_domain __itt_id __itt_string_handle __itt_metadata_type size_t void ITT_FORMAT p const __itt_domain __itt_id __itt_string_handle const wchar_t size_t ITT_FORMAT lu const __itt_domain __itt_id __itt_relation __itt_id tail

References __TBB_ASSERT, tbb::internal::__TBB_load_relaxed(), tbb::internal::__TBB_load_with_acquire(), tbb::internal::market::adjust_demand(), tbb::internal::atomic_update(), tbb::internal::atomic_impl< T >::compare_and_swap(), tbb::internal::task_stream< Levels >::empty(), EmptyTaskPool, head, int, tbb::internal::arena_base::my_limit, tbb::internal::arena_base::my_market, tbb::internal::arena_base::my_max_num_workers, tbb::internal::arena_base::my_pool_state, my_slots, tbb::internal::arena_base::my_task_stream, restore_priority_if_need(), SNAPSHOT_EMPTY, SNAPSHOT_FULL, and tail.

Referenced by on_thread_leaving().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mailbox()

mail_outbox& tbb::internal::arena::mailbox ( affinity_id  id)
inline

Get reference to mailbox corresponding to given affinity_id.

Definition at line 191 of file arena.h.

191  {
192  __TBB_ASSERT( 0<id, "affinity id must be positive integer" );
193  __TBB_ASSERT( id <= my_num_slots, "affinity id out of bounds" );
194 
195  return ((mail_outbox*)this)[-(int)id];
196  }
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:169
unsigned my_num_slots
The number of slots in the arena.
Definition: arena.h:136
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain ITT_FORMAT p const __itt_domain __itt_string_handle unsigned long long ITT_FORMAT lu const __itt_domain __itt_id __itt_string_handle __itt_metadata_type size_t void ITT_FORMAT p const __itt_domain __itt_id __itt_string_handle const wchar_t size_t ITT_FORMAT lu const __itt_domain __itt_id __itt_relation __itt_id ITT_FORMAT p const wchar_t int ITT_FORMAT __itt_group_mark d int

References __TBB_ASSERT, int, and tbb::internal::arena_base::my_num_slots.

Referenced by arena(), tbb::internal::generic_scheduler::attach_mailbox(), free_arena(), and tbb::internal::generic_scheduler::prepare_for_spawning().

Here is the caller graph for this function:

◆ num_arena_slots()

static int unsigned tbb::internal::arena::num_arena_slots ( unsigned  num_slots)
inlinestatic

Definition at line 182 of file arena.h.

182  {
183  return max(2u, num_slots);
184  }
T max(const T &val1, const T &val2)
Utility template function returning greater of the two values.
Definition: tbb_misc.h:118

References tbb::internal::max().

Referenced by allocate_arena(), arena(), and tbb::interface7::internal::task_arena_base::internal_attach().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ num_workers_active()

unsigned tbb::internal::arena::num_workers_active ( )
inline

The number of workers active in the arena.

Definition at line 220 of file arena.h.

220  {
222  }
atomic< unsigned > my_references
Reference counter for the arena.
Definition: arena.h:57
static const unsigned ref_external_bits
The number of least significant bits for external references.
Definition: arena.h:210

References tbb::internal::arena_base::my_references, and ref_external_bits.

Referenced by tbb::internal::market::arena_in_need(), and process().

Here is the caller graph for this function:

◆ occupy_free_slot()

template<bool as_worker>
size_t tbb::internal::arena::occupy_free_slot ( generic_scheduler s)

Tries to occupy a slot in the arena. On success, returns the slot index; if no slot is available, returns out_of_arena.

Definition at line 90 of file arena.cpp.

90  {
91  // Firstly, masters try to occupy reserved slots
92  size_t index = as_worker ? out_of_arena : occupy_free_slot_in_range( s, 0, my_num_reserved_slots );
93  if ( index == out_of_arena ) {
94  // Secondly, all threads try to occupy all non-reserved slots
96  // Likely this arena is already saturated
97  if ( index == out_of_arena )
98  return out_of_arena;
99  }
100 
101  ITT_NOTIFY(sync_acquired, my_slots + index);
102  atomic_update( my_limit, (unsigned)(index + 1), std::less<unsigned>() );
103  return index;
104 }
T1 atomic_update(tbb::atomic< T1 > &dst, T2 newValue, Pred compare)
Atomically replaces value of dst with newValue if they satisfy condition of compare predicate.
Definition: tbb_misc.h:185
void const char const char int ITT_FORMAT __itt_group_sync s
static const size_t out_of_arena
Definition: arena.h:275
#define ITT_NOTIFY(name, obj)
Definition: itt_notify.h:117
atomic< unsigned > my_limit
The maximal number of currently busy slots.
Definition: arena.h:65
unsigned my_num_reserved_slots
The number of reserved slots (can be occupied only by masters).
Definition: arena.h:139
unsigned my_num_slots
The number of slots in the arena.
Definition: arena.h:136
size_t occupy_free_slot_in_range(generic_scheduler &s, size_t lower, size_t upper)
Tries to occupy a slot in the specified range.
Definition: arena.cpp:75
arena_slot my_slots[1]
Definition: arena.h:283

References tbb::internal::atomic_update(), ITT_NOTIFY, tbb::internal::arena_base::my_limit, tbb::internal::arena_base::my_num_reserved_slots, tbb::internal::arena_base::my_num_slots, my_slots, occupy_free_slot_in_range(), out_of_arena, and s.

Referenced by process().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ occupy_free_slot_in_range()

size_t tbb::internal::arena::occupy_free_slot_in_range ( generic_scheduler s,
size_t  lower,
size_t  upper 
)

Tries to occupy a slot in the specified range.

Definition at line 75 of file arena.cpp.

75  {
76  if ( lower >= upper ) return out_of_arena;
77  // Start search for an empty slot from the one we occupied the last time
78  size_t index = s.my_arena_index;
79  if ( index < lower || index >= upper ) index = s.my_random.get() % (upper - lower) + lower;
80  __TBB_ASSERT( index >= lower && index < upper, NULL );
81  // Find a free slot
82  for ( size_t i = index; i < upper; ++i )
83  if ( occupy_slot(my_slots[i].my_scheduler, s) ) return i;
84  for ( size_t i = lower; i < index; ++i )
85  if ( occupy_slot(my_slots[i].my_scheduler, s) ) return i;
86  return out_of_arena;
87 }
void const char const char int ITT_FORMAT __itt_group_sync s
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:169
static const size_t out_of_arena
Definition: arena.h:275
arena_slot my_slots[1]
Definition: arena.h:283
static bool occupy_slot(generic_scheduler *&slot, generic_scheduler &s)
Definition: arena.cpp:71

References __TBB_ASSERT, my_slots, tbb::internal::occupy_slot(), out_of_arena, and s.

Referenced by occupy_free_slot().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ on_thread_leaving()

template<unsigned ref_param>
void tbb::internal::arena::on_thread_leaving ( )
inline

Notification that worker or master leaves its arena.

Definition at line 287 of file arena.h.

287  {
288  //
289  // Implementation of arena destruction synchronization logic contained various
290  // bugs/flaws at the different stages of its evolution, so below is a detailed
291  // description of the issues taken into consideration in the framework of the
292  // current design.
293  //
294  // In case of using fire-and-forget tasks (scheduled via task::enqueue())
295  // master thread is allowed to leave its arena before all its work is executed,
296  // and market may temporarily revoke all workers from this arena. Since revoked
297  // workers never attempt to reset arena state to EMPTY and cancel its request
298  // to RML for threads, the arena object is destroyed only when both the last
299  // thread is leaving it and arena's state is EMPTY (that is its master thread
300  // left and it does not contain any work).
301  // Thus resetting arena to EMPTY state (as earlier TBB versions did) should not
302  // be done here (or anywhere else in the master thread to that matter); doing so
303  // can result either in arena's premature destruction (at least without
304  // additional costly checks in workers) or in unnecessary arena state changes
305  // (and ensuing workers migration).
306  //
307  // A worker that checks for work presence and transitions arena to the EMPTY
308  // state (in snapshot taking procedure arena::is_out_of_work()) updates
309  // arena::my_pool_state first and only then arena::my_num_workers_requested.
310  // So the check for work absence must be done against the latter field.
311  //
312  // In a time window between decrementing the active threads count and checking
313  // if there is an outstanding request for workers. New worker thread may arrive,
314  // finish remaining work, set arena state to empty, and leave decrementing its
315  // refcount and destroying. Then the current thread will destroy the arena
316  // the second time. To preclude it a local copy of the outstanding request
317  // value can be stored before decrementing active threads count.
318  //
319  // But this technique may cause two other problem. When the stored request is
320  // zero, it is possible that arena still has threads and they can generate new
321  // tasks and thus re-establish non-zero requests. Then all the threads can be
322  // revoked (as described above) leaving this thread the last one, and causing
323  // it to destroy non-empty arena.
324  //
325  // The other problem takes place when the stored request is non-zero. Another
326  // thread may complete the work, set arena state to empty, and leave without
327  // arena destruction before this thread decrements the refcount. This thread
328  // cannot destroy the arena either. Thus the arena may be "orphaned".
329  //
330  // In both cases we cannot dereference arena pointer after the refcount is
331  // decremented, as our arena may already be destroyed.
332  //
333  // If this is the master thread, the market is protected by refcount to it.
334  // In case of workers market's liveness is ensured by the RML connection
335  // rundown protocol, according to which the client (i.e. the market) lives
336  // until RML server notifies it about connection termination, and this
337  // notification is fired only after all workers return into RML.
338  //
339  // Thus if we decremented refcount to zero we ask the market to check arena
340  // state (including the fact if it is alive) under the lock.
341  //
342  uintptr_t aba_epoch = my_aba_epoch;
343  market* m = my_market;
344  __TBB_ASSERT(my_references >= ref_param, "broken arena reference counter");
345 #if __TBB_STATISTICS_EARLY_DUMP
346  // While still holding a reference to the arena, compute how many external references are left.
347  // If just one, dump statistics.
348  if ( modulo_power_of_two(my_references,ref_worker)==ref_param ) // may only be true with ref_external
349  GATHER_STATISTIC( dump_arena_statistics() );
350 #endif
351 #if __TBB_ENQUEUE_ENFORCED_CONCURRENCY
352  // When there is no workers someone must free arena, as
353  // without workers, no one calls is_out_of_work().
354  // Skip workerless arenas because they have no demand for workers.
355  // TODO: consider more strict conditions for the cleanup,
356  // because it can create the demand of workers,
357  // but the arena can be already empty (and so ready for destroying)
358  if( ref_param==ref_external && my_num_slots != my_num_reserved_slots
359  && 0 == m->my_num_workers_soft_limit && my_concurrency_mode==cm_normal ) {
360  bool is_out = false;
361  for (int i=0; i<num_priority_levels; i++) {
362  is_out = is_out_of_work();
363  if (is_out)
364  break;
365  }
366  // We expect, that in worst case it's enough to have num_priority_levels-1
367  // calls to restore priorities and and yet another is_out_of_work() to conform
368  // that no work was found. But as market::set_active_num_workers() can be called
369  // concurrently, can't guarantee last is_out_of_work() return true.
370  }
371 #endif
372  if ( (my_references -= ref_param ) == 0 )
373  m->try_destroy_arena( this, aba_epoch );
374 }
market * my_market
The market that owns this arena.
Definition: arena.h:118
static const intptr_t num_priority_levels
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:169
bool is_out_of_work()
Check if there is job anywhere in arena.
Definition: arena.cpp:397
static const unsigned ref_external
Reference increment values for externals and workers.
Definition: arena.h:213
unsigned my_num_reserved_slots
The number of reserved slots (can be occupied only by masters).
Definition: arena.h:139
static const unsigned ref_worker
Definition: arena.h:214
unsigned my_num_slots
The number of slots in the arena.
Definition: arena.h:136
atomic< unsigned > my_references
Reference counter for the arena.
Definition: arena.h:57
argument_integer_type modulo_power_of_two(argument_integer_type arg, divisor_integer_type divisor)
A function to compute arg modulo divisor where divisor is a power of 2.
Definition: tbb_stddef.h:365
uintptr_t my_aba_epoch
ABA prevention marker.
Definition: arena.h:121
#define GATHER_STATISTIC(x)

References __TBB_ASSERT, GATHER_STATISTIC, is_out_of_work(), tbb::internal::modulo_power_of_two(), tbb::internal::arena_base::my_aba_epoch, tbb::internal::arena_base::my_market, tbb::internal::arena_base::my_num_reserved_slots, tbb::internal::arena_base::my_num_slots, tbb::internal::market::my_num_workers_soft_limit, tbb::internal::arena_base::my_references, tbb::internal::num_priority_levels, ref_external, ref_worker, and tbb::internal::market::try_destroy_arena().

Referenced by tbb::internal::generic_scheduler::cleanup_master(), and tbb::interface7::internal::task_arena_base::internal_initialize().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ process()

void tbb::internal::arena::process ( generic_scheduler s)

Registers the worker with the arena and enters TBB scheduler dispatch loop.

Definition at line 106 of file arena.cpp.

106  {
107  __TBB_ASSERT( is_alive(my_guard), NULL );
108  __TBB_ASSERT( governor::is_set(&s), NULL );
109  __TBB_ASSERT( s.my_innermost_running_task == s.my_dummy_task, NULL );
110  __TBB_ASSERT( s.worker_outermost_level(), NULL );
111 
112  __TBB_ASSERT( my_num_slots > 1, NULL );
113 
114  size_t index = occupy_free_slot</*as_worker*/true>( s );
115  if ( index == out_of_arena )
116  goto quit;
117 
118  __TBB_ASSERT( index >= my_num_reserved_slots, "Workers cannot occupy reserved slots" );
119  s.attach_arena( this, index, /*is_master*/false );
120 
121 #if !__TBB_FP_CONTEXT
123 #endif
124 
125 #if __TBB_ARENA_OBSERVER
126  __TBB_ASSERT( !s.my_last_local_observer, "There cannot be notified local observers when entering arena" );
127  my_observers.notify_entry_observers( s.my_last_local_observer, /*worker=*/true );
128 #endif /* __TBB_ARENA_OBSERVER */
129 
130  // Task pool can be marked as non-empty if the worker occupies the slot left by a master.
131  if ( s.my_arena_slot->task_pool != EmptyTaskPool ) {
132  __TBB_ASSERT( s.my_inbox.is_idle_state(false), NULL );
133  s.local_wait_for_all( *s.my_dummy_task, NULL );
134  __TBB_ASSERT( s.my_inbox.is_idle_state(true), NULL );
135  }
136 
137  for ( ;; ) {
138  __TBB_ASSERT( s.my_innermost_running_task == s.my_dummy_task, NULL );
139  __TBB_ASSERT( s.worker_outermost_level(), NULL );
140  __TBB_ASSERT( is_alive(my_guard), NULL );
141  __TBB_ASSERT( s.is_quiescent_local_task_pool_reset(),
142  "Worker cannot leave arena while its task pool is not reset" );
143  __TBB_ASSERT( s.my_arena_slot->task_pool == EmptyTaskPool, "Empty task pool is not marked appropriately" );
144  // This check prevents relinquishing more than necessary workers because
145  // of the non-atomicity of the decision making procedure
148  || recall_by_mandatory_request()
149 #endif
150  )
151  break;
152  // Try to steal a task.
153  // Passing reference count is technically unnecessary in this context,
154  // but omitting it here would add checks inside the function.
155  task* t = s.receive_or_steal_task( __TBB_ISOLATION_ARG( s.my_dummy_task->prefix().ref_count, no_isolation ) );
156  if (t) {
157  // A side effect of receive_or_steal_task is that my_innermost_running_task can be set.
158  // But for the outermost dispatch loop it has to be a dummy task.
159  s.my_innermost_running_task = s.my_dummy_task;
160  s.local_wait_for_all(*s.my_dummy_task,t);
161  }
162  }
163 #if __TBB_ARENA_OBSERVER
164  my_observers.notify_exit_observers( s.my_last_local_observer, /*worker=*/true );
165  s.my_last_local_observer = NULL;
166 #endif /* __TBB_ARENA_OBSERVER */
167 #if __TBB_TASK_PRIORITY
168  if ( s.my_offloaded_tasks )
169  orphan_offloaded_tasks( s );
170 #endif /* __TBB_TASK_PRIORITY */
171 #if __TBB_STATISTICS
172  ++s.my_counters.arena_roundtrips;
173  *my_slots[index].my_counters += s.my_counters;
174  s.my_counters.reset();
175 #endif /* __TBB_STATISTICS */
176  __TBB_store_with_release( my_slots[index].my_scheduler, (generic_scheduler*)NULL );
177  s.my_arena_slot = 0; // detached from slot
178  s.my_inbox.detach();
179  __TBB_ASSERT( s.my_inbox.is_idle_state(true), NULL );
180  __TBB_ASSERT( s.my_innermost_running_task == s.my_dummy_task, NULL );
181  __TBB_ASSERT( s.worker_outermost_level(), NULL );
182  __TBB_ASSERT( is_alive(my_guard), NULL );
183 quit:
184  // In contrast to earlier versions of TBB (before 3.0 U5) now it is possible
185  // that arena may be temporarily left unpopulated by threads. See comments in
186  // arena::on_thread_leaving() for more details.
187  on_thread_leaving<ref_worker>();
188 }
#define __TBB_ISOLATION_ARG(arg1, isolation)
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task * task
void __TBB_store_with_release(volatile T &location, V value)
Definition: tbb_machine.h:717
void const char const char int ITT_FORMAT __itt_group_sync s
unsigned num_workers_active()
The number of workers active in the arena.
Definition: arena.h:220
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:169
static const size_t out_of_arena
Definition: arena.h:275
static bool is_set(generic_scheduler *s)
Used to check validity of the local scheduler TLS contents.
Definition: governor.cpp:124
unsigned my_num_workers_allotted
The number of workers that have been marked out by the resource manager to service the arena.
Definition: arena.h:51
size_t occupy_free_slot(generic_scheduler &s)
Tries to occupy a slot in the arena. On success, returns the slot index; if no slot is available,...
Definition: arena.cpp:90
unsigned my_num_reserved_slots
The number of reserved slots (can be occupied only by masters).
Definition: arena.h:139
unsigned my_num_slots
The number of slots in the arena.
Definition: arena.h:136
cpu_ctl_env my_cpu_ctl_env
FPU control settings of arena's master thread captured at the moment of arena instantiation.
Definition: arena.h:125
#define __TBB_ENQUEUE_ENFORCED_CONCURRENCY
Definition: tbb_config.h:581
const isolation_tag no_isolation
Definition: task.h:127
arena_slot my_slots[1]
Definition: arena.h:283
#define EmptyTaskPool
Definition: scheduler.h:46

References __TBB_ASSERT, __TBB_ENQUEUE_ENFORCED_CONCURRENCY, __TBB_ISOLATION_ARG, tbb::internal::__TBB_store_with_release(), EmptyTaskPool, tbb::internal::governor::is_set(), tbb::internal::arena_base::my_cpu_ctl_env, tbb::internal::arena_base::my_num_reserved_slots, tbb::internal::arena_base::my_num_slots, tbb::internal::arena_base::my_num_workers_allotted, my_slots, tbb::internal::no_isolation, num_workers_active(), occupy_free_slot(), out_of_arena, s, and tbb::internal::cpu_ctl_env::set_env().

Referenced by tbb::internal::market::process().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ restore_priority_if_need()

void tbb::internal::arena::restore_priority_if_need ( )
private

If enqueued tasks found, restore arena priority and task presence status.

Definition at line 377 of file arena.cpp.

377  {
378  // Check for the presence of enqueued tasks "lost" on some of
379  // priority levels because updating arena priority and switching
380  // arena into "populated" (FULL) state happen non-atomically.
381  // Imposing atomicity would require task::enqueue() to use a lock,
382  // which is unacceptable.
383  if ( has_enqueued_tasks() ) {
384  advertise_new_work<work_enqueued>();
385 #if __TBB_TASK_PRIORITY
386  // update_arena_priority() expects non-zero arena::my_num_workers_requested,
387  // so must be called after advertise_new_work<work_enqueued>()
388  for ( int p = 0; p < num_priority_levels; ++p )
389  if ( !my_task_stream.empty(p) ) {
390  if ( p < my_bottom_priority || p > my_top_priority )
391  my_market->update_arena_priority(*this, p);
392  }
393 #endif
394  }
395 }
market * my_market
The market that owns this arena.
Definition: arena.h:118
bool empty(int level)
Checks existence of a task.
Definition: task_stream.h:142
static const intptr_t num_priority_levels
bool has_enqueued_tasks()
Check for the presence of enqueued tasks at all priority levels.
Definition: arena.cpp:369
void const char const char int ITT_FORMAT __itt_group_sync p
task_stream< num_priority_levels > my_task_stream
Task pool for the tasks scheduled via task::enqueue() method.
Definition: arena.h:73

References tbb::internal::task_stream< Levels >::empty(), has_enqueued_tasks(), tbb::internal::arena_base::my_market, tbb::internal::arena_base::my_task_stream, tbb::internal::num_priority_levels, and p.

Referenced by advertise_new_work(), and is_out_of_work().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ my_slots

◆ out_of_arena

const size_t tbb::internal::arena::out_of_arena = ~size_t(0)
static

◆ ref_external

◆ ref_external_bits

const unsigned tbb::internal::arena::ref_external_bits = 12
static

The number of least significant bits for external references.

Definition at line 210 of file arena.h.

Referenced by num_workers_active().

◆ ref_worker

const unsigned tbb::internal::arena::ref_worker = 1<<ref_external_bits
static

Definition at line 214 of file arena.h.

Referenced by tbb::internal::market::arena_in_need(), and on_thread_leaving().

◆ SNAPSHOT_EMPTY

const pool_state_t tbb::internal::arena::SNAPSHOT_EMPTY = 0
static

◆ SNAPSHOT_FULL

const pool_state_t tbb::internal::arena::SNAPSHOT_FULL = pool_state_t(-1)
static

At least one task has been offered for stealing since the last snapshot started.

Definition at line 207 of file arena.h.

Referenced by advertise_new_work(), is_busy_or_empty(), and is_out_of_work().


The documentation for this class was generated from the following files:

Copyright © 2005-2018 Intel Corporation. All Rights Reserved.

Intel, Pentium, Intel Xeon, Itanium, Intel XScale and VTune are registered trademarks or trademarks of Intel Corporation or its subsidiaries in the United States and other countries.

* Other names and brands may be claimed as the property of others.