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: stable/11/lib/libc/stdlib/quick_exit.c 325712 2017-11-11 12:16:19Z kib $ 27228322Stheraven */ 28228322Stheraven 29325712Skib#include <sys/types.h> 30325712Skib#include <machine/atomic.h> 31228322Stheraven#include <stdlib.h> 32228322Stheraven#include <pthread.h> 33228322Stheraven 34228322Stheraven/** 35228322Stheraven * Linked list of quick exit handlers. This is simpler than the atexit() 36228322Stheraven * version, because it is not required to support C++ destructors or 37228322Stheraven * DSO-specific cleanups. 38228322Stheraven */ 39228322Stheravenstruct quick_exit_handler { 40228322Stheraven struct quick_exit_handler *next; 41228322Stheraven void (*cleanup)(void); 42228322Stheraven}; 43228322Stheraven 44228322Stheraven/** 45228322Stheraven * Lock protecting the handlers list. 46228322Stheraven */ 47228322Stheravenstatic pthread_mutex_t atexit_mutex = PTHREAD_MUTEX_INITIALIZER; 48228322Stheraven/** 49228322Stheraven * Stack of cleanup handlers. These will be invoked in reverse order when 50228322Stheraven */ 51228322Stheravenstatic struct quick_exit_handler *handlers; 52228322Stheraven 53228322Stheravenint 54228322Stheravenat_quick_exit(void (*func)(void)) 55228322Stheraven{ 56228528Stheraven struct quick_exit_handler *h; 57228528Stheraven 58228528Stheraven h = malloc(sizeof(*h)); 59228322Stheraven 60228323Stheraven if (NULL == h) 61228528Stheraven return (1); 62228322Stheraven h->cleanup = func; 63228322Stheraven pthread_mutex_lock(&atexit_mutex); 64228322Stheraven h->next = handlers; 65325712Skib __compiler_membar(); 66228322Stheraven handlers = h; 67228322Stheraven pthread_mutex_unlock(&atexit_mutex); 68228323Stheraven return (0); 69228322Stheraven} 70228322Stheraven 71228323Stheravenvoid 72228323Stheravenquick_exit(int status) 73228322Stheraven{ 74228323Stheraven struct quick_exit_handler *h; 75228323Stheraven 76228322Stheraven /* 77228322Stheraven * XXX: The C++ spec requires us to call std::terminate if there is an 78228322Stheraven * exception here. 79228322Stheraven */ 80325712Skib for (h = handlers; NULL != h; h = h->next) { 81325712Skib __compiler_membar(); 82228322Stheraven h->cleanup(); 83325712Skib } 84228322Stheraven _Exit(status); 85228322Stheraven} 86