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 39 of file LimitedTaskQueue.h.

Constructor & Destructor Documentation

◆ LimitedTaskQueue() [1/2]

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

Definition at line 41 of file LimitedTaskQueue.h.

41 : m_queues{iLimit} {}

◆ LimitedTaskQueue() [2/2]

edm::LimitedTaskQueue::LimitedTaskQueue ( const LimitedTaskQueue )
privatedelete

Member Function Documentation

◆ concurrencyLimit()

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

◆ operator=()

const LimitedTaskQueue& edm::LimitedTaskQueue::operator= ( const LimitedTaskQueue )
privatedelete

◆ push()

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 127 of file LimitedTaskQueue.h.

127  {
128  auto set_to_run = std::make_shared<std::atomic<bool>>(false);
129  for (auto& q : m_queues) {
130  q.push([set_to_run, iAction]() mutable {
131  bool expected = false;
132  if (set_to_run->compare_exchange_strong(expected, true)) {
133  iAction();
134  }
135  });
136  }
137  }

References m_queues, and data-class-funcs::q.

Referenced by edm::Worker::TaskQueueAdaptor::push().

◆ pushAndPause()

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 160 of file LimitedTaskQueue.h.

160  {
161  auto set_to_run = std::make_shared<std::atomic<bool>>(false);
162  for (auto& q : m_queues) {
163  q.push([&q, set_to_run, iAction]() mutable {
164  bool expected = false;
165  if (set_to_run->compare_exchange_strong(expected, true)) {
166  q.pause();
167  iAction(Resumer(&q));
168  }
169  });
170  }
171  }

References m_queues, and data-class-funcs::q.

Referenced by edm::eventsetup::EventSetupRecordIOVQueue::startNewIOVAsync().

◆ pushAndWait()

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 140 of file LimitedTaskQueue.h.

140  {
141  tbb::empty_task* waitTask = new (tbb::task::allocate_root()) tbb::empty_task;
142  waitTask->set_ref_count(2);
143  auto set_to_run = std::make_shared<std::atomic<bool>>(false);
144  for (auto& q : m_queues) {
145  q.push([set_to_run, waitTask, iAction]() mutable {
146  bool expected = false;
147  if (set_to_run->compare_exchange_strong(expected, true)) {
148  // Exception needs to be caught in order to decrease the waitTask reference count at the end. The user of SerialTaskQueue should handle exceptions within iAction.
149  CMS_SA_ALLOW try { iAction(); } catch (...) {
150  }
151  waitTask->decrement_ref_count();
152  }
153  });
154  }
155  waitTask->wait_for_all();
156  tbb::task::destroy(*waitTask);
157  }

References CMS_SA_ALLOW, m_queues, and data-class-funcs::q.

Referenced by edm::Worker::TaskQueueAdaptor::pushAndWait().

Member Data Documentation

◆ m_queues

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

Definition at line 123 of file LimitedTaskQueue.h.

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

data-class-funcs.q
q
Definition: data-class-funcs.py:169
CMS_SA_ALLOW
#define CMS_SA_ALLOW
Definition: thread_safety_macros.h:5
edm::LimitedTaskQueue::m_queues
std::vector< SerialTaskQueue > m_queues
Definition: LimitedTaskQueue.h:123