Allow naming mutexes/waitables

This commit is contained in:
UnknownShadow200 2024-06-04 20:26:11 +10:00
parent f8b388551d
commit ba68f4c625
25 changed files with 63 additions and 63 deletions

View File

@ -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);

View File

@ -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));

View File

@ -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");
}

View File

@ -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. */

View File

@ -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;

View File

@ -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 <errno.h>
#include <Application.h>
#include <OS.h>
#include <Roster.h>
/*########################################################################################################################*
*--------------------------------------------------------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 <OS.h>
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

View File

@ -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");

View File

@ -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;

View File

@ -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) {

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;

View File

@ -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;

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;

View File

@ -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;
}

View File

@ -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;

View File

@ -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) { }

View File

@ -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);

View File

@ -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");

View File

@ -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);

View File

@ -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
}

View File

@ -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;
}