CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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...
 
void reset ()
 Resets access to the resource so that added tasks will wait. More...
 
 WaitingTaskList (unsigned int iInitialSize=2)
 Constructor. More...
 
 ~WaitingTaskList ()=default
 

Private Member Functions

void announce ()
 
WaitNodecreateNode (WaitingTask *iTask)
 
const WaitingTaskListoperator= (const WaitingTaskList &)=delete
 
 WaitingTaskList (const WaitingTaskList &)=delete
 

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 102 of file WaitingTaskList.h.

Constructor & Destructor Documentation

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 35 of file WaitingTaskList.cc.

35  :
36 m_head{nullptr},
37 m_nodeCache{new WaitNode[iInitialSize]},
38 m_nodeCacheSize{iInitialSize},
40 m_waiting{true}
41 {
42  auto nodeCache = m_nodeCache.get();
43  for(auto it = nodeCache, itEnd = nodeCache+m_nodeCacheSize; it!=itEnd; ++it) {
44  it->m_fromCache=true;
45  }
46 }
std::atomic< bool > m_waiting
std::atomic< unsigned int > m_lastAssignedCacheIndex
std::atomic< WaitNode * > m_head
std::unique_ptr< WaitNode[]> m_nodeCache
unsigned int m_nodeCacheSize
edm::WaitingTaskList::~WaitingTaskList ( )
default
edm::WaitingTaskList::WaitingTaskList ( const WaitingTaskList )
privatedelete

Member Function Documentation

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 92 of file WaitingTaskList.cc.

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

Referenced by edm::Worker::callWhenDoneAsync(), edm::Worker::doWork(), edm::Worker::doWorkAsync(), edm::InputProductResolver::prefetchAsync_(), edm::PuttableProductResolver::prefetchAsync_(), edm::UnscheduledProductResolver::prefetchAsync_(), edm::NoProcessProductResolver::prefetchAsync_(), edm::Path::processOneOccurrenceAsync(), and counter.Counter::register().

92  {
93  iTask->increment_ref_count();
94  if(!m_waiting) {
95  if(m_exceptionPtr) {
97  }
98  if(0==iTask->decrement_ref_count()) {
99  tbb::task::spawn(*iTask);
100  }
101  } else {
102  WaitNode* newHead = createNode(iTask);
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 }
std::atomic< bool > m_waiting
std::atomic< WaitNode * > m_head
void dependentTaskFailed(std::exception_ptr iPtr)
Called if waited for task failed.
Definition: WaitingTask.h:62
std::exception_ptr m_exceptionPtr
WaitNode * createNode(WaitingTask *iTask)
void WaitingTaskList::announce ( )
private

Handles spawning the tasks, safe to call from multiple threads

Definition at line 126 of file WaitingTaskList.cc.

References m_exceptionPtr, edm::WaitingTaskList::WaitNode::m_fromCache, m_head, edm::WaitingTaskList::WaitNode::m_task, gen::n, GetRecoTauVFromDQM_MC_cff::next, edm::WaitingTaskList::WaitNode::nextNode(), and t.

Referenced by add(), and doneWaiting().

127 {
128  //Need a temporary storage since one of these tasks could
129  // cause the next event to start processing which would refill
130  // this waiting list after it has been reset
131  WaitNode* n = m_head.exchange(nullptr);
132  WaitNode* next;
133  while(n) {
134  //it is possible that 'WaitingTaskList::add' is running in a different
135  // thread and we have a new 'head' but the old head has not yet been
136  // attached to the new head (we identify this since 'nextNode' will return itself).
137  // In that case we have to wait until the link has been established before going on.
138  while(n == (next=n->nextNode())) {
139  hardware_pause();
140  }
141  auto t = n->m_task;
142  if(m_exceptionPtr) {
143  t->dependentTaskFailed(m_exceptionPtr);
144  }
145  if(0==t->decrement_ref_count()){
146  tbb::task::spawn(*t);
147  }
148  if(!n->m_fromCache ) {
149  delete n;
150  }
151  n=next;
152  }
153 }
std::atomic< WaitNode * > m_head
std::exception_ptr m_exceptionPtr
WaitingTaskList::WaitNode * WaitingTaskList::createNode ( WaitingTask iTask)
private

Definition at line 73 of file WaitingTaskList.cc.

References cmsHarvester::index, 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().

74 {
75  unsigned int index = m_lastAssignedCacheIndex++;
76 
77  WaitNode* returnValue;
78  if( index < m_nodeCacheSize) {
79  returnValue = m_nodeCache.get()+index;
80  } else {
81  returnValue = new WaitNode;
82  returnValue->m_fromCache=false;
83  }
84  returnValue->m_task = iTask;
85  returnValue->m_next = returnValue;
86 
87  return returnValue;
88 }
std::atomic< unsigned int > m_lastAssignedCacheIndex
std::unique_ptr< WaitNode[]> m_nodeCache
unsigned int m_nodeCacheSize
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 156 of file WaitingTaskList.cc.

References announce(), m_exceptionPtr, and m_waiting.

Referenced by edm::Worker::doWork(), edm::Worker::doWorkAsync(), edm::Path::finished(), edm::InputProductResolver::prefetchAsync_(), edm::PuttableProductResolver::prefetchAsync_(), edm::UnscheduledProductResolver::prefetchAsync_(), edm::PuttableProductResolver::putProduct_(), edm::Worker::runModuleAfterAsyncPrefetch(), edm::NoProcessProductResolver::setCache(), and edm::Worker::skipOnPath().

157 {
158  m_exceptionPtr = iPtr;
159  m_waiting=false;
160  announce();
161 }
std::atomic< bool > m_waiting
std::exception_ptr m_exceptionPtr
const WaitingTaskList& edm::WaitingTaskList::operator= ( const WaitingTaskList )
privatedelete
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.

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

Referenced by edm::Path::processOneOccurrenceAsync(), edm::Worker::reset(), edm::InputProductResolver::resetProductData_(), edm::PuttableProductResolver::resetProductData_(), edm::UnscheduledProductResolver::resetProductData_(), and edm::NoProcessProductResolver::resetProductData_().

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

Member Data Documentation

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

Definition at line 170 of file WaitingTaskList.h.

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

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

Definition at line 168 of file WaitingTaskList.h.

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

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

Definition at line 172 of file WaitingTaskList.h.

Referenced by createNode(), and reset().

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

Definition at line 169 of file WaitingTaskList.h.

Referenced by createNode(), and reset().

unsigned int edm::WaitingTaskList::m_nodeCacheSize
private

Definition at line 171 of file WaitingTaskList.h.

Referenced by createNode(), and reset().

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

Definition at line 173 of file WaitingTaskList.h.

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