CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/HW/AsyncIOManager.cpp
Views: 1401
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/System.h"
28
#include "Core/HW/AsyncIOManager.h"
29
#include "Core/FileSystems/MetaFileSystem.h"
30
31
bool AsyncIOManager::HasOperation(u32 handle) {
32
std::lock_guard<std::mutex> guard(resultsLock_);
33
if (resultsPending_.find(handle) != resultsPending_.end()) {
34
return true;
35
}
36
if (results_.find(handle) != results_.end()) {
37
return true;
38
}
39
return false;
40
}
41
42
void AsyncIOManager::ScheduleOperation(const AsyncIOEvent &ev) {
43
{
44
std::lock_guard<std::mutex> guard(resultsLock_);
45
if (!resultsPending_.insert(ev.handle).second) {
46
ERROR_LOG_REPORT(Log::sceIo, "Scheduling operation for file %d while one is pending (type %d)", ev.handle, ev.type);
47
}
48
}
49
ScheduleEvent(ev);
50
}
51
52
void AsyncIOManager::Shutdown() {
53
std::lock_guard<std::mutex> guard(resultsLock_);
54
resultsPending_.clear();
55
results_.clear();
56
}
57
58
bool AsyncIOManager::HasResult(u32 handle) {
59
std::lock_guard<std::mutex> guard(resultsLock_);
60
return results_.find(handle) != results_.end();
61
}
62
63
bool AsyncIOManager::PopResult(u32 handle, AsyncIOResult &result) {
64
// This is called under lock from WaitResult, no need to lock again.
65
if (results_.find(handle) != results_.end()) {
66
result = results_[handle];
67
results_.erase(handle);
68
resultsPending_.erase(handle);
69
70
if (result.invalidateAddr && result.result > 0) {
71
currentMIPS->InvalidateICache(result.invalidateAddr, (int)result.result);
72
}
73
return true;
74
} else {
75
return false;
76
}
77
}
78
79
bool AsyncIOManager::ReadResult(u32 handle, AsyncIOResult &result) {
80
// This is called under lock from WaitResult, no need to lock again.
81
if (results_.find(handle) != results_.end()) {
82
result = results_[handle];
83
return true;
84
} else {
85
return false;
86
}
87
}
88
89
bool AsyncIOManager::WaitResult(u32 handle, AsyncIOResult &result) {
90
std::unique_lock<std::mutex> guard(resultsLock_);
91
ScheduleEvent(IO_EVENT_SYNC);
92
while (HasEvents() && ThreadEnabled() && resultsPending_.find(handle) != resultsPending_.end()) {
93
if (PopResult(handle, result)) {
94
return true;
95
}
96
resultsWait_.wait_for(guard, std::chrono::milliseconds(16));
97
}
98
return PopResult(handle, result);
99
}
100
101
u64 AsyncIOManager::ResultFinishTicks(u32 handle) {
102
AsyncIOResult result;
103
104
std::unique_lock<std::mutex> guard(resultsLock_);
105
ScheduleEvent(IO_EVENT_SYNC);
106
while (HasEvents() && ThreadEnabled() && resultsPending_.find(handle) != resultsPending_.end()) {
107
if (ReadResult(handle, result)) {
108
return result.finishTicks;
109
}
110
resultsWait_.wait_for(guard, std::chrono::milliseconds(16));
111
}
112
if (ReadResult(handle, result)) {
113
return result.finishTicks;
114
}
115
116
return 0;
117
}
118
119
void AsyncIOManager::ProcessEvent(AsyncIOEvent ev) {
120
switch (ev.type) {
121
case IO_EVENT_READ:
122
Read(ev.handle, ev.buf, ev.bytes, ev.invalidateAddr);
123
break;
124
125
case IO_EVENT_WRITE:
126
Write(ev.handle, ev.buf, ev.bytes);
127
break;
128
129
default:
130
ERROR_LOG_REPORT(Log::sceIo, "Unsupported IO event type");
131
}
132
}
133
134
void AsyncIOManager::Read(u32 handle, u8 *buf, size_t bytes, u32 invalidateAddr) {
135
int usec = 0;
136
s64 result = pspFileSystem.ReadFile(handle, buf, bytes, usec);
137
EventResult(handle, AsyncIOResult(result, usec, invalidateAddr));
138
}
139
140
void AsyncIOManager::Write(u32 handle, const u8 *buf, size_t bytes) {
141
int usec = 0;
142
s64 result = pspFileSystem.WriteFile(handle, buf, bytes, usec);
143
EventResult(handle, AsyncIOResult(result, usec));
144
}
145
146
void AsyncIOManager::EventResult(u32 handle, const AsyncIOResult &result) {
147
std::lock_guard<std::mutex> guard(resultsLock_);
148
if (results_.find(handle) != results_.end()) {
149
ERROR_LOG_REPORT(Log::sceIo, "Overwriting previous result for file action on handle %d", handle);
150
}
151
results_[handle] = result;
152
resultsWait_.notify_one();
153
}
154
155
void AsyncIOManager::DoState(PointerWrap &p) {
156
auto s = p.Section("AsyncIoManager", 1, 2);
157
if (!s)
158
return;
159
160
SyncThread();
161
std::lock_guard<std::mutex> guard(resultsLock_);
162
Do(p, resultsPending_);
163
if (s >= 2) {
164
Do(p, results_);
165
} else {
166
std::map<u32, size_t> oldResults;
167
Do(p, oldResults);
168
for (auto it = oldResults.begin(), end = oldResults.end(); it != end; ++it) {
169
results_[it->first] = AsyncIOResult(it->second);
170
}
171
}
172
}
173
174