From 6e5e43433e4dca8ce7d670b8a19414582fef2843 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Sat, 3 May 2003 14:47:56 +0000 Subject: [PATCH] atexit() is now thread-safe. Now calls _IO_cleanup() instead of __cleanup() to be able to be compiled against glibc stdio as well. Renamed variables to be much nicer. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@3148 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/kernel/libroot/posix/stdlib/exit.c | 34 +++++++++++++------------- 1 file changed, 17 insertions(+), 17 deletions(-) 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); } +