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