1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 5 * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the author nor the names of any co-contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include "namespace.h" 37 #include <signal.h> 38 #include <errno.h> 39 #include <stdlib.h> 40 #include <pthread.h> 41 #include "un-namespace.h" 42 43 #include "thr_private.h" 44 45 #undef pthread_cleanup_push 46 #undef pthread_cleanup_pop 47 48 /* old binary compatible interfaces */ 49 __weak_reference(_pthread_cleanup_push, pthread_cleanup_push); 50 __weak_reference(_pthread_cleanup_pop, pthread_cleanup_pop); 51 52 void 53 __pthread_cleanup_push_imp(void (*routine)(void *), void *arg, 54 struct _pthread_cleanup_info *info) 55 { 56 struct pthread *curthread = _get_curthread(); 57 struct pthread_cleanup *newbuf; 58 59 newbuf = (void *)info; 60 newbuf->routine = routine; 61 newbuf->routine_arg = arg; 62 newbuf->onheap = 0; 63 newbuf->prev = curthread->cleanup; 64 curthread->cleanup = newbuf; 65 } 66 67 void 68 __pthread_cleanup_pop_imp(int execute) 69 { 70 struct pthread *curthread = _get_curthread(); 71 struct pthread_cleanup *old; 72 73 if ((old = curthread->cleanup) != NULL) { 74 curthread->cleanup = old->prev; 75 if (execute) 76 old->routine(old->routine_arg); 77 if (old->onheap) 78 free(old); 79 } 80 } 81 82 void 83 _pthread_cleanup_push(void (*routine) (void *), void *arg) 84 { 85 struct pthread *curthread = _get_curthread(); 86 struct pthread_cleanup *newbuf; 87 #ifdef _PTHREAD_FORCED_UNWIND 88 curthread->unwind_disabled = 1; 89 #endif 90 if ((newbuf = (struct pthread_cleanup *) 91 malloc(sizeof(struct pthread_cleanup))) != NULL) { 92 newbuf->routine = routine; 93 newbuf->routine_arg = arg; 94 newbuf->onheap = 1; 95 newbuf->prev = curthread->cleanup; 96 curthread->cleanup = newbuf; 97 } 98 } 99 100 void 101 _pthread_cleanup_pop(int execute) 102 { 103 __pthread_cleanup_pop_imp(execute); 104 } 105