Redesign thread running API

This commit is contained in:
UnknownShadow200 2024-03-17 07:12:34 +11:00
parent 2e7c309e79
commit 83ba419a06
22 changed files with 89 additions and 131 deletions

View File

@ -1711,8 +1711,7 @@ static void Music_Start(void) {
music_joining = false; music_joining = false;
music_stopping = false; music_stopping = false;
music_thread = Thread_Create(Music_RunLoop); Thread_Run(&music_thread, Music_RunLoop, 256 * 1024, "Music");
Thread_Start2(music_thread, Music_RunLoop);
} }
static void Music_Stop(void) { static void Music_Stop(void) {

View File

@ -39,4 +39,14 @@ if (!head) { head = item; } else { tail->next = item; }\
tail = item;\ tail = item;\
item->next = NULL; item->next = NULL;
#define LinkedList_Remove(item, cur, head, tail)\
cur = head; \
if (head == item) head = item->next;\
\
while (cur) {\
if (cur->next == item) cur->next = item->next; \
\
tail = cur;\
cur = cur->next;\
}
#endif #endif

View File

@ -69,8 +69,8 @@ static void Gen_DoGen(void) {
} }
static void Gen_Run(void) { static void Gen_Run(void) {
void* thread = Thread_Create(Gen_DoGen); void* thread;
Thread_Start2(thread, Gen_DoGen); Thread_Run(&thread, Gen_DoGen, 128 * 1024, "Map gen");
Thread_Detach(thread); Thread_Detach(thread);
} }

View File

@ -1334,8 +1334,7 @@ static void Http_Init(void) {
pendingMutex = Mutex_Create(); pendingMutex = Mutex_Create();
processedMutex = Mutex_Create(); processedMutex = Mutex_Create();
curRequestMutex = Mutex_Create(); curRequestMutex = Mutex_Create();
workerThread = Thread_Create(WorkerLoop);
Thread_Run(&workerThread, WorkerLoop, 128 * 1024, "HTTP");
Thread_Start2(workerThread, WorkerLoop);
} }
#endif #endif

View File

