| The Gaudi Framework v32r1 (f65d50dc)

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:
63 class SerialWorkItem {
64 public:
68 SerialWorkItem( WorkItem* item, SerialTaskQueue* serializer ) : m_item( item ), m_serializer( serializer ) {}
70 void run();
71
72 private:
74 std::unique_ptr<WorkItem> m_item;
76 SerialTaskQueue* m_serializer;
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:
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 inline void SerialTaskQueue::SerialWorkItem::run() {
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_ */
Gaudi::SerialTaskQueue::WorkItem::~WorkItem
virtual ~WorkItem()
Definition: SerialTaskQueue.cpp:16
Gaudi::SerialTaskQueue::SerialWorkItem::m_item
std::unique_ptr< WorkItem > m_item
Pointer to the WorkItem to run.
Definition: SerialTaskQueue.h:74
Gaudi::SerialTaskQueue::SerialWorkItem::run
void run()
Execute the WorkItem and notify the SerialTaskQueue of the completion.
Definition: SerialTaskQueue.h:104
std::atomic< int >
Gaudi::SerialTaskQueue::i_startNextItem
void i_startNextItem()
Definition: SerialTaskQueue.cpp:32
Gaudi::SerialTaskQueue::SerialWorkItem::SerialWorkItem
SerialWorkItem(WorkItem *item, SerialTaskQueue *serializer)
Initialize the instance from the WorkiItem and the SerialTaskQueue (for synchronization).
Definition: SerialTaskQueue.h:68
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:63
Gaudi::SerialTaskQueue::SerialTaskQueue
SerialTaskQueue()
Default constructor.
Definition: SerialTaskQueue.cpp:12
Gaudi::SerialTaskQueue::wait
void wait() const
Block until all the currently enqueued tasks are completed.
Definition: SerialTaskQueue.cpp:27
Gaudi::SerialTaskQueue::~SerialTaskQueue
virtual ~SerialTaskQueue()
Block until all the enqueued tasks are completed.
Definition: SerialTaskQueue.cpp:14
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:83
std::unique_ptr
STL class.
Gaudi::SerialTaskQueue::add
void add(WorkItem *item)
Enqueue a WorkItem for execution.
Definition: SerialTaskQueue.cpp:18
Gaudi::SerialTaskQueue::SerialWorkItemRunner
Helper class to wrap a SerialWorkItem in a tbb::task.
Definition: SerialTaskQueue.h:80
Gaudi::SerialTaskQueue::noteCompletion
void noteCompletion()
Method used by the tasks to trigger the execution of the next task in the queue.
Definition: SerialTaskQueue.cpp:23
Gaudi::SerialTaskQueue::SerialWorkItemRunner::execute
tbb::task * execute() override
Call the run method of the work item.
Definition: SerialTaskQueue.h:85
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:76
Gaudi
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
Gaudi::SerialTaskQueue::m_count
std::atomic< int > m_count
Counter of the currently running tasks.
Definition: SerialTaskQueue.h:99