CMS 3D CMS Logo

List of all members | Classes | Public Member Functions | Private Member Functions | Private Attributes
edm::LimitedTaskQueue Class Reference

#include <LimitedTaskQueue.h>

Classes

class  Resumer
 

Public Member Functions

unsigned int concurrencyLimit () const
 
 LimitedTaskQueue (unsigned int iLimit)
 
template<typename T >
void push (T &&iAction)
 asynchronously pushes functor iAction into queue More...
 
template<typename T >
void pushAndPause (T &&iAction)
 asynchronously pushes functor iAction into queue then pause the queue and run iAction More...
 
template<typename T >
void pushAndWait (T &&iAction)
 synchronously pushes functor iAction into queue More...
 

Private Member Functions

 LimitedTaskQueue (const LimitedTaskQueue &)=delete
 
const LimitedTaskQueueoperator= (const LimitedTaskQueue &)=delete
 

Private Attributes

std::vector< SerialTaskQueuem_queues
 

Detailed Description

Definition at line 38 of file LimitedTaskQueue.h.

Constructor & Destructor Documentation

edm::LimitedTaskQueue::LimitedTaskQueue ( unsigned int  iLimit)
inline

Definition at line 41 of file LimitedTaskQueue.h.

References push(), and pushAndWait().

41  :
42  m_queues{iLimit}
43  { }
std::vector< SerialTaskQueue > m_queues
edm::LimitedTaskQueue::LimitedTaskQueue ( const LimitedTaskQueue )
privatedelete

Member Function Documentation

unsigned int edm::LimitedTaskQueue::concurrencyLimit ( ) const
inline

Definition at line 120 of file LimitedTaskQueue.h.

References edm::LimitedTaskQueue::Resumer::LimitedTaskQueue, m_queues, and edm::LimitedTaskQueue::Resumer::operator=().

120 { return m_queues.size(); }
std::vector< SerialTaskQueue > m_queues
const LimitedTaskQueue& edm::LimitedTaskQueue::operator= ( const LimitedTaskQueue )
privatedelete
template<typename T >
void LimitedTaskQueue::push ( T &&  iAction)

asynchronously pushes functor iAction into queue

The function will return immediately and iAction will either process concurrently with the calling thread or wait until the protected resource becomes available or until a CPU becomes available.

Parameters
[in]iActionMust be a functor that takes no arguments and return no values.

Definition at line 130 of file LimitedTaskQueue.h.

References m_queues, and lumiQueryAPI::q.

Referenced by LimitedTaskQueue(), and edm::Worker::TaskQueueAdaptor::push().

130  {
131  auto set_to_run = std::make_shared<std::atomic<bool>>(false);
132  for(auto& q: m_queues) {
133  q.push([set_to_run,iAction]() mutable{
134  bool expected = false;
135  if(set_to_run->compare_exchange_strong(expected,true)) {
136  iAction();
137  }
138  });
139  }
140  }
std::vector< SerialTaskQueue > m_queues
template<typename T >
void LimitedTaskQueue::pushAndPause ( T &&  iAction)

asynchronously pushes functor iAction into queue then pause the queue and run iAction

iAction must take as argument a copy of a LimitedTaskQueue::Resumer. To resume the queue let the last copy of the Resumer go out of scope, or call Resumer::resume(). Using this function will decrease the allowed concurrency limit by 1.

Definition at line 163 of file LimitedTaskQueue.h.

References m_queues, lumiQueryAPI::q, and edm::LimitedTaskQueue::Resumer::Resumer().

163  {
164  auto set_to_run = std::make_shared<std::atomic<bool>>(false);
165  for(auto& q: m_queues) {
166  q.push([&q,set_to_run,iAction]() mutable{
167  bool expected = false;
168  if(set_to_run->compare_exchange_strong(expected,true)) {
169  q.pause();
170  iAction(Resumer(&q));
171  }
172  });
173  }
174  }
std::vector< SerialTaskQueue > m_queues
template<typename T >
void LimitedTaskQueue::pushAndWait ( T &&  iAction)

synchronously pushes functor iAction into queue

The function will wait until iAction has completed before returning. If another task is already running on the queue, the system is allowed to find another TBB task to execute while waiting for the iAction to finish. In that way the core is not idled while waiting.

Parameters
[in]iActionMust be a functor that takes no arguments and return no values.

Definition at line 143 of file LimitedTaskQueue.h.

References pyrootRender::destroy(), m_queues, and lumiQueryAPI::q.

Referenced by LimitedTaskQueue(), and edm::Worker::TaskQueueAdaptor::pushAndWait().

143  {
144  tbb::empty_task* waitTask = new (tbb::task::allocate_root()) tbb::empty_task;
145  waitTask->set_ref_count(2);
146  auto set_to_run = std::make_shared<std::atomic<bool>>(false);
147  for(auto& q: m_queues) {
148  q.push([set_to_run,waitTask,iAction]() mutable{
149  bool expected = false;
150  if(set_to_run->compare_exchange_strong(expected,true)) {
151  try {
152  iAction();
153  }catch(...) {}
154  waitTask->decrement_ref_count();
155  }
156  });
157  }
158  waitTask->wait_for_all();
159  tbb::task::destroy(*waitTask);
160  }
def destroy(e)
Definition: pyrootRender.py:14
std::vector< SerialTaskQueue > m_queues

Member Data Documentation

std::vector<SerialTaskQueue> edm::LimitedTaskQueue::m_queues
private

Definition at line 126 of file LimitedTaskQueue.h.

Referenced by concurrencyLimit(), push(), pushAndPause(), and pushAndWait().