CMS 3D CMS Logo

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

#include <SerialTaskQueue.h>

Classes

class  QueuedTask
 
class  TaskBase
 

Public Member Functions

bool isPaused () const
 Checks to see if the queue has been paused. More...
 
bool pause ()
 Pauses processing of additional tasks from the queue. More...
 
template<typename T >
void push (const T &iAction)
 asynchronously pushes functor iAction into queue More...
 
template<typename T >
tbb::task * pushAndGetNextTaskToRun (const T &iAction)
 asynchronously pushes functor iAction into queue and finds next task to execute More...
 
template<typename T >
void pushAndWait (const T &iAction)
 synchronously pushes functor iAction into queue More...
 
bool resume ()
 Resumes processing if the queue was paused. More...
 
 SerialTaskQueue ()
 
 SerialTaskQueue (SerialTaskQueue &&iOther)
 
 ~SerialTaskQueue ()
 

Private Member Functions

tbb::task * finishedTask ()
 
const SerialTaskQueueoperator= (const SerialTaskQueue &)=delete
 
TaskBasepickNextTask ()
 
tbb::task * pushAndGetNextTask (TaskBase *)
 
void pushAndWait (tbb::empty_task *iWait, TaskBase *)
 
void pushTask (TaskBase *)
 
 SerialTaskQueue (const SerialTaskQueue &)=delete
 

Private Attributes

std::atomic< unsigned long > m_pauseCount
 
std::atomic< bool > m_taskChosen
 
tbb::concurrent_queue< TaskBase * > m_tasks
 

Friends

class TaskBase
 

Detailed Description

Definition at line 66 of file SerialTaskQueue.h.

Constructor & Destructor Documentation

edm::SerialTaskQueue::SerialTaskQueue ( )
inline

Definition at line 68 of file SerialTaskQueue.h.

Referenced by pause().

68 : m_taskChosen(false), m_pauseCount{0} {}
std::atomic< unsigned long > m_pauseCount
std::atomic< bool > m_taskChosen
edm::SerialTaskQueue::SerialTaskQueue ( SerialTaskQueue &&  iOther)
inline

Definition at line 70 of file SerialTaskQueue.h.

References m_taskChosen, m_tasks, and ~SerialTaskQueue().

71  : m_tasks(std::move(iOther.m_tasks)),
72  m_taskChosen(iOther.m_taskChosen.exchange(false)),
73  m_pauseCount(iOther.m_pauseCount.exchange(0)) {
74  assert(m_tasks.empty() and m_taskChosen == false);
75  }
std::atomic< unsigned long > m_pauseCount
std::atomic< bool > m_taskChosen
tbb::concurrent_queue< TaskBase * > m_tasks
def move(src, dest)
Definition: eostools.py:511
SerialTaskQueue::~SerialTaskQueue ( )

Definition at line 26 of file SerialTaskQueue.cc.

References isPaused(), m_taskChosen, m_tasks, or, and pushAndWait().

Referenced by SerialTaskQueue().

26  {
27  //be certain all tasks have completed
28  bool isEmpty = m_tasks.empty();
29  bool isTaskChosen = m_taskChosen;
30  if ((not isEmpty and not isPaused()) or isTaskChosen) {
31  pushAndWait([]() { return; });
32  }
33 }
void pushAndWait(const T &iAction)
synchronously pushes functor iAction into queue
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
bool isPaused() const
Checks to see if the queue has been paused.
std::atomic< bool > m_taskChosen
tbb::concurrent_queue< TaskBase * > m_tasks
edm::SerialTaskQueue::SerialTaskQueue ( const SerialTaskQueue )
privatedelete

Member Function Documentation

tbb::task * SerialTaskQueue::finishedTask ( )
private

Definition at line 63 of file SerialTaskQueue.cc.

References m_taskChosen, and pickNextTask().

Referenced by edm::SerialTaskQueue::TaskBase::finishedTask().

63  {
64  m_taskChosen.store(false);
65  return pickNextTask();
66 }
TaskBase * pickNextTask()
std::atomic< bool > m_taskChosen
bool edm::SerialTaskQueue::isPaused ( ) const
inline

Checks to see if the queue has been paused.

Returns
true if the queue is paused
See also
pause(), resume()

