1570af302Sopenharmony_ci#include <stdlib.h>
2570af302Sopenharmony_ci#include "libc.h"
3570af302Sopenharmony_ci#include "lock.h"
4570af302Sopenharmony_ci#include "fork_impl.h"
5570af302Sopenharmony_ci
6570af302Sopenharmony_ci#define COUNT 32
7570af302Sopenharmony_ci
8570af302Sopenharmony_cistatic void (*funcs[COUNT])(void);
9570af302Sopenharmony_cistatic int count;
10570af302Sopenharmony_cistatic volatile int lock[1];
11570af302Sopenharmony_civolatile int *const __at_quick_exit_lockptr = lock;
12570af302Sopenharmony_ci
13570af302Sopenharmony_civoid __funcs_on_quick_exit()
14570af302Sopenharmony_ci{
15570af302Sopenharmony_ci	void (*func)(void);
16570af302Sopenharmony_ci	LOCK(lock);
17570af302Sopenharmony_ci	while (count > 0) {
18570af302Sopenharmony_ci		func = funcs[--count];
19570af302Sopenharmony_ci		UNLOCK(lock);
20570af302Sopenharmony_ci		func();
21570af302Sopenharmony_ci		LOCK(lock);
22570af302Sopenharmony_ci	}
23570af302Sopenharmony_ci}
24570af302Sopenharmony_ci
25570af302Sopenharmony_ciint at_quick_exit(void (*func)(void))
26570af302Sopenharmony_ci{
27570af302Sopenharmony_ci	int r = 0;
28570af302Sopenharmony_ci	LOCK(lock);
29570af302Sopenharmony_ci	if (count == 32) r = -1;
30570af302Sopenharmony_ci	else funcs[count++] = func;
31570af302Sopenharmony_ci	UNLOCK(lock);
32570af302Sopenharmony_ci	return r;
33570af302Sopenharmony_ci}
34