Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
tbb::internal::task_stream< Levels > Class Template Reference

The container for "fairness-oriented" aka "enqueued" tasks. More...

#include <task_stream.h>

Inheritance diagram for tbb::internal::task_stream< Levels >:
Collaboration diagram for tbb::internal::task_stream< Levels >:

Public Member Functions

 task_stream ()
 
void initialize (unsigned n_lanes)
 
 ~task_stream ()
 
void push (task *source, int level, FastRandom &random)
 Push a task into a lane. More...
 
taskpop (int level, unsigned &last_used_lane)
 Try finding and popping a task. More...
 
bool empty (int level)
 Checks existence of a task. More...
 
intptr_t drain ()
 Destroys all remaining tasks in every lane. Returns the number of destroyed tasks. More...
 

Private Types

typedef queue_and_mutex< task *, spin_mutexlane_t
 

Private Attributes

population_t population [Levels]
 
padded< lane_t > * lanes [Levels]
 
unsigned N
 

Additional Inherited Members

- Private Member Functions inherited from tbb::internal::no_copy
 no_copy ()
 Allow default construction. More...
 

Detailed Description

template<int Levels>
class tbb::internal::task_stream< Levels >

The container for "fairness-oriented" aka "enqueued" tasks.

Definition at line 73 of file task_stream.h.

Member Typedef Documentation

◆ lane_t

template<int Levels>
typedef queue_and_mutex<task*, spin_mutex> tbb::internal::task_stream< Levels >::lane_t
private

Definition at line 74 of file task_stream.h.

Constructor & Destructor Documentation

◆ task_stream()

template<int Levels>
tbb::internal::task_stream< Levels >::task_stream ( )
inline

Definition at line 80 of file task_stream.h.

80  : N() {
81  for(int level = 0; level < Levels; level++) {
82  population[level] = 0;
83  lanes[level] = NULL;
84  }
85  }
population_t population[Levels]
Definition: task_stream.h:75
padded< lane_t > * lanes[Levels]
Definition: task_stream.h:76

◆ ~task_stream()

template<int Levels>
tbb::internal::task_stream< Levels >::~task_stream ( )
inline

Definition at line 99 of file task_stream.h.

99  {
100  for(int level = 0; level < Levels; level++)
101  if (lanes[level]) delete[] lanes[level];
102  }
padded< lane_t > * lanes[Levels]
Definition: task_stream.h:76

Member Function Documentation

◆ drain()

template<int Levels>
intptr_t tbb::internal::task_stream< Levels >::drain ( )
inline

Destroys all remaining tasks in every lane. Returns the number of destroyed tasks.

Tasks are not executed, because it would potentially create more tasks at a late stage. The scheduler is really expected to execute all tasks before task_stream destruction.

Definition at line 149 of file task_stream.h.

149  {
150  intptr_t result = 0;
151  for(int level = 0; level < Levels; level++)
152  for(unsigned i=0; i<N; ++i) {
153  lane_t& lane = lanes[level][i];
154  spin_mutex::scoped_lock lock(lane.my_mutex);
155  for(lane_t::queue_base_t::iterator it=lane.my_queue.begin();
156  it!=lane.my_queue.end(); ++it, ++result)
157  {
158  __TBB_ASSERT( is_bit_set( population[level], i ), NULL );
159  task* t = *it;
160  tbb::task::destroy(*t);
161  }
162  lane.my_queue.clear();
163  clear_one_bit( population[level], i );
164  }
165  return result;
166  }
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task * task
void clear_one_bit(population_t &dest, int pos)
Definition: task_stream.h:59
friend class scoped_lock
Definition: spin_mutex.h:180
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:169
population_t population[Levels]
Definition: task_stream.h:75
padded< lane_t > * lanes[Levels]
Definition: task_stream.h:76
queue_and_mutex< task *, spin_mutex > lane_t
Definition: task_stream.h:74
bool is_bit_set(population_t val, int pos)
Definition: task_stream.h:65
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 * lock

Referenced by tbb::internal::arena::free_arena().

Here is the caller graph for this function:

◆ empty()

template<int Levels>
bool tbb::internal::task_stream< Levels >::empty ( int  level)
inline