Definition at line 83 of file SerialTaskQueue.h.

References m_pauseCount.

Referenced by ~SerialTaskQueue().

83 { return m_pauseCount.load() != 0; }
std::atomic< unsigned long > m_pauseCount
const SerialTaskQueue& edm::SerialTaskQueue::operator= ( const SerialTaskQueue )
privatedelete

Referenced by pause().

bool edm::SerialTaskQueue::pause ( )
inline

Pauses processing of additional tasks from the queue.

Any task already running will not be paused however once that running task finishes no further tasks will be started. Multiple calls to pause() are allowed, however each call to pause() must be balanced by a call to resume().

Returns
false if queue was already paused.
See also
resume(), isPaused()

Definition at line 95 of file SerialTaskQueue.h.

References m_pauseCount, operator=(), push(), pushAndGetNextTaskToRun(), pushAndWait(), resume(), SerialTaskQueue(), and TrackValidation_cff::task.

Referenced by edm::EventProcessor::beginLumiAsync(), and edm::LimitedTaskQueue::Resumer::Resumer().

95 { return 1 == ++m_pauseCount; }
std::atomic< unsigned long > m_pauseCount
SerialTaskQueue::TaskBase * SerialTaskQueue::pickNextTask ( )
private

Definition at line 68 of file SerialTaskQueue.cc.

References LIKELY, m_pauseCount, m_taskChosen, m_tasks, and protons_cff::t.

Referenced by finishedTask(), pushAndGetNextTask(), and resume().

