1 /* $NetBSD: quick_exit.c,v 1.1 2013/04/26 18:29:55 christos Exp $ */
4 * Copyright (c) 2011 David Chisnall
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $FreeBSD: src/lib/libc/stdlib/quick_exit.c,v 1.4 2012/11/17 01:49:41 svnexp Exp $
30 #include <sys/cdefs.h>
31 __RCSID("$NetBSD: quick_exit.c,v 1.1 2013/04/26 18:29:55 christos Exp $");
33 #include "namespace.h"
34 #include "reentrant.h"
39 * Linked list of quick exit handlers. This is simpler than the atexit()
40 * version, because it is not required to support C++ destructors or
41 * DSO-specific cleanups.
43 struct quick_exit_handler
{
44 struct quick_exit_handler
*next
;
45 void (*cleanup
)(void);
49 * Lock protecting the handlers list.
52 extern mutex_t __atexit_mutex
;
56 * Stack of cleanup handlers. These will be invoked in reverse order when
58 static struct quick_exit_handler
*handlers
;
61 at_quick_exit(void (*func
)(void))
63 struct quick_exit_handler
*h
;
65 h
= malloc(sizeof(*h
));
71 mutex_lock(&__atexit_mutex
);
76 mutex_unlock(&__atexit_mutex
);
82 quick_exit(int status
)
84 struct quick_exit_handler
*h
;
87 * XXX: The C++ spec requires us to call std::terminate if there is an
90 for (h
= handlers
; NULL
!= h
; h
= h
->next
)