21 #ifndef _TBB_custom_scheduler_H 22 #define _TBB_custom_scheduler_H 42 #if __TBB_x86_32||__TBB_x86_64 55 template<
typename SchedulerTraits>
78 if( SchedulerTraits::itt_possible )
80 if( SchedulerTraits::has_slow_atomic &&
p.ref_count==1 )
89 __TBB_ASSERT(
p.ref_count==0,
"completion of task caused predecessor's reference count to underflow");
90 if( SchedulerTraits::itt_possible )
95 #if __TBB_TASK_ISOLATION 99 p.isolation = isolation;
103 #if __TBB_RECYCLE_TO_ENQUEUE 104 if (
p.state==task::to_enqueue) {
110 if( bypass_slot==NULL )
121 s->assert_task_pool_valid();
135 template<
typename SchedulerTraits>
138 bool outermost_worker_level = worker_outermost_level();
139 bool outermost_dispatch_level = outermost_worker_level || master_outermost_level();
140 bool can_steal_here = can_steal();
141 my_inbox.set_is_idle(
true );
142 #if __TBB_HOARD_NONLOCAL_TASKS 145 #if __TBB_TASK_PRIORITY 146 if ( outermost_dispatch_level ) {
147 if ( intptr_t skipped_priority = my_arena->my_skipped_fifo_priority ) {
151 if ( my_arena->my_skipped_fifo_priority.compare_and_swap(0, skipped_priority) == skipped_priority
152 && skipped_priority > my_arena->my_top_priority )
154 my_market->update_arena_priority( *my_arena, skipped_priority );
161 size_t n = my_arena->my_limit-1;
165 for(
int failure_count = -static_cast<int>(SchedulerTraits::itt_possible);; ++failure_count) {
168 if( completion_ref_count==1 ) {
169 if( SchedulerTraits::itt_possible ) {
170 if( failure_count!=-1 ) {
171 ITT_NOTIFY(sync_prepare, &completion_ref_count);
175 ITT_NOTIFY(sync_acquired, &completion_ref_count);
185 if ( outermost_worker_level && (my_arena->my_num_workers_allotted < my_arena->num_workers_active()
186 #if __TBB_ENQUEUE_ENFORCED_CONCURRENCY 187 || my_arena->recall_by_mandatory_request()
190 if( SchedulerTraits::itt_possible && failure_count != -1 )
194 #if __TBB_TASK_PRIORITY 195 const int p =
int(my_arena->my_top_priority);
197 static const int p = 0;
201 if ( n && !my_inbox.empty() ) {
203 #if __TBB_TASK_ISOLATION 207 if ( isolation !=
no_isolation && !t && !my_inbox.empty()
208 && my_inbox.is_idle_state(
true ) ) {
211 my_inbox.set_is_idle(
false );
221 !my_arena->my_task_stream.empty(
p) && (t = my_arena->my_task_stream.pop(
p, my_arena_slot->hint_for_pop)) ) {
222 ITT_NOTIFY(sync_acquired, &my_arena->my_task_stream);
225 #if __TBB_TASK_PRIORITY 228 __TBB_ASSERT( !is_proxy(*t),
"The proxy task cannot be offloaded" );
232 else if ( can_steal_here && n ) {
234 size_t k = my_random.get() % n;
241 if( k >= my_arena_index )
251 free_task<no_cache_small_task>(tp);
257 if( is_version_3_task(*t) ) {
258 my_innermost_running_task = t;
268 #if __TBB_ARENA_OBSERVER 269 my_arena->my_observers.notify_entry_observers( my_last_local_observer, is_worker() );
271 #if __TBB_SCHEDULER_OBSERVER 272 the_global_observer_list.notify_entry_observers( my_last_global_observer, is_worker() );
274 if ( SchedulerTraits::itt_possible && failure_count != -1 ) {
282 if( SchedulerTraits::itt_possible && failure_count==-1 ) {
292 const int failure_threshold = 2*
int(n+1);
293 if( failure_count>=failure_threshold ) {
297 failure_count = failure_threshold;
300 #if __TBB_TASK_PRIORITY 302 if ( my_arena->my_orphaned_tasks ) {
304 ++my_arena->my_abandonment_epoch;
305 task* orphans = (
task*)__TBB_FetchAndStoreW( &my_arena->my_orphaned_tasks, 0 );
309 my_local_reload_epoch--;
310 t = reload_tasks( orphans, link,
__TBB_ISOLATION_ARG( effective_reference_priority(), isolation ) );
312 *link = my_offloaded_tasks;
313 if ( !my_offloaded_tasks )
314 my_offloaded_task_list_tail_link = link;
315 my_offloaded_tasks = orphans;
317 __TBB_ASSERT( !my_offloaded_tasks == !my_offloaded_task_list_tail_link, NULL );
319 if( SchedulerTraits::itt_possible )
321 __TBB_ASSERT( !is_proxy(*t),
"The proxy task cannot be offloaded" );
327 const int yield_threshold = 100;
328 if( yield_count++ >= yield_threshold ) {
331 #if __TBB_TASK_PRIORITY 332 if( outermost_worker_level || my_arena->my_top_priority > my_arena->my_bottom_priority ) {
333 if ( my_arena->is_out_of_work() && outermost_worker_level ) {
335 if ( outermost_worker_level && my_arena->is_out_of_work() ) {
337 if( SchedulerTraits::itt_possible )
341 #if __TBB_TASK_PRIORITY 343 if ( my_offloaded_tasks ) {
346 my_local_reload_epoch--;
351 if ( !outermost_worker_level && *my_ref_top_priority > my_arena->my_top_priority ) {
353 my_ref_top_priority = &my_arena->my_top_priority;
355 __TBB_ASSERT(my_ref_reload_epoch == &my_arena->my_reload_epoch, NULL);
361 n = my_arena->my_limit-1;
364 if ( my_inbox.is_idle_state(
true ) )
365 my_inbox.set_is_idle(
false );
369 template<
typename SchedulerTraits>
374 assert_task_pool_valid();
377 if( SchedulerTraits::itt_possible )
379 #if __TBB_TASK_GROUP_CONTEXT 388 parents_work_done = 1,
392 #if __TBB_TASK_PRIORITY 394 volatile intptr_t *old_ref_top_priority = my_ref_top_priority;
397 volatile uintptr_t *old_ref_reload_epoch = my_ref_reload_epoch;
399 task* old_innermost_running_task = my_innermost_running_task;
402 __TBB_ASSERT( my_properties.outermost || my_innermost_running_task!=my_dummy_task,
"The outermost property should be set out of a dispatch loop" );
403 my_properties.outermost &= my_innermost_running_task==my_dummy_task;
404 #if __TBB_TASK_ISOLATION 405 isolation_tag isolation = my_innermost_running_task->prefix().isolation;
407 if( master_outermost_level() ) {
409 quit_point = &
parent == my_dummy_task ? all_local_work_done : parents_work_done;
411 quit_point = parents_work_done;
412 #if __TBB_TASK_PRIORITY 413 if ( &
parent != my_dummy_task ) {
417 my_ref_top_priority = &
parent.prefix().context->my_priority;
418 my_ref_reload_epoch = &my_arena->my_reload_epoch;
419 if(my_ref_reload_epoch != old_ref_reload_epoch)
420 my_local_reload_epoch = *my_ref_reload_epoch-1;
428 #if __TBB_TASK_ISOLATION 432 t->prefix().isolation = isolation;
437 #if TBB_USE_EXCEPTIONS 454 #if __TBB_TASK_ISOLATION 456 "A task from another isolated region is going to be executed" );
459 #if __TBB_TASK_GROUP_CONTEXT && TBB_USE_ASSERT 460 assert_context_valid(t->prefix().context);
461 if ( !t->prefix().context->my_cancellation_requested )
465 assert_task_pool_valid();
466 #if __TBB_TASK_PRIORITY 467 intptr_t
p = priority(*t);
468 if (
p != *my_ref_top_priority && (t->prefix().extra_state &
es_task_enqueued) == 0) {
469 assert_priority_valid(
p);
470 if (
p != my_arena->my_top_priority ) {
471 my_market->update_arena_priority( *my_arena,
p );
473 if (
p < effective_reference_priority() ) {
474 if ( !my_offloaded_tasks ) {
475 my_offloaded_task_list_tail_link = &t->prefix().next_offloaded;
477 *my_offloaded_task_list_tail_link = NULL;
479 offload_task( *t,
p );
480 if ( is_task_pool_published() ) {
489 goto stealing_ground;
494 my_innermost_running_task = t;
497 #if __TBB_TASK_GROUP_CONTEXT 498 if ( !t->prefix().context->my_cancellation_requested )
502 GATHER_STATISTIC( my_counters.avg_arena_concurrency += my_arena->num_workers_active() );
503 GATHER_STATISTIC( my_counters.avg_assigned_workers += my_arena->my_num_workers_allotted );
504 #if __TBB_TASK_PRIORITY 506 GATHER_STATISTIC( my_counters.avg_market_prio += my_market->my_global_top_priority );
508 ITT_STACK(SchedulerTraits::itt_possible, callee_enter, t->prefix().context->itt_caller);
510 ITT_STACK(SchedulerTraits::itt_possible, callee_leave, t->prefix().context->itt_caller);
513 "if task::execute() returns task, it must be marked as allocated" );
517 affinity_id next_affinity=t_next->prefix().affinity;
518 if (next_affinity != 0 && next_affinity != my_affinity_id)
523 assert_task_pool_valid();
524 switch( t->state() ) {
526 task*
s = t->parent();
528 __TBB_ASSERT( t->prefix().ref_count==0,
"Task still has children after it has been executed" );
532 free_task<no_hint>( *t );
534 assert_task_pool_valid();
540 #if __TBB_RECYCLE_TO_ENQUEUE 542 case task::to_enqueue:
544 __TBB_ASSERT( t_next != t,
"a task returned from method execute() can not be recycled in another way" );
548 assert_task_pool_valid();
552 __TBB_ASSERT( t_next,
"reexecution requires that method execute() return another task" );
553 __TBB_ASSERT( t_next != t,
"a task returned from method execute() can not be recycled in another way" );
556 local_spawn( t, t->prefix().next );
557 assert_task_pool_valid();
564 __TBB_ASSERT(
false,
"task is in READY state upon return from method execute()" );
577 assert_task_pool_valid();
578 if (
parent.prefix().ref_count == quit_point ) {
579 __TBB_ASSERT( quit_point != all_local_work_done, NULL );
584 if ( is_task_pool_published() ) {
587 __TBB_ASSERT( is_quiescent_local_task_pool_reset(), NULL );
590 assert_task_pool_valid();
597 #if __TBB_TASK_PRIORITY 600 #if __TBB_HOARD_NONLOCAL_TASKS 602 for (; my_nonlocal_free_list; my_nonlocal_free_list = t ) {
603 t = my_nonlocal_free_list->prefix().next;
604 free_nonlocal_small_task( *my_nonlocal_free_list );
607 if ( quit_point == all_local_work_done ) {
608 __TBB_ASSERT( !is_task_pool_published() && is_quiescent_local_task_pool_reset(), NULL );
610 my_innermost_running_task = old_innermost_running_task;
611 my_properties = old_properties;
612 #if __TBB_TASK_PRIORITY 613 my_ref_top_priority = old_ref_top_priority;
614 if(my_ref_reload_epoch != old_ref_reload_epoch)
615 my_local_reload_epoch = *old_ref_reload_epoch-1;
616 my_ref_reload_epoch = old_ref_reload_epoch;
630 #if TBB_USE_EXCEPTIONS 633 TbbCatchAll( t->prefix().context );
636 #if __TBB_RECYCLE_TO_ENQUEUE 638 || t->state() == task::to_enqueue
643 if( SchedulerTraits::itt_possible )
646 if( SchedulerTraits::itt_possible )
647 ITT_NOTIFY(sync_acquired, &t->prefix().ref_count);
656 my_innermost_running_task = old_innermost_running_task;
657 my_properties = old_properties;
658 #if __TBB_TASK_PRIORITY 659 my_ref_top_priority = old_ref_top_priority;
660 if(my_ref_reload_epoch != old_ref_reload_epoch)
661 my_local_reload_epoch = *old_ref_reload_epoch-1;
662 my_ref_reload_epoch = old_ref_reload_epoch;
665 if (
parent.prefix().ref_count != parents_work_done ) {
668 "Worker thread exits nested dispatch loop prematurely" );
671 parent.prefix().ref_count = 0;
676 #if __TBB_TASK_GROUP_CONTEXT 679 if ( parent_ctx->my_cancellation_requested ) {
681 if ( master_outermost_level() && parent_ctx == default_context() ) {
684 parent_ctx->my_cancellation_requested = 0;
692 context_guard.restore_default();
693 TbbRethrowException( pe );
696 __TBB_ASSERT(!is_worker() || !CancellationInfoPresent(*my_dummy_task),
697 "Worker's dummy task context modified");
698 __TBB_ASSERT(!master_outermost_level() || !CancellationInfoPresent(*my_dummy_task),
699 "Unexpected exception or cancellation data in the master's dummy task");
701 assert_task_pool_valid();
#define __TBB_ISOLATION_ARG(arg1, isolation)
task object is freshly allocated or recycled.
#define __TBB_control_consistency_helper()
bool ConcurrentWaitsEnabled(task &t)
void poison_pointer(T *__TBB_atomic &)
Base class for user-defined tasks.
void local_spawn(task *first, task *&next)
Work stealing task scheduler.
void const char const char int ITT_FORMAT __itt_group_sync s
#define __TBB_CONTEXT_ARG1(context)
static const intptr_t num_priority_levels
Memory prefix to a task object.
internal::tbb_exception_ptr exception_container_type
FastRandom my_random
Random number generator used for picking a random victim from which to steal.
void wait_for_all(task &parent, task *child) __TBB_override
Entry point from client code to the scheduler loop that dispatches tasks.
task * extract_task()
Returns a pointer to the encapsulated task or NULL, and frees proxy if necessary.
virtual void __TBB_EXPORTED_METHOD note_affinity(affinity_id id)
Invoked by scheduler to notify task that it ran on unexpected thread.
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
A scheduler with a customized evaluation loop.
static generic_scheduler * allocate_scheduler(market &m)
task is running, and will be destroyed after method execute() completes.
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
task * receive_or_steal_task(__TBB_ISOLATION_ARG(__TBB_atomic reference_count &completion_ref_count, isolation_tag isolation)) __TBB_override
Try getting a task from the mailbox or stealing from another scheduler.
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
static generic_scheduler * local_scheduler()
Obtain the thread-local instance of the TBB scheduler.
custom_scheduler< SchedulerTraits > scheduler_type
#define ITT_NOTIFY(name, obj)
static bool is_set(generic_scheduler *s)
Used to check validity of the local scheduler TLS contents.
task is in ready pool, or is going to be put there, or was just taken off.
task **__TBB_atomic task_pool
static const bool itt_possible
Used to form groups of tasks.
Bit-field representing properties of a sheduler.
#define ITT_SYNC_CREATE(obj, type, name)
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 sync_cancel
intptr_t isolation_tag
A tag for task isolation.
#define ITT_STACK(precond, name, obj)
void tally_completion_of_predecessor(task &s, __TBB_ISOLATION_ARG(task *&bypass_slot, isolation_tag isolation))
Decrements ref_count of a predecessor.
#define __TBB_ISOLATION_EXPR(isolation)
task * parent() const
task on whose behalf this task is working, or NULL if this is a root.
void *__TBB_EXPORTED_FUNC NFS_Allocate(size_t n_element, size_t element_size, void *hint)
Allocate memory on cache/sector line boundary.
Traits classes for scheduler.
void assert_task_valid(const task *)
task to be recycled as continuation
Set if ref_count might be changed by another thread. Used for debugging.
Set if the task has been stolen.
unsigned short affinity_id
An id as used for specifying affinity.
#define __TBB_FetchAndDecrementWrelease(P)
static const bool has_slow_atomic
arena * my_arena
The arena that I own (if master) or am servicing at the moment (if worker)
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 local_wait_for_all(task &parent, task *child) __TBB_override
Scheduler loop that dispatches tasks.
#define __TBB_fallthrough
custom_scheduler(market &m)
void set_ctx(__TBB_CONTEXT_ARG1(task_group_context *))
virtual task * execute()=0
Should be overridden by derived classes.
internal::task_prefix & prefix(internal::version_tag *=NULL) const
Get reference to corresponding task_prefix.
intptr_t reference_count
A reference count.
const isolation_tag no_isolation
void enqueue_task(task &, intptr_t, FastRandom &)
enqueue a task into starvation-resistance queue
static const intptr_t pool_bit
void reset_extra_state(task *t)
static const bool itt_possible
static const bool has_slow_atomic
#define GATHER_STATISTIC(x)