quick_exit.c revision 228323
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 228323 2011-12-07 16:12:54Z 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/** 43228322Stheraven * Lock protecting the handlers list. 44228322Stheraven */ 45228322Stheravenstatic pthread_mutex_t atexit_mutex = PTHREAD_MUTEX_INITIALIZER; 46228322Stheraven/** 47228322Stheraven * Stack of cleanup handlers. These will be invoked in reverse order when 48228322Stheraven */ 49228322Stheravenstatic struct quick_exit_handler *handlers; 50228322Stheraven 51228322Stheravenint 52228322Stheravenat_quick_exit(void (*func)(void)) 53228322Stheraven{ 54228322Stheraven struct quick_exit_handler *h = malloc(sizeof(struct quick_exit_handler)); 55228322Stheraven 56228323Stheraven if (NULL == h) 57228322Stheraven return 1; 58228322Stheraven h->cleanup = func; 59228322Stheraven pthread_mutex_lock(&atexit_mutex); 60228322Stheraven h->next = handlers; 61228322Stheraven handlers = h; 62228322Stheraven pthread_mutex_unlock(&atexit_mutex); 63228323Stheraven return (0); 64228322Stheraven} 65228322Stheraven 66228323Stheravenvoid 67228323Stheravenquick_exit(int status) 68228322Stheraven{ 69228323Stheraven struct quick_exit_handler *h; 70228323Stheraven 71228322Stheraven /* 72228322Stheraven * XXX: The C++ spec requires us to call std::terminate if there is an 73228322Stheraven * exception here. 74228322Stheraven */ 75228323Stheraven for (h = handlers; NULL != h; h = h->next) 76228322Stheraven h->cleanup(); 77228322Stheraven _Exit(status); 78228322Stheraven} 79