1 #include <stdlib.h> 2 #include <stdint.h> 3 #include "libc.h" 4 #include <stdbool.h> 5 #ifdef __LITEOS_A__ 6 #include <signal.h> 7 #include <atomic.h> 8 #include <pthread.h> 9 #include "syscall.h" 10 #include <bits/errno.h> 11 #ifdef __LITEOS_DEBUG__ 12 #include <debug.h> 13 extern bool g_enable_check; 14 extern void mem_check_deinit(void); 15 extern void clean_recycle_list(bool clean_all); 16 #endif 17 18 pthread_mutex_t __exit_mutex = PTHREAD_MUTEX_INITIALIZER; 19 #endif 20 bool g_global_destroyed = false; dummy()21static void dummy() 22 { 23 } 24 25 /* atexit.c and __stdio_exit.c override these. the latter is linked 26 * as a consequence of linking either __toread.c or __towrite.c. */ 27 weak_alias(dummy, __funcs_on_exit); 28 weak_alias(dummy, __stdio_exit); 29 weak_alias(dummy, _fini); 30 31 extern weak hidden void (*const __fini_array_start)(void), (*const __fini_array_end)(void); 32 libc_exit_fini(void)33static void libc_exit_fini(void) 34 { 35 uintptr_t a = (uintptr_t)&__fini_array_end; 36 for (; a>(uintptr_t)&__fini_array_start; a-=sizeof(void(*)())) 37 (*(void (**)())(a-sizeof(void(*)())))(); 38 _fini(); 39 } 40 41 weak_alias(libc_exit_fini, __libc_exit_fini); 42 43 #if !defined(__LITEOS__) && !defined(__HISPARK_LINUX__) 44 extern void __cxa_thread_finalize(); 45 #endif 46 exit(int code)47_Noreturn void exit(int code) 48 { 49 #ifdef __LITEOS_A__ 50 sigset_t set; 51 52 __block_app_sigs(&set); 53 54 int ret = pthread_mutex_trylock(&__exit_mutex); 55 if (ret == EBUSY) { 56 pthread_exit(NULL); 57 } 58 59 #ifdef __LITEOS_DEBUG__ 60 if (g_enable_check) { 61 check_leak(); 62 check_heap_integrity(); 63 mem_check_deinit(); 64 clean_recycle_list(true); 65 } 66 #endif 67 #endif 68 69 #if !defined(__LITEOS__) && !defined(__HISPARK_LINUX__) 70 // Call thread_local dtors. 71 __cxa_thread_finalize(); 72 #endif 73 __funcs_on_exit(); 74 g_global_destroyed = true; 75 __libc_exit_fini(); 76 __stdio_exit(); 77 _Exit(code); 78 } 79