diff --git a/src/kernel/libroot/posix/stdlib/exit.c b/src/kernel/libroot/posix/stdlib/exit.c index d6eb2b06ec..1dab60e976 100644 --- a/src/kernel/libroot/posix/stdlib/exit.c +++ b/src/kernel/libroot/posix/stdlib/exit.c @@ -14,21 +14,20 @@ * */ + #include #include #include #include #include -// ToDo: move this puppy to a more standard location #include "../stdio/local.h" +//#include "stdio_private.h" extern void _thread_do_exit_notification(void); - -static void (*_Exit_Stack[ATEXIT_MAX])(void) = {0}; -static int _Exit_SP = 0; - +static void (*_gExitStack[ATEXIT_MAX])(void) = {0}; +static int32 _gExitStackIndex = 0; void @@ -43,13 +42,13 @@ int atexit(void (*func)(void)) { // push the function pointer onto the exit stack - - if (_Exit_SP < ATEXIT_MAX) { - _Exit_Stack[_Exit_SP++] = func; - return 0; - } - - return -1; + int32 index = atomic_add(&_gExitStackIndex, 1); + + if (index >= ATEXIT_MAX) + return -1; + + _gExitStack[index] = func; + return 0; } @@ -60,12 +59,13 @@ exit(int status) _thread_do_exit_notification(); // unwind the exit stack, calling the registered functions - while (_Exit_SP > 0) - (*_Exit_Stack[--_Exit_SP])(); - + while (_gExitStackIndex-- > 0) + (*_gExitStack[_gExitStackIndex])(); + // close all open files - (*__cleanup)(); - + _IO_cleanup(); + // exit with status code sys_exit(status); } +