CMS 3D CMS Logo

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

#include <WaitingTaskList.h>

Classes

struct  WaitNode
 

Public Member Functions

void add (WaitingTask *)
 Adds task to the waiting list. More...
 
void doneWaiting (std::exception_ptr iPtr)
 Signals that the resource is now available and tasks should be spawned. More...
 
const WaitingTaskListoperator= (const WaitingTaskList &)=delete
 
void presetTaskAsFailed (std::exception_ptr iExcept)
 
void reset ()
 Resets access to the resource so that added tasks will wait. More...
 
 WaitingTaskList (const WaitingTaskList &)=delete
 
 WaitingTaskList (unsigned int iInitialSize=2)
 Constructor. More...
 
 ~WaitingTaskList ()=default
 

Private Member Functions

void announce ()
 
WaitNodecreateNode (WaitingTask *iTask)
 

Private Attributes

std::exception_ptr m_exceptionPtr
 
std::atomic< WaitNode * > m_head
 
std::atomic< unsigned int > m_lastAssignedCacheIndex
 
std::unique_ptr< WaitNode[]> m_nodeCache
 
unsigned int m_nodeCacheSize
 
std::atomic< bool > m_waiting
 

Detailed Description

Definition at line 101 of file WaitingTaskList.h.

Constructor & Destructor Documentation

◆ WaitingTaskList() [1/2]

WaitingTaskList::WaitingTaskList ( unsigned int  iInitialSize = 2)
explicit

Constructor.

The WaitingTaskList is initial set to waiting.

Parameters
[in]iInitialSizespecifies the initial size of the cache used to hold waiting tasks. The value is only useful for optimization as the object can resize itself.

Definition at line 37 of file WaitingTaskList.cc.

38  : m_head{nullptr},
39  m_nodeCache{new WaitNode[iInitialSize]},
40  m_nodeCacheSize{iInitialSize},
42  m_waiting{true} {
43  auto nodeCache = m_nodeCache.get();
44  for (auto it = nodeCache, itEnd = nodeCache + m_nodeCacheSize; it != itEnd; ++it) {
45  it->m_fromCache = true;
46  }
47 }

◆ WaitingTaskList() [2/2]

edm::WaitingTaskList::WaitingTaskList ( const WaitingTaskList )
delete

◆ ~WaitingTaskList()

edm::WaitingTaskList::~WaitingTaskList ( )
default

Member Function Documentation

◆ add()

void WaitingTaskList::add ( WaitingTask iTask)

Adds task to the waiting list.

If doneWaiting() has already been called then the added task will immediately be spawned. If that is not the case then the task will be held until doneWaiting() is called and will then be spawned. Calls to add() and doneWaiting() can safely be done concurrently.

Definition at line 90 of file WaitingTaskList.cc.

90  {
91  iTask->increment_ref_count();
92  if (!m_waiting) {
93  if (UNLIKELY(bool(m_exceptionPtr))) {
95  }
96  if (0 == iTask->decrement_ref_count()) {
97  tbb::task::spawn(*iTask);
98  }
99  } else {
100  WaitNode* newHead = createNode(iTask);
101  //This exchange is sequentially consistent thereby
102  // ensuring ordering between it and setNextNode
103  WaitNode* oldHead = m_head.exchange(newHead);
104  newHead->setNextNode(oldHead);
105 
106  //For the case where oldHead != nullptr,
107  // even if 'm_waiting' changed, we don't
108  // have to recheck since we beat 'announce()' in
109  // the ordering of 'm_head.exchange' call so iTask
110  // is guaranteed to be in the link list
111 
112  if (nullptr == oldHead) {
113  if (!m_waiting) {
114  //if finished waiting right before we did the
115  // exchange our task will not be spawned. Also,
116  // additional threads may be calling add() and swapping
117  // heads and linking us to the new head.
118  // It is safe to call announce from multiple threads
119  announce();
120  }
121  }
122  }
123 }

