From 6537fada36225077ddf15a2cee7d6cb8da035307 Mon Sep 17 00:00:00 2001 From: Francisco Javier Trujillo Mata Date: Sat, 20 Apr 2024 23:40:20 +0200 Subject: [PATCH] Fix lock API --- src/libcglue/Makefile.am | 24 ++++- src/libcglue/lock.c | 189 ++++++++++++++++++++++++++++++--------- 2 files changed, 168 insertions(+), 45 deletions(-) diff --git a/src/libcglue/Makefile.am b/src/libcglue/Makefile.am index ed548dcf..c8c2126a 100755 --- a/src/libcglue/Makefile.am +++ b/src/libcglue/Makefile.am @@ -33,9 +33,27 @@ GLUE_OBJS = __dummy_passwd.o __psp_heap_blockid.o __psp_free_heap.o _fork.o _wai INIT_OBJS = __libpthreadglue_init.o __libcglue_init.o __libcglue_deinit.o _exit.o abort.o exit.o -LOCK_OBJS = __retarget_lock_init.o __retarget_lock_acquire.o __retarget_lock_release.o __retarget_lock_try_acquire.o \ - __retarget_lock_close.o __retarget_lock_init_recursive.o __retarget_lock_acquire_recursive.o __retarget_lock_release_recursive.o \ - __retarget_lock_try_acquire_recursive.o __retarget_lock_close_recursive.o +LOCK_OBJS = \ + __lock___sfp_recursive_mutex.o \ + __lock___atexit_recursive_mutex.o \ + __lock___at_quick_exit_mutex.o \ + __lock___malloc_recursive_mutex.o \ + __lock___env_recursive_mutex.o \ + __lock___tz_mutex.o \ + __lock___dd_hash_mutex.o \ + __lock___arc4random_mutex.o \ + __retarget_lock_init.o \ + __retarget_lock_init_recursive.o \ + __retarget_lock_close.o \ + __retarget_lock_close_recursive.o \ + __retarget_lock_acquire.o \ + __retarget_lock_acquire_recursive.o \ + __retarget_lock_try_acquire.o \ + __retarget_lock_try_acquire_recursive.o \ + __retarget_lock_release.o \ + __retarget_lock_release_recursive.o \ + __locks_init.o \ + __locks_deinit.o MUTEXMAN_OBJS = __sbrk_mutex.o __fdman_mutex.o __init_mutex.o __deinit_mutex.o diff --git a/src/libcglue/lock.c b/src/libcglue/lock.c index 1fe550a1..99f015cc 100644 --- a/src/libcglue/lock.c +++ b/src/libcglue/lock.c @@ -21,14 +21,91 @@ // Structure representing the lock struct __lock { SceLwMutexWorkarea mutex; - int32_t thread_id; - int32_t count; }; +#ifdef F___lock___sfp_recursive_mutex +struct __lock __lock___sfp_recursive_mutex; +#endif + +#ifdef F___lock___atexit_recursive_mutex +struct __lock __lock___atexit_recursive_mutex; +#endif + +#ifdef F___lock___at_quick_exit_mutex +struct __lock __lock___at_quick_exit_mutex; +#endif + +#ifdef F___lock___malloc_recursive_mutex +struct __lock __lock___malloc_recursive_mutex; +#endif + +#ifdef F___lock___env_recursive_mutex +struct __lock __lock___env_recursive_mutex; +#endif + +#ifdef F___lock___tz_mutex +struct __lock __lock___tz_mutex; +#endif + +#ifdef F___lock___dd_hash_mutex +struct __lock __lock___dd_hash_mutex; +#endif + +#ifdef F___lock___arc4random_mutex +struct __lock __lock___arc4random_mutex; +#endif + +static inline void __common_lock_init(_LOCK_T lock) +{ + sceKernelCreateLwMutex(&lock->mutex, "lock API mutex", PSP_LW_MUTEX_ATTR_THFIFO, 0, 0); +} + +static inline void __common_lock_init_recursive(_LOCK_T lock) +{ + sceKernelCreateLwMutex(&lock->mutex, "lock API mutex Recursive", PSP_LW_MUTEX_ATTR_RECURSIVE, 0, 0); +} + +static inline void __common_lock_close(_LOCK_T lock) +{ + sceKernelDeleteLwMutex(&lock->mutex); +} + +static inline void __common_lock_close_recursive(_LOCK_T lock) +{ + sceKernelDeleteLwMutex(&lock->mutex); +} + #ifdef F___retarget_lock_init void __retarget_lock_init(_LOCK_T *lock) { - sceKernelCreateLwMutex(&(*lock)->mutex, "lock API mutex", 0, 0, 0); + _LOCK_T new_lock = (_LOCK_T)malloc(sizeof(struct __lock)); + __common_lock_init(new_lock); + *lock = new_lock; +} +#endif + +#ifdef F___retarget_lock_init_recursive +void __retarget_lock_init_recursive(_LOCK_T *lock) +{ + _LOCK_T new_lock = (_LOCK_T)malloc(sizeof(struct __lock)); + __common_lock_init_recursive(new_lock); + *lock = new_lock; +} +#endif + +#ifdef F___retarget_lock_close +void __retarget_lock_close(_LOCK_T lock) +{ + __common_lock_close(lock); + free(lock); +} +#endif + +#ifdef F___retarget_lock_close_recursive +void __retarget_lock_close_recursive(_LOCK_T lock) +{ + __common_lock_close_recursive(lock); + free(lock); } #endif @@ -39,10 +116,10 @@ void __retarget_lock_acquire(_LOCK_T lock) } #endif -#ifdef F___retarget_lock_release -void __retarget_lock_release(_LOCK_T lock) +#ifdef F___retarget_lock_acquire_recursive +void __retarget_lock_acquire_recursive(_LOCK_T lock) { - sceKernelUnlockLwMutex(&lock->mutex, 1); + sceKernelLockLwMutex(&lock->mutex, 1, 0); } #endif @@ -53,60 +130,88 @@ int __retarget_lock_try_acquire(_LOCK_T lock) } #endif -#ifdef F___retarget_lock_close -void __retarget_lock_close(_LOCK_T lock) +#ifdef F___retarget_lock_try_acquire_recursive +int __retarget_lock_try_acquire_recursive(_LOCK_T lock) { - sceKernelDeleteLwMutex(&lock->mutex); + return sceKernelTryLockLwMutex(&lock->mutex, 1); } #endif -#ifdef F___retarget_lock_init_recursive -void __retarget_lock_init_recursive(_LOCK_T *lock) +#ifdef F___retarget_lock_release +void __retarget_lock_release(_LOCK_T lock) { - sceKernelCreateLwMutex(&(*lock)->mutex, "lock API recursive mutex", 0, 0, 0); - (*lock)->count = 0; - (*lock)->thread_id = sceKernelGetThreadId(); -} -#endif - -#ifdef F___retarget_lock_acquire_recursive -void __retarget_lock_acquire_recursive(_LOCK_T lock) -{ - int32_t thread_id = sceKernelGetThreadId(); - if (lock->count == 0 || lock->thread_id != thread_id) { - sceKernelLockLwMutex(&lock->mutex, 1, 0); - } - lock->count++; + sceKernelUnlockLwMutex(&lock->mutex, 1); } #endif #ifdef F___retarget_lock_release_recursive void __retarget_lock_release_recursive(_LOCK_T lock) { - int32_t thread_id = sceKernelGetThreadId(); - if (lock->count == 1 || lock->thread_id != thread_id) { - sceKernelUnlockLwMutex(&lock->mutex, 1); - } - lock->count--; + sceKernelUnlockLwMutex(&lock->mutex, 1); } #endif -#ifdef F___retarget_lock_try_acquire_recursive -int __retarget_lock_try_acquire_recursive(_LOCK_T lock) +#ifdef F___locks_init +extern struct __lock __lock___malloc_recursive_mutex; +extern struct __lock __lock___atexit_recursive_mutex; +extern struct __lock __lock___at_quick_exit_mutex; +extern struct __lock __lock___sfp_recursive_mutex; +extern struct __lock __lock___env_recursive_mutex; +extern struct __lock __lock___tz_mutex; +extern struct __lock __lock___dd_hash_mutex; +extern struct __lock __lock___arc4random_mutex; + +void __locks_init() { - int res = 0; - int32_t thread_id = sceKernelGetThreadId(); - if (lock->count == 0 || lock->thread_id != thread_id) { - res = sceKernelTryLockLwMutex(&lock->mutex, 1) != 0; - } - lock->count++; - return res; + _LOCK_T lock_malloc = &__lock___malloc_recursive_mutex; + _LOCK_T lock_atexit = &__lock___atexit_recursive_mutex; + _LOCK_T lock_quick_exit = &__lock___at_quick_exit_mutex; + _LOCK_T lock_sfp = &__lock___sfp_recursive_mutex; + _LOCK_T lock_env = &__lock___env_recursive_mutex; + _LOCK_T lock_tz = &__lock___tz_mutex; + _LOCK_T lock_dd_hash = &__lock___dd_hash_mutex; + _LOCK_T lock_arc4random = &__lock___arc4random_mutex; + + __common_lock_init_recursive(lock_malloc); + __common_lock_init_recursive(lock_atexit); + __common_lock_init(lock_quick_exit); + __common_lock_init_recursive(lock_sfp); + __common_lock_init_recursive(lock_env); + __common_lock_init(lock_tz); + __common_lock_init(lock_dd_hash); + __common_lock_init(lock_arc4random); } #endif -#ifdef F___retarget_lock_close_recursive -void __retarget_lock_close_recursive(_LOCK_T lock) +#ifdef F___locks_deinit +extern struct __lock __lock___malloc_recursive_mutex; +extern struct __lock __lock___atexit_recursive_mutex; +extern struct __lock __lock___at_quick_exit_mutex; +extern struct __lock __lock___sfp_recursive_mutex; +extern struct __lock __lock___env_recursive_mutex; +extern struct __lock __lock___tz_mutex; +extern struct __lock __lock___dd_hash_mutex; +extern struct __lock __lock___arc4random_mutex; + +void __locks_deinit() { - sceKernelDeleteLwMutex(&lock->mutex); + _LOCK_T lock_malloc = &__lock___malloc_recursive_mutex; + _LOCK_T lock_atexit = &__lock___atexit_recursive_mutex; + _LOCK_T lock_quick_exit = &__lock___at_quick_exit_mutex; + _LOCK_T lock_sfp = &__lock___sfp_recursive_mutex; + _LOCK_T lock_env = &__lock___env_recursive_mutex; + _LOCK_T lock_tz = &__lock___tz_mutex; + _LOCK_T lock_dd_hash = &__lock___dd_hash_mutex; + _LOCK_T lock_arc4random = &__lock___arc4random_mutex; + + + __common_lock_close_recursive(lock_malloc); + __common_lock_close_recursive(lock_atexit); + __common_lock_close(lock_quick_exit); + __common_lock_close_recursive(lock_sfp); + __common_lock_close_recursive(lock_env); + __common_lock_close(lock_tz); + __common_lock_close(lock_dd_hash); + __common_lock_close(lock_arc4random); } #endif \ No newline at end of file