2013-08-11 18:40:41 +00:00
|
|
|
// Copyright (c) 2012- 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/.
|
|
|
|
|
2017-02-27 20:57:46 +00:00
|
|
|
#include <condition_variable>
|
|
|
|
#include <mutex>
|
|
|
|
|
2020-08-10 07:12:51 +00:00
|
|
|
#include "Common/Serialize/Serializer.h"
|
|
|
|
#include "Common/Serialize/SerializeFuncs.h"
|
|
|
|
#include "Common/Serialize/SerializeMap.h"
|
|
|
|
#include "Common/Serialize/SerializeSet.h"
|
2015-12-28 05:58:29 +00:00
|
|
|
#include "Core/MIPS/MIPS.h"
|
2013-08-11 18:40:41 +00:00
|
|
|
#include "Core/Reporting.h"
|
|
|
|
#include "Core/System.h"
|
|
|
|
#include "Core/HW/AsyncIOManager.h"
|
|
|
|
#include "Core/FileSystems/MetaFileSystem.h"
|
|
|
|
|
2014-03-02 21:45:12 +00:00
|
|
|
bool AsyncIOManager::HasOperation(u32 handle) {
|
|
|
|
if (resultsPending_.find(handle) != resultsPending_.end()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (results_.find(handle) != results_.end()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-11 18:40:41 +00:00
|
|
|
void AsyncIOManager::ScheduleOperation(AsyncIOEvent ev) {
|
2014-04-29 05:32:09 +00:00
|
|
|
{
|
2017-02-27 20:57:46 +00:00
|
|
|
std::lock_guard<std::mutex> guard(resultsLock_);
|
2014-04-29 05:32:09 +00:00
|
|
|
if (!resultsPending_.insert(ev.handle).second) {
|
|
|
|
ERROR_LOG_REPORT(SCEIO, "Scheduling operation for file %d while one is pending (type %d)", ev.handle, ev.type);
|
|
|
|
}
|
2014-01-27 03:05:07 +00:00
|
|
|
}
|
2013-08-11 18:40:41 +00:00
|
|
|
ScheduleEvent(ev);
|
|
|
|
}
|
|
|
|
|
2014-01-27 03:10:20 +00:00
|
|
|
void AsyncIOManager::Shutdown() {
|
2017-02-27 20:57:46 +00:00
|
|
|
std::lock_guard<std::mutex> guard(resultsLock_);
|
2014-01-27 03:10:20 +00:00
|
|
|
resultsPending_.clear();
|
|
|
|
results_.clear();
|
|
|
|
}
|
|
|
|
|
2014-12-26 03:02:49 +00:00
|
|
|
bool AsyncIOManager::HasResult(u32 handle) {
|
2017-02-27 20:57:46 +00:00
|
|
|
std::lock_guard<std::mutex> guard(resultsLock_);
|
2014-12-26 03:02:49 +00:00
|
|
|
return results_.find(handle) != results_.end();
|
|
|
|
}
|
|
|
|
|
2013-08-11 18:40:41 +00:00
|
|
|
bool AsyncIOManager::PopResult(u32 handle, AsyncIOResult &result) {
|
|
|
|
if (results_.find(handle) != results_.end()) {
|
|
|
|
result = results_[handle];
|
|
|
|
results_.erase(handle);
|
|
|
|
resultsPending_.erase(handle);
|
2015-12-28 05:58:29 +00:00
|
|
|
|
|
|
|
if (result.invalidateAddr && result.result > 0) {
|
|
|
|
currentMIPS->InvalidateICache(result.invalidateAddr, (int)result.result);
|
|
|
|
}
|
2013-08-11 18:40:41 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-28 00:36:51 +00:00
|
|
|
bool AsyncIOManager::ReadResult(u32 handle, AsyncIOResult &result) {
|
|
|
|
if (results_.find(handle) != results_.end()) {
|
|
|
|
result = results_[handle];
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-11 18:40:41 +00:00
|
|
|
bool AsyncIOManager::WaitResult(u32 handle, AsyncIOResult &result) {
|
2017-02-27 20:57:46 +00:00
|
|
|
std::unique_lock<std::mutex> guard(resultsLock_);
|
2013-08-11 18:40:41 +00:00
|
|
|
ScheduleEvent(IO_EVENT_SYNC);
|
2013-11-10 04:56:11 +00:00
|
|
|
while (HasEvents() && ThreadEnabled() && resultsPending_.find(handle) != resultsPending_.end()) {
|
2013-08-11 18:40:41 +00:00
|
|
|
if (PopResult(handle, result)) {
|
|
|
|
return true;
|
|
|
|
}
|
2017-02-27 20:57:46 +00:00
|
|
|
resultsWait_.wait_for(guard, std::chrono::milliseconds(16));
|
2013-08-11 18:40:41 +00:00
|
|
|
}
|
2017-02-27 20:57:46 +00:00
|
|
|
return PopResult(handle, result);
|
2013-08-11 18:40:41 +00:00
|
|
|
}
|
|
|
|
|
2013-12-28 00:36:51 +00:00
|
|
|
u64 AsyncIOManager::ResultFinishTicks(u32 handle) {
|
|
|
|
AsyncIOResult result;
|
|
|
|
|
2017-02-27 20:57:46 +00:00
|
|
|
std::unique_lock<std::mutex> guard(resultsLock_);
|
2013-12-28 00:36:51 +00:00
|
|
|
ScheduleEvent(IO_EVENT_SYNC);
|
|
|
|
while (HasEvents() && ThreadEnabled() && resultsPending_.find(handle) != resultsPending_.end()) {
|
|
|
|
if (ReadResult(handle, result)) {
|
|
|
|
return result.finishTicks;
|
|
|
|
}
|
2017-02-27 20:57:46 +00:00
|
|
|
resultsWait_.wait_for(guard, std::chrono::milliseconds(16));
|
2013-12-28 00:36:51 +00:00
|
|
|
}
|
|
|
|
if (ReadResult(handle, result)) {
|
|
|
|
return result.finishTicks;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-11 18:40:41 +00:00
|
|
|
void AsyncIOManager::ProcessEvent(AsyncIOEvent ev) {
|
|
|
|
switch (ev.type) {
|
|
|
|
case IO_EVENT_READ:
|
2015-12-28 05:58:29 +00:00
|
|
|
Read(ev.handle, ev.buf, ev.bytes, ev.invalidateAddr);
|
2013-08-11 18:40:41 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IO_EVENT_WRITE:
|
|
|
|
Write(ev.handle, ev.buf, ev.bytes);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2014-01-27 03:05:07 +00:00
|
|
|
ERROR_LOG_REPORT(SCEIO, "Unsupported IO event type");
|
2013-08-11 18:40:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-28 05:58:29 +00:00
|
|
|
void AsyncIOManager::Read(u32 handle, u8 *buf, size_t bytes, u32 invalidateAddr) {
|
2013-12-28 00:36:51 +00:00
|
|
|
int usec = 0;
|
|
|
|
s64 result = pspFileSystem.ReadFile(handle, buf, bytes, usec);
|
2015-12-28 05:58:29 +00:00
|
|
|
EventResult(handle, AsyncIOResult(result, usec, invalidateAddr));
|
2013-08-11 18:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncIOManager::Write(u32 handle, u8 *buf, size_t bytes) {
|
2013-12-28 00:36:51 +00:00
|
|
|
int usec = 0;
|
|
|
|
s64 result = pspFileSystem.WriteFile(handle, buf, bytes, usec);
|
|
|
|
EventResult(handle, AsyncIOResult(result, usec));
|
2013-08-11 18:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncIOManager::EventResult(u32 handle, AsyncIOResult result) {
|
2017-02-27 20:57:46 +00:00
|
|
|
std::lock_guard<std::mutex> guard(resultsLock_);
|
2013-08-11 18:40:41 +00:00
|
|
|
if (results_.find(handle) != results_.end()) {
|
2013-09-07 20:02:55 +00:00
|
|
|
ERROR_LOG_REPORT(SCEIO, "Overwriting previous result for file action on handle %d", handle);
|
2013-08-11 18:40:41 +00:00
|
|
|
}
|
|
|
|
results_[handle] = result;
|
|
|
|
resultsWait_.notify_one();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncIOManager::DoState(PointerWrap &p) {
|
2013-12-28 00:36:51 +00:00
|
|
|
auto s = p.Section("AsyncIoManager", 1, 2);
|
2013-09-15 03:23:03 +00:00
|
|
|
if (!s)
|
|
|
|
return;
|
|
|
|
|
2013-08-11 18:40:41 +00:00
|
|
|
SyncThread();
|
2017-02-27 20:57:46 +00:00
|
|
|
std::lock_guard<std::mutex> guard(resultsLock_);
|
2020-08-10 04:20:42 +00:00
|
|
|
Do(p, resultsPending_);
|
2013-12-28 00:36:51 +00:00
|
|
|
if (s >= 2) {
|
2020-08-10 04:20:42 +00:00
|
|
|
Do(p, results_);
|
2013-12-28 00:36:51 +00:00
|
|
|
} else {
|
|
|
|
std::map<u32, size_t> oldResults;
|
2020-08-10 04:20:42 +00:00
|
|
|
Do(p, oldResults);
|
2013-12-28 00:36:51 +00:00
|
|
|
for (auto it = oldResults.begin(), end = oldResults.end(); it != end; ++it) {
|
|
|
|
results_[it->first] = AsyncIOResult(it->second);
|
|
|
|
}
|
|
|
|
}
|
2013-08-11 18:40:41 +00:00
|
|
|
}
|