Nugget
Loading...
Searching...
No Matches
task.hh
Go to the documentation of this file.
1/*
2
3MIT License
4
5Copyright (c) 2022 PCSX-Redux authors
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26
27#pragma once
28
29#include <EASTL/fixed_vector.h>
30#include <EASTL/functional.h>
31
32namespace psyqo {
33
34class GPU;
35
46class TaskQueue {
47 public:
48 class Task;
49
56 void reset();
57
69 TaskQueue &startWith(eastl::function<void(Task *)> &&fun) { return startWith(Task(eastl::move(fun))); }
70
78 TaskQueue &then(Task &&);
79 TaskQueue &then(eastl::function<void(Task *)> &&fun) { return then(Task(eastl::move(fun))); }
80
91 TaskQueue &butCatch(eastl::function<void(TaskQueue *)> &&);
92
103 TaskQueue &finally(eastl::function<void(TaskQueue *)> &&);
104
115 void run();
116
123 Task schedule();
124
130 bool isRunning() const { return m_running; }
131
140 class Task {
141 public:
148 explicit Task(eastl::function<void(Task *)> &&fun) : m_runner(eastl::move(fun)) {}
149 Task(Task &&) = default;
150 Task(const Task &) = delete;
151 Task &operator=(Task &&) = default;
152 Task &operator=(const Task &) = delete;
153
162 void resolve() { m_taskQueue->runNext(); }
163
172 void reject() { m_taskQueue->runCatch(); }
173
183 void complete(bool success) {
184 if (success) {
185 resolve();
186 } else {
187 reject();
188 }
189 }
190
191 private:
192 eastl::function<void(Task *)> m_runner;
193 TaskQueue *m_taskQueue;
194 friend class TaskQueue;
195 };
196
205 static Task DelayedTask(uint32_t delay, GPU &);
206
207 private:
208 void runNext();
209 void runCatch();
210
211 eastl::fixed_vector<Task, 16> m_queue;
212 eastl::function<void(TaskQueue *)> m_catch;
213 eastl::function<void(TaskQueue *)> m_finally;
214 Task *m_parent = nullptr;
215 unsigned m_index = 0;
216 bool m_running = false;
217 friend class Task;
218};
219
220} // namespace psyqo
The singleton GPU class.
Definition gpu.hh:88
The Task class.
Definition task.hh:140
Task & operator=(const Task &)=delete
Task & operator=(Task &&)=default
Task(const Task &)=delete
void reject()
Rejects this task.
Definition task.hh:172
Task(eastl::function< void(Task *)> &&fun)
Construct a new Task object.
Definition task.hh:148
Task(Task &&)=default
void resolve()
Resolves this task.
Definition task.hh:162
void complete(bool success)
Resolves or rejects this task.
Definition task.hh:183
A task queue for processing tasks sequentially.
Definition task.hh:46
static Task DelayedTask(uint32_t delay, GPU &)
Creates a delayed task.
Definition task.cpp:101
Task schedule()
Schedules the task queue to another task queue.
Definition task.cpp:68
TaskQueue & then(eastl::function< void(Task *)> &&fun)
Definition task.hh:79
TaskQueue & startWith(Task &&)
Enqueues a task for execution.
Definition task.cpp:37
void run()
Runs the task queue.
Definition task.cpp:61
TaskQueue & butCatch(eastl::function< void(TaskQueue *)> &&)
Sets the exception handler.
Definition task.cpp:49
TaskQueue & startWith(eastl::function< void(Task *)> &&fun)
Definition task.hh:69
bool isRunning() const
Queries the status of the queue.
Definition task.hh:130
void reset()
Resets the queue.
Definition task.cpp:31
TaskQueue & then(Task &&)
Enqueues a task for execution.
Definition task.cpp:42
Definition cdrom-loader.hh:39
void void(ptr, size)
void uint32_t(classId, spec)