forked from blastrock/pkgj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pkgi_thread.hpp
231 lines (204 loc) · 5.01 KB
/
pkgi_thread.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#pragma once
#include <functional>
#include <memory>
#include <stdexcept>
#include <string>
#include <psp2/kernel/threadmgr.h>
extern "C" {
#include "pkgi.h"
}
class ScopeProcessLock
{
public:
ScopeProcessLock(const ScopeProcessLock&) = delete;
ScopeProcessLock(ScopeProcessLock&&) = delete;
ScopeProcessLock& operator=(const ScopeProcessLock&) = delete;
ScopeProcessLock& operator=(ScopeProcessLock&&) = delete;
ScopeProcessLock()
{
pkgi_lock_process();
}
~ScopeProcessLock()
{
pkgi_unlock_process();
}
};
class Mutex
{
public:
Mutex(const Mutex&) = delete;
Mutex(Mutex&&) = delete;
Mutex& operator=(const Mutex&) = delete;
Mutex& operator=(Mutex&&) = delete;
Mutex(const std::string& name)
{
// I don't know what this 2 is
const auto res =
sceKernelCreateLwMutex(&_mutex, name.c_str(), 0, 0, nullptr);
if (res < 0)
{
// TODO throw
LOG("create mutex failed error=0x%08x", res);
}
}
~Mutex()
{
const auto res = sceKernelDeleteLwMutex(&_mutex);
if (res < 0)
{
// TODO assert
LOG("delete mutex failed error=0x%08x", res);
}
}
void lock()
{
const auto res = sceKernelLockLwMutex(&_mutex, 1, nullptr);
if (res < 0)
{
// TODO throw
LOG("lock failed error=0x%08x", res);
}
}
bool try_lock()
{
// I don't know how to handle errors
// const auto res = sceKernelTryLockLwMutex(&_mutex);
// if (res < 0)
//{
// // TODO throw
// LOG("lock failed error=0x%08x", res);
//}
throw std::runtime_error("try_lock not implemented");
}
void unlock()
{
const auto res = sceKernelUnlockLwMutex(&_mutex, 1);
if (res < 0)
{
// TODO throw
LOG("unlock failed error=0x%08x", res);
}
}
private:
SceKernelLwMutexWork _mutex;
friend class Cond;
};
class Cond
{
public:
Cond(const Cond&) = delete;
Cond(Cond&&) = delete;
Cond& operator=(const Cond&) = delete;
Cond& operator=(Cond&&) = delete;
Cond(const std::string& name) : _mutex(name + "_mutex")
{
const auto res = sceKernelCreateLwCond(
&_cond, name.c_str(), 0, &_mutex._mutex, nullptr);
if (res < 0)
{
// TODO throw
LOG("create cond failed error=0x%08x", res);
}
}
~Cond()
{
const auto res = sceKernelDeleteLwCond(&_cond);
if (res < 0)
{
// TODO assert
LOG("delete cond failed error=0x%08x", res);
}
}
void notify_one()
{
const auto res = sceKernelSignalLwCond(&_cond);
if (res < 0)
{
// TODO throw
LOG("cond signal failed error=0x%08x", res);
}
}
void wait()
{
const auto res = sceKernelWaitLwCond(&_cond, nullptr);
if (res < 0)
{
// TODO throw
LOG("wait cond failed error=0x%08x", res);
}
}
Mutex& get_mutex()
{
return _mutex;
}
private:
Mutex _mutex;
SceKernelLwCondWork _cond;
};
class Thread
{
public:
using EntryPoint = std::function<void()>;
Thread(const Thread&) = delete;
Thread(Thread&&) = delete;
Thread& operator=(const Thread&) = delete;
Thread& operator=(Thread&&) = delete;
Thread(const std::string& name, EntryPoint entry)
{
_tid = sceKernelCreateThread(
name.c_str(), &entry_point, 0xb0, 0x4000, 0, 0, nullptr);
if (_tid < 0)
{
// TODO throw
LOG("create thread failed error=0x%08x", _tid);
}
auto entryp = new EntryPoint(std::move(entry));
const auto res = sceKernelStartThread(_tid, sizeof(entryp), &entryp);
if (res < 0)
{
delete entryp;
// TODO throw
LOG("start thread failed error=0x%08x", res);
}
}
~Thread()
{
const auto res = sceKernelDeleteThread(_tid);
if (res < 0)
{
// TODO assert
LOG("thread delete failed error=0x%08x", res);
}
}
void join()
{
int stat;
const auto res = sceKernelWaitThreadEnd(_tid, &stat, nullptr);
if (res < 0)
{
// TODO assert
LOG("thread join failed error=0x%08x", res);
}
}
private:
SceUID _tid;
static int entry_point(SceSize, void* argp)
{
try
{
auto entryp = std::unique_ptr<EntryPoint>(
*static_cast<EntryPoint**>(argp));
(*entryp)();
LOG("thread successfully terminated");
}
catch (const std::exception& e)
{
LOG("got exception from thread: %s", e.what());
}
catch (...)
{
LOG("got unknown exception from thread");
}
return 0;
}
};