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
This commit is contained in:
parent
f57208999e
commit
6e5e43433e
@ -14,21 +14,20 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <syscalls.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
#include <limits.h>
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user