xref: /freebsd/lib/libthr/thread/thr_clean.c (revision 8a16b7a18f5d0b031f09832fd7752fba717e2a97)
1*8a16b7a1SPedro F. Giffuni /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
4*8a16b7a1SPedro F. Giffuni 
5bb535300SJeff Roberson  * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>.
6bb535300SJeff Roberson  * All rights reserved.
7bb535300SJeff Roberson  *
8bb535300SJeff Roberson  * Redistribution and use in source and binary forms, with or without
9bb535300SJeff Roberson  * modification, are permitted provided that the following conditions
10bb535300SJeff Roberson  * are met:
11bb535300SJeff Roberson  * 1. Redistributions of source code must retain the above copyright
12bb535300SJeff Roberson  *    notice, this list of conditions and the following disclaimer.
13bb535300SJeff Roberson  * 2. Redistributions in binary form must reproduce the above copyright
14bb535300SJeff Roberson  *    notice, this list of conditions and the following disclaimer in the
15bb535300SJeff Roberson  *    documentation and/or other materials provided with the distribution.
16fed32d75SWarner Losh  * 3. Neither the name of the author nor the names of any co-contributors
17bb535300SJeff Roberson  *    may be used to endorse or promote products derived from this software
18bb535300SJeff Roberson  *    without specific prior written permission.
19bb535300SJeff Roberson  *
20bb535300SJeff Roberson  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
21bb535300SJeff Roberson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22bb535300SJeff Roberson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23bb535300SJeff Roberson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24bb535300SJeff Roberson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25bb535300SJeff Roberson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26bb535300SJeff Roberson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27bb535300SJeff Roberson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28bb535300SJeff Roberson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29bb535300SJeff Roberson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30bb535300SJeff Roberson  * SUCH DAMAGE.
31bb535300SJeff Roberson  */
32a091d823SDavid Xu 
3332793011SKonstantin Belousov #include <sys/cdefs.h>
3432793011SKonstantin Belousov __FBSDID("$FreeBSD$");
3532793011SKonstantin Belousov 
3637a6356bSDavid Xu #include "namespace.h"
37bb535300SJeff Roberson #include <signal.h>
38bb535300SJeff Roberson #include <errno.h>
39bb535300SJeff Roberson #include <stdlib.h>
40bb535300SJeff Roberson #include <pthread.h>
4137a6356bSDavid Xu #include "un-namespace.h"
42a091d823SDavid Xu 
43bb535300SJeff Roberson #include "thr_private.h"
44bb535300SJeff Roberson 
4583a07587SDavid Xu #undef pthread_cleanup_push
4683a07587SDavid Xu #undef pthread_cleanup_pop
4783a07587SDavid Xu 
4883a07587SDavid Xu /* old binary compatible interfaces */
49bb535300SJeff Roberson __weak_reference(_pthread_cleanup_push, pthread_cleanup_push);
50bb535300SJeff Roberson __weak_reference(_pthread_cleanup_pop, pthread_cleanup_pop);
51bb535300SJeff Roberson 
52bb535300SJeff Roberson void
5383a07587SDavid Xu __pthread_cleanup_push_imp(void (*routine)(void *), void *arg,
5483a07587SDavid Xu 	struct _pthread_cleanup_info *info)
55bb535300SJeff Roberson {
56a091d823SDavid Xu 	struct pthread	*curthread = _get_curthread();
5783a07587SDavid Xu 	struct pthread_cleanup *newbuf;
58bb535300SJeff Roberson 
5983a07587SDavid Xu 	newbuf = (void *)info;
6083a07587SDavid Xu 	newbuf->routine = routine;
6183a07587SDavid Xu 	newbuf->routine_arg = arg;
6283a07587SDavid Xu 	newbuf->onheap = 0;
6383a07587SDavid Xu 	newbuf->prev = curthread->cleanup;
6483a07587SDavid Xu 	curthread->cleanup = newbuf;
6583a07587SDavid Xu }
66bb535300SJeff Roberson 
6783a07587SDavid Xu void
6883a07587SDavid Xu __pthread_cleanup_pop_imp(int execute)
6983a07587SDavid Xu {
7083a07587SDavid Xu 	struct pthread	*curthread = _get_curthread();
7183a07587SDavid Xu 	struct pthread_cleanup *old;
7283a07587SDavid Xu 
7383a07587SDavid Xu 	if ((old = curthread->cleanup) != NULL) {
7483a07587SDavid Xu 		curthread->cleanup = old->prev;
7583a07587SDavid Xu 		if (execute)
7683a07587SDavid Xu 			old->routine(old->routine_arg);
7783a07587SDavid Xu 		if (old->onheap)
7883a07587SDavid Xu 			free(old);
7983a07587SDavid Xu 	}
8083a07587SDavid Xu }
8183a07587SDavid Xu 
8283a07587SDavid Xu void
8383a07587SDavid Xu _pthread_cleanup_push(void (*routine) (void *), void *arg)
8483a07587SDavid Xu {
8583a07587SDavid Xu 	struct pthread	*curthread = _get_curthread();
8683a07587SDavid Xu 	struct pthread_cleanup *newbuf;
878be6abcdSDavid Xu #ifdef _PTHREAD_FORCED_UNWIND
888690b9f6SDavid Xu 	curthread->unwind_disabled = 1;
898be6abcdSDavid Xu #endif
9083a07587SDavid Xu 	if ((newbuf = (struct pthread_cleanup *)
919acf5917SPedro F. Giffuni 	    malloc(sizeof(struct pthread_cleanup))) != NULL) {
9283a07587SDavid Xu 		newbuf->routine = routine;
9383a07587SDavid Xu 		newbuf->routine_arg = arg;
9483a07587SDavid Xu 		newbuf->onheap = 1;
9583a07587SDavid Xu 		newbuf->prev = curthread->cleanup;
9683a07587SDavid Xu 		curthread->cleanup = newbuf;
97bb535300SJeff Roberson 	}
98bb535300SJeff Roberson }
99bb535300SJeff Roberson 
100bb535300SJeff Roberson void
101bb535300SJeff Roberson _pthread_cleanup_pop(int execute)
102bb535300SJeff Roberson {
10383a07587SDavid Xu 	__pthread_cleanup_pop_imp(execute);
104bb535300SJeff Roberson }
105