Radium Engine  1.5.20
Loading...
Searching...
No Matches
TaskQueue.hpp
1#pragma once
2
3#include <Core/RaCore.hpp>
4#include <Core/Utils/Index.hpp>
5#include <Core/Utils/Timer.hpp> // Ra::Core::TimePoint
6
7#include <atomic>
8#include <condition_variable>
9#include <deque>
10#include <memory>
11#include <mutex>
12#include <shared_mutex>
13#include <string>
14#include <thread>
15#include <vector>
16
17namespace Ra {
18namespace Core {
19class Task;
20}
21} // namespace Ra
22
23namespace Ra {
24namespace Core {
47class RA_CORE_API TaskQueue
48{
49 public:
51 using TaskId = Utils::Index;
52
54 struct TimerData {
55 Utils::TimePoint start;
57 uint threadId;
58 std::string taskName;
59 };
60
61 public:
64 explicit TaskQueue( uint numThreads );
65
67 ~TaskQueue();
68
69 //
70 // Task management
71 //
72
76 TaskId registerTask( std::unique_ptr<Task> task );
77
81 void removeTask( TaskId taskId );
82
83 TaskId getTaskId( const std::string& taskName ) const;
84
87 void addDependency( TaskId predecessor, TaskId successor );
88
91 bool addDependency( const std::string& predecessors, TaskId successor );
92 bool addDependency( TaskId predecessor, const std::string& successors );
93
96 void addPendingDependency( const std::string& predecessors, TaskId successor );
97 void addPendingDependency( TaskId predecessor, const std::string& successors );
98
99 //
100 // Task queue operations
101 //
102
105 void startTasks();
106
113 void runTasksInThisThread();
114
116 void waitForTasks();
117
119 const std::vector<TimerData>& getTimerData();
120
122 void flushTaskQueue();
123
125 void printTaskGraph( std::ostream& output ) const;
126
127 private:
129 void runThread( uint id );
130
133 void queueTask( TaskId task );
134
137 void detectCycles();
138
140 void resolveDependencies();
141
142 private:
147
149 std::vector<std::thread> m_workerThreads;
150
151 //
152 // mutex protected variables.
153 //
154
158 std::vector<std::vector<TaskId>> m_dependencies;
159
163
165 std::vector<TimerData> m_timerData;
166
168 std::vector<uint> m_remainingDependencies;
170 std::deque<TaskId> m_taskQueue;
172 uint m_processingTasks;
173
175 std::atomic_bool m_shuttingDown;
177 std::condition_variable_any m_threadNotifier;
178 std::condition_variable_any m_waitForTasksNotifier;
179
181 mutable std::shared_mutex m_mutex;
182};
183
184} // namespace Core
185} // namespace Ra
This class allows tasks to be registered and then executed in parallel on separate threads.
Definition TaskQueue.hpp:48
Utils::Index TaskId
Identifier for a task in the task queue.
Definition TaskQueue.hpp:51
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:3
Record of a task's start and end time.
Definition TaskQueue.hpp:54