Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/HW/AsyncIOManager.cpp
3186 views
1
// Copyright (c) 2012- PPSSPP Project.
2
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.
6
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.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#include <condition_variable>
19
#include <mutex>
20
21
#include "Common/Serialize/Serializer.h"
22
#include "Common/Serialize/SerializeFuncs.h"
23
#include "Common/Serialize/SerializeMap.h"
24
#include "Common/Serialize/SerializeSet.h"
25
#include "Core/MIPS/MIPS.h"
26
#include "Core/Reporting.h"
27
#include "Core/HW/AsyncIOManager.h"
28
#include "Core/FileSystems/MetaFileSystem.h"
29
30
bool AsyncIOManager::HasOperation(u32 handle) {
31
std::lock_guard<std::mutex> guard(resultsLock_);
32
if (resultsPending_.find(handle) != resultsPending_.end()) {
33
return true;
34
}
35
if (results_.find(handle) != results_.end()) {
36
return true;
37
}
38
return false;
39
}
40
41
void AsyncIOManager::ScheduleOperation(const AsyncIOEvent &ev) {
42
{
43
std::lock_guard<std::mutex> guard(resultsLock_);
44
if (!resultsPending_.insert(ev.handle).second) {
45
ERROR_LOG_REPORT(Log::sceIo, "Scheduling operation for file %d while one is pending (type %d)", ev.handle, ev.type);
46
}
47
}
48
ScheduleEvent(ev);
49
}
50
51
void AsyncIOManager::Shutdown() {
52
std::lock_guard<std::mutex> guard(resultsLock_);
53
resultsPending_.clear();
54
results_.clear();
55
}
56
57
bool AsyncIOManager::HasResult(u32 handle) {
58
std::lock_guard<std::mutex> guard(resultsLock_);
59
return results_.find(handle) != results_.end();
60
}
61
62
bool AsyncIOManager::PopResult(u32 handle, AsyncIOResult &result) {
63
// This is called under lock from WaitResult, no need to lock again.
64
if (results_.find(handle) != results_.end()) {
65
result = results_[handle];
66
results_.erase(handle);
67
resultsPending_.erase(handle);
68
69
if (result.invalidateAddr && result.result > 0) {
70
currentMIPS->InvalidateICache(result.invalidateAddr, (int)result.result);
71
}
72
return true;
73
} else {
74
return false;
75
}
76
}
77
78
bool AsyncIOManager::ReadResult(u32 handle, AsyncIOResult &result) {
79
// This is called under lock from WaitResult, no need to lock again.
80
if (results_.find(handle) != results_.end()) {
81
result = results_[handle];
82
return true;
83
} else {
84
return false;
85
}
86
}
87
88
bool AsyncIOManager::WaitResult(u32 handle, AsyncIOResult &result) {
89
std::unique_lock<std::mutex> guard(resultsLock_);
90
ScheduleEvent(IO_EVENT_SYNC);
91
while (HasEvents() && ThreadEnabled() && resultsPending_.find(handle) != resultsPending_.end()) {
92
if (PopResult(handle, result)) {
93
return true;
94
}
95
resultsWait_.wait_for(guard, std::chrono::milliseconds(16));
96
}
97
return PopResult(handle, result);
98
}
99
100
u64 AsyncIOManager::ResultFinishTicks(u32 handle) {
101
AsyncIOResult result;
102
103
std::unique_lock<std::mutex> guard(resultsLock_);
104
ScheduleEvent(IO_EVENT_SYNC);
105
while (HasEvents() && ThreadEnabled() && resultsPending_.find(handle) != resultsPending_.end()) {
106
if (ReadResult(handle, result)) {
107
return result.finishTicks;
108
}
109
resultsWait_.wait_for(guard, std::chrono::milliseconds(16));
110
}
111
if (ReadResult(handle, result)) {
112
return result.finishTicks;
113
}
114
115
return 0;
116
}
117
118
void AsyncIOManager::ProcessEvent(AsyncIOEvent ev) {
119
switch (ev.type) {
120
case IO_EVENT_READ:
121
Read(ev.handle, ev.buf, ev.bytes, ev.invalidateAddr);
122
break;
123
124
case IO_EVENT_WRITE:
125
Write(ev.handle, ev.buf, ev.bytes);
126
break;
127
128
default:
129
ERROR_LOG_REPORT(Log::sceIo, "Unsupported IO event type");
130
}
131
}
132
133
void AsyncIOManager::Read(u32 handle, u8 *buf, size_t bytes, u32 invalidateAddr) {
134
int usec = 0;
135
s64 result = pspFileSystem.ReadFile(handle, buf, bytes, usec);
136
EventResult(handle, AsyncIOResult(result, usec, invalidateAddr));
137
}
138
139
void AsyncIOManager::Write(u32 handle, const u8 *buf, size_t bytes) {
140
int usec = 0;
141
s64 result = pspFileSystem.WriteFile(handle, buf, bytes, usec);
142
EventResult(handle, AsyncIOResult(result, usec));
143
}
144
145
void AsyncIOManager::EventResult(u32 handle, const AsyncIOResult &result) {
146
std::lock_guard<std::mutex> guard(resultsLock_);
147
if (results_.find(handle) != results_.end()) {
148
ERROR_LOG_REPORT(Log::sceIo, "Overwriting previous result for file action on handle %d", handle);
149
}
150
results_[handle] = result;
151
resultsWait_.notify_one();
152
}
153
154
void AsyncIOManager::DoState(PointerWrap &p) {
155
auto s = p.Section("AsyncIoManager", 1, 2);
156
if (!s)
157
return;
158
159
SyncThread();
160
std::lock_guard<std::mutex> guard(resultsLock_);
161
Do(p, resultsPending_);
162
if (s >= 2) {
163
Do(p, results_);
164
} else {
165
std::map<u32, size_t> oldResults;
166
Do(p, oldResults);
167
for (auto it = oldResults.begin(), end = oldResults.end(); it != end; ++it) {
168
results_[it->first] = AsyncIOResult(it->second);
169
}
170
}
171
}
172
173