The Gaudi Framework  v33r0 (d5ea422b)
SerialTaskQueue.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 /*
12  * SerialTaskQueue.h
13  *
14  * @date 2012-10-21
15  * @author Marco Clemencic
16  */
17 
18 #ifndef SERIALTASKQUEUE_H_
19 #define SERIALTASKQUEUE_H_
20 
21 #include <atomic>
22 #include <memory>
23 
24 #include <tbb/concurrent_queue.h>
25 #include <tbb/task.h>
26 
27 namespace Gaudi {
28 
43  public:
46  class WorkItem {
47  public:
48  virtual ~WorkItem();
50  virtual void run() = 0;
51  };
52 
55 
57  virtual ~SerialTaskQueue();
58 
60  void add( WorkItem* item );
61 
64  void noteCompletion();
65 
69  void wait() const;
70 
71  private:
74  public:
78  SerialWorkItem( WorkItem* item, SerialTaskQueue* serializer ) : m_item( item ), m_serializer( serializer ) {}
80  void run();
81 
82  private:
87  };
88 
90  class SerialWorkItemRunner : public tbb::task {
91  public:
95  tbb::task* execute() override {
96  m_item->run();
97  return NULL;
98  }
99 
100  private:
104  };
105 
106  void i_startNextItem();
107 
111  tbb::concurrent_queue<SerialWorkItem*> m_queue;
112  };
113 
115  // run the wrapped task
116  m_item->run();
117 
118  // We need to keep the pointer on the stack because we are going to delete
119  // ourselves.
120  SerialTaskQueue* serializer = m_serializer;
121 
122  // We call the delete before returning the control to the serialized so that
123  // possible complex code in the task destructor is executed serially.
124  delete this;
125 
126  // Notify the queue of the completion, so that it can schedule the next task.
127  serializer->noteCompletion();
128  }
129 
130 } /* namespace Gaudi */
131 #endif /* SERIALTASKQUEUE_H_ */
std::unique_ptr< WorkItem > m_item
Pointer to the WorkItem to run.
void run()
Execute the WorkItem and notify the SerialTaskQueue of the completion.
SerialWorkItem(WorkItem *item, SerialTaskQueue *serializer)
Initialize the instance from the WorkiItem and the SerialTaskQueue (for synchronization).
Base class for the task to be executed by the serial queue.
Class for a generic serial queue of tasks (modeled on the Intel Threading Building Blocks Design Patt...
tbb::concurrent_queue< SerialWorkItem * > m_queue
Queue of the tasks to be executed.
Wrapper for the WorkItem class for internal concurrency bookkeeping.
SerialTaskQueue()
Default constructor.
void wait() const
Block until all the currently enqueued tasks are completed.
virtual ~SerialTaskQueue()
Block until all the enqueued tasks are completed.
virtual void run()=0
Method to be implemented by the actual task classes.
SerialWorkItemRunner(SerialWorkItem *item)
Initialize the instance.
STL class.
void add(WorkItem *item)
Enqueue a WorkItem for execution.
Helper class to wrap a SerialWorkItem in a tbb::task.
void noteCompletion()
Method used by the tasks to trigger the execution of the next task in the queue.
tbb::task * execute() override
Call the run method of the work item.
SerialWorkItem * m_item
Pointer to the work item to be executed.
SerialTaskQueue * m_serializer
Pointer to the SerialTaskQueue used for the synchronization.
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
std::atomic< int > m_count
Counter of the currently running tasks.