| The Gaudi Framework v28r2-patches (bc4ad17a)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
32 class SerialTaskQueue {
33 public:
36 class WorkItem {
37 public:
38 virtual ~WorkItem();
40 virtual void run() = 0;
41 };
42
44 SerialTaskQueue();
45
47 virtual ~SerialTaskQueue();
48
50 void add(WorkItem* item);
51
54 void noteCompletion();
55
59 void wait() const;
60
61 private:
62
64 class SerialWorkItem {
65 public:
69 SerialWorkItem(WorkItem* item, SerialTaskQueue* serializer):
70 m_item(item), m_serializer(serializer) {}
72 void run();
73 private:
75 std::unique_ptr<WorkItem> m_item;
77 SerialTaskQueue* m_serializer;
78 };
79
81 class SerialWorkItemRunner: public tbb::task {
82 public:
84 SerialWorkItemRunner(SerialWorkItem* item): m_item(item) {}
86 tbb::task* execute() override {
87 m_item->run();
88 return NULL;
89 }
90 private:
93 SerialWorkItem* m_item;
94 };
95
96 void i_startNextItem();
97
99 std::atomic<int> m_count;
101 tbb::concurrent_queue<SerialWorkItem*> m_queue;
102
103 };
104
105
106 inline void SerialTaskQueue::SerialWorkItem::run() {
107 // run the wrapped task
108 m_item->run();
109
110 // We need to keep the pointer on the stack because we are going to delete
111 // ourselves.
112 SerialTaskQueue* serializer = m_serializer;
113
114 // We call the delete before returning the control to the serialized so that
115 // possible complex code in the task destructor is executed serially.
116 delete this;
117
118 // Notify the queue of the completion, so that it can schedule the next task.
119 serializer->noteCompletion();
120 }
121
122 } /* namespace Gaudi */
123 #endif /* SERIALTASKQUEUE_H_ */
Gaudi::SerialTaskQueue::WorkItem::~WorkItem
virtual ~WorkItem()
Definition: SerialTaskQueue.cpp:19
Gaudi::SerialTaskQueue::SerialWorkItem::m_item
std::unique_ptr< WorkItem > m_item
Pointer to the WorkItem to run.
Definition: SerialTaskQueue.h:75
Gaudi::SerialTaskQueue::SerialWorkItem::run
void run()
Execute the WorkItem and notify the SerialTaskQueue of the completion.
Definition: SerialTaskQueue.h:106
std::atomic< int >
Gaudi::SerialTaskQueue::i_startNextItem
void i_startNextItem()
Definition: SerialTaskQueue.cpp:38
Gaudi::SerialTaskQueue::SerialWorkItem::SerialWorkItem
SerialWorkItem(WorkItem *item, SerialTaskQueue *serializer)
Initialize the instance from the WorkiItem and the SerialTaskQueue (for synchronization).
Definition: SerialTaskQueue.h:69
Gaudi::SerialTaskQueue::WorkItem
Base class for the task to be executed by the serial queue.
Definition: SerialTaskQueue.h:36
Gaudi::SerialTaskQueue
Class for a generic serial queue of tasks (modeled on the Intel Threading Building Blocks Design Patt...
Definition: SerialTaskQueue.h:32
Gaudi::SerialTaskQueue::m_queue
tbb::concurrent_queue< SerialWorkItem * > m_queue
Queue of the tasks to be executed.
Definition: SerialTaskQueue.h:101
Gaudi::SerialTaskQueue::SerialWorkItem
Wrapper for the WorkItem class for internal concurrency bookkeeping.
Definition: SerialTaskQueue.h:64
Gaudi::SerialTaskQueue::wait
void wait() const
Block until all the currently enqueued tasks are completed.
Definition: SerialTaskQueue.cpp:33
Gaudi::SerialTaskQueue::SerialTaskQueue
SerialTaskQueue()
Default constructor.
Definition: SerialTaskQueue.cpp:12
Gaudi::SerialTaskQueue::~SerialTaskQueue
virtual ~SerialTaskQueue()
Block until all the enqueued tasks are completed.
Definition: SerialTaskQueue.cpp:15
Gaudi::SerialTaskQueue::WorkItem::run
virtual void run()=0
Method to be implemented by the actual task classes.
Gaudi::SerialTaskQueue::SerialWorkItemRunner::SerialWorkItemRunner
SerialWorkItemRunner(SerialWorkItem *item)
Initialize the instance.
Definition: SerialTaskQueue.h:84
std::unique_ptr
STL class.
Gaudi::SerialTaskQueue::add
void add(WorkItem *item)
Enqueue a WorkItem for execution.
Definition: SerialTaskQueue.cpp:22
Gaudi::SerialTaskQueue::SerialWorkItemRunner
Helper class to wrap a SerialWorkItem in a tbb::task.
Definition: SerialTaskQueue.h:81
Gaudi::SerialTaskQueue::noteCompletion
void noteCompletion()
Method used by the tasks to trigger the execution of the next task in the queue.
Definition: SerialTaskQueue.cpp:28
Gaudi::SerialTaskQueue::SerialWorkItemRunner::execute
tbb::task * execute() override
Call the run method of the work item.
Definition: SerialTaskQueue.h:86
Gaudi::SerialTaskQueue::SerialWorkItemRunner::m_item
SerialWorkItem * m_item
Pointer to the work item to be executed.
Definition: SerialTaskQueue.h:93
Gaudi::SerialTaskQueue::SerialWorkItem::m_serializer
SerialTaskQueue * m_serializer
Pointer to the SerialTaskQueue used for the synchronization.
Definition: SerialTaskQueue.h:77
Gaudi
Helper functions to set/get the application return code.
Definition: __init__.py:1
Gaudi::SerialTaskQueue::m_count
std::atomic< int > m_count
Counter of the currently running tasks.
Definition: SerialTaskQueue.h:99