The Gaudi Framework  v29r0 (ff2e7097)
SerialTaskQueue.h
Go to the documentation of this file.
1 /*
2  * SerialTaskQueue.h
3  *
4  * @date 2012-10-21
5  * @author Marco Clemencic
6  */
7 
8 #ifndef SERIALTASKQUEUE_H_
9 #define SERIALTASKQUEUE_H_
10 
11 #include <atomic>
12 #include <memory>
13 
14 #include <tbb/concurrent_queue.h>
15 #include <tbb/task.h>
16 
17 namespace Gaudi
18 {
19 
34  {
35  public:
38  class WorkItem
39  {
40  public:
41  virtual ~WorkItem();
43  virtual void run() = 0;
44  };
45 
48 
50  virtual ~SerialTaskQueue();
51 
53  void add( WorkItem* item );
54 
57  void noteCompletion();
58 
62  void wait() const;
63 
64  private:
67  {
68  public:
72  SerialWorkItem( WorkItem* item, SerialTaskQueue* serializer ) : m_item( item ), m_serializer( serializer ) {}
74  void run();
75 
76  private:
81  };
82 
84  class SerialWorkItemRunner : public tbb::task
85  {
86  public:
88  SerialWorkItemRunner( SerialWorkItem* item ) : m_item( item ) {}
90  tbb::task* execute() override
91  {
92  m_item->run();
93  return NULL;
94  }
95 
96  private:
100  };
101 
102  void i_startNextItem();
103 
107  tbb::concurrent_queue<SerialWorkItem*> m_queue;
108  };
109 
111  {
112  // run the wrapped task
113  m_item->run();
114 
115  // We need to keep the pointer on the stack because we are going to delete
116  // ourselves.
117  SerialTaskQueue* serializer = m_serializer;
118 
119  // We call the delete before returning the control to the serialized so that
120  // possible complex code in the task destructor is executed serially.
121  delete this;
122 
123  // Notify the queue of the completion, so that it can schedule the next task.
124  serializer->noteCompletion();
125  }
126 
127 } /* namespace Gaudi */
128 #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.
void wait() const
Block until all the currently enqueued tasks are completed.
SerialTaskQueue()
Default constructor.
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.
Helper functions to set/get the application return code.
Definition: __init__.py:1
std::atomic< int > m_count
Counter of the currently running tasks.