mq_notify: block all (application) signals in the worker thread
[musl] / src / exit / exit.c
index fc29148..a6869b3 100644 (file)
@@ -1,33 +1,33 @@
 #include <stdlib.h>
-#include <unistd.h>
-#include <stdio.h>
+#include <stdint.h>
 #include "libc.h"
-#include "atomic.h"
-#include "syscall.h"
 
 static void dummy()
 {
 }
 
-/* __towrite.c and atexit.c override these */
+/* atexit.c and __stdio_exit.c override these. the latter is linked
+ * as a consequence of linking either __toread.c or __towrite.c. */
 weak_alias(dummy, __funcs_on_exit);
-weak_alias(dummy, __fflush_on_exit);
+weak_alias(dummy, __stdio_exit);
+weak_alias(dummy, _fini);
 
-void exit(int code)
+extern weak hidden void (*const __fini_array_start)(void), (*const __fini_array_end)(void);
+
+static void libc_exit_fini(void)
 {
-       static int lock;
+       uintptr_t a = (uintptr_t)&__fini_array_end;
+       for (; a>(uintptr_t)&__fini_array_start; a-=sizeof(void(*)()))
+               (*(void (**)())(a-sizeof(void(*)())))();
+       _fini();
+}
 
-       /* If more than one thread calls exit, hang until _Exit ends it all */
-       while (a_swap(&lock, 1)) __syscall(SYS_pause);
+weak_alias(libc_exit_fini, __libc_exit_fini);
 
-       /* Only do atexit & stdio flush if they were actually used */
+_Noreturn void exit(int code)
+{
        __funcs_on_exit();
-       __fflush_on_exit();
-
-       /* Destructor s**t is kept separate from atexit to avoid bloat */
-       if (libc.fini) libc.fini();
-       if (libc.ldso_fini) libc.ldso_fini();
-
+       __libc_exit_fini();
+       __stdio_exit();
        _Exit(code);
-       for(;;);
 }