CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Core/HW/AsyncIOManager.cpp
Views: 1401
// Copyright (c) 2012- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#include <condition_variable>18#include <mutex>1920#include "Common/Serialize/Serializer.h"21#include "Common/Serialize/SerializeFuncs.h"22#include "Common/Serialize/SerializeMap.h"23#include "Common/Serialize/SerializeSet.h"24#include "Core/MIPS/MIPS.h"25#include "Core/Reporting.h"26#include "Core/System.h"27#include "Core/HW/AsyncIOManager.h"28#include "Core/FileSystems/MetaFileSystem.h"2930bool AsyncIOManager::HasOperation(u32 handle) {31std::lock_guard<std::mutex> guard(resultsLock_);32if (resultsPending_.find(handle) != resultsPending_.end()) {33return true;34}35if (results_.find(handle) != results_.end()) {36return true;37}38return false;39}4041void AsyncIOManager::ScheduleOperation(const AsyncIOEvent &ev) {42{43std::lock_guard<std::mutex> guard(resultsLock_);44if (!resultsPending_.insert(ev.handle).second) {45ERROR_LOG_REPORT(Log::sceIo, "Scheduling operation for file %d while one is pending (type %d)", ev.handle, ev.type);46}47}48ScheduleEvent(ev);49}5051void AsyncIOManager::Shutdown() {52std::lock_guard<std::mutex> guard(resultsLock_);53resultsPending_.clear();54results_.clear();55}5657bool AsyncIOManager::HasResult(u32 handle) {58std::lock_guard<std::mutex> guard(resultsLock_);59return results_.find(handle) != results_.end();60}6162bool AsyncIOManager::PopResult(u32 handle, AsyncIOResult &result) {63// This is called under lock from WaitResult, no need to lock again.64if (results_.find(handle) != results_.end()) {65result = results_[handle];66results_.erase(handle);67resultsPending_.erase(handle);6869if (result.invalidateAddr && result.result > 0) {70currentMIPS->InvalidateICache(result.invalidateAddr, (int)result.result);71}72return true;73} else {74return false;75}76}7778bool AsyncIOManager::ReadResult(u32 handle, AsyncIOResult &result) {79// This is called under lock from WaitResult, no need to lock again.80if (results_.find(handle) != results_.end()) {81result = results_[handle];82return true;83} else {84return false;85}86}8788bool AsyncIOManager::WaitResult(u32 handle, AsyncIOResult &result) {89std::unique_lock<std::mutex> guard(resultsLock_);90ScheduleEvent(IO_EVENT_SYNC);91while (HasEvents() && ThreadEnabled() && resultsPending_.find(handle) != resultsPending_.end()) {92if (PopResult(handle, result)) {93return true;94}95resultsWait_.wait_for(guard, std::chrono::milliseconds(16));96}97return PopResult(handle, result);98}99100u64 AsyncIOManager::ResultFinishTicks(u32 handle) {101AsyncIOResult result;102103std::unique_lock<std::mutex> guard(resultsLock_);104ScheduleEvent(IO_EVENT_SYNC);105while (HasEvents() && ThreadEnabled() && resultsPending_.find(handle) != resultsPending_.end()) {106if (ReadResult(handle, result)) {107return result.finishTicks;108}109resultsWait_.wait_for(guard, std::chrono::milliseconds(16));110}111if (ReadResult(handle, result)) {112return result.finishTicks;113}114115return 0;116}117118void AsyncIOManager::ProcessEvent(AsyncIOEvent ev) {119switch (ev.type) {120case IO_EVENT_READ:121Read(ev.handle, ev.buf, ev.bytes, ev.invalidateAddr);122break;123124case IO_EVENT_WRITE:125Write(ev.handle, ev.buf, ev.bytes);126break;127128default:129ERROR_LOG_REPORT(Log::sceIo, "Unsupported IO event type");130}131}132133void AsyncIOManager::Read(u32 handle, u8 *buf, size_t bytes, u32 invalidateAddr) {134int usec = 0;135s64 result = pspFileSystem.ReadFile(handle, buf, bytes, usec);136EventResult(handle, AsyncIOResult(result, usec, invalidateAddr));137}138139void AsyncIOManager::Write(u32 handle, const u8 *buf, size_t bytes) {140int usec = 0;141s64 result = pspFileSystem.WriteFile(handle, buf, bytes, usec);142EventResult(handle, AsyncIOResult(result, usec));143}144145void AsyncIOManager::EventResult(u32 handle, const AsyncIOResult &result) {146std::lock_guard<std::mutex> guard(resultsLock_);147if (results_.find(handle) != results_.end()) {148ERROR_LOG_REPORT(Log::sceIo, "Overwriting previous result for file action on handle %d", handle);149}150results_[handle] = result;151resultsWait_.notify_one();152}153154void AsyncIOManager::DoState(PointerWrap &p) {155auto s = p.Section("AsyncIoManager", 1, 2);156if (!s)157return;158159SyncThread();160std::lock_guard<std::mutex> guard(resultsLock_);161Do(p, resultsPending_);162if (s >= 2) {163Do(p, results_);164} else {165std::map<u32, size_t> oldResults;166Do(p, oldResults);167for (auto it = oldResults.begin(), end = oldResults.end(); it != end; ++it) {168results_[it->first] = AsyncIOResult(it->second);169}170}171}172173174