Radium Engine
1.6.3
Loading...
Searching...
No Matches
TaskQueue.hpp
1
#pragma once
2
3
#include <Core/CoreMacros.hpp>
4
#include <Core/RaCore.hpp>
5
#include <Core/Utils/Index.hpp>
6
#include <Core/Utils/Timer.hpp>
// Ra::Core::TimePoint
7
#include <atomic>
8
#include <condition_variable>
9
#include <deque>
10
#include <iosfwd>
11
#include <memory>
12
#include <mutex>
13
#include <shared_mutex>
14
#include <string>
15
#include <thread>
16
#include <utility>
17
#include <vector>
18
19
namespace
Ra
{
20
namespace
Core {
21
class
Task;
22
}
23
}
// namespace Ra
24
25
namespace
Ra
{
26
namespace
Core {
49
class
RA_CORE_API
TaskQueue
50
{
51
public
:
53
using
TaskId
= Utils::Index;
54
56
struct
TimerData
{
57
Utils::TimePoint
start;
58
Utils::TimePoint
end;
59
uint threadId;
60
std::string
taskName;
61
};
62
63
public
:
66
explicit
TaskQueue
( uint numThreads );
67
69
~TaskQueue
();
70
71
//
72
// Task management
73
//
74
78
TaskId
registerTask(
std::unique_ptr<Task>
task );
79
83
void
removeTask(
TaskId
taskId );
84
85
TaskId
getTaskId(
const
std::string
& taskName )
const
;
86
89
void
addDependency(
TaskId
predecessor,
TaskId
successor );
90
93
bool
addDependency(
const
std::string
& predecessors,
TaskId
successor );
94
bool
addDependency(
TaskId
predecessor,
const
std::string
& successors );
95
98
void
addPendingDependency(
const
std::string
& predecessors,
TaskId
successor );
99
void
addPendingDependency(
TaskId
predecessor,
const
std::string
& successors );
100
101
//
102
// Task queue operations
103
//
104
107
void
startTasks();
108
115
void
runTasksInThisThread();
116
118
void
waitForTasks();
119
121
const
std::vector<TimerData>
& getTimerData();
122
124
void
flushTaskQueue();
125
127
void
printTaskGraph(
std::ostream
& output )
const
;
128
129
private
:
131
void
runThread( uint
id
);
132
135
void
queueTask(
TaskId
task );
136
139
void
detectCycles();
140
142
void
resolveDependencies();
143
144
private
:
146
using
wlock
=
std::unique_lock<std::shared_mutex>
;
148
using
rlock
=
std::shared_lock<std::shared_mutex>
;
149
151
std::vector<std::thread>
m_workerThreads;
152
153
//
154
// mutex protected variables.
155
//
156
158
std::vector<std::unique_ptr<Task>
> m_tasks;
160
std::vector<std::vector<TaskId>
> m_dependencies;
161
163
std::vector<std::pair<TaskId, std::string>
> m_pendingDepsPre;
164
std::vector<std::pair<std::string, TaskId>
> m_pendingDepsSucc;
165
167
std::vector<TimerData>
m_timerData;
168
170
std::vector<uint>
m_remainingDependencies;
172
std::deque<TaskId>
m_taskQueue;
174
uint m_processingTasks;
175
177
std::atomic_bool m_shuttingDown;
179
std::condition_variable_any
m_threadNotifier;
180
std::condition_variable_any
m_waitForTasksNotifier;
181
183
mutable
std::shared_mutex m_mutex;
184
};
185
186
}
// namespace Core
187
}
// namespace Ra
std::ostream
std::string
Ra::Core::TaskQueue
This class allows tasks to be registered and then executed in parallel on separate threads.
Definition
TaskQueue.hpp:50
Ra::Core::TaskQueue::TaskId
Utils::Index TaskId
Identifier for a task in the task queue.
Definition
TaskQueue.hpp:53
std::condition_variable_any
std::deque
Ra
hepler function to manage enum as underlying types in VariableSet
Definition
Cage.cpp:4
std::shared_lock
Ra::Core::TaskQueue::TimerData
Record of a task's start and end time.
Definition
TaskQueue.hpp:56
std::chrono::time_point< Clock >
std::unique_lock
std::unique_ptr
std::vector
src
Core
Tasks
TaskQueue.hpp
Generated by
1.12.0