#include #include "atomic.h" #include "libc.h" #define COUNT 32 static void (*funcs[COUNT])(void); static volatile int count; void __funcs_on_quick_exit() { void (*func)(void); /* No need for atomics here, we are alone. */ int total = count; /* The counter might have been overrun at some time. */ if (total > 32) total = 32; else count = 32; while (total > 0) { func = funcs[--total]; func(); } } int at_quick_exit(void (*func)(void)) { int pos = a_fetch_add(&count, 1); if (pos >= 32) { if (pos > INT_MAX/2) pos = 32; return -1; } funcs[pos] = func; /* make sure that the effect is visible to everybody */ a_barrier(); return 0; }