@ -479,24 +479,13 @@ void Model_Register(struct Model* model) {
} }
void Model_Unregister(struct Model* model) { void Model_Unregister(struct Model* model) {
struct Model* cur;
int i; int i;
LinkedList_Remove(model, cur, models_head, models_tail);
/* remove the model from the list */
struct Model* item = models_head;
if (models_head == model) {
models_head = model->next;
}
while (item) {
if (item->next == model) {
item->next = model->next;
}
models_tail = item;
item = item->next;
}
/* unset this model from all entities, replacing with default fallback */ /* unset this model from all entities, replacing with default fallback */
for (i = 0; i < ENTITIES_MAX_COUNT; i++) { for (i = 0; i < ENTITIES_MAX_COUNT; i++)
{
struct Entity* e = Entities.List[i]; struct Entity* e = Entities.List[i];
if (e && e->Model == model) { if (e && e->Model == model) {
cc_string humanModelName = String_FromReadonly(Models.Human->name); cc_string humanModelName = String_FromReadonly(Models.Human->name);

View File

@ -208,12 +208,9 @@ cc_result File_Length(cc_file file, cc_uint32* len);
typedef void (*Thread_StartFunc)(void); typedef void (*Thread_StartFunc)(void);
/* Blocks the current thread for the given number of milliseconds. */ /* Blocks the current thread for the given number of milliseconds. */
CC_API void Thread_Sleep(cc_uint32 milliseconds); CC_API void Thread_Sleep(cc_uint32 milliseconds);
/* Initialises a new thread that will run the given function. */ /* Initialises and starts a new thread that runs the given function. */
/* Because of backend differences, func must also be provided in Thread_Start2 */
CC_API void* Thread_Create(Thread_StartFunc func);
/* Starts a new thread that runs the given function. */
/* NOTE: Threads must either be detached or joined, otherwise data leaks. */ /* NOTE: Threads must either be detached or joined, otherwise data leaks. */
CC_API void Thread_Start2(void* handle, Thread_StartFunc func); CC_API void Thread_Run(void** handle, Thread_StartFunc func, int stackSize, const char* name);
/* Frees the platform specific persistent data associated with the thread. */ /* Frees the platform specific persistent data associated with the thread. */
/* NOTE: Once a thread has been detached, Thread_Join can no longer be used. */ /* NOTE: Once a thread has been detached, Thread_Join can no longer be used. */
CC_API void Thread_Detach(void* handle); CC_API void Thread_Detach(void* handle);

View File

@ -223,13 +223,9 @@ static void Exec3DSThread(void* param) {
((Thread_StartFunc)param)(); ((Thread_StartFunc)param)();
} }
void* Thread_Create(Thread_StartFunc func) { void Thread_Run(void** handle, Thread_StartFunc func, int stackSize, const char* name) {
//TODO: Not quite correct, but eh //TODO: Not quite correct, but eh
return threadCreate(Exec3DSThread, (void*)func, 256 * 1024, 0x3f, -2, false); *handle = threadCreate(Exec3DSThread, (void*)func, stackSize, 0x3f, -2, false);
}
void Thread_Start2(void* handle, Thread_StartFunc func) {
} }
void Thread_Detach(void* handle) { void Thread_Detach(void* handle) {

View File

@ -252,8 +252,7 @@ static void JNICALL java_runGameAsync(JNIEnv* env, jobject instance) {
Platform_LogConst("Running game async!"); Platform_LogConst("Running game async!");
/* The game must be run on a separate thread, as blocking the */ /* The game must be run on a separate thread, as blocking the */
/* main UI thread will cause a 'App not responding..' messagebox */ /* main UI thread will cause a 'App not responding..' messagebox */
thread = Thread_Create(android_main); Thread_Run(&thread, android_main, 1024 * 1024, "Game"); // TODO check stack size needed
Thread_Start2(thread, android_main);
Thread_Detach(thread); Thread_Detach(thread);
} }
static const JNINativeMethod methods[] = { static const JNINativeMethod methods[] = {

View File

@ -256,14 +256,11 @@ static void* ExecThread(void* param) {
return NULL; return NULL;
} }
void* Thread_Create(Thread_StartFunc func) { void Thread_Run(void** handle, Thread_StartFunc func, int stackSize, const char* name) {
kthread_attr_t attrs = { 0 }; kthread_attr_t attrs = { 0 };
attrs.stack_size = 96 * 1024; attrs.stack_size = stackSize;
attrs.label = "CC thread"; attrs.label = name;
return thd_create_ex(&attrs, ExecThread, func); *handle = thd_create_ex(&attrs, ExecThread, func);
}
void Thread_Start2(void* handle, Thread_StartFunc func) {
} }
void Thread_Detach(void* handle) { void Thread_Detach(void* handle) {

View File

@ -234,13 +234,11 @@ static void* ExecThread(void* param) {
return NULL; return NULL;
} }
void* Thread_Create(Thread_StartFunc func) { void Thread_Run(void** handle, Thread_StartFunc func, int stackSize, const char* name) {
return Mem_Alloc(1, sizeof(lwp_t), "thread"); lwp_t* thread = (lwp_t*)Mem_Alloc(1, sizeof(lwp_t), "thread");
} *handle = thread;
void Thread_Start2(void* handle, Thread_StartFunc func) { int res = LWP_CreateThread(thread, ExecThread, (void*)func, NULL, stackSize, 80);
lwp_t* ptr = (lwp_t*)handle;
int res = LWP_CreateThread(ptr, ExecThread, (void*)func, NULL, 256 * 1024, 80);
if (res) Logger_Abort2(res, "Creating thread"); if (res) Logger_Abort2(res, "Creating thread");
} }

View File

@ -173,8 +173,8 @@ void Thread_Sleep(cc_uint32 milliseconds) {
wait_ms(milliseconds); wait_ms(milliseconds);
} }
void* Thread_Create(Thread_StartFunc func) { void Thread_Run(void** handle, Thread_StartFunc func, int stackSize, const char* name) {
return NULL; *handle = NULL;
} }
void Thread_Start2(void* handle, Thread_StartFunc func) { void Thread_Start2(void* handle, Thread_StartFunc func) {

View File

@ -155,8 +155,8 @@ void Thread_Sleep(cc_uint32 milliseconds) {
swiDelay(8378 * milliseconds); // TODO probably wrong swiDelay(8378 * milliseconds); // TODO probably wrong
} }
void* Thread_Create(Thread_StartFunc func) { void Thread_Run(void** handle, Thread_StartFunc func, int stackSize, const char* name) {
return NULL; *handle = NULL;
} }
void Thread_Start2(void* handle, Thread_StartFunc func) { void Thread_Start2(void* handle, Thread_StartFunc func) {

View File

@ -225,8 +225,6 @@ cc_result File_Length(cc_file file, cc_uint32* len) {
/*########################################################################################################################* /*########################################################################################################################*
*--------------------------------------------------------Threading--------------------------------------------------------* *--------------------------------------------------------Threading--------------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
#define STACK_SIZE (128 * 1024)
void Thread_Sleep(cc_uint32 milliseconds) { void Thread_Sleep(cc_uint32 milliseconds) {
DelayThread(milliseconds * 1000); DelayThread(milliseconds * 1000);
} }
@ -243,23 +241,19 @@ static int ExecThread(void* param) {
return 0; // TODO detach ? return 0; // TODO detach ?
} }
void* Thread_Create(Thread_StartFunc func) { void Thread_Run(void** handle, Thread_StartFunc func, int stackSize, const char* name) {
ee_thread_t thread = { 0 }; ee_thread_t thread = { 0 };
thread.func = ExecThread; thread.func = ExecThread;
thread.stack = Mem_Alloc(STACK_SIZE, 1, "Thread stack"); thread.stack = Mem_Alloc(stackSize, 1, "Thread stack");
thread.stack_size = STACK_SIZE; thread.stack_size = stackSize;
thread.gp_reg = &_gp; thread.gp_reg = &_gp;
thread.initial_priority = 18; thread.initial_priority = 18;
int thdID = CreateThread(&thread); int thdID = CreateThread(&thread);
if (thdID < 0) Logger_Abort2(thdID, "Creating thread"); if (thdID < 0) Logger_Abort2(thdID, "Creating thread");
return (void*)thdID; *handle = thdID;
}
void Thread_Start2(void* handle, Thread_StartFunc func) {
int thdID = (int)handle;
int res = StartThread(thdID, (void*)func);
int res = StartThread(thdID, (void*)func);
if (res < 0) Logger_Abort2(res, "Running thread"); if (res < 0) Logger_Abort2(res, "Running thread");
} }

View File

@ -236,16 +236,13 @@ void Thread_Sleep(cc_uint32 milliseconds) {
static void ExecThread(void* param) { static void ExecThread(void* param) {
((Thread_StartFunc)param)(); ((Thread_StartFunc)param)();
} }
#define STACK_SIZE (128 * 1024)
void* Thread_Create(Thread_StartFunc func) { void Thread_Run(void** handle, Thread_StartFunc func, int stackSize, const char* name) {
return Mem_Alloc(1, sizeof(sys_ppu_thread_t), "thread"); sys_ppu_thread_t* thread = (sys_ppu_thread_t*)Mem_Alloc(1, sizeof(sys_ppu_thread_t), "thread");
} *handle = thread;
void Thread_Start2(void* handle, Thread_StartFunc func) {
sys_ppu_thread_t* thread = (sys_ppu_thread_t*)handle;
int res = sysThreadCreate(thread, ExecThread, (void*)func, int res = sysThreadCreate(thread, ExecThread, (void*)func,
0, STACK_SIZE, THREAD_JOINABLE, "CC thread"); 0, stackSize, THREAD_JOINABLE, name);
if (res) Logger_Abort2(res, "Creating thread"); if (res) Logger_Abort2(res, "Creating thread");
} }

View File

@ -218,18 +218,15 @@ static int ExecThread(unsigned int argc, void *argv) {
return 0; return 0;
} }
void* Thread_Create(Thread_StartFunc func) { void Thread_Run(void** handle, Thread_StartFunc func, int stackSize, const char* name) {
#define CC_THREAD_PRIORITY 17 // TODO: 18? #define CC_THREAD_PRIORITY 17 // TODO: 18?
#define CC_THREAD_STACKSIZE 128 * 1024
#define CC_THREAD_ATTRS 0 // TODO PSP_THREAD_ATTR_VFPU? #define CC_THREAD_ATTRS 0 // TODO PSP_THREAD_ATTR_VFPU?
return (void*)sceKernelCreateThread("CC thread", ExecThread, CC_THREAD_PRIORITY, int threadID = sceKernelCreateThread(name, ExecThread, CC_THREAD_PRIORITY,
CC_THREAD_STACKSIZE, CC_THREAD_ATTRS, NULL); stackSize, CC_THREAD_ATTRS, NULL);
}
*handle = (int)threadID;
void Thread_Start2(void* handle, Thread_StartFunc func) { sceKernelStartThread(threadID, sizeof(func_), (void*)&func_);
Thread_StartFunc func_ = func;
sceKernelStartThread((int)handle, sizeof(func_), (void*)&func_);
} }
void Thread_Detach(void* handle) { void Thread_Detach(void* handle) {

View File

@ -202,13 +202,15 @@ static int ExecThread(unsigned int argc, void *argv) {
return 0; return 0;
} }
void* Thread_Create(Thread_StartFunc func) { void Thread_Run(void** handle, Thread_StartFunc func, int stackSize, const char* name) {
#define CC_THREAD_PRIORITY 0x10000100 #define CC_THREAD_PRIORITY 0x10000100
#define CC_THREAD_STACKSIZE 128 * 1024
#define CC_THREAD_ATTRS 0 // TODO PSP_THREAD_ATTR_VFPU? #define CC_THREAD_ATTRS 0 // TODO PSP_THREAD_ATTR_VFPU?
return (void*)sceKernelCreateThread("CC thread", ExecThread, CC_THREAD_PRIORITY, int threadID = sceKernelCreateThread(name, ExecThread, CC_THREAD_PRIORITY,
CC_THREAD_STACKSIZE, CC_THREAD_ATTRS, 0, NULL); stackSize, CC_THREAD_ATTRS, 0, NULL);
*handle = (int)threadID;
sceKernelStartThread(threadID, sizeof(func_), (void*)&func_);
} }
void Thread_Start2(void* handle, Thread_StartFunc func) { void Thread_Start2(void* handle, Thread_StartFunc func) {

View File

@ -332,13 +332,12 @@ static void* ExecThread(void* param) {
} }
#endif #endif
void* Thread_Create(Thread_StartFunc func) { void Thread_Run(void** handle, Thread_StartFunc func, int stackSize, const char* name) {
return Mem_Alloc(1, sizeof(pthread_t), "thread"); pthread_t* ptr = (pthread_t*)Mem_Alloc(1, sizeof(pthread_t), "thread");
} int res;
*handle = ptr;
void Thread_Start2(void* handle, Thread_StartFunc func) {
pthread_t* ptr = (pthread_t*)handle; res = pthread_create(ptr, NULL, ExecThread, (void*)func);
int res = pthread_create(ptr, NULL, ExecThread, (void*)func);
if (res) Logger_Abort2(res, "Creating thread"); if (res) Logger_Abort2(res, "Creating thread");
} }

View File

@ -229,23 +229,19 @@ static int ExecThread(int argc, const char **argv) {
return 0; return 0;
} }
#define STACK_SIZE 128 * 1024 void Thread_Run(void** handle, Thread_StartFunc func, int stackSize, const char* name) {
void* Thread_Create(Thread_StartFunc func) {
OSThread* thread = (OSThread*)Mem_Alloc(1, sizeof(OSThread), "thread"); OSThread* thread = (OSThread*)Mem_Alloc(1, sizeof(OSThread), "thread");
void* stack = memalign(16, STACK_SIZE); void* stack = memalign(16, stackSize);
OSCreateThread(thread, ExecThread, OSCreateThread(thread, ExecThread,
1, (Thread_StartFunc)func, 1, (Thread_StartFunc)func,
stack + STACK_SIZE, STACK_SIZE, stack + stackSize, stackSize,
16, OS_THREAD_ATTRIB_AFFINITY_ANY); 16, OS_THREAD_ATTRIB_AFFINITY_ANY);
*handle = thread;
// TODO revisit this // TODO revisit this
OSSetThreadRunQuantum(thread, 1000); // force yield after 1 millisecond OSSetThreadRunQuantum(thread, 1000); // force yield after 1 millisecond
return thread; OSResumeThread(thread);
}
void Thread_Start2(void* handle, Thread_StartFunc func) {
OSResumeThread((OSThread*)handle);
} }
void Thread_Detach(void* handle) { void Thread_Detach(void* handle) {

View File

@ -296,17 +296,13 @@ static DWORD WINAPI ExecThread(void* param) {
return 0; return 0;
} }
void* Thread_Create(Thread_StartFunc func) { void Thread_Run(void** handle, Thread_StartFunc func, int stackSize, const char* name) {
DWORD threadID; DWORD threadID;
void* handle = CreateThread(NULL, 0, ExecThread, (void*)func, CREATE_SUSPENDED, &threadID); HANDLE thread = CreateThread(NULL, 0, ExecThread, (void*)func, CREATE_SUSPENDED, &threadID);
if (!handle) { if (!thread) Logger_Abort2(GetLastError(), "Creating thread");
Logger_Abort2(GetLastError(), "Creating thread");
} *handle = thread;
return handle; ResumeThread(thread);
}
void Thread_Start2(void* handle, Thread_StartFunc func) {
ResumeThread((HANDLE)handle);
} }
void Thread_Detach(void* handle) { void Thread_Detach(void* handle) {

View File

@ -232,17 +232,13 @@ static DWORD WINAPI ExecThread(void* param) {
return 0; return 0;
} }
void* Thread_Create(Thread_StartFunc func) { void Thread_Run(void** handle, Thread_StartFunc func, int stackSize, const char* name) {
DWORD threadID; DWORD threadID;
void* handle = CreateThread(NULL, 0, ExecThread, (void*)func, CREATE_SUSPENDED, &threadID); HANDLE thread = CreateThread(NULL, 0, ExecThread, (void*)func, CREATE_SUSPENDED, &threadID);
if (!handle) { if (!thread) Logger_Abort2(GetLastError(), "Creating thread");
Logger_Abort2(GetLastError(), "Creating thread");
}
return handle;
}
void Thread_Start2(void* handle, Thread_StartFunc func) { *handle = thread;
NtResumeThread((HANDLE)handle, NULL); NtResumeThread(thread, NULL);
} }
void Thread_Detach(void* handle) { void Thread_Detach(void* handle) {

View File

@ -197,8 +197,8 @@ cc_result File_Length(cc_file file, cc_uint32* len) {
*#############################################################################################################p############*/ *#############################################################################################################p############*/
void Thread_Sleep(cc_uint32 milliseconds) { mdelay(milliseconds); } void Thread_Sleep(cc_uint32 milliseconds) { mdelay(milliseconds); }
void* Thread_Create(Thread_StartFunc func) { void Thread_Run(void** handle, Thread_StartFunc func, int stackSize, const char* name) {
return NULL; // TODO *handle = NULL; // TODO
} }
void Thread_Start2(void* handle, Thread_StartFunc func) {// TODO void Thread_Start2(void* handle, Thread_StartFunc func) {// TODO

View File

@ -65,13 +65,10 @@ static int32 ExecThread(void* param) {
return 0; return 0;
} }
void* Thread_Create(Thread_StartFunc func) { void Thread_Run(void** handle, Thread_StartFunc func, int stackSize, const char* name) {
thread_id thread = spawn_thread(ExecThread, "CC thread", B_NORMAL_PRIORITY, func); thread_id thread = spawn_thread(ExecThread, name, B_NORMAL_PRIORITY, func);
return (void*)thread; *handle = (void*)thread;
}
void Thread_Start2(void* handle, Thread_StartFunc func) {
thread_id thread = (thread_id)handle;
resume_thread(thread); resume_thread(thread);
} }
@ -366,8 +363,8 @@ static void AppThread(void) {
} }
static void RunApp(void) { static void RunApp(void) {
void* thread = Thread_Create(AppThread); void* thread;
Thread_Start2(thread, AppThread); Thread_Run(&thread, AppThread, 128 * 1024, "App thread");
Thread_Detach(thread); Thread_Detach(thread);
// wait for BApplication to be started in other thread // wait for BApplication to be started in other thread