Skip to content

Commit 1197eb1

Browse files
committed
Run cupIoWork in the main thread
1 parent 53e612b commit 1197eb1

File tree

4 files changed

+39
-56
lines changed

4 files changed

+39
-56
lines changed

io.c

+16-41
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <string.h>
44
#include <pspkernel.h>
55
#include <psprtc.h>
6+
#include "main.h"
67
#include "io.h"
78

89
typedef struct _queue_t queue_t;
@@ -13,35 +14,33 @@ struct _queue_t {
1314
size_t size;
1415
};
1516

16-
static SceUID cupIoThid = 0;
17-
1817
static queue_t *queue = NULL;
1918

20-
static size_t left = 0;
19+
static int left = 0;
2120
static char buf[4096];
2221

23-
static int cupIoThread(SceSize args, void *argp)
22+
void cupIoWork()
2423
{
2524
SceUID log = -1;
2625
SceUID fd;
2726
const char *p;
2827
int ret;
2928

30-
ret = sceIoChdir(argp);
31-
if (ret)
32-
return ret;
33-
3429
while (1) {
3530
sceKernelSleepThread();
3631

37-
if (left) {
32+
while (left) {
3833
if (log < 0)
3934
log = sceIoOpen("LOG.TXT",
4035
PSP_O_WRONLY | PSP_O_APPEND | PSP_O_CREAT,
4136
0777);
4237
if (log >= 0) {
43-
sceIoWrite(log, buf, left);
44-
left = 0;
38+
ret = sceIoWrite(log, buf, left);
39+
if (ret > 0) {
40+
left -= ret;
41+
if (left < 0)
42+
left = 0;
43+
}
4544

4645
if (!sceIoClose(log))
4746
log = -1;
@@ -65,7 +64,7 @@ static int cupIoThread(SceSize args, void *argp)
6564
}
6665
}
6766

68-
int logTime()
67+
int cupPrintTime()
6968
{
7069
int ret;
7170
pspTime time;
@@ -95,7 +94,7 @@ int cupPuts(const char *s)
9594
if (left >= sizeof(buf))
9695
return SCE_KERNEL_ERROR_NO_MEMORY;
9796

98-
logTime();
97+
cupPrintTime();
9998

10099
p = buf + left;
101100
while (*s && left < sizeof(buf)) {
@@ -105,7 +104,7 @@ int cupPuts(const char *s)
105104
left++;
106105
}
107106

108-
sceKernelWakeupThread(cupIoThid);
107+
sceKernelWakeupThread(thid);
109108

110109
return 0;
111110
}
@@ -121,7 +120,7 @@ int cupPrintf(const char *fmt, ...)
121120
if (left >= sizeof(buf))
122121
return 0;
123122

124-
logTime();
123+
cupPrintTime();
125124

126125
va_start(va, fmt);
127126

@@ -131,35 +130,11 @@ int cupPrintf(const char *fmt, ...)
131130

132131
va_end(va);
133132

134-
sceKernelWakeupThread(cupIoThid);
135-
136-
return ret;
137-
}
138-
139-
int cupIoInit(SceSize len, char *path)
140-
{
141-
int ret;
142-
143-
ret = sceKernelCreateThread(
144-
"cupIoThread", cupIoThread, 1, 2048, 0, NULL);
145-
if (ret < 0)
146-
return cupIoThid;
147-
cupIoThid = ret;
148-
149-
ret = sceKernelStartThread(cupIoThid, len, path);
150-
if (ret)
151-
sceKernelDeleteThread(cupIoThid);
152-
153-
cupPrintf("capusbpsp started\n");
133+
sceKernelWakeupThread(thid);
154134

155135
return ret;
156136
}
157137

158-
int cupIoDeinit()
159-
{
160-
return cupIoThid ? sceKernelTerminateDeleteThread(cupIoThid) : 0;
161-
}
162-
163138
int cupIoWrite(const char *pre, const void *data, SceSize size)
164139
{
165140
pspTime time;
@@ -195,7 +170,7 @@ int cupIoWrite(const char *pre, const void *data, SceSize size)
195170

196171
memcpy(p + len + 1, data, size);
197172

198-
sceKernelWakeupThread(cupIoThid);
173+
sceKernelWakeupThread(thid);
199174

200175
return 0;
201176
}

io.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33

44
#include <psptypes.h>
55

6+
void cupIoWork();
7+
8+
int cupPrintTime();
69
int cupPuts(const char *s);
710
int cupPrintf(const char *fmt, ...);
811

9-
int cupIoInit(SceSize len, char *path);
10-
int cupIoDeinit();
1112
int cupIoWrite(const char *pre, const void *data, SceSize size);
13+
1214
#endif

main.c

+13-13
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static char modname[32];
4141
static const SceModule2 *module = NULL;
4242
static const stub_t *stub = NULL;
4343

44-
static SceUID mainThid = 0;
44+
SceUID thid = 0;
4545

4646
static int32_t jAsm(const void *p)
4747
{
@@ -150,16 +150,18 @@ int mainThread(SceSize args, void *argp)
150150
module = (SceModule2 *)sceKernelFindModuleByName(modname);
151151
} while (module == NULL);
152152

153+
sceKernelChangeThreadPriority(thid, 1);
154+
153155
stub = findStub(module, "sceUsbBus_driver");
154156

155157
if (stub == NULL)
156-
ret = SCE_KERNEL_ERROR_ERROR;
157-
else {
158-
ret = hook(stub, calls, CALL_NUM);
159-
cupIoInit(args, argp);
160-
}
158+
return SCE_KERNEL_ERROR_ERROR;
159+
160+
hook(stub, calls, CALL_NUM);
161+
cupIoWork();
161162

162163
exit:
164+
thid = 0;
163165
sceKernelExitDeleteThread(ret);
164166
return ret;
165167
}
@@ -172,21 +174,19 @@ int module_start(SceSize args, void *argp)
172174
module_info.modname, mainThread, 111, 2048, 0, NULL);
173175
if (ret < 0)
174176
return ret;
175-
mainThid = ret;
177+
thid = ret;
176178

177-
ret = sceKernelStartThread(mainThid, args, argp);
179+
ret = sceKernelStartThread(thid, args, argp);
178180
if (ret)
179-
sceKernelDeleteThread(mainThid);
181+
sceKernelDeleteThread(thid);
180182

181183
return ret;
182184
}
183185

184186
int module_stop()
185187
{
186-
if (mainThid)
187-
sceKernelTerminateDeleteThread(mainThid);
188-
189-
cupIoDeinit();
188+
if (thid)
189+
sceKernelTerminateDeleteThread(thid);
190190

191191
if (module == (SceModule2 *)sceKernelFindModuleByName(modname)
192192
&& stub == findStub(module, "sceUsbBus_driver"))

main.h

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifdef _MAIN_H_
2+
#define _MAIN_H_
3+
4+
extern SceUID thid;
5+
6+
#endif

0 commit comments

Comments
 (0)