mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-12 00:56:40 -04:00
Redesign thread running API
This commit is contained in:
parent
2e7c309e79
commit
83ba419a06
@ -1711,8 +1711,7 @@ static void Music_Start(void) {
|
||||
music_joining = false;
|
||||
music_stopping = false;
|
||||
|
||||
music_thread = Thread_Create(Music_RunLoop);
|
||||
Thread_Start2(music_thread, Music_RunLoop);
|
||||
Thread_Run(&music_thread, Music_RunLoop, 256 * 1024, "Music");
|
||||
}
|
||||
|
||||
static void Music_Stop(void) {
|
||||
|
10
src/Funcs.h
10
src/Funcs.h
@ -39,4 +39,14 @@ if (!head) { head = item; } else { tail->next = item; }\
|
||||
tail = item;\
|
||||
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
|
||||
|
@ -69,8 +69,8 @@ static void Gen_DoGen(void) {
|
||||
}
|
||||
|
||||
static void Gen_Run(void) {
|
||||
void* thread = Thread_Create(Gen_DoGen);
|
||||
Thread_Start2(thread, Gen_DoGen);
|
||||
void* thread;
|
||||
Thread_Run(&thread, Gen_DoGen, 128 * 1024, "Map gen");
|
||||
Thread_Detach(thread);
|
||||
}
|
||||
|
||||
|
@ -1334,8 +1334,7 @@ static void Http_Init(void) {
|
||||
pendingMutex = Mutex_Create();
|
||||
processedMutex = Mutex_Create();
|
||||
curRequestMutex = Mutex_Create();
|
||||
workerThread = Thread_Create(WorkerLoop);
|
||||
|
||||
Thread_Start2(workerThread, WorkerLoop);
|
||||
Thread_Run(&workerThread, WorkerLoop, 128 * 1024, "HTTP");
|
||||
}
|
||||
#endif
|
||||
|
19
src/Model.c
19
src/Model.c
@ -479,24 +479,13 @@ void Model_Register(struct Model* model) {
|
||||
}
|
||||
|
||||
void Model_Unregister(struct Model* model) {
|
||||
struct Model* cur;
|
||||
int i;
|
||||
|
||||
/* 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;
|
||||
}
|
||||
LinkedList_Remove(model, cur, models_head, models_tail);
|
||||
|
||||
/* 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];
|
||||
if (e && e->Model == model) {
|
||||
cc_string humanModelName = String_FromReadonly(Models.Human->name);
|
||||
|
@ -208,12 +208,9 @@ cc_result File_Length(cc_file file, cc_uint32* len);
|
||||
typedef void (*Thread_StartFunc)(void);
|
||||
/* Blocks the current thread for the given number of milliseconds. */
|
||||
CC_API void Thread_Sleep(cc_uint32 milliseconds);
|
||||
/* Initialises a new thread that will run 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. */
|
||||
/* Initialises and starts a new thread that runs the given function. */
|
||||
/* 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. */
|
||||
/* NOTE: Once a thread has been detached, Thread_Join can no longer be used. */
|
||||
CC_API void Thread_Detach(void* handle);
|
||||
|
@ -223,13 +223,9 @@ static void Exec3DSThread(void* 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
|
||||
return threadCreate(Exec3DSThread, (void*)func, 256 * 1024, 0x3f, -2, false);
|
||||
}
|
||||
|
||||
void Thread_Start2(void* handle, Thread_StartFunc func) {
|
||||
|
||||
*handle = threadCreate(Exec3DSThread, (void*)func, stackSize, 0x3f, -2, false);
|
||||
}
|
||||
|
||||
void Thread_Detach(void* handle) {
|
||||
|
@ -252,8 +252,7 @@ static void JNICALL java_runGameAsync(JNIEnv* env, jobject instance) {
|
||||
Platform_LogConst("Running game async!");
|
||||
/* The game must be run on a separate thread, as blocking the */
|
||||
/* main UI thread will cause a 'App not responding..' messagebox */
|
||||
thread = Thread_Create(android_main);
|
||||
Thread_Start2(thread, android_main);
|
||||
Thread_Run(&thread, android_main, 1024 * 1024, "Game"); // TODO check stack size needed
|
||||
Thread_Detach(thread);
|
||||
}
|
||||
static const JNINativeMethod methods[] = {
|
||||
|
@ -256,14 +256,11 @@ static void* ExecThread(void* param) {
|
||||
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 };
|
||||
attrs.stack_size = 96 * 1024;
|
||||
attrs.label = "CC thread";
|
||||
return thd_create_ex(&attrs, ExecThread, func);
|
||||
}
|
||||
|
||||
void Thread_Start2(void* handle, Thread_StartFunc func) {
|
||||
attrs.stack_size = stackSize;
|
||||
attrs.label = name;
|
||||
*handle = thd_create_ex(&attrs, ExecThread, func);
|
||||
}
|
||||
|
||||
void Thread_Detach(void* handle) {
|
||||
|
@ -234,13 +234,11 @@ static void* ExecThread(void* param) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void* Thread_Create(Thread_StartFunc func) {
|
||||
return Mem_Alloc(1, sizeof(lwp_t), "thread");
|
||||
}
|
||||
void Thread_Run(void** handle, Thread_StartFunc func, int stackSize, const char* name) {
|
||||
lwp_t* thread = (lwp_t*)Mem_Alloc(1, sizeof(lwp_t), "thread");
|
||||
*handle = thread;
|
||||
|
||||
void Thread_Start2(void* handle, Thread_StartFunc func) {
|
||||
lwp_t* ptr = (lwp_t*)handle;
|
||||
int res = LWP_CreateThread(ptr, ExecThread, (void*)func, NULL, 256 * 1024, 80);
|
||||
int res = LWP_CreateThread(thread, ExecThread, (void*)func, NULL, stackSize, 80);
|
||||
if (res) Logger_Abort2(res, "Creating thread");
|
||||
}
|
||||
|
||||
|
@ -173,8 +173,8 @@ void Thread_Sleep(cc_uint32 milliseconds) {
|
||||
wait_ms(milliseconds);
|
||||
}
|
||||
|
||||
void* Thread_Create(Thread_StartFunc func) {
|
||||
return NULL;
|
||||
void Thread_Run(void** handle, Thread_StartFunc func, int stackSize, const char* name) {
|
||||
*handle = NULL;
|
||||
}
|
||||
|
||||
void Thread_Start2(void* handle, Thread_StartFunc func) {
|
||||
|
@ -155,8 +155,8 @@ void Thread_Sleep(cc_uint32 milliseconds) {
|
||||
swiDelay(8378 * milliseconds); // TODO probably wrong
|
||||
}
|
||||
|
||||
void* Thread_Create(Thread_StartFunc func) {
|
||||
return NULL;
|
||||
void Thread_Run(void** handle, Thread_StartFunc func, int stackSize, const char* name) {
|
||||
*handle = NULL;
|
||||
}
|
||||
|
||||
void Thread_Start2(void* handle, Thread_StartFunc func) {
|
||||
|
@ -225,8 +225,6 @@ cc_result File_Length(cc_file file, cc_uint32* len) {
|
||||
/*########################################################################################################################*
|
||||
*--------------------------------------------------------Threading--------------------------------------------------------*
|
||||
*#########################################################################################################################*/
|
||||
#define STACK_SIZE (128 * 1024)
|
||||
|
||||
void Thread_Sleep(cc_uint32 milliseconds) {
|
||||
DelayThread(milliseconds * 1000);
|
||||
}
|
||||
@ -243,23 +241,19 @@ static int ExecThread(void* param) {
|
||||
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 };
|
||||
thread.func = ExecThread;
|
||||
thread.stack = Mem_Alloc(STACK_SIZE, 1, "Thread stack");
|
||||
thread.stack_size = STACK_SIZE;
|
||||
thread.stack = Mem_Alloc(stackSize, 1, "Thread stack");
|
||||
thread.stack_size = stackSize;
|
||||
thread.gp_reg = &_gp;
|
||||
thread.initial_priority = 18;
|
||||
|
||||
int thdID = CreateThread(&thread);
|
||||
if (thdID < 0) Logger_Abort2(thdID, "Creating thread");
|
||||
return (void*)thdID;
|
||||
}
|
||||
|
||||
void Thread_Start2(void* handle, Thread_StartFunc func) {
|
||||
int thdID = (int)handle;
|
||||
int res = StartThread(thdID, (void*)func);
|
||||
*handle = thdID;
|
||||
|
||||
int res = StartThread(thdID, (void*)func);
|
||||
if (res < 0) Logger_Abort2(res, "Running thread");
|
||||
}
|
||||
|
||||
|
@ -236,16 +236,13 @@ void Thread_Sleep(cc_uint32 milliseconds) {
|
||||
static void ExecThread(void* param) {
|
||||
((Thread_StartFunc)param)();
|
||||
}
|
||||
#define STACK_SIZE (128 * 1024)
|
||||
|
||||
void* Thread_Create(Thread_StartFunc func) {
|
||||
return Mem_Alloc(1, sizeof(sys_ppu_thread_t), "thread");
|
||||
}
|
||||
void Thread_Run(void** handle, Thread_StartFunc func, int stackSize, const char* name) {
|
||||
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,
|
||||
0, STACK_SIZE, THREAD_JOINABLE, "CC thread");
|
||||
0, stackSize, THREAD_JOINABLE, name);
|
||||
if (res) Logger_Abort2(res, "Creating thread");
|
||||
}
|
||||
|
||||
|
@ -218,18 +218,15 @@ static int ExecThread(unsigned int argc, void *argv) {
|
||||
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_STACKSIZE 128 * 1024
|
||||
#define CC_THREAD_ATTRS 0 // TODO PSP_THREAD_ATTR_VFPU?
|
||||
|
||||
return (void*)sceKernelCreateThread("CC thread", ExecThread, CC_THREAD_PRIORITY,
|
||||
CC_THREAD_STACKSIZE, CC_THREAD_ATTRS, NULL);
|
||||
}
|
||||
int threadID = sceKernelCreateThread(name, ExecThread, CC_THREAD_PRIORITY,
|
||||
stackSize, CC_THREAD_ATTRS, NULL);
|
||||
|
||||
void Thread_Start2(void* handle, Thread_StartFunc func) {
|
||||
Thread_StartFunc func_ = func;
|
||||
sceKernelStartThread((int)handle, sizeof(func_), (void*)&func_);
|
||||
*handle = (int)threadID;
|
||||
sceKernelStartThread(threadID, sizeof(func_), (void*)&func_);
|
||||
}
|
||||
|
||||
void Thread_Detach(void* handle) {
|
||||
|
@ -202,13 +202,15 @@ static int ExecThread(unsigned int argc, void *argv) {
|
||||
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_STACKSIZE 128 * 1024
|
||||
#define CC_THREAD_ATTRS 0 // TODO PSP_THREAD_ATTR_VFPU?
|
||||
|
||||
return (void*)sceKernelCreateThread("CC thread", ExecThread, CC_THREAD_PRIORITY,
|
||||
CC_THREAD_STACKSIZE, CC_THREAD_ATTRS, 0, NULL);
|
||||
int threadID = sceKernelCreateThread(name, ExecThread, CC_THREAD_PRIORITY,
|
||||
stackSize, CC_THREAD_ATTRS, 0, NULL);
|
||||
|
||||
*handle = (int)threadID;
|
||||
sceKernelStartThread(threadID, sizeof(func_), (void*)&func_);
|
||||
}
|
||||
|
||||
void Thread_Start2(void* handle, Thread_StartFunc func) {
|
||||
|
@ -332,13 +332,12 @@ static void* ExecThread(void* param) {
|
||||
}
|
||||
#endif
|
||||
|
||||
void* Thread_Create(Thread_StartFunc func) {
|
||||
return Mem_Alloc(1, sizeof(pthread_t), "thread");
|
||||
}
|
||||
void Thread_Run(void** handle, Thread_StartFunc func, int stackSize, const char* name) {
|
||||
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;
|
||||
int res = pthread_create(ptr, NULL, ExecThread, (void*)func);
|
||||
res = pthread_create(ptr, NULL, ExecThread, (void*)func);
|
||||
if (res) Logger_Abort2(res, "Creating thread");
|
||||
}
|
||||
|
||||
|
@ -229,23 +229,19 @@ static int ExecThread(int argc, const char **argv) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define STACK_SIZE 128 * 1024
|
||||
void* Thread_Create(Thread_StartFunc func) {
|
||||
void Thread_Run(void** handle, Thread_StartFunc func, int stackSize, const char* name) {
|
||||
OSThread* thread = (OSThread*)Mem_Alloc(1, sizeof(OSThread), "thread");
|
||||
void* stack = memalign(16, STACK_SIZE);
|
||||
void* stack = memalign(16, stackSize);
|
||||
|
||||
OSCreateThread(thread, ExecThread,
|
||||
1, (Thread_StartFunc)func,
|
||||
stack + STACK_SIZE, STACK_SIZE,
|
||||
stack + stackSize, stackSize,
|
||||
16, OS_THREAD_ATTRIB_AFFINITY_ANY);
|
||||
|
||||
*handle = thread;
|
||||
// TODO revisit this
|
||||
OSSetThreadRunQuantum(thread, 1000); // force yield after 1 millisecond
|
||||
return thread;
|
||||
}
|
||||
|
||||
void Thread_Start2(void* handle, Thread_StartFunc func) {
|
||||
OSResumeThread((OSThread*)handle);
|
||||
OSResumeThread(thread);
|
||||
}
|
||||
|
||||
void Thread_Detach(void* handle) {
|
||||
|
@ -296,17 +296,13 @@ static DWORD WINAPI ExecThread(void* param) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void* Thread_Create(Thread_StartFunc func) {
|
||||
void Thread_Run(void** handle, Thread_StartFunc func, int stackSize, const char* name) {
|
||||
DWORD threadID;
|
||||
void* handle = CreateThread(NULL, 0, ExecThread, (void*)func, CREATE_SUSPENDED, &threadID);
|
||||
if (!handle) {
|
||||
Logger_Abort2(GetLastError(), "Creating thread");
|
||||
}
|
||||
return handle;
|
||||
}
|
||||
HANDLE thread = CreateThread(NULL, 0, ExecThread, (void*)func, CREATE_SUSPENDED, &threadID);
|
||||
if (!thread) Logger_Abort2(GetLastError(), "Creating thread");
|
||||
|
||||
void Thread_Start2(void* handle, Thread_StartFunc func) {
|
||||
ResumeThread((HANDLE)handle);
|
||||
*handle = thread;
|
||||
ResumeThread(thread);
|
||||
}
|
||||
|
||||
void Thread_Detach(void* handle) {
|
||||
|
@ -232,17 +232,13 @@ static DWORD WINAPI ExecThread(void* param) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void* Thread_Create(Thread_StartFunc func) {
|
||||
void Thread_Run(void** handle, Thread_StartFunc func, int stackSize, const char* name) {
|
||||
DWORD threadID;
|
||||
void* handle = CreateThread(NULL, 0, ExecThread, (void*)func, CREATE_SUSPENDED, &threadID);
|
||||
if (!handle) {
|
||||
Logger_Abort2(GetLastError(), "Creating thread");
|
||||
}
|
||||
return handle;
|
||||
}
|
||||
HANDLE thread = CreateThread(NULL, 0, ExecThread, (void*)func, CREATE_SUSPENDED, &threadID);
|
||||
if (!thread) Logger_Abort2(GetLastError(), "Creating thread");
|
||||
|
||||
void Thread_Start2(void* handle, Thread_StartFunc func) {
|
||||
NtResumeThread((HANDLE)handle, NULL);
|
||||
*handle = thread;
|
||||
NtResumeThread(thread, NULL);
|
||||
}
|
||||
|
||||
void Thread_Detach(void* handle) {
|
||||
|
@ -197,8 +197,8 @@ cc_result File_Length(cc_file file, cc_uint32* len) {
|
||||
*#############################################################################################################p############*/
|
||||
void Thread_Sleep(cc_uint32 milliseconds) { mdelay(milliseconds); }
|
||||
|
||||
void* Thread_Create(Thread_StartFunc func) {
|
||||
return NULL; // TODO
|
||||
void Thread_Run(void** handle, Thread_StartFunc func, int stackSize, const char* name) {
|
||||
*handle = NULL; // TODO
|
||||
}
|
||||
|
||||
void Thread_Start2(void* handle, Thread_StartFunc func) {// TODO
|
||||
|
@ -65,13 +65,10 @@ static int32 ExecThread(void* param) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void* Thread_Create(Thread_StartFunc func) {
|
||||
thread_id thread = spawn_thread(ExecThread, "CC thread", B_NORMAL_PRIORITY, func);
|
||||
return (void*)thread;
|
||||
}
|
||||
void Thread_Run(void** handle, Thread_StartFunc func, int stackSize, const char* name) {
|
||||
thread_id thread = spawn_thread(ExecThread, name, B_NORMAL_PRIORITY, func);
|
||||
*handle = (void*)thread;
|
||||
|
||||
void Thread_Start2(void* handle, Thread_StartFunc func) {
|
||||
thread_id thread = (thread_id)handle;
|
||||
resume_thread(thread);
|
||||
}
|
||||
|
||||
@ -366,8 +363,8 @@ static void AppThread(void) {
|
||||
}
|
||||
|
||||
static void RunApp(void) {
|
||||
void* thread = Thread_Create(AppThread);
|
||||
Thread_Start2(thread, AppThread);
|
||||
void* thread;
|
||||
Thread_Run(&thread, AppThread, 128 * 1024, "App thread");
|
||||
Thread_Detach(thread);
|
||||
|
||||
// wait for BApplication to be started in other thread
|
||||
|
Loading…
x
Reference in New Issue
Block a user