References announce(), createNode(), edm::WaitingTask::dependentTaskFailed(), m_exceptionPtr, m_head, m_waiting, edm::WaitingTaskList::WaitNode::setNextNode(), and UNLIKELY.

Referenced by edm::Worker::callWhenDoneAsync(), edm::eventsetup::EventSetupRecordIOVQueue::checkForNewIOVs(), edm::Worker::doWork(), edm::Worker::doWorkAsync(), edm::Worker::doWorkNoPrefetchingAsync(), edm::eventsetup::Callback< T, TReturn, TRecord, TDecorator >::prefetchAsync(), edm::InputProductResolver::prefetchAsync_(), edm::PuttableProductResolver::prefetchAsync_(), edm::UnscheduledProductResolver::prefetchAsync_(), edm::SwitchProducerProductResolver::prefetchAsync_(), edm::SwitchAliasProductResolver::prefetchAsync_(), edm::NoProcessProductResolver::prefetchAsync_(), edm::eventsetup::ESSourceDataProxyBase::prefetchAsyncImpl(), edm::eventsetup::DataProxyTemplate< RecordT, DataT >::prefetchAsyncImpl(), edm::Path::processOneOccurrenceAsync(), counter.Counter::register(), SequenceTypes.Task::remove(), SequenceTypes.Task::replace(), and edm::eventsetup::EventSetupRecordIOVQueue::startNewIOVAsync().

◆ announce()

void WaitingTaskList::announce ( )
private

Handles spawning the tasks, safe to call from multiple threads

Definition at line 139 of file WaitingTaskList.cc.

139  {
140  //Need a temporary storage since one of these tasks could
141  // cause the next event to start processing which would refill
142  // this waiting list after it has been reset
143  WaitNode* n = m_head.exchange(nullptr);
144  WaitNode* next;
145  while (n) {
146  //it is possible that 'WaitingTaskList::add' is running in a different
147  // thread and we have a new 'head' but the old head has not yet been
148  // attached to the new head (we identify this since 'nextNode' will return itself).
149  // In that case we have to wait until the link has been established before going on.
150  while (n == (next = n->nextNode())) {
151  hardware_pause();
152  }
153  auto t = n->m_task;
154  if (UNLIKELY(bool(m_exceptionPtr))) {
155  t->dependentTaskFailed(m_exceptionPtr);
156  }
157  if (!n->m_fromCache) {
158  delete n;
159  }
160  n = next;
161 
162  //the task may indirectly call WaitingTaskList::reset
163  // so we need to call spawn after we are done using the node.
164  if (0 == t->decrement_ref_count()) {
165  tbb::task::spawn(*t);
166  }
167  }
168 }

References m_exceptionPtr, m_head, dqmiodumpmetadata::n, GetRecoTauVFromDQM_MC_cff::next, submitPVValidationJobs::t, and UNLIKELY.

Referenced by add(), and doneWaiting().

◆ createNode()

WaitingTaskList::WaitNode * WaitingTaskList::createNode ( WaitingTask iTask)
private

Definition at line 71 of file WaitingTaskList.cc.

71  {
72  unsigned int index = m_lastAssignedCacheIndex++;
73 
74  WaitNode* returnValue;
75  if (index < m_nodeCacheSize) {
76  returnValue = m_nodeCache.get() + index;
77  } else {
78  returnValue = new WaitNode;
79  returnValue->m_fromCache = false;
80  }
81  returnValue->m_task = iTask;
82  //No other thread can see m_next yet. The caller to create node
83  // will be doing a synchronization operation anyway which will
84  // make sure m_task and m_next are synched across threads
85  returnValue->m_next.store(returnValue, std::memory_order_relaxed);
86 
87  return returnValue;
88 }

References edm::WaitingTaskList::WaitNode::m_fromCache, m_lastAssignedCacheIndex, edm::WaitingTaskList::WaitNode::m_next, m_nodeCache, m_nodeCacheSize, and edm::WaitingTaskList::WaitNode::m_task.

Referenced by add().

◆ doneWaiting()

void WaitingTaskList::doneWaiting ( std::exception_ptr  iPtr)

