quick_exit.c revision 228322
1228322Stheraven/*- 2228322Stheraven * Copyright (c) 2011 David Chisnall 3228322Stheraven * All rights reserved. 4228322Stheraven * 5228322Stheraven * Redistribution and use in source and binary forms, with or without 6228322Stheraven * modification, are permitted provided that the following conditions 7228322Stheraven * are met: 8228322Stheraven * 1. Redistributions of source code must retain the above copyright 9228322Stheraven * notice, this list of conditions and the following disclaimer. 10228322Stheraven * 2. Redistributions in binary form must reproduce the above copyright 11228322Stheraven * notice, this list of conditions and the following disclaimer in the 12228322Stheraven * documentation and/or other materials provided with the distribution. 13228322Stheraven * 14228322Stheraven * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15228322Stheraven * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16228322Stheraven * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17228322Stheraven * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18228322Stheraven * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19228322Stheraven * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20228322Stheraven * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21228322Stheraven * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22228322Stheraven * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23228322Stheraven * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24228322Stheraven * SUCH DAMAGE. 25228322Stheraven * 26228322Stheraven * $FreeBSD: head/lib/libc/stdlib/quick_exit.c 228322 2011-12-07 15:25:48Z theraven $ 27228322Stheraven */ 28228322Stheraven 29228322Stheraven#include <stdlib.h> 30228322Stheraven#include <pthread.h> 31228322Stheraven 32228322Stheraven/** 33228322Stheraven * Linked list of quick exit handlers. This is simpler than the atexit() 34228322Stheraven * version, because it is not required to support C++ destructors or 35228322Stheraven * DSO-specific cleanups. 36228322Stheraven */ 37228322Stheravenstruct quick_exit_handler { 38228322Stheraven struct quick_exit_handler *next; 39228322Stheraven void (*cleanup)(void); 40228322Stheraven}; 41228322Stheraven 42228322Stheraven__attribute((weak)) 43228322Stheravenvoid _ZSt9terminatev(void); 44228322Stheraven 45228322Stheraven/** 46228322Stheraven * Lock protecting the handlers list. 47228322Stheraven */ 48228322Stheravenstatic pthread_mutex_t atexit_mutex = PTHREAD_MUTEX_INITIALIZER; 49228322Stheraven/** 50228322Stheraven * Stack of cleanup handlers. These will be invoked in reverse order when 51228322Stheraven */ 52228322Stheravenstatic struct quick_exit_handler *handlers; 53228322Stheraven 54228322Stheravenint 55228322Stheravenat_quick_exit(void (*func)(void)) 56228322Stheraven{ 57228322Stheraven struct quick_exit_handler *h = malloc(sizeof(struct quick_exit_handler)); 58228322Stheraven 59228322Stheraven if (0 == h) { 60228322Stheraven return 1; 61228322Stheraven } 62228322Stheraven h->cleanup = func; 63228322Stheraven pthread_mutex_lock(&atexit_mutex); 64228322Stheraven h->next = handlers; 65228322Stheraven handlers = h; 66228322Stheraven pthread_mutex_unlock(&atexit_mutex); 67228322Stheraven return 0; 68228322Stheraven} 69228322Stheraven 70228322Stheravenvoid quick_exit(int status) 71228322Stheraven{ 72228322Stheraven /* 73228322Stheraven * XXX: The C++ spec requires us to call std::terminate if there is an 74228322Stheraven * exception here. 75228322Stheraven */ 76228322Stheraven for (struct quick_exit_handler *h = handlers ; NULL != h ; h = h->next) 77228322Stheraven { 78228322Stheraven h->cleanup(); 79228322Stheraven } 80228322Stheraven _Exit(status); 81228322Stheraven} 82