2013-08-10 16:08:31 +00:00
|
|
|
// 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
|
|
|
|
|
2017-02-27 20:57:46 +00:00
|
|
|
#include <mutex>
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <deque>
|
|
|
|
|
2013-08-11 22:51:13 +00:00
|
|
|
#include "Core/System.h"
|
2013-08-10 16:08:31 +00:00
|
|
|
#include "Core/CoreTiming.h"
|
|
|
|
|
|
|
|
template <typename B, typename Event, typename EventType, EventType EVENT_INVALID, EventType EVENT_SYNC, EventType EVENT_FINISH>
|
|
|
|
struct ThreadEventQueue : public B {
|
2013-11-10 04:58:42 +00:00
|
|
|
ThreadEventQueue() : threadEnabled_(false), eventsRunning_(false), eventsHaveRun_(false) {
|
2013-10-13 22:14:30 +00:00
|
|
|
}
|
2022-12-11 04:32:12 +00:00
|
|
|
virtual ~ThreadEventQueue() {
|
|
|
|
}
|
2013-10-13 22:14:30 +00:00
|
|
|
|
2013-08-10 16:08:31 +00:00
|
|
|
void SetThreadEnabled(bool threadEnabled) {
|
|
|
|
threadEnabled_ = threadEnabled;
|
|
|
|
}
|
|
|
|
|
2013-11-10 04:56:11 +00:00
|
|
|
bool ThreadEnabled() {
|
|
|
|
return threadEnabled_;
|
|
|
|
}
|
|
|
|
|
2013-08-10 16:08:31 +00:00
|
|
|
void ScheduleEvent(Event ev) {
|
2015-01-10 23:45:57 +00:00
|
|
|
if (threadEnabled_) {
|
2017-02-27 20:57:46 +00:00
|
|
|
std::lock_guard<std::recursive_mutex> guard(eventsLock_);
|
2013-08-10 16:08:31 +00:00
|
|
|
events_.push_back(ev);
|
|
|
|
eventsWait_.notify_one();
|
2015-01-10 23:45:57 +00:00
|
|
|
} else {
|
|
|
|
events_.push_back(ev);
|
2013-08-10 16:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!threadEnabled_) {
|
|
|
|
RunEventsUntil(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasEvents() {
|
2015-01-10 23:45:57 +00:00
|
|
|
if (threadEnabled_) {
|
2017-02-27 20:57:46 +00:00
|
|
|
std::lock_guard<std::recursive_mutex> guard(eventsLock_);
|
2015-01-10 23:45:57 +00:00
|
|
|
return !events_.empty();
|
|
|
|
} else {
|
|
|
|
return !events_.empty();
|
|
|
|
}
|
2013-08-10 16:08:31 +00:00
|
|
|
}
|
|
|
|
|
2013-10-13 19:13:11 +00:00
|
|
|
void NotifyDrain() {
|
2015-01-10 23:45:57 +00:00
|
|
|
if (threadEnabled_) {
|
2017-02-27 20:57:46 +00:00
|
|
|
std::lock_guard<std::recursive_mutex> guard(eventsLock_);
|
2015-01-10 23:45:57 +00:00
|
|
|
eventsDrain_.notify_one();
|
|
|
|
}
|
2013-10-13 19:13:11 +00:00
|
|
|
}
|
|
|
|
|
2013-08-10 16:08:31 +00:00
|
|
|
Event GetNextEvent() {
|
2015-01-10 23:45:57 +00:00
|
|
|
if (threadEnabled_) {
|
2017-02-27 20:57:46 +00:00
|
|
|
std::lock_guard<std::recursive_mutex> guard(eventsLock_);
|
2015-01-10 23:45:57 +00:00
|
|
|
if (events_.empty()) {
|
|
|
|
NotifyDrain();
|
|
|
|
return EVENT_INVALID;
|
|
|
|
}
|
2013-08-10 16:08:31 +00:00
|
|
|
|
2015-01-10 23:45:57 +00:00
|
|
|
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;
|
|
|
|
}
|
2013-08-10 16:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RunEventsUntil(u64 globalticks) {
|
2015-01-10 23:45:57 +00:00
|
|
|
if (!threadEnabled_) {
|
|
|
|
do {
|
|
|
|
for (Event ev = GetNextEvent(); EventType(ev) != EVENT_INVALID; ev = GetNextEvent()) {
|
2015-01-11 00:10:47 +00:00
|
|
|
ProcessEventIfApplicable(ev, globalticks);
|
2015-01-10 23:45:57 +00:00
|
|
|
}
|
|
|
|
} while (CoreTiming::GetTicks() < globalticks);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-27 20:57:46 +00:00
|
|
|
std::unique_lock<std::recursive_mutex> guard(eventsLock_);
|
2013-10-13 19:13:11 +00:00
|
|
|
eventsRunning_ = true;
|
2013-11-10 04:58:42 +00:00
|
|
|
eventsHaveRun_ = true;
|
2013-08-10 16:08:31 +00:00
|
|
|
do {
|
2017-02-27 20:57:46 +00:00
|
|
|
while (events_.empty() && !ShouldExitEventLoop()) {
|
|
|
|
eventsWait_.wait(guard);
|
2014-04-29 05:12:33 +00:00
|
|
|
}
|
2014-07-06 21:02:00 +00:00
|
|
|
// Quit the loop if the queue is drained and coreState has tripped, or threading is disabled.
|
2017-02-27 20:57:46 +00:00
|
|
|
if (events_.empty()) {
|
2014-07-06 21:02:00 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-04-29 05:12:33 +00:00
|
|
|
|
2013-08-10 16:08:31 +00:00
|
|
|
for (Event ev = GetNextEvent(); EventType(ev) != EVENT_INVALID; ev = GetNextEvent()) {
|
2017-02-27 20:57:46 +00:00
|
|
|
guard.unlock();
|
2015-01-11 00:10:47 +00:00
|
|
|
ProcessEventIfApplicable(ev, globalticks);
|
2017-02-27 20:57:46 +00:00
|
|
|
guard.lock();
|
2013-08-10 16:08:31 +00:00
|
|
|
}
|
|
|
|
} while (CoreTiming::GetTicks() < globalticks);
|
2013-10-13 19:13:11 +00:00
|
|
|
|
|
|
|
// This will force the waiter to check coreState, even if we didn't actually drain.
|
|
|
|
NotifyDrain();
|
|
|
|
eventsRunning_ = false;
|
2013-08-10 16:08:31 +00:00
|
|
|
}
|
|
|
|
|
2013-11-10 04:58:42 +00:00
|
|
|
void SyncBeginFrame() {
|
2015-01-10 23:45:57 +00:00
|
|
|
if (threadEnabled_) {
|
2017-02-27 20:57:46 +00:00
|
|
|
std::lock_guard<std::recursive_mutex> guard(eventsLock_);
|
2015-01-10 23:45:57 +00:00
|
|
|
eventsHaveRun_ = false;
|
|
|
|
} else {
|
|
|
|
eventsHaveRun_ = false;
|
|
|
|
}
|
2013-11-10 04:58:42 +00:00
|
|
|
}
|
|
|
|
|
2014-04-29 06:42:17 +00:00
|
|
|
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_) {
|
2020-07-04 18:19:56 +00:00
|
|
|
if (eventsHaveRun_ || coreState == CORE_BOOT_ERROR || coreState == CORE_RUNTIME_ERROR || coreState == CORE_POWERDOWN) {
|
2014-04-29 06:42:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-13 16:35:02 +00:00
|
|
|
// Force ignores coreState.
|
|
|
|
void SyncThread(bool force = false) {
|
2013-08-10 16:08:31 +00:00
|
|
|
if (!threadEnabled_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-27 20:57:46 +00:00
|
|
|
std::unique_lock<std::recursive_mutex> guard(eventsLock_);
|
2013-08-10 16:08:31 +00:00
|
|
|
// 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);
|
2014-04-29 06:42:17 +00:00
|
|
|
while (ShouldSyncThread(force)) {
|
2017-02-27 20:57:46 +00:00
|
|
|
eventsDrain_.wait(guard);
|
2013-08-10 16:08:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FinishEventLoop() {
|
2015-01-10 23:45:57 +00:00
|
|
|
if (!threadEnabled_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-27 20:57:46 +00:00
|
|
|
std::lock_guard<std::recursive_mutex> guard(eventsLock_);
|
2015-01-10 23:45:57 +00:00
|
|
|
// Don't schedule a finish if it's not even running.
|
|
|
|
if (eventsRunning_) {
|
|
|
|
ScheduleEvent(EVENT_FINISH);
|
2013-08-10 16:08:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void ProcessEvent(Event ev) = 0;
|
2013-08-11 18:40:41 +00:00
|
|
|
virtual bool ShouldExitEventLoop() = 0;
|
2013-08-10 16:08:31 +00:00
|
|
|
|
2015-01-11 00:10:47 +00:00
|
|
|
inline void ProcessEventIfApplicable(Event &ev, u64 &globalticks) {
|
|
|
|
switch (EventType(ev)) {
|
|
|
|
case EVENT_FINISH:
|
|
|
|
// Stop waiting.
|
|
|
|
globalticks = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EVENT_SYNC:
|
2015-07-24 21:26:35 +00:00
|
|
|
// Nothing special to do, this event it just to wait on, see SyncThread.
|
2015-01-11 00:10:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ProcessEvent(ev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-10 16:08:31 +00:00
|
|
|
private:
|
|
|
|
bool threadEnabled_;
|
2013-10-13 19:13:11 +00:00
|
|
|
bool eventsRunning_;
|
2013-11-10 04:58:42 +00:00
|
|
|
bool eventsHaveRun_;
|
2013-08-10 16:08:31 +00:00
|
|
|
std::deque<Event> events_;
|
2017-02-27 20:57:46 +00:00
|
|
|
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_;
|
2013-11-10 18:53:08 +00:00
|
|
|
};
|