From af5e24ef06087174a7ee5c6d3434e7111ed12589 Mon Sep 17 00:00:00 2001 From: Francisco Javier Trujillo Mata Date: Thu, 25 Nov 2021 11:10:47 +0100 Subject: [PATCH 1/2] Deinit mutexs --- src/libcglue/Makefile.am | 2 +- src/libcglue/mutexman.c | 10 ++++++++++ src/libcglue/terminate.c | 3 +++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/libcglue/Makefile.am b/src/libcglue/Makefile.am index 65366263..a4816424 100644 --- a/src/libcglue/Makefile.am +++ b/src/libcglue/Makefile.am @@ -32,7 +32,7 @@ GLUE_OBJS = __fill_stat.o __psp_heap_blockid.o __psp_free_heap.o _fork.o _wait.o INIT_OBJS = __libcglue_init.o -MUTEXMAN_OBJS = __malloc_mutex.o __sbrk_mutex.o __fdman_mutex.o __init_mutex.o +MUTEXMAN_OBJS = __malloc_mutex.o __sbrk_mutex.o __fdman_mutex.o __init_mutex.o __deinit_mutex.o NETDB_OBJS = h_errno.o gethostbyaddr.o gethostbyname.o diff --git a/src/libcglue/mutexman.c b/src/libcglue/mutexman.c index 995ef5d4..e5628369 100644 --- a/src/libcglue/mutexman.c +++ b/src/libcglue/mutexman.c @@ -42,4 +42,14 @@ void __init_mutex() sceKernelCreateLwMutex(&__sbrk_mutex, "sbrk mutex", 0, 0, 0); sceKernelCreateLwMutex(&__fdman_mutex, "fdman mutex", 0, 0, 0); } +#endif + +#ifdef F___deinit_mutex +/* Create mutex used for making thread safe mallock and get fd */ +void __deinit_mutex() +{ + sceKernelDeleteLwMutex(&__malloc_mutex); + sceKernelDeleteLwMutex(&__sbrk_mutex); + sceKernelDeleteLwMutex(&__fdman_mutex); +} #endif \ No newline at end of file diff --git a/src/libcglue/terminate.c b/src/libcglue/terminate.c index a3eb0b25..1e6f9082 100644 --- a/src/libcglue/terminate.c +++ b/src/libcglue/terminate.c @@ -18,18 +18,21 @@ extern int sce_newlib_nocreate_thread_in_start __attribute__((weak)); int __psp_free_heap(void); +void __deinit_mutex(void); void _exit(int status) { if (&sce_newlib_nocreate_thread_in_start != NULL) { /* Free the heap created by _sbrk(). */ __psp_free_heap(); + __deinit_mutex(); sceKernelSelfStopUnloadModule(1, 0, NULL); } else { if (status == 0) { /* Free the heap created by _sbrk(). */ __psp_free_heap(); + __deinit_mutex(); } sceKernelExitThread(status); From cfb9b049546f2df5849468d96bbdb0d67ca8c804 Mon Sep 17 00:00:00 2001 From: Francisco Javier Trujillo Mata Date: Thu, 25 Nov 2021 17:20:18 +0100 Subject: [PATCH 2/2] Improve exit process --- src/libcglue/Makefile.am | 10 ++----- src/libcglue/init.c | 52 +++++++++++++++++++++++++++++++++ src/libcglue/terminate.c | 63 ---------------------------------------- src/user/pspmoduleinfo.h | 3 +- 4 files changed, 56 insertions(+), 72 deletions(-) delete mode 100644 src/libcglue/terminate.c diff --git a/src/libcglue/Makefile.am b/src/libcglue/Makefile.am index a4816424..afa9fbd6 100644 --- a/src/libcglue/Makefile.am +++ b/src/libcglue/Makefile.am @@ -30,7 +30,7 @@ GLUE_OBJS = __fill_stat.o __psp_heap_blockid.o __psp_free_heap.o _fork.o _wait.o _isatty.o symlink.o truncate.o chmod.o fchmod.o fchmodat.o pathconf.o readlink.o utime.o fchown.o getentropy.o -INIT_OBJS = __libcglue_init.o +INIT_OBJS = __libcglue_init.o __libcglue_deinit.o _exit.o abort.o exit.o MUTEXMAN_OBJS = __malloc_mutex.o __sbrk_mutex.o __fdman_mutex.o __init_mutex.o __deinit_mutex.o @@ -46,8 +46,6 @@ SLEEP_OBJS = nanosleep.o SOCKET_OBJS = socket.o __socket_close.o accept.o bind.o connect.o getsockopt.o listen.o recv.o recvfrom.o send.o sendto.o \ setsockopt.o shutdown.o getpeername.o getsockname.o inet_ntoa.o sendmsg.o recvmsg.o -TERMINATE_OBJS = _exit.o abort.o exit.o - TIMEZONE_OBJS = __timezone_update.o lib_LIBRARIES = libcglue.a @@ -75,11 +73,10 @@ libcglue_a_SOURCES = \ select.c \ sleep.c \ socket.c \ - terminate.c \ timezone.c libcglue_a_LIBADD = $(CWD_OBJS) $(ERRNO_OBJS) $(FDMAN_OBJS) $(INIT_OBJS) $(GLUE_OBJS) $(MUTEXMAN_OBJS) $(NETDB_OBJS) $(PIPE_OBJS) \ - $(SELECT_OBJS) $(SOCKET_OBJS) $(SLEEP_OBJS) $(TERMINATE_OBJS) $(TIMEZONE_OBJS) + $(SELECT_OBJS) $(SOCKET_OBJS) $(SLEEP_OBJS) $(TIMEZONE_OBJS) $(CWD_OBJS): cwd.c $(AM_V_CC)$(COMPILE) -DF_$* $< -c -o $@ @@ -114,8 +111,5 @@ $(SLEEP_OBJS): sleep.c $(SOCKET_OBJS): socket.c $(AM_V_CC)$(COMPILE) -DF_$* $< -c -o $@ -$(TERMINATE_OBJS): terminate.c - $(AM_V_CC)$(COMPILE) -DF_$* $< -c -o $@ - $(TIMEZONE_OBJS): timezone.c $(AM_V_CC)$(COMPILE) -DF_$* $< -c -o $@ diff --git a/src/libcglue/init.c b/src/libcglue/init.c index e6296067..ca630196 100644 --- a/src/libcglue/init.c +++ b/src/libcglue/init.c @@ -8,6 +8,7 @@ * Copyright (c) 2005 Marcus R. Brown * Copyright (c) 2005 James Forshaw * Copyright (c) 2005 John Kelley + * Copyright (c) 2021 Francisco Javier Trujillo Mata * */ @@ -16,10 +17,16 @@ #include #include +#include + void __init_cwd(char *argv_0); void __timezone_update(); void __fdman_init(); void __init_mutex(); +void __psp_free_heap(); +void __deinit_mutex(); + +extern int sce_newlib_nocreate_thread_in_start __attribute__((weak)); #ifdef F___libcglue_init /* Note: This function is being called from crt0.c/crt0_prx.c. @@ -44,4 +51,49 @@ void __libcglue_init(int argc, char *argv[]) /* Initialize timezone */ __timezone_update(); } +#endif + +#ifdef F___libcglue_deinit +/* Note: This function is being called from terminate.c. +* It is a weak function because can be override by user program +*/ +__attribute__((weak)) +void __libcglue_deinit() +{ + __psp_free_heap(); + __deinit_mutex(); +} +#else +void __libcglue_deinit(); +#endif + +#ifdef F__exit +void _exit(int status) +{ + __libcglue_deinit(); + + if (&sce_newlib_nocreate_thread_in_start != NULL) { + sceKernelSelfStopUnloadModule(1, 0, NULL); + } else { + sceKernelExitGame(); + } +} +#else +void _exit(int status); +#endif + +#ifdef F_abort +__attribute__((weak)) +void abort() +{ + _exit(1); +} +#endif + +#ifdef F_exit +__attribute__((weak)) +void exit(int retval) +{ + _exit(retval); +} #endif \ No newline at end of file diff --git a/src/libcglue/terminate.c b/src/libcglue/terminate.c deleted file mode 100644 index 1e6f9082..00000000 --- a/src/libcglue/terminate.c +++ /dev/null @@ -1,63 +0,0 @@ -/* - * PSP Software Development Kit - https://github.com/pspdev - * ----------------------------------------------------------------------- - * Licensed under the BSD license, see LICENSE in PSPSDK root for details. - * - * terminate.c - Process exit functions. - * - * Copyright (c) 2005 Marcus R. Brown - * Copyright (c) 2005 James Forshaw - * Copyright (c) 2005 John Kelley - * - */ -#include -#include -#include - -#ifdef F__exit -extern int sce_newlib_nocreate_thread_in_start __attribute__((weak)); - -int __psp_free_heap(void); -void __deinit_mutex(void); - -void _exit(int status) -{ - if (&sce_newlib_nocreate_thread_in_start != NULL) { - /* Free the heap created by _sbrk(). */ - __psp_free_heap(); - __deinit_mutex(); - - sceKernelSelfStopUnloadModule(1, 0, NULL); - } else { - if (status == 0) { - /* Free the heap created by _sbrk(). */ - __psp_free_heap(); - __deinit_mutex(); - } - - sceKernelExitThread(status); - } - - while (1) ; -} -#else -void _exit(int status); -#endif - -#ifdef F_abort -__attribute__((weak)) -void abort() -{ - while (1) - _exit(1); -} -#endif - -#ifdef F_exit -__attribute__((weak)) -void exit(int retval) -{ - while (1) - _exit(retval); -} -#endif diff --git a/src/user/pspmoduleinfo.h b/src/user/pspmoduleinfo.h index 3d33aadd..ab644a31 100644 --- a/src/user/pspmoduleinfo.h +++ b/src/user/pspmoduleinfo.h @@ -146,6 +146,7 @@ enum PspModuleInfoAttr /* Disable the use of newlib, getting a minimal binary. */ #define PSP_DISABLE_NEWLIB() \ - void __libcglue_init(int argc, char *argv[]) {} + void __libcglue_init(int argc, char *argv[]) {} \ + void __libcglue_deinit() {} #endif /* PSPMODULEINFO_H */