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.h
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
#pragma once
19
20
#include <map>
21
#include <set>
22
#include <mutex>
23
24
#include "Core/ThreadEventQueue.h"
25
26
class NoBase {
27
};
28
29
enum AsyncIOEventType {
30
IO_EVENT_INVALID,
31
IO_EVENT_SYNC,
32
IO_EVENT_FINISH,
33
IO_EVENT_READ,
34
IO_EVENT_WRITE,
35
};
36
37
struct AsyncIOEvent {
38
AsyncIOEvent(AsyncIOEventType t) : type(t) {}
39
AsyncIOEventType type;
40
u32 handle;
41
u8 *buf;
42
size_t bytes;
43
u32 invalidateAddr;
44
45
operator AsyncIOEventType() const {
46
return type;
47
}
48
};
49
50
struct AsyncIOResult {
51
AsyncIOResult() : result(0), finishTicks(0), invalidateAddr(0) {
52
}
53
54
explicit AsyncIOResult(s64 r) : result(r), finishTicks(0), invalidateAddr(0) {
55
}
56
57
AsyncIOResult(s64 r, int usec, u32 addr = 0) : result(r), invalidateAddr(addr) {
58
finishTicks = CoreTiming::GetTicks() + usToCycles(usec);
59
}
60
61
void DoState(PointerWrap &p) {
62
auto s = p.Section("AsyncIOResult", 1, 2);
63
if (!s)
64
return;
65
66
Do(p, result);
67
Do(p, finishTicks);
68
if (s >= 2) {
69
Do(p, invalidateAddr);
70
} else {
71
invalidateAddr = 0;
72
}
73
}
74
75
s64 result;
76
u64 finishTicks;
77
u32 invalidateAddr;
78
};
79
80
typedef ThreadEventQueue<NoBase, AsyncIOEvent, AsyncIOEventType, IO_EVENT_INVALID, IO_EVENT_SYNC, IO_EVENT_FINISH> IOThreadEventQueue;
81
class AsyncIOManager : public IOThreadEventQueue {
82
public:
83
void DoState(PointerWrap &p);
84
85
bool HasOperation(u32 handle);
86
void ScheduleOperation(const AsyncIOEvent &ev);
87
void Shutdown();
88
89
bool HasResult(u32 handle);
90
bool WaitResult(u32 handle, AsyncIOResult &result);
91
u64 ResultFinishTicks(u32 handle);
92
93
protected:
94
void ProcessEvent(AsyncIOEvent ref) override;
95
bool ShouldExitEventLoop() override {
96
return coreState == CORE_BOOT_ERROR || coreState == CORE_RUNTIME_ERROR || coreState == CORE_POWERDOWN;
97
}
98
99
private:
100
bool PopResult(u32 handle, AsyncIOResult &result);
101
bool ReadResult(u32 handle, AsyncIOResult &result);
102
void Read(u32 handle, u8 *buf, size_t bytes, u32 invalidateAddr);
103
void Write(u32 handle, const u8 *buf, size_t bytes);
104
105
void EventResult(u32 handle, const AsyncIOResult &result);
106
107
std::mutex resultsLock_;
108
std::condition_variable resultsWait_;
109
std::set<u32> resultsPending_;
110
std::map<u32, AsyncIOResult> results_;
111
};
112
113