1
0

at_quick_exit.c 538 B

123456789101112131415161718192021222324252627282930313233
  1. #include <stdlib.h>
  2. #include "libc.h"
  3. #include "lock.h"
  4. #include "fork_impl.h"
  5. #define COUNT 32
  6. static void (*funcs[COUNT])(void);
  7. static int count;
  8. static volatile int lock[1];
  9. volatile int *const __at_quick_exit_lockptr = lock;
  10. void __funcs_on_quick_exit()
  11. {
  12. void (*func)(void);
  13. LOCK(lock);
  14. while (count > 0) {
  15. func = funcs[--count];
  16. UNLOCK(lock);
  17. func();
  18. LOCK(lock);
  19. }
  20. }
  21. int at_quick_exit(void (*func)(void))
  22. {
  23. int r = 0;
  24. LOCK(lock);
  25. if (count == 32) r = -1;
  26. else funcs[count++] = func;
  27. UNLOCK(lock);
  28. return r;
  29. }