1#define _ALL_SOURCE
2#include "libc.h"
3#include <stdlib.h>
4#include <threads.h>
5
6#define COUNT 32
7
8static void (*funcs[COUNT])(void);
9static int count;
10static mtx_t lock = MTX_INIT;
11
12void __funcs_on_quick_exit(void) {
13    void (*func)(void);
14    mtx_lock(&lock);
15    while (count > 0) {
16        func = funcs[--count];
17        mtx_unlock(&lock);
18        func();
19        mtx_lock(&lock);
20    }
21    mtx_unlock(&lock);
22}
23
24int at_quick_exit(void (*func)(void)) {
25    if (count == COUNT)
26        return -1;
27    mtx_lock(&lock);
28    funcs[count++] = func;
29    mtx_unlock(&lock);
30    return 0;
31}
32