Signals that the resource is now available and tasks should be spawned.

The owner of the resource calls this function to allow the waiting tasks to start accessing it. If the task fails, a non 'null' std::exception_ptr should be used. To have tasks wait again one must call reset(). Calls to add() and doneWaiting() can safely be done concurrently.

Definition at line 170 of file WaitingTaskList.cc.

170  {
171  m_exceptionPtr = iPtr;
172  m_waiting = false;
173  announce();
174 }

References announce(), m_exceptionPtr, and m_waiting.

Referenced by edm::Worker::doWork(), edm::Worker::doWorkNoPrefetchingAsync(), edm::eventsetup::EventSetupsController::eventSetupForInstance(), edm::Path::finished(), edm::InputProductResolver::prefetchAsync_(), edm::PuttableProductResolver::prefetchAsync_(), edm::UnscheduledProductResolver::prefetchAsync_(), edm::SwitchProducerProductResolver::prefetchAsync_(), edm::SwitchAliasProductResolver::prefetchAsync_(), edm::eventsetup::ESSourceDataProxyBase::prefetchAsyncImpl(), edm::eventsetup::DataProxyTemplate< RecordT, DataT >::prefetchAsyncImpl(), edm::Worker::prePrefetchSelectionAsync(), edm::PuttableProductResolver::putProduct_(), edm::SwitchProducerProductResolver::putProduct_(), edm::LuminosityBlockProcessingStatus::resetResources(), edm::Worker::runModuleAfterAsyncPrefetch(), edm::eventsetup::Callback< T, TReturn, TRecord, TDecorator >::runProducerAsync(), edm::NoProcessProductResolver::setCache(), edm::Worker::skipOnPath(), and edm::LuminosityBlockProcessingStatus::~LuminosityBlockProcessingStatus().

◆ operator=()

const WaitingTaskList& edm::WaitingTaskList::operator= ( const WaitingTaskList )
delete

◆ presetTaskAsFailed()

void WaitingTaskList::presetTaskAsFailed ( std::exception_ptr  iExcept)

Use in the case where you need to inform the parent task of a failure before some other child task which may be run later reports a different, but related failure. You must later call doneWaiting with same exception later in the same thread.

Definition at line 125 of file WaitingTaskList.cc.

125  {
126  if (iExcept and m_waiting) {
127  WaitNode* node = m_head.load();
128  while (node) {
129  WaitNode* next;
130  while (node == (next = node->nextNode())) {
131  hardware_pause();
132  }
133  node->m_task->dependentTaskFailed(iExcept);
134  node = next;
135  }
136  }
137 }

References edm::WaitingTask::dependentTaskFailed(), m_head, edm::WaitingTaskList::WaitNode::m_task, m_waiting, GetRecoTauVFromDQM_MC_cff::next, and edm::WaitingTaskList::WaitNode::nextNode().

Referenced by edm::Path::workerFinished().

◆ reset()

void WaitingTaskList::reset ( void  )

Resets access to the resource so that added tasks will wait.

The owner of the resouce calls reset() to make tasks wait. Calling reset() is NOT thread safe. The system must guarantee that no tasks are using the resource when reset() is called and neither add() nor doneWaiting() can be called concurrently with reset().

Definition at line 52 of file WaitingTaskList.cc.

52  {
53  m_exceptionPtr = std::exception_ptr{};
54  unsigned int nSeenTasks = m_lastAssignedCacheIndex;
56  assert(m_head == nullptr);
57  if (nSeenTasks > m_nodeCacheSize) {
58  //need to expand so next time we don't have to do any
59  // memory requests
60  m_nodeCacheSize = nSeenTasks;
61  m_nodeCache = std::make_unique<WaitNode[]>(nSeenTasks);
62  auto nodeCache = m_nodeCache.get();
63  for (auto it = nodeCache, itEnd = nodeCache + m_nodeCacheSize; it != itEnd; ++it) {
64  it->m_fromCache = true;
65  }
66  }
67  //this will make sure all cores see the changes
68  m_waiting = true;
69 }

