summaryrefslogtreecommitdiff
path: root/src/exit/at_quick_exit.c
blob: ac28dfd93a9f23f41f2b20a5ce741fe29113d464 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdlib.h>
#include "libc.h"

#define COUNT 32

static void (*funcs[COUNT])(void);
static int count;
static volatile int lock[2];

void __funcs_on_quick_exit()
{
	void (*func)(void);
	LOCK(lock);
	while (count > 0) {
		func = funcs[--count];
		UNLOCK(lock);
		func();
		LOCK(lock);
	}
}

int at_quick_exit(void (*func)(void))
{
	int r = 0;
	LOCK(lock);
	if (count == 32) r = -1;
	else funcs[count++] = func;
	UNLOCK(lock);
	return r;
}