forked from pcsx-redux/nugget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.hh
220 lines (191 loc) · 7.09 KB
/
task.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
MIT License
Copyright (c) 2022 PCSX-Redux authors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include <EASTL/fixed_vector.h>
#include <EASTL/functional.h>
namespace psyqo {
class GPU;
/**
* @brief A task queue for processing tasks sequentially.
*
* @details This class is used to process a sequence of tasks one after the other.
* The design is loosely inspired by the JavaScript Promise API. A task is
* effectively a lambda function. A queue can be run multiple times, and can also
* be embedded within another queue.
*
*/
class TaskQueue {
public:
class Task;
/**
* @brief Resets the queue.
*
* @details This method resets the queue to its initial state.
* Calling this method while the queue is running is undefined behavior.
*/
void reset();
/**
* @brief Enqueues a task for execution.
*
* @details This method will enqueue a task for execution, while resetting
* the queue first. This means that any previously enqueued tasks will be
* removed first. Any exception handler or finally handler will also be
* cleared out. Calling this while the queue is running is undefined
* behavior.
*
*/
TaskQueue &startWith(Task &&);
TaskQueue &startWith(eastl::function<void(Task *)> &&fun) { return startWith(Task(eastl::move(fun))); }
/**
* @brief Enqueues a task for execution.
*
* @details This method will enqueue a task for execution. The task will
* be executed after all previously enqueued tasks have been executed.
* Calling this while the queue is running is undefined behavior.
*/
TaskQueue &then(Task &&);
TaskQueue &then(eastl::function<void(Task *)> &&fun) { return then(Task(eastl::move(fun))); }
/**
* @brief Sets the exception handler.
*
* @details This method will set the exception handler. The exception
* handler will be called if any of the tasks in the queue throws an
* exception. The exception handler will be called with the task queue
* as its argument. Calling this while the queue is running is undefined
* behavior.
*
*/
TaskQueue &butCatch(eastl::function<void(TaskQueue *)> &&);
/**
* @brief Sets the finally handler.
*
* @details This method will set the finally handler. The finally handler
* will be called after all tasks in the queue have been executed, or
* after the exception handler. The finally handler will be called with
* the task queue as its argument. Calling this while the queue is running
* is undefined behavior.
*
*/
TaskQueue &finally(eastl::function<void(TaskQueue *)> &&);
/**
* @brief Runs the task queue.
*
* @details This method will start running the queue. The queue will
* continue to run until all tasks have been executed, or until an
* exception is thrown. This method can be called multiple times, in
* order to execute the whole queue multiple times. Calling this
* while the queue is already running is undefined behavior.
*
*/
void run();
/**
* @brief Schedules the task queue to another task queue.
*
* @details This method will enable embedding the queue into another
* one. Exceptions will be cascading.
*/
Task schedule();
/**
* @brief Queries the status of the queue.
*
* @return true if the queue is running, false otherwise.
*/
bool isRunning() const { return m_running; }
/**
* @brief The Task class.
*
* @details This class is the holder for a task to execute within the queue.
* It can be constructed with a lambda, or be moved. It effectively acts
* as a promise, but without data attached to it.
*
*/
class Task {
public:
/**
* @brief Construct a new Task object
*
* @param fun The lambda to execute for this task. It will receive the task
* as its argument.
*/
explicit Task(eastl::function<void(Task *)> &&fun) : m_runner(eastl::move(fun)) {}
Task(Task &&) = default;
Task(const Task &) = delete;
Task &operator=(Task &&) = default;
Task &operator=(const Task &) = delete;
/**
* @brief Resolves this task.
*
* @details This method is to be called by the task's lambda function to
* resolve the task. It will continue to the next task in the queue, or
* call the finally handler if there are no more tasks.
*
*/
void resolve() { m_taskQueue->runNext(); }
/**
* @brief Rejects this task.
*
* @details This method is to be called by the task's lambda function to
* reject the task. It will call the exception handler, and then the
* finally handler.
*
*/
void reject() { m_taskQueue->runCatch(); }
/**
* @brief Resolves or rejects this task.
*
* @details This method is to be called by the task's lambda function to
* resolve or reject the task. It is a convenience method that will
* either call resolve() or reject() depending on the value of the
* first argument.
*
*/
void complete(bool success) {
if (success) {
resolve();
} else {
reject();
}
}
private:
eastl::function<void(Task *)> m_runner;
TaskQueue *m_taskQueue;
friend class TaskQueue;
};
/**
* @brief Creates a delayed task.
*
* @details This method is a convenience method to create a task that will
* be executed after a delay. The delay is specified in microseconds.
*
* @param delay The delay in microseconds.
*/
static Task DelayedTask(uint32_t delay, GPU &);
private:
void runNext();
void runCatch();
eastl::fixed_vector<Task, 16> m_queue;
eastl::function<void(TaskQueue *)> m_catch;
eastl::function<void(TaskQueue *)> m_finally;
Task *m_parent = nullptr;
unsigned m_index = 0;
bool m_running = false;
friend class Task;
};
} // namespace psyqo