References cms::cuda::assert(), m_exceptionPtr, m_head, m_lastAssignedCacheIndex, m_nodeCache, m_nodeCacheSize, and m_waiting.

Referenced by edm::eventsetup::ESSourceDataProxyBase::invalidateCache(), edm::eventsetup::DataProxyTemplate< RecordT, DataT >::invalidateCache(), edm::eventsetup::Callback< T, TReturn, TRecord, TDecorator >::newRecordComing(), edm::Path::processOneOccurrenceAsync(), edm::Worker::reset(), edm::InputProductResolver::resetProductData_(), edm::PuttableProductResolver::resetProductData_(), edm::UnscheduledProductResolver::resetProductData_(), edm::SwitchBaseProductResolver::resetProductData_(), and edm::NoProcessProductResolver::resetProductData_().

Member Data Documentation

◆ m_exceptionPtr

std::exception_ptr edm::WaitingTaskList::m_exceptionPtr
private

Definition at line 168 of file WaitingTaskList.h.

Referenced by add(), announce(), doneWaiting(), and reset().

◆ m_head

std::atomic<WaitNode*> edm::WaitingTaskList::m_head
private

Definition at line 166 of file WaitingTaskList.h.

Referenced by add(), announce(), presetTaskAsFailed(), and reset().

◆ m_lastAssignedCacheIndex

std::atomic<unsigned int> edm::WaitingTaskList::m_lastAssignedCacheIndex
private

Definition at line 170 of file WaitingTaskList.h.

Referenced by createNode(), and reset().

◆ m_nodeCache

std::unique_ptr<WaitNode[]> edm::WaitingTaskList::m_nodeCache
private

Definition at line 167 of file WaitingTaskList.h.

Referenced by createNode(), and reset().

◆ m_nodeCacheSize

unsigned int edm::WaitingTaskList::m_nodeCacheSize
private

Definition at line 169 of file WaitingTaskList.h.

Referenced by createNode(), and reset().

◆ m_waiting

std::atomic<bool> edm::WaitingTaskList::m_waiting
private

Definition at line 171 of file WaitingTaskList.h.

Referenced by add(), doneWaiting(), presetTaskAsFailed(), and reset().

dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
edm::WaitingTaskList::m_head
std::atomic< WaitNode * > m_head
Definition: WaitingTaskList.h:166
edm::WaitingTaskList::createNode
WaitNode * createNode(WaitingTask *iTask)
Definition: WaitingTaskList.cc:71
edm::WaitingTaskList::m_nodeCache
std::unique_ptr< WaitNode[]> m_nodeCache
Definition: WaitingTaskList.h:167
cms::cuda::assert
assert(be >=bs)
edm::WaitingTaskList::m_waiting
std::atomic< bool > m_waiting
Definition: WaitingTaskList.h:171
edm::WaitingTaskList::m_lastAssignedCacheIndex
std::atomic< unsigned int > m_lastAssignedCacheIndex
Definition: WaitingTaskList.h:170
UNLIKELY
#define UNLIKELY(x)
Definition: Likely.h:21
edm::WaitingTask::dependentTaskFailed
void dependentTaskFailed(std::exception_ptr iPtr)
Called if waited for task failed.
Definition: WaitingTask.h:59
hardware_pause
edm::WaitingTaskList::m_nodeCacheSize
unsigned int m_nodeCacheSize
Definition: WaitingTaskList.h:169
edm::WaitingTaskList::announce
void announce()
Definition: WaitingTaskList.cc:139
edm::WaitingTaskList::m_exceptionPtr
std::exception_ptr m_exceptionPtr
Definition: WaitingTaskList.h:168
AlignmentPI::index
index
Definition: AlignmentPayloadInspectorHelper.h:46
submitPVValidationJobs.t
string t
Definition: submitPVValidationJobs.py:644
GetRecoTauVFromDQM_MC_cff.next
next
Definition: GetRecoTauVFromDQM_MC_cff.py:31