forked from belek666/Open-PS2-Loader
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ioman.c
360 lines (270 loc) · 7.24 KB
/
ioman.c
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include "include/opl.h"
#include "include/ioman.h"
#include <kernel.h>
#include <string.h>
#include <malloc.h>
#include <stdio.h>
#include <unistd.h>
#ifdef __EESIO_DEBUG
#include <sio.h>
#endif
#define MAX_IO_REQUESTS 64
#define MAX_IO_HANDLERS 64
extern void *_gp;
static int gIOTerminate = 0;
#define THREAD_STACK_SIZE (96 * 1024)
static u8 thread_stack[THREAD_STACK_SIZE] ALIGNED(16);
struct io_request_t
{
int type;
void *data;
struct io_request_t *next;
};
struct io_handler_t
{
int type;
io_request_handler_t handler;
};
/// Circular request queue
static struct io_request_t *gReqList;
static struct io_request_t *gReqEnd;
static struct io_handler_t gRequestHandlers[MAX_IO_HANDLERS];
static int gHandlerCount;
// id of the processing thread
static s32 gIOThreadId;
// lock for tip processing
static s32 gProcSemaId;
// lock for queue end
static s32 gEndSemaId;
// ioPrintf sema id
static s32 gIOPrintfSemaId;
static ee_thread_t gIOThread;
static ee_sema_t gQueueSema;
static int isIOBlocked = 0;
static int isIORunning = 0;
int ioRegisterHandler(int type, io_request_handler_t handler)
{
WaitSema(gProcSemaId);
if (handler == NULL)
return IO_ERR_INVALID_HANDLER;
if (gHandlerCount >= MAX_IO_HANDLERS)
return IO_ERR_TOO_MANY_HANDLERS;
int i;
for (i = 0; i < gHandlerCount; ++i) {
if (gRequestHandlers[i].type == type)
return IO_ERR_DUPLICIT_HANDLER;
}
gRequestHandlers[gHandlerCount].type = type;
gRequestHandlers[gHandlerCount].handler = handler;
gHandlerCount++;
SignalSema(gProcSemaId);
return IO_OK;
}
static io_request_handler_t ioGetHandler(int type)
{
int i;
for (i = 0; i < gHandlerCount; ++i) {
struct io_handler_t *h = &gRequestHandlers[i];
if (h->type == type)
return h->handler;
}
return NULL;
}
static void ioProcessRequest(struct io_request_t *req)
{
if (!req)
return;
io_request_handler_t hlr = ioGetHandler(req->type);
// invalidate the request
void *data = req->data;
if (hlr)
hlr(data);
}
static void ioWorkerThread(void *arg)
{
while (!gIOTerminate) {
SleepThread();
// if term requested exit immediately from the loop
if (gIOTerminate)
break;
// do we have a request in the queue?
WaitSema(gProcSemaId);
while (gReqList) {
// if term requested exit immediately from the loop
if (gIOTerminate)
break;
struct io_request_t *req = gReqList;
ioProcessRequest(req);
// lock the queue tip as well now
WaitSema(gEndSemaId);
// can't be sure if the request was
gReqList = req->next;
free(req);
if (!gReqList)
gReqEnd = NULL;
SignalSema(gEndSemaId);
}
SignalSema(gProcSemaId);
}
// delete the pending requests
while (gReqList) {
struct io_request_t *req = gReqList;
gReqList = gReqList->next;
free(req);
}
// delete the semaphores
DeleteSema(gProcSemaId);
DeleteSema(gEndSemaId);
isIORunning = 0;
ExitDeleteThread();
}
static void ioSimpleActionHandler(void *data)
{
io_simpleaction_t action = (io_simpleaction_t)data;
if (action)
action();
}
void ioInit(void)
{
gIOTerminate = 0;
gHandlerCount = 0;
gReqList = NULL;
gReqEnd = NULL;
gIOThreadId = 0;
gQueueSema.init_count = 1;
gQueueSema.max_count = 1;
gQueueSema.option = 0;
gProcSemaId = CreateSema(&gQueueSema);
gEndSemaId = CreateSema(&gQueueSema);
gIOPrintfSemaId = CreateSema(&gQueueSema);
// default custom simple action handler
ioRegisterHandler(IO_CUSTOM_SIMPLEACTION, &ioSimpleActionHandler);
gIOThread.attr = 0;
gIOThread.stack_size = THREAD_STACK_SIZE;
gIOThread.gp_reg = &_gp;
gIOThread.func = &ioWorkerThread;
gIOThread.stack = thread_stack;
gIOThread.initial_priority = 30;
isIORunning = 1;
gIOThreadId = CreateThread(&gIOThread);
StartThread(gIOThreadId, NULL);
}
int ioPutRequest(int type, void *data)
{
if (isIOBlocked)
return IO_ERR_IO_BLOCKED;
// check the type before queueing
if (!ioGetHandler(type))
return IO_ERR_INVALID_HANDLER;
WaitSema(gEndSemaId);
// We don't have to lock the tip of the queue...
// If it exists, it won't be touched, if it does not exist, it is not being processed
struct io_request_t *req = gReqEnd;
if (!req) {
gReqList = (struct io_request_t *)malloc(sizeof(struct io_request_t));
req = gReqList;
gReqEnd = gReqList;
} else {
req->next = (struct io_request_t *)malloc(sizeof(struct io_request_t));
req = req->next;
gReqEnd = req;
}
req->next = NULL;
req->type = type;
req->data = data;
SignalSema(gEndSemaId);
// Worker thread cannot wake itself up (WakeupThread will return an error), but it will find the new request before sleeping.
WakeupThread(gIOThreadId);
return IO_OK;
}
int ioRemoveRequests(int type)
{
// lock the deletion sema and the queue end sema as well
WaitSema(gProcSemaId);
WaitSema(gEndSemaId);
int count = 0;
struct io_request_t *req = gReqList;
struct io_request_t *last = NULL;
while (req) {
if (req->type == type) {
struct io_request_t *next = req->next;
if (last)
last->next = next;
if (req == gReqList)
gReqList = next;
if (req == gReqEnd)
gReqEnd = last;
count++;
free(req);
req = next;
} else {
last = req;
req = req->next;
}
}
SignalSema(gEndSemaId);
SignalSema(gProcSemaId);
return count;
}
void ioEnd(void)
{
// termination requested flag
gIOTerminate = 1;
// wake up and wait for end
WakeupThread(gIOThreadId);
}
int ioGetPendingRequestCount(void)
{
int count = 0;
struct io_request_t *req = gReqList;
WaitSema(gProcSemaId);
while (req) {
count++;
req = req->next;
}
SignalSema(gProcSemaId);
return count;
}
int ioHasPendingRequests(void)
{
return gReqList != NULL ? 1 : 0;
}
#ifdef __EESIO_DEBUG
static char tbuf[2048];
#endif
int ioPrintf(const char *format, ...)
{
if (isIORunning == 1)
WaitSema(gIOPrintfSemaId);
va_list args;
va_start(args, format);
#ifdef __EESIO_DEBUG
int ret = vsnprintf((char *)tbuf, sizeof(tbuf), format, args);
sio_putsn(tbuf);
#else
int ret = vprintf(format, args);
#endif
va_end(args);
if (isIORunning == 1)
SignalSema(gIOPrintfSemaId);
return ret;
}
int ioBlockOps(int block)
{
ee_thread_status_t status;
int ThreadID;
if (block && !isIOBlocked) {
isIOBlocked = 1;
ThreadID = GetThreadId();
ReferThreadStatus(ThreadID, &status);
ChangeThreadPriority(ThreadID, 90);
// wait for all io to finish
while (ioHasPendingRequests()) {
};
ChangeThreadPriority(ThreadID, status.current_priority);
// now all io should be blocked
} else if (!block && isIOBlocked) {
isIOBlocked = 0;
}
return IO_OK;
}