From b5289fd749bc4d2637610d712f8fe650f214f1d5 Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Sat, 25 Aug 2012 22:49:47 -0400 Subject: [PATCH] add c11 quick_exit and at_quick_exit functions --- include/stdlib.h | 2 ++ src/exit/at_quick_exit.c | 29 +++++++++++++++++++++++++++++ src/exit/quick_exit.c | 15 +++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 src/exit/at_quick_exit.c create mode 100644 src/exit/quick_exit.c diff --git a/include/stdlib.h b/include/stdlib.h index 0a8de866..c1d93e71 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -43,6 +43,8 @@ void abort (void); int atexit (void (*) (void)); void exit (int); void _Exit (int); +int at_quick_exit (void (*) (void)); +void quick_exit (int); char *getenv (const char *); diff --git a/src/exit/at_quick_exit.c b/src/exit/at_quick_exit.c new file mode 100644 index 00000000..85c3d26e --- /dev/null +++ b/src/exit/at_quick_exit.c @@ -0,0 +1,29 @@ +#include +#include "libc.h" + +#define COUNT 32 + +static void (*funcs[COUNT])(void); +static int count; +static 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)) +{ + if (count == 32) return -1; + LOCK(lock); + funcs[count++] = func; + UNLOCK(lock); + return 0; +} diff --git a/src/exit/quick_exit.c b/src/exit/quick_exit.c new file mode 100644 index 00000000..18d5288a --- /dev/null +++ b/src/exit/quick_exit.c @@ -0,0 +1,15 @@ +#include +#include "syscall.h" +#include "atomic.h" +#include "libc.h" + +static void dummy() { } +weak_alias(dummy, __funcs_on_quick_exit); + +void quick_exit(int code) +{ + static int lock; + while (a_swap(&lock, 1)) __syscall(SYS_pause); + __funcs_on_quick_exit(); + _Exit(code); +} -- 2.25.1