28 #if __TBB_STATISTICS_STDOUT
35 #if __TBB_NUMA_SUPPORT
38 binding_handler* binding_handler_ptr;
40 numa_binding_observer( task_arena* ta,
int numa_id,
int num_slots )
41 : task_scheduler_observer(*ta)
42 , my_numa_node_id(numa_id)
43 , binding_handler_ptr(
tbb::
internal::construct_binding_handler(num_slots))
47 tbb::internal::bind_thread_to_node(
55 ~numa_binding_observer(){
56 tbb::internal::destroy_binding_handler(binding_handler_ptr);
61 int numa_id,
int num_slots ) {
62 numa_binding_observer* binding_observer = NULL;
64 if (numa_id >= 0 && numa_topology::nodes_count() > 1) {
65 binding_observer =
new numa_binding_observer(ta, numa_id, num_slots);
66 __TBB_ASSERT(binding_observer,
"Failure during NUMA binding observer allocation and construction");
67 binding_observer->observe(
true);
69 return binding_observer;
72 void destroy_binding_observer( numa_binding_observer* binding_observer ) {
73 __TBB_ASSERT(binding_observer,
"Trying to deallocate NULL pointer");
74 binding_observer->observe(
false);
75 delete binding_observer;
91 #if __TBB_TASK_GROUP_CONTEXT
96 #if __TBB_TASK_PRIORITY
103 my_ref_top_priority = &a->my_top_priority;
104 my_ref_reload_epoch = &a->my_reload_epoch;
106 my_local_reload_epoch = *my_ref_reload_epoch;
112 return !slot &&
as_atomic( slot ).compare_and_swap( &
s, NULL ) == NULL;
118 size_t index =
s.my_arena_index;
119 if ( index < lower || index >= upper ) index =
s.my_random.get() % (upper - lower) + lower;
122 for (
size_t i = index; i < upper; ++i )
124 for (
size_t i = lower; i < index; ++i )
129 template <
bool as_worker>
149 __TBB_ASSERT(
s.my_innermost_running_task ==
s.my_dummy_task, NULL );
159 s.attach_arena(
this, index,
false );
161 #if !__TBB_FP_CONTEXT
165 #if __TBB_ARENA_OBSERVER
166 __TBB_ASSERT( !
s.my_last_local_observer,
"There cannot be notified local observers when entering arena" );
167 my_observers.notify_entry_observers(
s.my_last_local_observer,
true );
173 s.local_wait_for_all( *
s.my_dummy_task, NULL );
178 __TBB_ASSERT(
s.my_innermost_running_task ==
s.my_dummy_task, NULL );
182 "Worker cannot leave arena while its task pool is not reset" );
195 s.my_innermost_running_task =
s.my_dummy_task;
196 s.local_wait_for_all(*
s.my_dummy_task,t);
199 #if __TBB_ARENA_OBSERVER
200 my_observers.notify_exit_observers(
s.my_last_local_observer,
true );
201 s.my_last_local_observer = NULL;
203 #if __TBB_TASK_PRIORITY
204 if (
s.my_offloaded_tasks )
205 orphan_offloaded_tasks(
s );
208 ++
s.my_counters.arena_roundtrips;
209 *
my_slots[index].my_counters +=
s.my_counters;
210 s.my_counters.reset();
216 __TBB_ASSERT(
s.my_innermost_running_task ==
s.my_dummy_task, NULL );
223 on_thread_leaving<ref_worker>();
227 __TBB_ASSERT( !my_guard,
"improperly allocated arena?" );
230 #if __TBB_TASK_PRIORITY
231 __TBB_ASSERT( !my_reload_epoch && !my_orphaned_tasks && !my_skipped_fifo_priority,
"New arena object is not zeroed" );
240 #if __TBB_TASK_PRIORITY
241 my_bottom_priority = my_top_priority = normalized_normal_priority;
244 #if __TBB_ARENA_OBSERVER
245 my_observers.my_arena =
this;
247 #if __TBB_PREVIEW_RESUMABLE_TASKS
248 my_co_cache.init(4 * num_slots);
256 #if __TBB_PREVIEW_RESUMABLE_TASKS
263 #if __TBB_PREVIEW_CRITICAL_TASKS
267 my_slots[i].my_counters =
new (
NFS_Allocate(1,
sizeof(statistics_counters), NULL) ) statistics_counters;
272 #if __TBB_PREVIEW_CRITICAL_TASKS
274 ITT_SYNC_CREATE(&my_critical_task_stream, SyncType_Scheduler, SyncObj_CriticalTaskStream);
276 #if __TBB_ENQUEUE_ENFORCED_CONCURRENCY
277 my_local_concurrency_mode =
false;
278 my_global_concurrency_mode =
false;
280 #if !__TBB_FP_CONTEXT
290 unsigned char* storage = (
unsigned char*)
NFS_Allocate( 1, n, NULL );
292 memset( storage, 0, n );
301 #if __TBB_ENQUEUE_ENFORCED_CONCURRENCY
304 #if !__TBB_STATISTICS_EARLY_DUMP
308 intptr_t drained = 0;
321 #if __TBB_PREVIEW_RESUMABLE_TASKS
323 my_co_cache.cleanup();
325 #if __TBB_PREVIEW_CRITICAL_TASKS
326 __TBB_ASSERT( my_critical_task_stream.drain()==0,
"Not all critical tasks were executed");
328 #if __TBB_COUNT_TASK_NODES
329 my_market->update_task_node_count( -drained );
333 #if __TBB_TASK_GROUP_CONTEXT
334 __TBB_ASSERT( my_default_ctx,
"Master thread never entered the arena?" );
335 my_default_ctx->~task_group_context();
338 #if __TBB_ARENA_OBSERVER
339 if ( !my_observers.empty() )
340 my_observers.clear();
346 #if TBB_USE_ASSERT > 1
353 void arena::dump_arena_statistics () {
354 statistics_counters total;
356 #if __TBB_STATISTICS_EARLY_DUMP
359 *
my_slots[i].my_counters +=
s->my_counters;
365 dump_statistics( *
my_slots[i].my_counters, i );
368 dump_statistics( *
my_slots[0].my_counters, 0 );
369 #if __TBB_STATISTICS_STDOUT
370 #if !__TBB_STATISTICS_TOTALS_ONLY
371 printf(
"----------------------------------------------\n" );
373 dump_statistics( total, workers_counters_total );
375 dump_statistics( total, arena_counters_total );
376 #if !__TBB_STATISTICS_TOTALS_ONLY
377 printf(
"==============================================\n" );
383 #if __TBB_TASK_PRIORITY
388 inline bool arena::may_have_tasks ( generic_scheduler*
s,
bool& tasks_present,
bool& dequeuing_possible ) {
389 if ( !
s ||
s->my_arena !=
this )
391 dequeuing_possible |=
s->worker_outermost_level();
392 if (
s->my_pool_reshuffling_pending ) {
395 tasks_present =
true;
398 if (
s->my_offloaded_tasks ) {
399 tasks_present =
true;
400 if (
s->my_local_reload_epoch < *
s->my_ref_reload_epoch ) {
409 void arena::orphan_offloaded_tasks(generic_scheduler&
s) {
412 ++my_abandonment_epoch;
413 __TBB_ASSERT(
s.my_offloaded_task_list_tail_link && !*
s.my_offloaded_task_list_tail_link, NULL );
416 orphans =
const_cast<task*
>(my_orphaned_tasks);
417 *
s.my_offloaded_task_list_tail_link = orphans;
418 }
while (
as_atomic(my_orphaned_tasks).compare_and_swap(
s.my_offloaded_tasks, orphans) != orphans );
419 s.my_offloaded_tasks = NULL;
421 s.my_offloaded_task_list_tail_link = NULL;
441 advertise_new_work<work_enqueued>();
442 #if __TBB_TASK_PRIORITY
447 if ( p < my_bottom_priority || p > my_top_priority )
473 #if __TBB_TASK_PRIORITY
475 intptr_t top_priority = my_top_priority;
479 for( k=0; k<n; ++k ) {
490 bool work_absent = k == n;
491 #if __TBB_PREVIEW_CRITICAL_TASKS
492 bool no_critical_tasks = my_critical_task_stream.empty(0);
493 work_absent &= no_critical_tasks;
495 #if __TBB_TASK_PRIORITY
498 bool tasks_present = !work_absent || my_orphaned_tasks;
499 bool dequeuing_possible =
false;
504 uintptr_t abandonment_epoch = my_abandonment_epoch;
510 the_context_state_propagation_mutex.lock();
511 work_absent = !may_have_tasks(
my_slots[0].my_scheduler, tasks_present, dequeuing_possible );
512 the_context_state_propagation_mutex.unlock();
525 for( k = 1; work_absent && k < n; ++k ) {
528 work_absent = !may_have_tasks(
my_slots[k].my_scheduler, tasks_present, dequeuing_possible );
531 work_absent = work_absent
533 && abandonment_epoch == my_abandonment_epoch;
538 #if __TBB_TASK_PRIORITY
540 work_absent = work_absent && (!dequeuing_possible || no_fifo_tasks)
541 && top_priority == my_top_priority && reload_epoch == my_reload_epoch;
544 work_absent = work_absent && no_fifo_tasks;
547 #if __TBB_TASK_PRIORITY
548 if ( top_priority > my_bottom_priority ) {
549 if (
my_market->lower_arena_priority(*
this, top_priority - 1, reload_epoch)
552 atomic_update( my_skipped_fifo_priority, top_priority, std::less<intptr_t>());
555 else if ( !tasks_present && !my_orphaned_tasks && no_fifo_tasks ) {
568 #if __TBB_TASK_PRIORITY
585 #if __TBB_COUNT_TASK_NODES
586 intptr_t arena::workers_task_node_count() {
591 result +=
s->my_task_node_count;
599 #if __TBB_RECYCLE_TO_ENQUEUE
610 __TBB_ASSERT( ref_count!=0,
"attempt to enqueue task whose parent has a ref_count==0 (forgot to set_ref_count?)" );
611 __TBB_ASSERT( ref_count>0,
"attempt to enqueue task whose parent has a ref_count<0" );
616 #if __TBB_PREVIEW_CRITICAL_TASKS
618 #if __TBB_TASK_PRIORITY
621 bool is_critical = internal::is_critical( t );
629 if(
s &&
s->my_arena_slot ) {
632 #if __TBB_TASK_ISOLATION
635 unsigned& lane =
s->my_arena_slot->hint_for_critical;
642 advertise_new_work<work_spawned>();
648 #if __TBB_TASK_PRIORITY
649 intptr_t
p = prio ? normalize_priority(
priority_t(prio)) : normalized_normal_priority;
650 assert_priority_valid(
p);
651 #if __TBB_PREVIEW_CRITICAL_TASKS && __TBB_CPF_BUILD
654 my_task_stream.push( &t,
p, random );
656 if (
p != my_top_priority )
657 my_market->update_arena_priority( *
this,
p );
659 __TBB_ASSERT_EX(prio == 0,
"the library is not configured to respect the task priority");
660 #if __TBB_PREVIEW_CRITICAL_TASKS && __TBB_CPF_BUILD
663 my_task_stream.push( &t, 0, random );
666 advertise_new_work<work_enqueued>();
667 #if __TBB_TASK_PRIORITY
668 if (
p != my_top_priority )
669 my_market->update_arena_priority( *
this,
p );
683 #if __TBB_PREVIEW_RESUMABLE_TASKS
688 s->nested_arena_entry(a, slot_index);
692 #if __TBB_TASK_GROUP_CONTEXT
701 #if __TBB_TASK_PRIORITY
718 #if __TBB_PREVIEW_CRITICAL_TASKS
721 #if __TBB_TASK_GROUP_CONTEXT
734 #if __TBB_TASK_PRIORITY
735 if ( my_offloaded_tasks )
736 my_arena->orphan_offloaded_tasks( *
this );
737 my_offloaded_tasks = NULL;
747 #if __TBB_ARENA_OBSERVER
748 my_last_local_observer = 0;
749 my_arena->my_observers.notify_entry_observers( my_last_local_observer,
false );
751 #if __TBB_PREVIEW_RESUMABLE_TASKS
757 #if __TBB_ARENA_OBSERVER
758 my_arena->my_observers.notify_exit_observers( my_last_local_observer,
false );
760 #if __TBB_TASK_PRIORITY
761 if ( my_offloaded_tasks )
762 my_arena->orphan_offloaded_tasks( *
this );
779 #if __TBB_PREVIEW_RESUMABLE_TASKS
780 class resume_task :
public task {
787 if (
s->prepare_resume(my_target)) {
788 s->resume(my_target);
792 prefix().state = task::to_resume;
800 generic_scheduler* co_sched = curr.my_arena->my_co_cache.pop();
807 co_sched->my_arena = curr.my_arena;
810 co_sched->my_dummy_task->prefix().context = co_sched->my_arena->my_default_ctx;
817 void internal_suspend(
void* suspend_callback,
void* user_callback) {
819 __TBB_ASSERT(
s.my_arena_slot->my_scheduler_is_recalled != NULL, NULL);
820 bool is_recalled = *
s.my_arena_slot->my_scheduler_is_recalled;
821 generic_scheduler& target = is_recalled ? *
s.my_arena_slot->my_scheduler :
create_coroutine(
s);
823 generic_scheduler::callback_t callback = {
824 (generic_scheduler::suspend_callback_t)suspend_callback, user_callback, &
s };
825 target.set_post_resume_action(generic_scheduler::PRA_CALLBACK, &callback);
829 void internal_resume(task::suspend_point tag) {
830 generic_scheduler&
s = *
static_cast<generic_scheduler*
>(tag);
831 task* t =
new(&
s.allocate_task(
sizeof(resume_task),
__TBB_CONTEXT_ARG(NULL,
s.my_dummy_task->context()))) resume_task(
s);
837 arena& a = *
s.my_arena;
848 task::suspend_point internal_current_suspend_point() {
860 namespace interface7 {
866 #if __TBB_NUMA_SUPPORT
876 #if __TBB_TASK_GROUP_CONTEXT
877 new_arena->my_default_ctx =
new (
NFS_Allocate(1,
sizeof(task_group_context), NULL) )
880 new_arena->my_default_ctx->capture_fp_settings();
887 m.release(
true,
false );
889 #if __TBB_TASK_GROUP_CONTEXT
892 #if __TBB_TASK_GROUP_CONTEXT || __TBB_NUMA_SUPPORT
894 #if __TBB_NUMA_SUPPORT
895 my_arena->my_numa_binding_observer = tbb::internal::construct_binding_observer(
898 #if __TBB_TASK_GROUP_CONTEXT
911 #if __TBB_NUMA_SUPPORT
912 if(
my_arena->my_numa_binding_observer != NULL ) {
913 tbb::internal::destroy_binding_observer(
my_arena->my_numa_binding_observer);
914 my_arena->my_numa_binding_observer = NULL;
920 #if __TBB_TASK_GROUP_CONTEXT
929 if(
s &&
s->my_arena ) {
935 #if __TBB_TASK_GROUP_CONTEXT
951 #if __TBB_TASK_GROUP_CONTEXT
954 "The task will not be executed because default task_group_context of task_arena is cancelled. Has previously enqueued task thrown an exception?");
959 class delegated_task :
public task {
960 internal::delegate_base & my_delegate;
961 concurrent_monitor & my_monitor;
964 generic_scheduler&
s = *(generic_scheduler*)prefix().owner;
965 __TBB_ASSERT(
s.outermost_level(),
"expected to be enqueued and received on the outermost level");
966 struct outermost_context : internal::no_copy {
968 generic_scheduler &
s;
970 task_group_context * orig_ctx;
971 scheduler_properties orig_props;
972 outermost_context(delegated_task *_t, generic_scheduler &_s)
973 : t(_t),
s(_s), orig_dummy(
s.my_dummy_task), orig_props(
s.my_properties) {
975 #if __TBB_TASK_GROUP_CONTEXT
976 orig_ctx = t->prefix().context;
977 t->prefix().context =
s.my_arena->my_default_ctx;
983 ~outermost_context() {
984 #if __TBB_TASK_GROUP_CONTEXT
986 t->prefix().context = orig_ctx;
989 s.my_properties = orig_props;
990 s.my_dummy_task = orig_dummy;
999 task_prefix& prefix = my_root->prefix();
1000 #if __TBB_PREVIEW_RESUMABLE_TASKS
1001 reference_count old_ref_count = __TBB_FetchAndStoreW(&prefix.ref_count, 1);
1003 if (old_ref_count == internal::abandon_flag + 2) {
1006 tbb::task::resume(prefix.abandoned_scheduler);
1011 my_monitor.notify(*
this);
1014 delegated_task( internal::delegate_base &
d, concurrent_monitor &
s,
task * t )
1015 : my_delegate(
d), my_monitor(
s), my_root(t) {}
1017 bool operator()(uintptr_t ctx)
const {
return (
void*)ctx == (
void*)&my_delegate; }
1025 bool same_arena =
s->my_arena ==
my_arena;
1026 size_t index1 =
s->my_arena_index;
1031 #if __TBB_USE_OPTIONAL_RTTI
1039 internal::delegated_function< graph_funct, void >* deleg_funct =
1040 dynamic_cast< internal::delegated_function< graph_funct, void>*
>(&
d);
1045 (internal::forward< graph_funct >(deleg_funct->my_func)), 0);
1049 concurrent_monitor::thread_context waiter;
1050 #if __TBB_TASK_GROUP_CONTEXT
1052 #if __TBB_FP_CONTEXT
1057 root.prefix().ref_count = 2;
1072 s->local_wait_for_all(root, NULL);
1073 #if TBB_USE_EXCEPTIONS
1086 #if TBB_USE_EXCEPTIONS
1089 TbbRethrowException(pe);
1092 #if __TBB_USE_OPTIONAL_RTTI
1098 context_guard_helper<
false> context_guard;
1100 #if TBB_USE_EXCEPTIONS
1106 #if TBB_USE_EXCEPTIONS
1109 context_guard.restore_default();
1114 exception_container.register_pending_exception();
1116 TbbRethrowException(exception_container.my_exception);
1124 class wait_task :
public task {
1125 binary_semaphore & my_signal;
1129 __TBB_ASSERT(
s->outermost_level(),
"The enqueued task can be processed only on outermost level" );
1130 if (
s->is_worker() ) {
1133 s->my_innermost_running_task =
s->my_dummy_task;
1134 s->local_wait_for_all( *
s->my_dummy_task, NULL );
1135 s->my_innermost_running_task =
this;
1136 }
else s->my_arena->is_out_of_work();
1141 wait_task ( binary_semaphore & sema ) : my_signal(sema) {}
1148 __TBB_ASSERT(
s->my_arena !=
my_arena ||
s->my_arena_index == 0,
"task_arena::wait_until_empty() is not supported within a worker context" );
1152 if( !
s->my_arena_index )
1154 s->wait_until_empty();
1160 s->wait_until_empty();
1162 binary_semaphore waiter;
1175 return s?
int(
s->my_arena_index) : -1;
1178 #if __TBB_TASK_ISOLATION
1183 isolation_guard(
isolation_tag &isolation ) : guarded( isolation ), previous_value( isolation ) {}
1184 ~isolation_guard() {
1185 guarded = previous_value;
1192 __TBB_ASSERT(
s,
"this_task_arena::isolate() needs an initialized scheduler" );
1195 isolation_tag& current_isolation =
s->my_innermost_running_task->prefix().isolation;
1197 isolation_guard guard( current_isolation );
1198 current_isolation = isolation? isolation :
reinterpret_cast<isolation_tag>(&
d);
1211 __TBB_ASSERT( !ta || ta->my_max_concurrency==1, NULL );
1212 return a->my_num_reserved_slots + a->my_max_num_workers;