68  {
69  bool expect = false;
70  if
71  LIKELY(0 == m_pauseCount and m_taskChosen.compare_exchange_strong(expect, true)) {
72  TaskBase* t = nullptr;
73  if
74  LIKELY(m_tasks.try_pop(t)) { return t; }
75  //no task was actually pulled
76  m_taskChosen.store(false);
77 
78  //was a new entry added after we called 'try_pop' but before we did the clear?
79  expect = false;
80  if (not m_tasks.empty() and m_taskChosen.compare_exchange_strong(expect, true)) {
81  TaskBase* t = nullptr;
82  if (m_tasks.try_pop(t)) {
83  return t;
84  }
85  //no task was still pulled since a different thread beat us to it
86  m_taskChosen.store(false);
87  }
88  }
89  return nullptr;
90 }
std::atomic< unsigned long > m_pauseCount
#define LIKELY(x)
Definition: Likely.h:20
std::atomic< bool > m_taskChosen
tbb::concurrent_queue< TaskBase * > m_tasks
template<typename T >
void SerialTaskQueue::push ( const 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 186 of file SerialTaskQueue.h.

References pushTask().

Referenced by edm::EventProcessor::beginLumiAsync(), and pause().

186  {
187  QueuedTask<T>* pTask{new (tbb::task::allocate_root()) QueuedTask<T>{iAction}};
188  pTask->setQueue(this);
189  pushTask(pTask);
190  }
void pushTask(TaskBase *)
tbb::task * SerialTaskQueue::pushAndGetNextTask ( TaskBase iTask)
private

Definition at line 53 of file SerialTaskQueue.cc.

References LIKELY, m_tasks, pickNextTask(), and TrackValidation_cff::task.

Referenced by pushAndGetNextTaskToRun(), pushAndWait(), and pushTask().

53  {
54  tbb::task* returnValue{nullptr};
55  if
56  LIKELY(nullptr != iTask) {
57  m_tasks.push(iTask);
58  returnValue = pickNextTask();
59  }
60  return returnValue;
61 }
#define LIKELY(x)
Definition: Likely.h:20
TaskBase * pickNextTask()
tbb::concurrent_queue< TaskBase * > m_tasks
template<typename T >
tbb::task * SerialTaskQueue::pushAndGetNextTaskToRun ( const T iAction)

asynchronously pushes functor iAction into queue and finds next task to execute

This function is useful if you are accessing the SerialTaskQueue for the execute() method of a TBB task and want to efficiently schedule the next task from the queue. In that case you can take the return value and return it directly from your execute() method. The function will return immediately and not wait for iAction to run.

Parameters
[in]iActionMust be a functor that takes no arguments and return no values.
Returns
Returns either the next task that the user must schedule with TBB or a nullptr.

Definition at line 202 of file SerialTaskQueue.h.

References pushAndGetNextTask().

Referenced by pause().

202  {
203  QueuedTask<T>* pTask{new (tbb::task::allocate_root()) QueuedTask<T>{iAction}};
204  pTask->setQueue(this);
205  return pushAndGetNextTask(pTask);
206  }
tbb::task * pushAndGetNextTask(TaskBase *)
template<typename T >
void SerialTaskQueue::pushAndWait ( const 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 193 of file SerialTaskQueue.h.

Referenced by pause(), and ~SerialTaskQueue().

193  {
194  tbb::empty_task* waitTask = new (tbb::task::allocate_root()) tbb::empty_task;
195  waitTask->set_ref_count(2);
196  QueuedTask<T>* pTask{new (waitTask->allocate_child()) QueuedTask<T>{iAction}};
197  pTask->setQueue(this);
198  pushAndWait(waitTask, pTask);
199  }
void pushAndWait(const T &iAction)
synchronously pushes functor iAction into queue
void SerialTaskQueue::pushAndWait ( tbb::empty_task *  iWait,
TaskBase iTask 
)
private

Definition at line 92 of file SerialTaskQueue.cc.

References pyrootRender::destroy(), LIKELY, and pushAndGetNextTask().

92  {
93  auto nextTask = pushAndGetNextTask(iTask);
94  if
95  LIKELY(nullptr != nextTask) {
96  if
97  LIKELY(nextTask == iTask) {
98  //spawn and wait for all requires the task to have its parent set
99  iWait->spawn_and_wait_for_all(*nextTask);
100  }
101  else {
102  tbb::task::spawn(*nextTask);
103  iWait->wait_for_all();
104  }
105  }
106  else {
107  //a task must already be running in this queue
108  iWait->wait_for_all();
109  }
110  tbb::task::destroy(*iWait);
111 }
def destroy(e)
Definition: pyrootRender.py:15
tbb::task * pushAndGetNextTask(TaskBase *)
#define LIKELY(x)
Definition: Likely.h:20
void SerialTaskQueue::pushTask ( TaskBase iTask)
private

Definition at line 46 of file SerialTaskQueue.cc.

References pushAndGetNextTask(), protons_cff::t, and TrackValidation_cff::task.

Referenced by push().

46  {
47  tbb::task* t = pushAndGetNextTask(iTask);
48  if (nullptr != t) {
49  tbb::task::spawn(*t);
50  }
51 }
tbb::task * pushAndGetNextTask(TaskBase *)
bool SerialTaskQueue::resume ( )

Resumes processing if the queue was paused.

Multiple calls to resume() are allowed if there were multiple calls to pause(). Only when we reach as many resume() calls as pause() calls will the queue restart.

Returns
true if the call really restarts the queue
See also
pause(), isPaused()

Definition at line 35 of file SerialTaskQueue.cc.

References m_pauseCount, pickNextTask(), protons_cff::t, and TrackValidation_cff::task.

Referenced by edm::EventProcessor::globalEndLumiAsync(), edm::LimitedTaskQueue::Resumer::operator=(), pause(), and edm::Worker::RunModuleTask< T >::EnableQueueGuard::~EnableQueueGuard().

35  {
36  if (0 == --m_pauseCount) {
38  if (nullptr != t) {
39  tbb::task::spawn(*t);
40  }
41  return true;
42  }
43  return false;
44 }
std::atomic< unsigned long > m_pauseCount
TaskBase * pickNextTask()

Friends And Related Function Documentation

friend class TaskBase
friend

Definition at line 169 of file SerialTaskQueue.h.

Member Data Documentation

std::atomic<unsigned long> edm::SerialTaskQueue::m_pauseCount
private

Definition at line 182 of file SerialTaskQueue.h.

Referenced by isPaused(), pause(), pickNextTask(), and resume().

std::atomic<bool> edm::SerialTaskQueue::m_taskChosen
private

Definition at line 181 of file SerialTaskQueue.h.

Referenced by finishedTask(), pickNextTask(), SerialTaskQueue(), and ~SerialTaskQueue().

tbb::concurrent_queue<TaskBase*> edm::SerialTaskQueue::m_tasks
private