Checks existence of a task.

Definition at line 142 of file task_stream.h.

142  {
143  return !population[level];
144  }
population_t population[Levels]
Definition: task_stream.h:75

Referenced by tbb::internal::arena::has_enqueued_tasks(), tbb::internal::arena::is_out_of_work(), and tbb::internal::arena::restore_priority_if_need().

Here is the caller graph for this function:

◆ initialize()

template<int Levels>
void tbb::internal::task_stream< Levels >::initialize ( unsigned  n_lanes)
inline

Definition at line 87 of file task_stream.h.

87  {
88  const unsigned max_lanes = sizeof(population_t) * CHAR_BIT;
89 
90  N = n_lanes>=max_lanes ? max_lanes : n_lanes>2 ? 1<<(__TBB_Log2(n_lanes-1)+1) : 2;
91  __TBB_ASSERT( N==max_lanes || N>=n_lanes && ((N-1)&N)==0, "number of lanes miscalculated");
92  __TBB_ASSERT( N <= sizeof(population_t) * CHAR_BIT, NULL );
93  for(int level = 0; level < Levels; level++) {
94  lanes[level] = new padded<lane_t>[N];
95  __TBB_ASSERT( !population[level], NULL );
96  }
97  }
intptr_t __TBB_Log2(uintptr_t x)
Definition: tbb_machine.h:864
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:169
population_t population[Levels]
Definition: task_stream.h:75
padded< lane_t > * lanes[Levels]
Definition: task_stream.h:76
uintptr_t population_t
Definition: task_stream.h:50

Referenced by tbb::internal::arena::arena().

Here is the caller graph for this function:

◆ pop()

template<int Levels>
task* tbb::internal::task_stream< Levels >::pop ( int  level,
unsigned &  last_used_lane 
)
inline

Try finding and popping a task.

Definition at line 120 of file task_stream.h.

120  {
121  task* result = NULL;
122  // Lane selection is round-robin. Each thread should keep its last used lane.
123  unsigned idx = (last_used_lane+1)&(N-1);
124  for( ; population[level]; idx=(idx+1)&(N-1) ) {
125  if( is_bit_set( population[level], idx ) ) {
126  lane_t& lane = lanes[level][idx];
128  if( lock.try_acquire(lane.my_mutex) && !lane.my_queue.empty() ) {
129  result = lane.my_queue.front();
130  lane.my_queue.pop_front();
131  if( lane.my_queue.empty() )
132  clear_one_bit( population[level], idx );
133  break;
134  }
135  }
136  }
137  last_used_lane = idx;
138  return result;
139  }
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task * task
void clear_one_bit(population_t &dest, int pos)
Definition: task_stream.h:59
friend class scoped_lock
Definition: spin_mutex.h:180
population_t population[Levels]
Definition: task_stream.h:75
padded< lane_t > * lanes[Levels]
Definition: task_stream.h:76
queue_and_mutex< task *, spin_mutex > lane_t
Definition: task_stream.h:74
bool is_bit_set(population_t val, int pos)
Definition: task_stream.h:65
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 * lock

◆ push()

template<int Levels>
void tbb::internal::task_stream< Levels >::push ( task source,
int  level,
FastRandom random 
)
inline

Push a task into a lane.

Definition at line 105 of file task_stream.h.

105  {
106  // Lane selection is random. Each thread should keep a separate seed value.
107  unsigned idx;
108  for( ; ; ) {
109  idx = random.get() & (N-1);
111  if( lock.try_acquire(lanes[level][idx].my_mutex) ) {
112  lanes[level][idx].my_queue.push_back(source);
113  set_one_bit( population[level], idx ); //TODO: avoid atomic op if the bit is already set
114  break;
115  }
116  }
117  }
friend class scoped_lock
Definition: spin_mutex.h:180
population_t population[Levels]
Definition: task_stream.h:75
padded< lane_t > * lanes[Levels]
Definition: task_stream.h:76
void set_one_bit(population_t &dest, int pos)
Definition: task_stream.h:53
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 * lock

Referenced by tbb::internal::arena::enqueue_task().

Here is the caller graph for this function:

Member Data Documentation

◆ lanes

◆ N

◆ population


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

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.