diff --git a/src/Audio.c b/src/Audio.c index 4f9480047..6142f21fb 100644 --- a/src/Audio.c +++ b/src/Audio.c @@ -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) { diff --git a/src/Funcs.h b/src/Funcs.h index 5d1222c50..1edd33549 100644 --- a/src/Funcs.h +++ b/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 diff --git a/src/Generator.c b/src/Generator.c index df7fffb71..7ad9649a6 100644 --- a/src/Generator.c +++ b/src/Generator.c @@ -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); } diff --git a/src/Http_Worker.c b/src/Http_Worker.c index 347bbf4ef..93fad4935 100644 --- a/src/Http_Worker.c +++ b/src/Http_Worker.c @@ -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 diff --git a/src/Model.c b/src/Model.c index 7b4ad8c72..1d0dd2678 100644 --- a/src/Model.c +++ b/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); diff --git a/src/Platform.h b/src/Platform.h index 412b59a72..3b1635847 100644 --- a/src/Platform.h +++ b/src/Platform.h @@ -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); diff --git a/src/Platform_3DS.c b/src/Platform_3DS.c index cb1c173b8..89fd1291d 100644 --- a/src/Platform_3DS.c +++ b/src/Platform_3DS.c @@ -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) { diff --git a/src/Platform_Android.c b/src/Platform_Android.c index 98d330f60..e09918dea 100644 --- a/src/Platform_Android.c +++ b/src/Platform_Android.c @@ -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[] = { diff --git a/src/Platform_Dreamcast.c b/src/Platform_Dreamcast.c index b4e13bb70..b180ef2dd 100644 --- a/src/Platform_Dreamcast.c +++ b/src/Platform_Dreamcast.c @@ -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) { diff --git a/src/Platform_GCWii.c b/src/Platform_GCWii.c index 7a003697d..97acf81df 100644 --- a/src/Platform_GCWii.c +++ b/src/Platform_GCWii.c @@ -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_Start2(void* handle, Thread_StartFunc func) { - lwp_t* ptr = (lwp_t*)handle; - int res = LWP_CreateThread(ptr, ExecThread, (void*)func, NULL, 256 * 1024, 80); +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; + + int res = LWP_CreateThread(thread, ExecThread, (void*)func, NULL, stackSize, 80); if (res) Logger_Abort2(res, "Creating thread"); } diff --git a/src/Platform_N64.c b/src/Platform_N64.c index 7c57cedde..7b7428caa 100644 --- a/src/Platform_N64.c +++ b/src/Platform_N64.c @@ -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) { diff --git a/src/Platform_NDS.c b/src/Platform_NDS.c index af95c81ef..678b33a0f 100644 --- a/src/Platform_NDS.c +++ b/src/Platform_NDS.c @@ -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) { diff --git a/src/Platform_PS2.c b/src/Platform_PS2.c index 0bfe63c34..ada09b7c4 100644 --- a/src/Platform_PS2.c +++ b/src/Platform_PS2.c @@ -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"); } diff --git a/src/Platform_PS3.c b/src/Platform_PS3.c index 6fddd2265..a70279d0c 100644 --- a/src/Platform_PS3.c +++ b/src/Platform_PS3.c @@ -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_Start2(void* handle, Thread_StartFunc func) { - sys_ppu_thread_t* thread = (sys_ppu_thread_t*)handle; +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; + 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"); } diff --git a/src/Platform_PSP.c b/src/Platform_PSP.c index 4cd3fef71..b3dd1a8e1 100644 --- a/src/Platform_PSP.c +++ b/src/Platform_PSP.c @@ -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); -} - -void Thread_Start2(void* handle, Thread_StartFunc func) { - Thread_StartFunc func_ = func; - sceKernelStartThread((int)handle, sizeof(func_), (void*)&func_); + int threadID = sceKernelCreateThread(name, ExecThread, CC_THREAD_PRIORITY, + stackSize, CC_THREAD_ATTRS, NULL); + + *handle = (int)threadID; + sceKernelStartThread(threadID, sizeof(func_), (void*)&func_); } void Thread_Detach(void* handle) { diff --git a/src/Platform_PSVita.c b/src/Platform_PSVita.c index 05d476689..525ebe237 100644 --- a/src/Platform_PSVita.c +++ b/src/Platform_PSVita.c @@ -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) { diff --git a/src/Platform_Posix.c b/src/Platform_Posix.c index 2e9db9259..763147851 100644 --- a/src/Platform_Posix.c +++ b/src/Platform_Posix.c @@ -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_Start2(void* handle, Thread_StartFunc func) { - pthread_t* ptr = (pthread_t*)handle; - int res = pthread_create(ptr, NULL, ExecThread, (void*)func); +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; + + res = pthread_create(ptr, NULL, ExecThread, (void*)func); if (res) Logger_Abort2(res, "Creating thread"); } diff --git a/src/Platform_WiiU.c b/src/Platform_WiiU.c index 5d11faa0f..ba3069e48 100644 --- a/src/Platform_WiiU.c +++ b/src/Platform_WiiU.c @@ -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) { diff --git a/src/Platform_Windows.c b/src/Platform_Windows.c index 9db4f92e8..52d128dab 100644 --- a/src/Platform_Windows.c +++ b/src/Platform_Windows.c @@ -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; -} - -void Thread_Start2(void* handle, Thread_StartFunc func) { - ResumeThread((HANDLE)handle); + HANDLE thread = CreateThread(NULL, 0, ExecThread, (void*)func, CREATE_SUSPENDED, &threadID); + if (!thread) Logger_Abort2(GetLastError(), "Creating thread"); + + *handle = thread; + ResumeThread(thread); } void Thread_Detach(void* handle) { diff --git a/src/Platform_Xbox.c b/src/Platform_Xbox.c index 3ffd446eb..5cb8c223e 100644 --- a/src/Platform_Xbox.c +++ b/src/Platform_Xbox.c @@ -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) { diff --git a/src/Platform_Xbox360.c b/src/Platform_Xbox360.c index ff60a1439..c230bc370 100644 --- a/src/Platform_Xbox360.c +++ b/src/Platform_Xbox360.c @@ -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 diff --git a/src/interop_BeOS.cpp b/src/interop_BeOS.cpp index 74e737cc4..42cf752bb 100644 --- a/src/interop_BeOS.cpp +++ b/src/interop_BeOS.cpp @@ -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_Start2(void* handle, Thread_StartFunc func) { - thread_id thread = (thread_id)handle; +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; + 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