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