1
// Copyright (c) 2013- PPSSPP Project.
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
20
#include "base/mutex.h"
21
#include "Core/System.h"
22
#include "Core/CoreTiming.h"
25
template <typename B, typename Event, typename EventType, EventType EVENT_INVALID, EventType EVENT_SYNC, EventType EVENT_FINISH>
26
struct ThreadEventQueue : public B {
27
ThreadEventQueue() : threadEnabled_(false), eventsRunning_(false), eventsHaveRun_(false) {
30
void SetThreadEnabled(bool threadEnabled) {
31
threadEnabled_ = threadEnabled;
34
bool ThreadEnabled() {
35
return threadEnabled_;
38
void ScheduleEvent(Event ev) {
40
lock_guard guard(eventsLock_);
41
events_.push_back(ev);
42
eventsWait_.notify_one();
44
events_.push_back(ev);
47
if (!threadEnabled_) {
54
lock_guard guard(eventsLock_);
55
return !events_.empty();
57
return !events_.empty();
63
lock_guard guard(eventsLock_);
64
eventsDrain_.notify_one();
68
Event GetNextEvent() {
70
lock_guard guard(eventsLock_);
71
if (events_.empty()) {
76
Event ev = events_.front();
80
if (events_.empty()) {
83
Event ev = events_.front();
89
void RunEventsUntil(u64 globalticks) {
90
if (!threadEnabled_) {
92
for (Event ev = GetNextEvent(); EventType(ev) != EVENT_INVALID; ev = GetNextEvent()) {
93
ProcessEventIfApplicable(ev, globalticks);
95
} while (CoreTiming::GetTicks() < globalticks);
99
lock_guard guard(eventsLock_);
100
eventsRunning_ = true;
101
eventsHaveRun_ = true;
103
while (!HasEvents() && !ShouldExitEventLoop()) {
104
eventsWait_.wait(eventsLock_);
106
// Quit the loop if the queue is drained and coreState has tripped, or threading is disabled.
111
for (Event ev = GetNextEvent(); EventType(ev) != EVENT_INVALID; ev = GetNextEvent()) {
112
eventsLock_.unlock();
113
ProcessEventIfApplicable(ev, globalticks);
116
} while (CoreTiming::GetTicks() < globalticks);
118
// This will force the waiter to check coreState, even if we didn't actually drain.
120
eventsRunning_ = false;
123
void SyncBeginFrame() {
124
if (threadEnabled_) {
125
lock_guard guard(eventsLock_);
126
eventsHaveRun_ = false;
128
eventsHaveRun_ = false;
132
inline bool ShouldSyncThread(bool force) {
135
if (coreState != CORE_RUNNING && !force)
138
// Don't run if it's not running, but wait for startup.
139
if (!eventsRunning_) {
140
if (eventsHaveRun_ || coreState == CORE_ERROR || coreState == CORE_POWERDOWN) {
148
// Force ignores coreState.
149
void SyncThread(bool force = false) {
150
if (!threadEnabled_) {
154
lock_guard guard(eventsLock_);
155
// While processing the last event, HasEvents() will be false even while not done.
156
// So we schedule a nothing event and wait for that to finish.
157
ScheduleEvent(EVENT_SYNC);
158
while (ShouldSyncThread(force)) {
159
eventsDrain_.wait(eventsLock_);
163
void FinishEventLoop() {
164
if (!threadEnabled_) {
168
lock_guard guard(eventsLock_);
169
// Don't schedule a finish if it's not even running.
170
if (eventsRunning_) {
171
ScheduleEvent(EVENT_FINISH);
176
virtual void ProcessEvent(Event ev) = 0;
177
virtual bool ShouldExitEventLoop() = 0;
179
inline void ProcessEventIfApplicable(Event &ev, u64 &globalticks) {
180
switch (EventType(ev)) {
187
// Nothing special to do, this event it just to wait on, see SyncThread.
199
std::deque<Event> events_;
200
recursive_mutex eventsLock_;
201
condition_variable eventsWait_;
202
condition_variable eventsDrain_;