Debian Codegraph @

libc-top-half/musl/src/exit/at_quick_exit.c

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