forked from hrydgard/ppsspp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadEventQueue.h
205 lines (172 loc) · 5.08 KB
/
ThreadEventQueue.h
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
// Copyright (c) 2013- PPSSPP Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0 or later versions.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#pragma once
#include <mutex>
#include <condition_variable>
#include <deque>
#include "Core/System.h"
#include "Core/CoreTiming.h"
template <typename B, typename Event, typename EventType, EventType EVENT_INVALID, EventType EVENT_SYNC, EventType EVENT_FINISH>
struct ThreadEventQueue : public B {
ThreadEventQueue() : threadEnabled_(false), eventsRunning_(false), eventsHaveRun_(false) {
}
void SetThreadEnabled(bool threadEnabled) {
threadEnabled_ = threadEnabled;
}
bool ThreadEnabled() {
return threadEnabled_;
}
void ScheduleEvent(Event ev) {
if (threadEnabled_) {
std::lock_guard<std::recursive_mutex> guard(eventsLock_);
events_.push_back(ev);
eventsWait_.notify_one();
} else {
events_.push_back(ev);
}
if (!threadEnabled_) {
RunEventsUntil(0);
}
}
bool HasEvents() {
if (threadEnabled_) {
std::lock_guard<std::recursive_mutex> guard(eventsLock_);
return !events_.empty();
} else {
return !events_.empty();
}
}
void NotifyDrain() {
if (threadEnabled_) {
std::lock_guard<std::recursive_mutex> guard(eventsLock_);
eventsDrain_.notify_one();
}
}
Event GetNextEvent() {
if (threadEnabled_) {
std::lock_guard<std::recursive_mutex> guard(eventsLock_);
if (events_.empty()) {
NotifyDrain();
return EVENT_INVALID;
}
Event ev = events_.front();
events_.pop_front();
return ev;
} else {
if (events_.empty()) {
return EVENT_INVALID;
}
Event ev = events_.front();
events_.pop_front();
return ev;
}
}
void RunEventsUntil(u64 globalticks) {
if (!threadEnabled_) {
do {
for (Event ev = GetNextEvent(); EventType(ev) != EVENT_INVALID; ev = GetNextEvent()) {
ProcessEventIfApplicable(ev, globalticks);
}
} while (CoreTiming::GetTicks() < globalticks);
return;
}
std::unique_lock<std::recursive_mutex> guard(eventsLock_);
eventsRunning_ = true;
eventsHaveRun_ = true;
do {
while (events_.empty() && !ShouldExitEventLoop()) {
eventsWait_.wait(guard);
}
// Quit the loop if the queue is drained and coreState has tripped, or threading is disabled.
if (events_.empty()) {
break;
}
for (Event ev = GetNextEvent(); EventType(ev) != EVENT_INVALID; ev = GetNextEvent()) {
guard.unlock();
ProcessEventIfApplicable(ev, globalticks);
guard.lock();
}
} while (CoreTiming::GetTicks() < globalticks);
// This will force the waiter to check coreState, even if we didn't actually drain.
NotifyDrain();
eventsRunning_ = false;
}
void SyncBeginFrame() {
if (threadEnabled_) {
std::lock_guard<std::recursive_mutex> guard(eventsLock_);
eventsHaveRun_ = false;
} else {
eventsHaveRun_ = false;
}
}
inline bool ShouldSyncThread(bool force) {
if (!HasEvents())
return false;
if (coreState != CORE_RUNNING && !force)
return false;
// Don't run if it's not running, but wait for startup.
if (!eventsRunning_) {
if (eventsHaveRun_ || coreState == CORE_ERROR || coreState == CORE_POWERDOWN) {
return false;
}
}
return true;
}
// Force ignores coreState.
void SyncThread(bool force = false) {
if (!threadEnabled_) {
return;
}
std::unique_lock<std::recursive_mutex> guard(eventsLock_);
// While processing the last event, HasEvents() will be false even while not done.
// So we schedule a nothing event and wait for that to finish.
ScheduleEvent(EVENT_SYNC);
while (ShouldSyncThread(force)) {
eventsDrain_.wait(guard);
}
}
void FinishEventLoop() {
if (!threadEnabled_) {
return;
}
std::lock_guard<std::recursive_mutex> guard(eventsLock_);
// Don't schedule a finish if it's not even running.
if (eventsRunning_) {
ScheduleEvent(EVENT_FINISH);
}
}
protected:
virtual void ProcessEvent(Event ev) = 0;
virtual bool ShouldExitEventLoop() = 0;
inline void ProcessEventIfApplicable(Event &ev, u64 &globalticks) {
switch (EventType(ev)) {
case EVENT_FINISH:
// Stop waiting.
globalticks = 0;
break;
case EVENT_SYNC:
// Nothing special to do, this event it just to wait on, see SyncThread.
break;
default:
ProcessEvent(ev);
}
}
private:
bool threadEnabled_;
bool eventsRunning_;
bool eventsHaveRun_;
std::deque<Event> events_;
std::recursive_mutex eventsLock_; // TODO: Should really make this non-recursive - condition_variable_any is dangerous
std::condition_variable_any eventsWait_;
std::condition_variable_any eventsDrain_;
};