| The Gaudi Framework v30r0 (c919700c)

- 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 {
19
33 class SerialTaskQueue
34 {
35 public:
38 class WorkItem
39 {
40 public:
41 virtual ~WorkItem();
43 virtual void run() = 0;
44 };
45
47 SerialTaskQueue();
48
50 virtual ~SerialTaskQueue();
51
53 void add( WorkItem* item );
54
57 void noteCompletion();
58
62 void wait() const;
63
64 private:
66 class SerialWorkItem
67 {
68 public:
72 SerialWorkItem( WorkItem* item, SerialTaskQueue* serializer ) : m_item( item ), m_serializer( serializer ) {}
74 void run();
75
76 private:
78 std::unique_ptr<WorkItem> m_item;
80 SerialTaskQueue* m_serializer;
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:
99 SerialWorkItem* m_item;
100 };
101
102 void i_startNextItem();
103
105 std::atomic<int> m_count;
107 tbb::concurrent_queue<SerialWorkItem*> m_queue;
108 };
109
110 inline void SerialTaskQueue::SerialWorkItem::run()
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_ */
Gaudi::SerialTaskQueue::WorkItem::~WorkItem
virtual ~WorkItem()
Definition: SerialTaskQueue.cpp:17
Gaudi::SerialTaskQueue::SerialWorkItem::m_item
std::unique_ptr< WorkItem > m_item
Pointer to the WorkItem to run.
Definition: SerialTaskQueue.h:78
Gaudi::SerialTaskQueue::SerialWorkItem::run
void run()
Execute the WorkItem and notify the SerialTaskQueue of the completion.
Definition: SerialTaskQueue.h:110
std::atomic< int >
Gaudi::SerialTaskQueue::i_startNextItem
void i_startNextItem()
Definition: SerialTaskQueue.cpp:37
Gaudi::SerialTaskQueue::SerialWorkItem::SerialWorkItem
SerialWorkItem(WorkItem *item, SerialTaskQueue *serializer)
Initialize the instance from the WorkiItem and the SerialTaskQueue (for synchronization).
Definition: SerialTaskQueue.h:72
Gaudi::SerialTaskQueue::WorkItem
Base class for the task to be executed by the serial queue.
Definition: SerialTaskQueue.h:38
Gaudi::SerialTaskQueue
Class for a generic serial queue of tasks (modeled on the Intel Threading Building Blocks Design Patt...
Definition: SerialTaskQueue.h:33
Gaudi::SerialTaskQueue::m_queue
tbb::concurrent_queue< SerialWorkItem * > m_queue
Queue of the tasks to be executed.
Definition: SerialTaskQueue.h:107
Gaudi::SerialTaskQueue::SerialWorkItem
Wrapper for the WorkItem class for internal concurrency bookkeeping.
Definition: SerialTaskQueue.h:66
Gaudi::SerialTaskQueue::wait
void wait() const
Block until all the currently enqueued tasks are completed.
Definition: SerialTaskQueue.cpp:30
Gaudi::SerialTaskQueue::SerialTaskQueue
SerialTaskQueue()
Default constructor.
Definition: SerialTaskQueue.cpp:13
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:88
std::unique_ptr
STL class.
Gaudi::SerialTaskQueue::add
void add(WorkItem *item)
Enqueue a WorkItem for execution.
Definition: SerialTaskQueue.cpp:19
Gaudi::SerialTaskQueue::SerialWorkItemRunner
Helper class to wrap a SerialWorkItem in a tbb::task.
Definition: SerialTaskQueue.h:84
Gaudi::SerialTaskQueue::noteCompletion
void noteCompletion()
Method used by the tasks to trigger the execution of the next task in the queue.
Definition: SerialTaskQueue.cpp:25
Gaudi::SerialTaskQueue::SerialWorkItemRunner::execute
tbb::task * execute() override
Call the run method of the work item.
Definition: SerialTaskQueue.h:90
Gaudi::SerialTaskQueue::SerialWorkItemRunner::m_item
SerialWorkItem * m_item
Pointer to the work item to be executed.
Definition: SerialTaskQueue.h:99
Gaudi::SerialTaskQueue::SerialWorkItem::m_serializer
SerialTaskQueue * m_serializer
Pointer to the SerialTaskQueue used for the synchronization.
Definition: SerialTaskQueue.h:80
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:105