From ba68f4c625f0d29308d1619aa374e56e5b8026d2 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Tue, 4 Jun 2024 20:26:11 +1000 Subject: [PATCH] Allow naming mutexes/waitables --- src/Audio.c | 2 +- src/AudioBackend.c | 2 +- src/Http_Worker.c | 8 ++++---- src/Platform.h | 4 ++-- src/Platform_3DS.c | 4 ++-- src/Platform_BeOS.cpp | 14 ++++++++------ src/Platform_Dreamcast.c | 4 ++-- src/Platform_GCWii.c | 4 ++-- src/Platform_MacClassic.c | 10 ++++------ src/Platform_N64.c | 4 ++-- src/Platform_NDS.c | 4 ++-- src/Platform_PS1.c | 4 ++-- src/Platform_PS2.c | 4 ++-- src/Platform_PS3.c | 4 ++-- src/Platform_PSP.c | 8 ++++---- src/Platform_PSVita.c | 8 ++++---- src/Platform_Posix.c | 4 ++-- src/Platform_Saturn.c | 4 ++-- src/Platform_Switch.c | 6 +++--- src/Platform_Web.c | 4 ++-- src/Platform_WiiU.c | 6 +++--- src/Platform_Windows.c | 4 ++-- src/Platform_Xbox.c | 4 ++-- src/Platform_Xbox360.c | 4 ++-- src/Window_BeOS.cpp | 2 +- 25 files changed, 63 insertions(+), 63 deletions(-) diff --git a/src/Audio.c b/src/Audio.c index 341264a04..6b2e7fa4a 100644 --- a/src/Audio.c +++ b/src/Audio.c @@ -522,7 +522,7 @@ static void Music_Init(void) { /* music is delayed between 2 - 7 minutes by default */ music_minDelay = Options_GetInt(OPT_MIN_MUSIC_DELAY, 0, 3600, 120) * MILLIS_PER_SEC; music_maxDelay = Options_GetInt(OPT_MAX_MUSIC_DELAY, 0, 3600, 420) * MILLIS_PER_SEC; - music_waitable = Waitable_Create(); + music_waitable = Waitable_Create("Music sleep"); volume = Options_GetInt(OPT_MUSIC_VOLUME, 0, 100, DEFAULT_MUSIC_VOLUME); Audio_SetMusic(volume); diff --git a/src/AudioBackend.c b/src/AudioBackend.c index fafe33c75..6d1d0c529 100644 --- a/src/AudioBackend.c +++ b/src/AudioBackend.c @@ -941,7 +941,7 @@ cc_bool AudioBackend_Init(void) { if (switchAudio) return true; switchAudio = true; - if (!audrv_mutex) audrv_mutex = Mutex_Create(); + if (!audrv_mutex) audrv_mutex = Mutex_Create("Audio sync"); Mem_Set(audioPools, 0, sizeof(audioPools)); diff --git a/src/Http_Worker.c b/src/Http_Worker.c index 7d60e90ee..817a01d87 100644 --- a/src/Http_Worker.c +++ b/src/Http_Worker.c @@ -1377,10 +1377,10 @@ static void Http_Init(void) { RequestList_Init(&pendingReqs); RequestList_Init(&processedReqs); - workerWaitable = Waitable_Create(); - pendingMutex = Mutex_Create(); - processedMutex = Mutex_Create(); - curRequestMutex = Mutex_Create(); + workerWaitable = Waitable_Create("HTTP wakeup"); + pendingMutex = Mutex_Create("HTTP pending"); + processedMutex = Mutex_Create("HTTP processed"); + curRequestMutex = Mutex_Create("HTTP current"); Thread_Run(&workerThread, WorkerLoop, 128 * 1024, "HTTP"); } diff --git a/src/Platform.h b/src/Platform.h index a2e681f1e..ca4b932b3 100644 --- a/src/Platform.h +++ b/src/Platform.h @@ -226,7 +226,7 @@ CC_API void Thread_Detach(void* handle); CC_API void Thread_Join(void* handle); /* Allocates a new mutex. (used to synchronise access to a shared resource) */ -CC_API void* Mutex_Create(void); +CC_API void* Mutex_Create(const char* name); /* Frees an allocated mutex. */ CC_API void Mutex_Free(void* handle); /* Locks the given mutex, blocking other threads from entering. */ @@ -235,7 +235,7 @@ CC_API void Mutex_Lock(void* handle); CC_API void Mutex_Unlock(void* handle); /* Allocates a new waitable. (used to conditionally wake-up a blocked thread) */ -CC_API void* Waitable_Create(void); +CC_API void* Waitable_Create(const char* name); /* Frees an allocated waitable. */ CC_API void Waitable_Free(void* handle); /* Signals a waitable, waking up blocked threads. */ diff --git a/src/Platform_3DS.c b/src/Platform_3DS.c index 87df96c08..eed2e6b43 100644 --- a/src/Platform_3DS.c +++ b/src/Platform_3DS.c @@ -228,7 +228,7 @@ void Thread_Join(void* handle) { threadFree(thread); } -void* Mutex_Create(void) { +void* Mutex_Create(const char* name) { LightLock* lock = (LightLock*)Mem_Alloc(1, sizeof(LightLock), "mutex"); LightLock_Init(lock); return lock; @@ -246,7 +246,7 @@ void Mutex_Unlock(void* handle) { LightLock_Unlock((LightLock*)handle); } -void* Waitable_Create(void) { +void* Waitable_Create(const char* name) { LightEvent* event = (LightEvent*)Mem_Alloc(1, sizeof(LightEvent), "waitable"); LightEvent_Init(event, RESET_ONESHOT); return event; diff --git a/src/Platform_BeOS.cpp b/src/Platform_BeOS.cpp index d2bcf2162..77a3bcaf3 100644 --- a/src/Platform_BeOS.cpp +++ b/src/Platform_BeOS.cpp @@ -1,12 +1,14 @@ #include "Core.h" #if defined CC_BUILD_BEOS || defined CC_BUILD_HAIKU extern "C" { +#include "Platform.h" #include "String.h" #include "Funcs.h" #include "Utils.h" } #include -#include +#include +#include /*########################################################################################################################* *--------------------------------------------------------Platform---------------------------------------------------------* @@ -38,7 +40,6 @@ cc_result Process_StartOpen(const cc_string* args) { *#########################################################################################################################*/ // NOTE: BeOS only, as haiku uses the more efficient pthreads implementation in Platform_Posix.c #if defined CC_BUILD_BEOS -#include void Thread_Sleep(cc_uint32 milliseconds) { snooze(milliseconds * 1000); } static int32 ExecThread(void* param) { @@ -60,8 +61,8 @@ void Thread_Join(void* handle) { wait_for_thread(thread, NULL); } -void* Mutex_Create(void) { - sem_id id = create_sem(1, "CC MUTEX"); +void* Mutex_Create(const char* name) { + sem_id id = create_sem(1, name); return (void*)id; } @@ -80,8 +81,8 @@ void Mutex_Unlock(void* handle) { release_sem(id); } -void* Waitable_Create(void) { - sem_id id = create_sem(0, "CC WAITABLE"); +void* Waitable_Create(const char* name) { + sem_id id = create_sem(0, name); return (void*)id; } @@ -106,3 +107,4 @@ void Waitable_WaitFor(void* handle, cc_uint32 milliseconds) { acquire_sem_etc(id, 1, B_RELATIVE_TIMEOUT, microseconds); } #endif +#endif diff --git a/src/Platform_Dreamcast.c b/src/Platform_Dreamcast.c index 0962842e7..0be9731ac 100644 --- a/src/Platform_Dreamcast.c +++ b/src/Platform_Dreamcast.c @@ -272,7 +272,7 @@ void Thread_Join(void* handle) { thd_join((kthread_t*)handle, NULL); } -void* Mutex_Create(void) { +void* Mutex_Create(const char* name) { mutex_t* ptr = (mutex_t*)Mem_Alloc(1, sizeof(mutex_t), "mutex"); int res = mutex_init(ptr, MUTEX_TYPE_NORMAL); if (res) Logger_Abort2(errno, "Creating mutex"); @@ -295,7 +295,7 @@ void Mutex_Unlock(void* handle) { if (res) Logger_Abort2(errno, "Unlocking mutex"); } -void* Waitable_Create(void) { +void* Waitable_Create(const char* name) { semaphore_t* ptr = (semaphore_t*)Mem_Alloc(1, sizeof(semaphore_t), "waitable"); int res = sem_init(ptr, 0); if (res) Logger_Abort2(errno, "Creating waitable"); diff --git a/src/Platform_GCWii.c b/src/Platform_GCWii.c index 9f660cd84..3c1895adc 100644 --- a/src/Platform_GCWii.c +++ b/src/Platform_GCWii.c @@ -272,7 +272,7 @@ void Thread_Join(void* handle) { Mem_Free(ptr); } -void* Mutex_Create(void) { +void* Mutex_Create(const char* name) { mutex_t* ptr = (mutex_t*)Mem_Alloc(1, sizeof(mutex_t), "mutex"); int res = LWP_MutexInit(ptr, false); if (res) Logger_Abort2(res, "Creating mutex"); @@ -305,7 +305,7 @@ struct WaitData { int signalled; // For when Waitable_Signal is called before Waitable_Wait }; -void* Waitable_Create(void) { +void* Waitable_Create(const char* name) { struct WaitData* ptr = (struct WaitData*)Mem_Alloc(1, sizeof(struct WaitData), "waitable"); int res; diff --git a/src/Platform_MacClassic.c b/src/Platform_MacClassic.c index 9e90d6b5a..2c98a54d2 100644 --- a/src/Platform_MacClassic.c +++ b/src/Platform_MacClassic.c @@ -248,9 +248,8 @@ void Thread_Join(void* handle) { // TODO } -void* Mutex_Create(void) { - // TODO - return 1; +void* Mutex_Create(const char* name) { + return NULL; } void Mutex_Free(void* handle) { @@ -265,9 +264,8 @@ void Mutex_Unlock(void* handle) { // TODO } -void* Waitable_Create(void) { - return 1; - // TODO +void* Waitable_Create(const char* name) { + return NULL; } void Waitable_Free(void* handle) { diff --git a/src/Platform_N64.c b/src/Platform_N64.c index 81cf6b52a..61afcd5b1 100644 --- a/src/Platform_N64.c +++ b/src/Platform_N64.c @@ -180,7 +180,7 @@ void Thread_Detach(void* handle) { void Thread_Join(void* handle) { } -void* Mutex_Create(void) { +void* Mutex_Create(const char* name) { return NULL; } @@ -193,7 +193,7 @@ void Mutex_Lock(void* handle) { void Mutex_Unlock(void* handle) { } -void* Waitable_Create(void) { +void* Waitable_Create(const char* name) { return NULL; } diff --git a/src/Platform_NDS.c b/src/Platform_NDS.c index 33af86065..03765814f 100644 --- a/src/Platform_NDS.c +++ b/src/Platform_NDS.c @@ -245,7 +245,7 @@ void Thread_Detach(void* handle) { void Thread_Join(void* handle) { } -void* Mutex_Create(void) { +void* Mutex_Create(const char* name) { return NULL; } @@ -258,7 +258,7 @@ void Mutex_Lock(void* handle) { void Mutex_Unlock(void* handle) { } -void* Waitable_Create(void) { +void* Waitable_Create(const char* name) { return NULL; } diff --git a/src/Platform_PS1.c b/src/Platform_PS1.c index f4ad6f5a4..63513fcee 100644 --- a/src/Platform_PS1.c +++ b/src/Platform_PS1.c @@ -160,7 +160,7 @@ void Thread_Detach(void* handle) { void Thread_Join(void* handle) { } -void* Mutex_Create(void) { +void* Mutex_Create(const char* name) { return NULL; } @@ -173,7 +173,7 @@ void Mutex_Lock(void* handle) { void Mutex_Unlock(void* handle) { } -void* Waitable_Create(void) { +void* Waitable_Create(const char* name) { return NULL; } diff --git a/src/Platform_PS2.c b/src/Platform_PS2.c index 51de819db..100271afd 100644 --- a/src/Platform_PS2.c +++ b/src/Platform_PS2.c @@ -276,7 +276,7 @@ void Thread_Join(void* handle) { } } -void* Mutex_Create(void) { +void* Mutex_Create(const char* name) { ee_sema_t sema = { 0 }; sema.init_count = 1; sema.max_count = 1; @@ -307,7 +307,7 @@ void Mutex_Unlock(void* handle) { if (res < 0) Logger_Abort2(res, "Unlocking mutex"); } -void* Waitable_Create(void) { +void* Waitable_Create(const char* name) { ee_sema_t sema = { 0 }; sema.init_count = 0; sema.max_count = 1; diff --git a/src/Platform_PS3.c b/src/Platform_PS3.c index b4c15a258..87b9d74e0 100644 --- a/src/Platform_PS3.c +++ b/src/Platform_PS3.c @@ -260,7 +260,7 @@ void Thread_Join(void* handle) { Mem_Free(thread); } -void* Mutex_Create(void) { +void* Mutex_Create(const char* name) { sys_mutex_attr_t attr; sysMutexAttrInitialize(attr); @@ -289,7 +289,7 @@ void Mutex_Unlock(void* handle) { if (res) Logger_Abort2(res, "Unlocking mutex"); } -void* Waitable_Create(void) { +void* Waitable_Create(const char* name) { sys_sem_attr_t attr = { 0 }; attr.attr_protocol = SYS_SEM_ATTR_PROTOCOL; attr.attr_pshared = SYS_SEM_ATTR_PSHARED; diff --git a/src/Platform_PSP.c b/src/Platform_PSP.c index 2712e5f81..53c013b16 100644 --- a/src/Platform_PSP.c +++ b/src/Platform_PSP.c @@ -239,9 +239,9 @@ void Thread_Join(void* handle) { sceKernelDeleteThread((int)handle); } -void* Mutex_Create(void) { +void* Mutex_Create(const char* name) { SceLwMutexWorkarea* ptr = (SceLwMutexWorkarea*)Mem_Alloc(1, sizeof(SceLwMutexWorkarea), "mutex"); - int res = sceKernelCreateLwMutex(ptr, "CC mutex", 0, 0, NULL); + int res = sceKernelCreateLwMutex(ptr, name, 0, 0, NULL); if (res) Logger_Abort2(res, "Creating mutex"); return ptr; } @@ -262,8 +262,8 @@ void Mutex_Unlock(void* handle) { if (res) Logger_Abort2(res, "Unlocking mutex"); } -void* Waitable_Create(void) { - int evid = sceKernelCreateEventFlag("CC event", PSP_EVENT_WAITMULTIPLE, 0, NULL); +void* Waitable_Create(const char* name) { + int evid = sceKernelCreateEventFlag(name, PSP_EVENT_WAITMULTIPLE, 0, NULL); if (evid < 0) Logger_Abort2(evid, "Creating waitable"); return (void*)evid; } diff --git a/src/Platform_PSVita.c b/src/Platform_PSVita.c index afefa9210..8cca2787b 100644 --- a/src/Platform_PSVita.c +++ b/src/Platform_PSVita.c @@ -222,9 +222,9 @@ void Thread_Join(void* handle) { sceKernelDeleteThread((int)handle); } -void* Mutex_Create(void) { +void* Mutex_Create(const char* name) { SceKernelLwMutexWork* ptr = (SceKernelLwMutexWork*)Mem_Alloc(1, sizeof(SceKernelLwMutexWork), "mutex"); - int res = sceKernelCreateLwMutex(ptr, "CC mutex", 0, 0, NULL); + int res = sceKernelCreateLwMutex(ptr, name, 0, 0, NULL); if (res) Logger_Abort2(res, "Creating mutex"); return ptr; } @@ -245,8 +245,8 @@ void Mutex_Unlock(void* handle) { if (res) Logger_Abort2(res, "Unlocking mutex"); } -void* Waitable_Create(void) { - int evid = sceKernelCreateEventFlag("CC event", SCE_EVENT_WAITMULTIPLE, 0, NULL); +void* Waitable_Create(const char* name) { + int evid = sceKernelCreateEventFlag(name, SCE_EVENT_WAITMULTIPLE, 0, NULL); if (evid < 0) Logger_Abort2(evid, "Creating waitable"); return (void*)evid; } diff --git a/src/Platform_Posix.c b/src/Platform_Posix.c index 796324fe9..e91e85588 100644 --- a/src/Platform_Posix.c +++ b/src/Platform_Posix.c @@ -389,7 +389,7 @@ void Thread_Join(void* handle) { Mem_Free(ptr); } -void* Mutex_Create(void) { +void* Mutex_Create(const char* name) { pthread_mutex_t* ptr = (pthread_mutex_t*)Mem_Alloc(1, sizeof(pthread_mutex_t), "mutex"); int res = pthread_mutex_init(ptr, NULL); if (res) Logger_Abort2(res, "Creating mutex"); @@ -418,7 +418,7 @@ struct WaitData { int signalled; /* For when Waitable_Signal is called before Waitable_Wait */ }; -void* Waitable_Create(void) { +void* Waitable_Create(const char* name) { struct WaitData* ptr = (struct WaitData*)Mem_Alloc(1, sizeof(struct WaitData), "waitable"); int res; diff --git a/src/Platform_Saturn.c b/src/Platform_Saturn.c index 449d72c69..e0f37d471 100644 --- a/src/Platform_Saturn.c +++ b/src/Platform_Saturn.c @@ -151,7 +151,7 @@ void Thread_Detach(void* handle) { void Thread_Join(void* handle) { } -void* Mutex_Create(void) { +void* Mutex_Create(const char* name) { return NULL; } @@ -164,7 +164,7 @@ void Mutex_Lock(void* handle) { void Mutex_Unlock(void* handle) { } -void* Waitable_Create(void) { +void* Waitable_Create(const char* name) { return NULL; } diff --git a/src/Platform_Switch.c b/src/Platform_Switch.c index 271e31f1c..855ce7e55 100644 --- a/src/Platform_Switch.c +++ b/src/Platform_Switch.c @@ -252,7 +252,7 @@ void Thread_Join(void* handle) { Mem_Free(thread); } -void* Mutex_Create(void) { +void* Mutex_Create(const char* name) { Mutex* mutex = (Mutex*)Mem_Alloc(1, sizeof(Mutex), "mutex"); mutexInit(mutex); return mutex; @@ -277,7 +277,7 @@ struct WaitData { int signalled; // For when Waitable_Signal is called before Waitable_Wait }; -void* Waitable_Create(void) { +void* Waitable_Create(const char* name) { struct WaitData* ptr = (struct WaitData*)Mem_Alloc(1, sizeof(struct WaitData), "waitable"); mutexInit(&ptr->mutex); @@ -326,7 +326,7 @@ void Waitable_WaitFor(void* handle, cc_uint32 milliseconds) { } /* -void* Waitable_Create(void) { +void* Waitable_Create(const char* name) { LEvent* ptr = (LEvent*)Mem_Alloc(1, sizeof(LEvent), "waitable"); leventInit(ptr, false, true); return ptr; diff --git a/src/Platform_Web.c b/src/Platform_Web.c index 4c800699e..49cf7e4be 100644 --- a/src/Platform_Web.c +++ b/src/Platform_Web.c @@ -228,12 +228,12 @@ cc_result File_Length(cc_file file, cc_uint32* len) { /* No real threading support with emscripten backend */ void Thread_Sleep(cc_uint32 milliseconds) { } -void* Mutex_Create(void) { return NULL; } +void* Mutex_Create(const char* name) { return NULL; } void Mutex_Free(void* handle) { } void Mutex_Lock(void* handle) { } void Mutex_Unlock(void* handle) { } -void* Waitable_Create(void) { return NULL; } +void* Waitable_Create(const char* name) { return NULL; } void Waitable_Free(void* handle) { } void Waitable_Signal(void* handle) { } void Waitable_Wait(void* handle) { } diff --git a/src/Platform_WiiU.c b/src/Platform_WiiU.c index fcc9ce144..d11a7dae2 100644 --- a/src/Platform_WiiU.c +++ b/src/Platform_WiiU.c @@ -254,10 +254,10 @@ void Thread_Join(void* handle) { OSJoinThread((OSThread*)handle, &result); } -void* Mutex_Create(void) { +void* Mutex_Create(const char* name) { OSFastMutex* mutex = (OSFastMutex*)Mem_Alloc(1, sizeof(OSFastMutex), "mutex"); - OSFastMutex_Init(mutex, "CC mutex"); + OSFastMutex_Init(mutex, name); return mutex; } @@ -273,7 +273,7 @@ void Mutex_Unlock(void* handle) { OSFastMutex_Unlock((OSFastMutex*)handle); } -void* Waitable_Create(void) { +void* Waitable_Create(const char* name) { OSEvent* event = (OSEvent*)Mem_Alloc(1, sizeof(OSEvent), "waitable"); OSInitEvent(event, false, OS_EVENT_MODE_AUTO); diff --git a/src/Platform_Windows.c b/src/Platform_Windows.c index 774fd2488..819ba0c5b 100644 --- a/src/Platform_Windows.c +++ b/src/Platform_Windows.c @@ -351,7 +351,7 @@ void Thread_Join(void* handle) { Thread_Detach(handle); } -void* Mutex_Create(void) { +void* Mutex_Create(const char* name) { CRITICAL_SECTION* ptr = (CRITICAL_SECTION*)Mem_Alloc(1, sizeof(CRITICAL_SECTION), "mutex"); InitializeCriticalSection(ptr); return ptr; @@ -364,7 +364,7 @@ void Mutex_Free(void* handle) { void Mutex_Lock(void* handle) { EnterCriticalSection((CRITICAL_SECTION*)handle); } void Mutex_Unlock(void* handle) { LeaveCriticalSection((CRITICAL_SECTION*)handle); } -void* Waitable_Create(void) { +void* Waitable_Create(const char* name) { void* handle = CreateEventA(NULL, false, false, NULL); if (!handle) { Logger_Abort2(GetLastError(), "Creating waitable"); diff --git a/src/Platform_Xbox.c b/src/Platform_Xbox.c index 06b47689d..9b84b5548 100644 --- a/src/Platform_Xbox.c +++ b/src/Platform_Xbox.c @@ -261,7 +261,7 @@ void Thread_Join(void* handle) { Thread_Detach(handle); } -void* Mutex_Create(void) { +void* Mutex_Create(const char* name) { CRITICAL_SECTION* ptr = (CRITICAL_SECTION*)Mem_Alloc(1, sizeof(CRITICAL_SECTION), "mutex"); RtlInitializeCriticalSection(ptr); return ptr; @@ -280,7 +280,7 @@ void Mutex_Unlock(void* handle) { RtlLeaveCriticalSection((CRITICAL_SECTION*)handle); } -void* Waitable_Create(void) { +void* Waitable_Create(const char* name) { HANDLE handle; NTSTATUS status = NtCreateEvent(&handle, NULL, SynchronizationEvent, false); diff --git a/src/Platform_Xbox360.c b/src/Platform_Xbox360.c index 0bd3e6e65..0eaf831e0 100644 --- a/src/Platform_Xbox360.c +++ b/src/Platform_Xbox360.c @@ -205,7 +205,7 @@ void Thread_Detach(void* handle) {// TODO void Thread_Join(void* handle) {// TODO } -void* Mutex_Create(void) { +void* Mutex_Create(const char* name) { return Mem_AllocCleared(1, sizeof(int), "mutex"); } @@ -215,7 +215,7 @@ void Mutex_Free(void* handle) { void Mutex_Lock(void* handle) { } // TODO void Mutex_Unlock(void* handle) { } // TODO -void* Waitable_Create(void) { +void* Waitable_Create(const char* name) { return NULL; // TODO } diff --git a/src/Window_BeOS.cpp b/src/Window_BeOS.cpp index d1f4af7d8..c087a4b1e 100644 --- a/src/Window_BeOS.cpp +++ b/src/Window_BeOS.cpp @@ -52,7 +52,7 @@ static int events_count, events_capacity; static CCEvent* events_list, events_default[EVENTS_DEFAULT_MAX]; static void Events_Init(void) { - events_mutex = Mutex_Create(); + events_mutex = Mutex_Create("BeOS events"); events_capacity = EVENTS_DEFAULT_MAX; events_list = events_default; }