exit.c 693 B

123456789101112131415161718192021222324252627282930313233
  1. #include <stdlib.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include "libc.h"
  5. #include "atomic.h"
  6. #include "syscall.h"
  7. static void dummy()
  8. {
  9. }
  10. /* __towrite.c and atexit.c override these */
  11. weak_alias(dummy, __funcs_on_exit);
  12. weak_alias(dummy, __fflush_on_exit);
  13. void exit(int code)
  14. {
  15. static int lock;
  16. /* If more than one thread calls exit, hang until _Exit ends it all */
  17. while (a_swap(&lock, 1)) __syscall(SYS_pause);
  18. /* Only do atexit & stdio flush if they were actually used */
  19. __funcs_on_exit();
  20. __fflush_on_exit();
  21. /* Destructor s**t is kept separate from atexit to avoid bloat */
  22. if (libc.fini) libc.fini();
  23. if (libc.ldso_fini) libc.ldso_fini();
  24. _Exit(code);
  25. for(;;);
  26. }