Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
arena.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2005-2018 Intel Corporation
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 
16 
17 
18 
19 */
20 
21 #include "tbb/global_control.h" // thread_stack_size
22 
23 #include "scheduler.h"
24 #include "governor.h"
25 #include "arena.h"
26 #include "itt_notify.h"
27 #include "semaphore.h"
29 
30 #include <functional>
31 
32 #if __TBB_STATISTICS_STDOUT
33 #include <cstdio>
34 #endif
35 
36 namespace tbb {
37 namespace internal {
38 
39 // put it here in order to enable compiler to inline it into arena::process and nested_arena_entry
40 void generic_scheduler::attach_arena( arena* a, size_t index, bool is_master ) {
41  __TBB_ASSERT( a->my_market == my_market, NULL );
42  my_arena = a;
43  my_arena_index = index;
44  my_arena_slot = a->my_slots + index;
45  attach_mailbox( affinity_id(index+1) );
46  if ( is_master && my_inbox.is_idle_state( true ) ) {
47  // Master enters an arena with its own task to be executed. It means that master is not
48  // going to enter stealing loop and take affinity tasks.
49  my_inbox.set_is_idle( false );
50  }
51 #if __TBB_TASK_GROUP_CONTEXT
52  // Context to be used by root tasks by default (if the user has not specified one).
53  if( !is_master )
54  my_dummy_task->prefix().context = a->my_default_ctx;
55 #endif /* __TBB_TASK_GROUP_CONTEXT */
56 #if __TBB_TASK_PRIORITY
57  // In the current implementation master threads continue processing even when
58  // there are other masters with higher priority. Only TBB worker threads are
59  // redistributed between arenas based on the latters' priority. Thus master
60  // threads use arena's top priority as a reference point (in contrast to workers
61  // that use my_market->my_global_top_priority).
62  if( is_master ) {
63  my_ref_top_priority = &a->my_top_priority;
64  my_ref_reload_epoch = &a->my_reload_epoch;
65  }
66  my_local_reload_epoch = *my_ref_reload_epoch;
67  __TBB_ASSERT( !my_offloaded_tasks, NULL );
68 #endif /* __TBB_TASK_PRIORITY */
69 }
70 
71 inline static bool occupy_slot( generic_scheduler*& slot, generic_scheduler& s ) {
72  return !slot && as_atomic( slot ).compare_and_swap( &s, NULL ) == NULL;
73 }
74 
75 size_t arena::occupy_free_slot_in_range( generic_scheduler& s, size_t lower, size_t upper ) {
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 }
88 
89 template <bool as_worker>
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 }
105 
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 }
189 
190 arena::arena ( market& m, unsigned num_slots, unsigned num_reserved_slots ) {
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 */
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 }
234 
235 arena& arena::allocate_arena( market& m, unsigned num_slots, unsigned num_reserved_slots ) {
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 }
245 
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 }
294 
295 #if __TBB_STATISTICS
296 void arena::dump_arena_statistics () {
297  statistics_counters total;
298  for( unsigned i = 0; i < my_num_slots; ++i ) {
299 #if __TBB_STATISTICS_EARLY_DUMP
301  if ( s )
302  *my_slots[i].my_counters += s->my_counters;
303 #else
304  __TBB_ASSERT( !my_slots[i].my_scheduler, NULL );
305 #endif
306  if ( i != 0 ) {
307  total += *my_slots[i].my_counters;
308  dump_statistics( *my_slots[i].my_counters, i );
309  }
310  }
311  dump_statistics( *my_slots[0].my_counters, 0 );
312 #if __TBB_STATISTICS_STDOUT
313 #if !__TBB_STATISTICS_TOTALS_ONLY
314  printf( "----------------------------------------------\n" );
315 #endif
316  dump_statistics( total, workers_counters_total );
317  total += *my_slots[0].my_counters;
318  dump_statistics( total, arena_counters_total );
319 #if !__TBB_STATISTICS_TOTALS_ONLY
320  printf( "==============================================\n" );
321 #endif
322 #endif /* __TBB_STATISTICS_STDOUT */
323 }
324 #endif /* __TBB_STATISTICS */
325 
326 #if __TBB_TASK_PRIORITY
327 // The method inspects a scheduler to determine:
328 // 1. if it has tasks that can be retrieved and executed (via the return value);
329 // 2. if it has any tasks at all, including those of lower priority (via tasks_present);
330 // 3. if it is able to work with enqueued tasks (via dequeuing_possible).
331 inline bool arena::may_have_tasks ( generic_scheduler* s, bool& tasks_present, bool& dequeuing_possible ) {
332  if ( !s || s->my_arena != this )
333  return false;
334  dequeuing_possible |= s->worker_outermost_level();
335  if ( s->my_pool_reshuffling_pending ) {
336  // This primary task pool is nonempty and may contain tasks at the current
337  // priority level. Its owner is winnowing lower priority tasks at the moment.
338  tasks_present = true;
339  return true;
340  }
341  if ( s->my_offloaded_tasks ) {
342  tasks_present = true;
343  if ( s->my_local_reload_epoch < *s->my_ref_reload_epoch ) {
344  // This scheduler's offload area is nonempty and may contain tasks at the
345  // current priority level.
346  return true;
347  }
348  }
349  return false;
350 }
351 
352 void arena::orphan_offloaded_tasks(generic_scheduler& s) {
353  __TBB_ASSERT( s.my_offloaded_tasks, NULL );
354  GATHER_STATISTIC( ++s.my_counters.prio_orphanings );
355  ++my_abandonment_epoch;
356  __TBB_ASSERT( s.my_offloaded_task_list_tail_link && !*s.my_offloaded_task_list_tail_link, NULL );
357  task* orphans;
358  do {
359  orphans = const_cast<task*>(my_orphaned_tasks);
360  *s.my_offloaded_task_list_tail_link = orphans;
361  } while ( as_atomic(my_orphaned_tasks).compare_and_swap(s.my_offloaded_tasks, orphans) != orphans );
362  s.my_offloaded_tasks = NULL;
363 #if TBB_USE_ASSERT
364  s.my_offloaded_task_list_tail_link = NULL;
365 #endif /* TBB_USE_ASSERT */
366 }
367 #endif /* __TBB_TASK_PRIORITY */
368 
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 }
376 
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 }
396 
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 }
531 
532 #if __TBB_COUNT_TASK_NODES
533 intptr_t arena::workers_task_node_count() {
534  intptr_t result = 0;
535  for( unsigned i = 1; i < my_num_slots; ++i ) {
537  if( s )
538  result += s->my_task_node_count;
539  }
540  return result;
541 }
542 #endif /* __TBB_COUNT_TASK_NODES */
543 
544 void arena::enqueue_task( task& t, intptr_t prio, FastRandom &random )
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 }
581 
583 public:
584  nested_arena_context(generic_scheduler *s, arena* a, size_t slot_index, bool type, bool same)
585  : my_scheduler(*s), my_orig_ctx(NULL), same_arena(same) {
586  if (same_arena) {
590  } else {
591  my_orig_state = *s;
593  s->nested_arena_entry(a, slot_index);
594  }
595  }
597 #if __TBB_TASK_GROUP_CONTEXT
598  my_scheduler.my_dummy_task->prefix().context = my_orig_ctx; // restore context of dummy task
599 #endif
600  if (same_arena) {
603  } else {
605  static_cast<scheduler_state&>(my_scheduler) = my_orig_state; // restore arena settings
606 #if __TBB_TASK_PRIORITY
607  my_scheduler.my_local_reload_epoch = *my_orig_state.my_ref_reload_epoch;
608 #endif
610  }
611  }
612 
613 private:
617  const bool same_arena;
618 
623 #if __TBB_TASK_GROUP_CONTEXT
624  // Save dummy's context and replace it by arena's context
626  my_scheduler.my_dummy_task->prefix().context = a->my_default_ctx;
627 #endif
628  }
629 };
630 
631 void generic_scheduler::nested_arena_entry(arena* a, size_t slot_index) {
632  __TBB_ASSERT( is_alive(a->my_guard), NULL );
633  __TBB_ASSERT( a!=my_arena, NULL);
634 
635  // overwrite arena settings
636 #if __TBB_TASK_PRIORITY
637  if ( my_offloaded_tasks )
638  my_arena->orphan_offloaded_tasks( *this );
639  my_offloaded_tasks = NULL;
640 #endif /* __TBB_TASK_PRIORITY */
641  attach_arena( a, slot_index, /*is_master*/true );
642  __TBB_ASSERT( my_arena == a, NULL );
644  // TODO? ITT_NOTIFY(sync_acquired, a->my_slots + index);
645  // TODO: it requires market to have P workers (not P-1)
646  // TODO: a preempted worker should be excluded from assignment to other arenas e.g. my_slack--
647  if( !is_worker() && slot_index >= my_arena->my_num_reserved_slots )
649 #if __TBB_ARENA_OBSERVER
650  my_last_local_observer = 0; // TODO: try optimize number of calls
651  my_arena->my_observers.notify_entry_observers( my_last_local_observer, /*worker=*/false );
652 #endif
653 }
654 
656 #if __TBB_ARENA_OBSERVER
657  my_arena->my_observers.notify_exit_observers( my_last_local_observer, /*worker=*/false );
658 #endif /* __TBB_ARENA_OBSERVER */
659 #if __TBB_TASK_PRIORITY
660  if ( my_offloaded_tasks )
661  my_arena->orphan_offloaded_tasks( *this );
662 #endif
665  // Free the master slot.
666  __TBB_ASSERT(my_arena->my_slots[my_arena_index].my_scheduler, "A slot is already empty");
668  my_arena->my_exit_monitors.notify_one(); // do not relax!
669 }
670 
672  my_dummy_task->prefix().ref_count++; // prevents exit from local_wait_for_all when local work is done enforcing the stealing
675  my_dummy_task->prefix().ref_count--;
676 }
677 
678 } // namespace internal
679 } // namespace tbb
680 
681 #include "scheduler_utility.h"
682 #include "tbb/task_arena.h" // task_arena_base
683 
684 namespace tbb {
685 namespace interface7 {
686 namespace internal {
687 
690  if( my_max_concurrency < 1 )
692  __TBB_ASSERT( my_master_slots <= (unsigned)my_max_concurrency, "Number of slots reserved for master should not exceed arena concurrency");
694  // add an internal market reference; a public reference was added in create_arena
695  market &m = market::global_market( /*is_public=*/false );
696  // allocate default context for task_arena
697 #if __TBB_TASK_GROUP_CONTEXT
698  new_arena->my_default_ctx = new ( NFS_Allocate(1, sizeof(task_group_context), NULL) )
700 #if __TBB_FP_CONTEXT
701  new_arena->my_default_ctx->capture_fp_settings();
702 #endif
703 #endif /* __TBB_TASK_GROUP_CONTEXT */
704  // threads might race to initialize the arena
705  if(as_atomic(my_arena).compare_and_swap(new_arena, NULL) != NULL) {
706  __TBB_ASSERT(my_arena, NULL); // another thread won the race
707  // release public market reference
708  m.release( /*is_public=*/true, /*blocking_terminate=*/false );
709  new_arena->on_thread_leaving<arena::ref_external>(); // destroy unneeded arena
710 #if __TBB_TASK_GROUP_CONTEXT
712  } else {
713  new_arena->my_default_ctx->my_version_and_traits |= my_version_and_traits & exact_exception_flag;
714  as_atomic(my_context) = new_arena->my_default_ctx;
715 #endif
716  }
717  // TODO: should it trigger automatic initialization of this thread?
719 }
720 
722  if( my_arena ) {// task_arena was initialized
723  my_arena->my_market->release( /*is_public=*/true, /*blocking_terminate=*/false );
724  my_arena->on_thread_leaving<arena::ref_external>();
725  my_arena = 0;
726 #if __TBB_TASK_GROUP_CONTEXT
727  my_context = 0;
728 #endif
729  }
730 }
731 
733  __TBB_ASSERT(!my_arena, NULL);
735  if( s && s->my_arena ) {
736  // There is an active arena to attach to.
737  // It's still used by s, so won't be destroyed right away.
738  my_arena = s->my_arena;
739  __TBB_ASSERT( my_arena->my_references > 0, NULL );
740  my_arena->my_references += arena::ref_external;
741 #if __TBB_TASK_GROUP_CONTEXT
742  my_context = my_arena->my_default_ctx;
744 #endif
745  my_master_slots = my_arena->my_num_reserved_slots;
746  my_max_concurrency = my_master_slots + my_arena->my_max_num_workers;
748  // increases market's ref count for task_arena
749  market::global_market( /*is_public=*/true );
750  }
751 }
752 
753 void task_arena_base::internal_enqueue( task& t, intptr_t prio ) const {
754  __TBB_ASSERT(my_arena, NULL);
756  __TBB_ASSERT(s, "Scheduler is not initialized"); // we allocated a task so can expect the scheduler
757 #if __TBB_TASK_GROUP_CONTEXT
758  __TBB_ASSERT(my_arena->my_default_ctx == t.prefix().context, NULL);
759  __TBB_ASSERT(!my_arena->my_default_ctx->is_group_execution_cancelled(), // TODO: any better idea?
760  "The task will not be executed because default task_group_context of task_arena is cancelled. Has previously enqueued task thrown an exception?");
761 #endif
762  my_arena->enqueue_task( t, prio, s->my_random );
763 }
764 
765 class delegated_task : public task {
766  internal::delegate_base & my_delegate;
771  __TBB_ASSERT(s.outermost_level(), "expected to be enqueued and received on the outermost level");
772  struct outermost_context : internal::no_copy {
773  delegated_task * t;
775  task * orig_dummy;
776  task_group_context * orig_ctx;
777  scheduler_properties orig_props;
778  outermost_context(delegated_task *_t, generic_scheduler &_s)
779  : t(_t), s(_s), orig_dummy(s.my_dummy_task), orig_props(s.my_properties) {
780  __TBB_ASSERT(s.my_innermost_running_task == t, NULL);
781 #if __TBB_TASK_GROUP_CONTEXT
782  orig_ctx = t->prefix().context;
783  t->prefix().context = s.my_arena->my_default_ctx;
784 #endif
785  // Mimics outermost master
786  s.my_dummy_task = t;
787  s.my_properties.type = scheduler_properties::master;
788  }
789  ~outermost_context() {
790 #if __TBB_TASK_GROUP_CONTEXT
791  // Restore context for sake of registering potential exception
792  t->prefix().context = orig_ctx;
793 #endif
794  s.my_properties = orig_props;
795  s.my_dummy_task = orig_dummy;
796  }
797  } scope(this, s);
798  my_delegate();
799  return NULL;
800  }
802  // potential exception was already registered. It must happen before the notification
803  __TBB_ASSERT(my_root->ref_count()==2, NULL);
804  __TBB_store_with_release(my_root->prefix().ref_count, 1); // must precede the wakeup
805  my_monitor.notify(*this); // do not relax, it needs a fence!
806  }
807 public:
808  delegated_task( internal::delegate_base & d, concurrent_monitor & s, task * t )
809  : my_delegate(d), my_monitor(s), my_root(t) {}
810  // predicate for concurrent_monitor notification
811  bool operator()(uintptr_t ctx) const { return (void*)ctx == (void*)&my_delegate; }
812 };
813 
814 void task_arena_base::internal_execute(internal::delegate_base& d) const {
815  __TBB_ASSERT(my_arena, NULL);
817  __TBB_ASSERT(s, "Scheduler is not initialized");
818 
819  bool same_arena = s->my_arena == my_arena;
820  size_t index1 = s->my_arena_index;
821  if (!same_arena) {
822  index1 = my_arena->occupy_free_slot</* as_worker*/false>(*s);
823  if (index1 == arena::out_of_arena) {
824 
825 #if __TBB_USE_OPTIONAL_RTTI
826  // Workaround for the bug inside graph. If the thread can not occupy arena slot during task_arena::execute()
827  // and all aggregator operations depend on this task completion (all other threads are inside arena already)
828  // deadlock appears, because enqueued task will never enter arena.
829  // Workaround: check if the task came from graph via RTTI (casting to graph::spawn_functor)
830  // and enqueue this task with non-blocking internal_enqueue method.
831  // TODO: have to change behaviour later in next GOLD release (maybe to add new library entry point - try_execute)
833  internal::delegated_function< graph_funct, void >* deleg_funct =
834  dynamic_cast< internal::delegated_function< graph_funct, void>* >(&d);
835 
836  if (deleg_funct) {
838  internal::function_task< internal::strip< graph_funct >::type >
839  (internal::forward< graph_funct >(deleg_funct->my_func)), 0);
840  return;
841  } else {
842 #endif /* __TBB_USE_OPTIONAL_RTTI */
844 #if __TBB_TASK_GROUP_CONTEXT
846 #if __TBB_FP_CONTEXT
847  exec_context.copy_fp_settings(*my_context);
848 #endif
849 #endif
850  auto_empty_task root(__TBB_CONTEXT_ARG(s, &exec_context));
851  root.prefix().ref_count = 2;
852  my_arena->enqueue_task(*new(task::allocate_root(__TBB_CONTEXT_ARG1(exec_context)))
853  delegated_task(d, my_arena->my_exit_monitors, &root),
854  0, s->my_random); // TODO: priority?
855  size_t index2 = arena::out_of_arena;
856  do {
857  my_arena->my_exit_monitors.prepare_wait(waiter, (uintptr_t)&d);
858  if (__TBB_load_with_acquire(root.prefix().ref_count) < 2) {
859  my_arena->my_exit_monitors.cancel_wait(waiter);
860  break;
861  }
862  index2 = my_arena->occupy_free_slot</*as_worker*/false>(*s);
863  if (index2 != arena::out_of_arena) {
864  my_arena->my_exit_monitors.cancel_wait(waiter);
866  s->local_wait_for_all(root, NULL);
867 #if TBB_USE_EXCEPTIONS
868  __TBB_ASSERT(!exec_context.my_exception, NULL); // exception can be thrown above, not deferred
869 #endif
870  __TBB_ASSERT(root.prefix().ref_count == 0, NULL);
871  break;
872  }
873  my_arena->my_exit_monitors.commit_wait(waiter);
874  } while (__TBB_load_with_acquire(root.prefix().ref_count) == 2);
875  if (index2 == arena::out_of_arena) {
876  // notify a waiting thread even if this thread did not enter arena,
877  // in case it was woken by a leaving thread but did not need to enter
878  my_arena->my_exit_monitors.notify_one(); // do not relax!
879  }
880 #if TBB_USE_EXCEPTIONS
881  // process possible exception
883  TbbRethrowException(pe);
884 #endif
885  return;
886 #if __TBB_USE_OPTIONAL_RTTI
887  } // if task came from graph
888 #endif
889  } // if (index1 == arena::out_of_arena)
890  } // if (!same_arena)
891 
892  context_guard_helper</*report_tasks=*/false> context_guard;
893  context_guard.set_ctx(__TBB_CONTEXT_ARG1(my_context));
894 #if TBB_USE_EXCEPTIONS
895  try {
896 #endif
897  //TODO: replace dummy tasks for workers as well to avoid using of the_dummy_context
899  d();
900 #if TBB_USE_EXCEPTIONS
901  }
902  catch (...) {
903  context_guard.restore_default(); // TODO: is it needed on Windows?
905  else {
908  exception_container.register_pending_exception();
909  __TBB_ASSERT(exception_container.my_exception, NULL);
910  TbbRethrowException(exception_container.my_exception);
911  }
912  }
913 #endif
914 }
915 
916 // this wait task is a temporary approach to wait for arena emptiness for masters without slots
917 // TODO: it will be rather reworked for one source of notification from is_out_of_work
918 class wait_task : public task {
922  __TBB_ASSERT( s, NULL );
923  __TBB_ASSERT( s->outermost_level(), "The enqueued task can be processed only on outermost level" );
924  if ( s->is_worker() ) {
925  __TBB_ASSERT( s->my_innermost_running_task == this, NULL );
926  // Mimic worker on outermost level to run remaining tasks
927  s->my_innermost_running_task = s->my_dummy_task;
928  s->local_wait_for_all( *s->my_dummy_task, NULL );
929  s->my_innermost_running_task = this;
930  } else s->my_arena->is_out_of_work(); // avoids starvation of internal_wait: issuing this task makes arena full
931  my_signal.V();
932  return NULL;
933  }
934 public:
935  wait_task ( binary_semaphore & sema ) : my_signal(sema) {}
936 };
937 
939  __TBB_ASSERT(my_arena, NULL);
941  __TBB_ASSERT(s, "Scheduler is not initialized");
942  __TBB_ASSERT(s->my_arena != my_arena || s->my_arena_index == 0, "task_arena::wait_until_empty() is not supported within a worker context" );
943  if( s->my_arena == my_arena ) {
944  //unsupported, but try do something for outermost master
945  __TBB_ASSERT(s->master_outermost_level(), "unsupported");
946  if( !s->my_arena_index )
947  while( my_arena->num_workers_active() )
948  s->wait_until_empty();
949  } else for(;;) {
950  while( my_arena->my_pool_state != arena::SNAPSHOT_EMPTY ) {
951  if( !__TBB_load_with_acquire(my_arena->my_slots[0].my_scheduler) // TODO TEMP: one master, make more masters
952  && as_atomic(my_arena->my_slots[0].my_scheduler).compare_and_swap(s, NULL) == NULL ) {
954  s->wait_until_empty();
955  } else {
956  binary_semaphore waiter; // TODO: replace by a single event notification from is_out_of_work
957  internal_enqueue( *new( task::allocate_root(__TBB_CONTEXT_ARG1(*my_context)) ) wait_task(waiter), 0 ); // TODO: priority?
958  waiter.P(); // TODO: concurrent_monitor
959  }
960  }
961  if( !my_arena->num_workers_active() && !my_arena->my_slots[0].my_scheduler) // no activity
962  break; // spin until workers active but avoid spinning in a worker
963  __TBB_Yield(); // wait until workers and master leave
964  }
965 }
966 
969  return s? int(s->my_arena_index) : -1;
970 }
971 
972 #if __TBB_TASK_ISOLATION
973 class isolation_guard : tbb::internal::no_copy {
974  isolation_tag &guarded;
975  isolation_tag previous_value;
976 public:
977  isolation_guard( isolation_tag &isolation ) : guarded( isolation ), previous_value( isolation ) {}
978  ~isolation_guard() {
979  guarded = previous_value;
980  }
981 };
982 
983 void isolate_within_arena( delegate_base& d, intptr_t reserved ) {
984  __TBB_ASSERT_EX( reserved == 0, NULL );
985  // TODO: Decide what to do if the scheduler is not initialized. Is there a use case for it?
986  generic_scheduler* s = governor::local_scheduler_weak();
987  __TBB_ASSERT( s, "this_task_arena::isolate() needs an initialized scheduler" );
988  // Theoretically, we can keep the current isolation in the scheduler; however, it makes sense to store it in innermost
989  // running task because it can in principle be queried via task::self().
990  isolation_tag& current_isolation = s->my_innermost_running_task->prefix().isolation;
991  // We temporarily change the isolation tag of the currently running task. It will be restored in the destructor of the guard.
992  isolation_guard guard( current_isolation );
993  current_isolation = reinterpret_cast<isolation_tag>(&d);
994  d();
995 }
996 #endif /* __TBB_TASK_ISOLATION */
997 
999  arena* a = NULL;
1000  if( ta ) // for special cases of ta->max_concurrency()
1001  a = ta->my_arena;
1003  a = s->my_arena; // the current arena if any
1004 
1005  if( a ) { // Get parameters from the arena
1006  __TBB_ASSERT( !ta || ta->my_max_concurrency==1, NULL );
1008  } else {
1009  __TBB_ASSERT( !ta || ta->my_max_concurrency==automatic, NULL );
1011  }
1012 }
1013 } // tbb::interfaceX::internal
1014 } // tbb::interfaceX
1015 } // tbb
Class representing where mail is put.
Definition: mailbox.h:100
#define __TBB_ISOLATION_ARG(arg1, isolation)
task object is freshly allocated or recycled.
Definition: task.h:604
void __TBB_EXPORTED_METHOD internal_terminate()
Definition: arena.cpp:721
arena_slot * my_arena_slot
Pointer to the slot in the arena we own at the moment.
Definition: scheduler.h:67
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
void on_thread_leaving()
Notification that worker or master leaves its arena.
Definition: arena.h:287
int my_num_workers_requested
The number of workers that are currently requested from the resource manager.
Definition: arena.h:79
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
nested_arena_context(generic_scheduler *s, arena *a, size_t slot_index, bool type, bool same)
Definition: arena.cpp:584
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 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 type
static int allocation_size(unsigned num_slots)
Definition: arena.h:186
void __TBB_EXPORTED_METHOD internal_wait() const
Definition: arena.cpp:938
void copy_fp_settings(const task_group_context &src)
Copies FPU control setting from another context.
static unsigned default_num_threads()
Definition: governor.h:85
#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
A fast random number generator.
Definition: tbb_misc.h:134
Base class for user-defined tasks.
Definition: task.h:576
bool is_idle_state(bool value) const
Indicate whether thread that reads this mailbox is idle.
Definition: mailbox.h:222
Work stealing task scheduler.
Definition: scheduler.h:115
task_group_context * my_context
default context of the arena
Definition: task_arena.h:110
const size_t NFS_MaxLineSize
Compile-time constant that is upper bound on cache line/sector size.
Definition: tbb_stddef.h:220
void __TBB_store_with_release(volatile T &location, V value)
Definition: tbb_machine.h:717
atomic< T > & as_atomic(T &t)
Definition: atomic.h:547
void const char const char int ITT_FORMAT __itt_group_sync s
internal::delegate_base & my_delegate
Definition: arena.cpp:766
#define __TBB_CONTEXT_ARG1(context)
void set_is_idle(bool value)
Indicate whether thread that reads this mailbox is idle.
Definition: mailbox.h:215
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
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
static const intptr_t num_priority_levels
binary_semaphore for concurrent monitor
Definition: semaphore.h:225
Base class for types that should not be copied or assigned.
Definition: tbb_stddef.h:335
internal::tbb_exception_ptr exception_container_type
Definition: task.h:328
Smart holder for the empty task class with automatic destruction.
void free_arena()
Completes arena shutdown, destructs and deallocates it.
Definition: arena.cpp:246
void mimic_outermost_level(arena *a, bool type)
Definition: arena.cpp:619
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
bool has_enqueued_tasks()
Check for the presence of enqueued tasks at all priority levels.
Definition: arena.cpp:369
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
#define __TBB_override
Definition: tbb_stddef.h:244
void const char const char int ITT_FORMAT __itt_group_sync p
scheduler_properties my_properties
Definition: scheduler.h:86
task * my_dummy_task
Fake root task created by slave threads.
Definition: scheduler.h:160
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 __itt_event ITT_FORMAT __itt_group_mark d void const wchar_t const wchar_t int ITT_FORMAT __itt_group_sync __itt_group_fsync x void const wchar_t int const wchar_t int int ITT_FORMAT __itt_group_sync __itt_group_fsync x void ITT_FORMAT __itt_group_sync __itt_group_fsync p void ITT_FORMAT __itt_group_sync __itt_group_fsync p void size_t ITT_FORMAT lu no args __itt_obj_prop_t __itt_obj_state_t ITT_FORMAT d const char ITT_FORMAT s __itt_frame ITT_FORMAT p const char const char ITT_FORMAT s __itt_counter ITT_FORMAT p __itt_counter unsigned long long ITT_FORMAT lu const wchar_t ITT_FORMAT S __itt_mark_type const wchar_t ITT_FORMAT S __itt_mark_type const char ITT_FORMAT s __itt_mark_type ITT_FORMAT d __itt_caller ITT_FORMAT p __itt_caller ITT_FORMAT p no args const __itt_domain __itt_clock_domain unsigned long long __itt_id ITT_FORMAT lu const __itt_domain __itt_clock_domain unsigned long long __itt_id __itt_id void ITT_FORMAT p const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_clock_domain unsigned long long __itt_id __itt_string_handle __itt_scope scope
bool is_out_of_work()
Check if there is job anywhere in arena.
Definition: arena.cpp:397
static const size_t out_of_arena
Definition: arena.h:275
static market & global_market(bool is_public, unsigned max_num_workers=0, size_t stack_size=0)
Factory method creating new market object.
Definition: market.cpp:100
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
bool operator()(uintptr_t ctx) const
Definition: arena.cpp:811
void process(generic_scheduler &)
Registers the worker with the arena and enters TBB scheduler dispatch loop.
Definition: arena.cpp:106
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
void attach_mailbox(affinity_id id)
Definition: scheduler.h:564
void attach_arena(arena *, size_t index, bool is_master)
Definition: arena.cpp:40
void adjust_demand(arena &, int delta)
Request that arena's need in workers should be adjusted.
Definition: market.cpp:590
void free_task_pool()
Deallocate task pool that was allocated by means of allocate_task_pool.
T __TBB_load_with_acquire(const volatile T &location)
Definition: tbb_machine.h:713
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
static void assume_scheduler(generic_scheduler *s)
Temporarily set TLS slot to the given scheduler.
Definition: governor.cpp:120
static const pool_state_t SNAPSHOT_EMPTY
No tasks to steal since last snapshot was taken.
Definition: arena.h:204
static generic_scheduler * local_scheduler_if_initialized()
Definition: governor.h:136
#define ITT_NOTIFY(name, obj)
Definition: itt_notify.h:117
unsigned hint_for_pop
Hint provided for operations with the container of starvation-resistant tasks.
task_group_context * my_orig_ctx
Definition: arena.cpp:616
intptr_t my_version_and_traits
Special settings.
Definition: task_arena.h:120
static bool is_set(generic_scheduler *s)
Used to check validity of the local scheduler TLS contents.
Definition: governor.cpp:124
static arena & allocate_arena(market &, unsigned num_slots, unsigned num_reserved_slots)
Allocate an instance of arena.
Definition: arena.cpp:235
task is in ready pool, or is going to be put there, or was just taken off.
Definition: task.h:602
static generic_scheduler * local_scheduler_weak()
Definition: governor.h:131
void __TBB_EXPORTED_METHOD internal_attach()
Definition: arena.cpp:732
delegated_task(internal::delegate_base &d, concurrent_monitor &s, task *t)
Definition: arena.cpp:808
int my_max_concurrency
Concurrency level for deferred initialization.
Definition: task_arena.h:114
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 __TBB_EXPORTED_FUNC isolate_within_arena(delegate_base &d, intptr_t reserved=0)
Used to form groups of tasks.
Definition: task.h:319
void __TBB_EXPORTED_METHOD internal_execute(delegate_base &) const
Definition: arena.cpp:814
Bit-field representing properties of a sheduler.
Definition: scheduler.h:50
#define ITT_SYNC_CREATE(obj, type, name)
Definition: itt_notify.h:120
void __TBB_EXPORTED_METHOD internal_enqueue(task &, intptr_t) const
Definition: arena.cpp:753
internal::arena * my_arena
NULL if not currently initialized.
Definition: task_arena.h:106
static const int automatic
Typedef for number of threads that is automatic.
Definition: task_arena.h:150
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 isolation_tag
A tag for task isolation.
Definition: task.h:126
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
static arena * create_arena(int num_slots, int num_reserved_slots, size_t stack_size)
Creates an arena object.
Definition: market.cpp:300
uintptr_t pool_state_t
Definition: arena.h:201
wait_task(binary_semaphore &sema)
Definition: arena.cpp:935
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
task * parent() const
task on whose behalf this task is working, or NULL if this is a root.
Definition: task.h:809
uintptr_t my_version_and_traits
Version for run-time checks and behavioral traits of the context.
Definition: task.h:407
void __TBB_EXPORTED_METHOD register_pending_exception()
Records the pending exception, and cancels the task group.
unsigned my_master_slots
Reserved master slots.
Definition: task_arena.h:117
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.
value_type compare_and_swap(value_type value, value_type comparand)
Definition: atomic.h:289
state_type state() const
Current execution state.
Definition: task.h:838
void notify_one()
Notify one thread about the event.
uintptr_t my_arenas_aba_epoch
ABA prevention marker to assign to newly created arenas.
Definition: market.h:147
bool type
Indicates that a scheduler acts as a master or a worker.
Definition: scheduler.h:54
static int __TBB_EXPORTED_FUNC internal_current_slot()
Definition: arena.cpp:967
void restore_priority_if_need()
If enqueued tasks found, restore arena priority and task presence status.
Definition: arena.cpp:377
The graph class.
#define __TBB_CONTEXT_ARG(arg1, context)
Set if ref_count might be changed by another thread. Used for debugging.
unsigned my_num_reserved_slots
The number of reserved slots (can be occupied only by masters).
Definition: arena.h:139
market * my_market
The market I am in.
Definition: scheduler.h:146
#define __TBB_Yield()
Definition: ibm_aix51.h:48
int ref_count() const
The internal reference count.
Definition: task.h:841
void spin_wait_while_eq(const volatile T &location, U value)
Spin WHILE the value of the variable is equal to a given value.
Definition: tbb_machine.h:395
exception_container_type * my_exception
Pointer to the container storing exception being propagated across this task group.
Definition: task.h:410
unsigned short affinity_id
An id as used for specifying affinity.
Definition: task.h:122
unsigned my_num_slots
The number of slots in the arena.
Definition: arena.h:136
virtual void local_wait_for_all(task &parent, task *child)=0
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
__TBB_atomic reference_count ref_count
Reference count used for synchronization.
Definition: task.h:240
arena * my_arena
The arena that I own (if master) or am servicing at the moment (if worker)
Definition: scheduler.h:70
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
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
static internal::allocate_root_proxy allocate_root()
Returns proxy for overloaded new that allocates a root task.
Definition: task.h:620
static void one_time_init()
Definition: governor.cpp:160
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.
bool outermost
Indicates that a scheduler is on outermost level.
Definition: scheduler.h:57
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
static int __TBB_EXPORTED_FUNC internal_max_concurrency(const task_arena *)
Definition: arena.cpp:998
T __TBB_load_relaxed(const volatile T &location)
Definition: tbb_machine.h:739
void nested_arena_entry(arena *, size_t)
Definition: arena.cpp:631
void set_ctx(__TBB_CONTEXT_ARG1(task_group_context *))
Definition: scheduler.h:769
generic_scheduler * my_scheduler
Scheduler of the thread attached to the slot.
size_t my_arena_index
Index of the arena slot the scheduler occupies now, or occupied last time.
Definition: scheduler.h:64
internal::task_prefix & prefix(internal::version_tag *=NULL) const
Get reference to corresponding task_prefix.
Definition: task.h:920
bool is_worker() const
True if running on a worker thread, false otherwise.
Definition: scheduler.h:570
#define __TBB_ENQUEUE_ENFORCED_CONCURRENCY
Definition: tbb_config.h:581
intptr_t reference_count
A reference count.
Definition: task.h:119
void notify(const P &predicate)
Notify waiting threads of the event that satisfies the given predicate.
uintptr_t my_aba_epoch
ABA prevention marker.
Definition: arena.h:121
const isolation_tag no_isolation
Definition: task.h:127
concurrent_monitor my_exit_monitors
Waiting object for master threads that cannot join the arena.
Definition: arena.h:154
task * execute() __TBB_override
Should be overridden by derived classes.
Definition: arena.cpp:769
void enqueue_task(task &, intptr_t, FastRandom &)
enqueue a task into starvation-resistance queue
Definition: arena.cpp:544
arena_slot my_slots[1]
Definition: arena.h:283
intptr_t drain()
Drain the mailbox.
Definition: mailbox.h:172
binary_semaphore & my_signal
Definition: arena.cpp:919
void __TBB_EXPORTED_METHOD internal_initialize()
Definition: arena.cpp:688
void push(task *source, int level, FastRandom &random)
Push a task into a lane.
Definition: task_stream.h:105
task * my_innermost_running_task
Innermost task whose task::execute() is running. A dummy task on the outermost level.
Definition: scheduler.h:73
#define EmptyTaskPool
Definition: scheduler.h:46
priority_t
Definition: task.h:279
#define GATHER_STATISTIC(x)
static bool occupy_slot(generic_scheduler *&slot, generic_scheduler &s)
Definition: arena.cpp:71
task * execute() __TBB_override
Should be overridden by derived classes.
Definition: arena.cpp:920
generic_scheduler & my_scheduler
Definition: arena.cpp:614
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)

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.