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
|
|
|
|
|
|
|
|
#include "native/base/mutex.h"
|
2013-08-11 22:51:13 +00:00
|
|
|
#include "Core/System.h"
|
2013-08-10 16:08:31 +00:00
|
|
|
#include "Core/CoreTiming.h"
|
|
|
|
#include <deque>
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
{
|
|
|
|
lock_guard guard(eventsLock_);
|
|
|
|
events_.push_back(ev);
|
|
|
|
eventsWait_.notify_one();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!threadEnabled_) {
|
|
|
|
RunEventsUntil(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasEvents() {
|
|
|
|
lock_guard guard(eventsLock_);
|
|
|
|
return !events_.empty();
|
|
|
|
}
|
|
|
|
|
2013-10-13 19:13:11 +00:00
|
|
|
void NotifyDrain() {
|
|
|
|
lock_guard guard(eventsLock_);
|
|
|
|
eventsDrain_.notify_one();
|
|
|
|
}
|
|
|
|
|
2013-08-10 16:08:31 +00:00
|
|
|
Event GetNextEvent() {
|
|
|
|
lock_guard guard(eventsLock_);
|
|
|
|
if (events_.empty()) {
|
2013-10-13 19:13:11 +00:00
|
|
|
NotifyDrain();
|
2013-08-10 16:08:31 +00:00
|
|
|
return EVENT_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
Event ev = events_.front();
|
|
|
|
events_.pop_front();
|
|
|
|
return ev;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RunEventsUntil(u64 globalticks) {
|
2013-09-29 03:56:38 +00:00
|
|
|
lock_guard guard(eventsLock_);
|
2013-10-13 19:13:11 +00:00
|
|
|
eventsRunning_ = true;
|
2013-11-10 04:58:42 +00:00
|
|
|
eventsHaveRun_ = true;
|
2013-10-13 19:13:11 +00:00
|
|
|
|
2013-08-10 16:08:31 +00:00
|
|
|
do {
|
|
|
|
for (Event ev = GetNextEvent(); EventType(ev) != EVENT_INVALID; ev = GetNextEvent()) {
|
2013-09-29 03:56:38 +00:00
|
|
|
eventsLock_.unlock();
|
2013-08-10 16:08:31 +00:00
|
|
|
switch (EventType(ev)) {
|
|
|
|
case EVENT_FINISH:
|
|
|
|
// Stop waiting.
|
|
|
|
globalticks = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EVENT_SYNC:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ProcessEvent(ev);
|
|
|
|
}
|
2013-09-29 03:56:38 +00:00
|
|
|
eventsLock_.lock();
|
2013-08-10 16:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Quit the loop if the queue is drained and coreState has tripped, or threading is disabled.
|
2013-08-11 18:40:41 +00:00
|
|
|
if (ShouldExitEventLoop() || !threadEnabled_) {
|
2013-10-13 19:13:11 +00:00
|
|
|
break;
|
2013-08-10 16:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// coreState changes won't wake us, so recheck periodically.
|
2013-09-29 03:56:38 +00:00
|
|
|
if (!HasEvents()) {
|
|
|
|
eventsWait_.wait(eventsLock_);
|
|
|
|
}
|
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() {
|
|
|
|
lock_guard guard(eventsLock_);
|
|
|
|
eventsHaveRun_ = false;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-09-29 03:56:38 +00:00
|
|
|
lock_guard 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);
|
2013-11-10 04:58:42 +00:00
|
|
|
while (HasEvents() && (eventsRunning_ || !eventsHaveRun_) && (force || coreState == CORE_RUNNING)) {
|
|
|
|
eventsDrain_.wait_for(eventsLock_, 1000);
|
2013-08-10 16:08:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FinishEventLoop() {
|
|
|
|
if (threadEnabled_) {
|
2013-11-10 05:11:50 +00:00
|
|
|
lock_guard guard(eventsLock_);
|
|
|
|
// 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
|
|
|
|
|
|
|
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_;
|
|
|
|
recursive_mutex eventsLock_;
|
|
|
|
condition_variable eventsWait_;
|
|
|
|
condition_variable eventsDrain_;
|
|
|
|
};
|