Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 
33  public:
36  class WorkItem {
37  public:
38  virtual ~WorkItem();
40  virtual void run() = 0;
41  };
42 
45 
47  virtual ~SerialTaskQueue();
48 
50  void add( WorkItem* item );
51 
54  void noteCompletion();
55 
59  void wait() const;
60 
61  private:
64  public:
68  SerialWorkItem( WorkItem* item, SerialTaskQueue* serializer ) : m_item( item ), m_serializer( serializer ) {}
70  void run();
71 
72  private:
77  };
78 
80  class SerialWorkItemRunner : public tbb::task {
81  public:
83  SerialWorkItemRunner( SerialWorkItem* item ) : m_item( item ) {}
85  tbb::task* execute() override {
86  m_item->run();
87  return NULL;
88  }
89 
90  private:
94  };
95 
96  void i_startNextItem();
97 
101  tbb::concurrent_queue<SerialWorkItem*> m_queue;
102  };
103 
105  // run the wrapped task
106  m_item->run();
107 
108  // We need to keep the pointer on the stack because we are going to delete
109  // ourselves.
110  SerialTaskQueue* serializer = m_serializer;
111 
112  // We call the delete before returning the control to the serialized so that
113  // possible complex code in the task destructor is executed serially.
114  delete this;
115 
116  // Notify the queue of the completion, so that it can schedule the next task.
117  serializer->noteCompletion();
118  }
119 
120 } /* namespace Gaudi */
121 #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.