xref: /freebsd/lib/libthr/thread/thr_clean.c (revision f8bbbce458194ff4312c610d32a64ff4a3a71d45)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
48a16b7a1SPedro 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 
3337a6356bSDavid Xu #include "namespace.h"
34bb535300SJeff Roberson #include <signal.h>
35bb535300SJeff Roberson #include <errno.h>
36bb535300SJeff Roberson #include <stdlib.h>
37bb535300SJeff Roberson #include <pthread.h>
3837a6356bSDavid Xu #include "un-namespace.h"
39a091d823SDavid Xu 
40bb535300SJeff Roberson #include "thr_private.h"
41bb535300SJeff Roberson 
4283a07587SDavid Xu #undef pthread_cleanup_push
4383a07587SDavid Xu #undef pthread_cleanup_pop
4483a07587SDavid Xu 
4583a07587SDavid Xu /* old binary compatible interfaces */
46*0ab1bfc7SKonstantin Belousov __weak_reference(_thr_cleanup_pop, pthread_cleanup_pop);
47*0ab1bfc7SKonstantin Belousov __weak_reference(_thr_cleanup_pop, _pthread_cleanup_pop);
48*0ab1bfc7SKonstantin Belousov __weak_reference(_thr_cleanup_push, pthread_cleanup_push);
49*0ab1bfc7SKonstantin Belousov __weak_reference(_thr_cleanup_push, _pthread_cleanup_push);
50*0ab1bfc7SKonstantin Belousov 
51*0ab1bfc7SKonstantin Belousov /* help static linking when libc symbols have preference */
52*0ab1bfc7SKonstantin Belousov __weak_reference(__thr_cleanup_push_imp, __pthread_cleanup_push_imp);
53*0ab1bfc7SKonstantin Belousov __weak_reference(__thr_cleanup_pop_imp, __pthread_cleanup_pop_imp);
54bb535300SJeff Roberson 
55bb535300SJeff Roberson void
__thr_cleanup_push_imp(void (* routine)(void *),void * arg,struct _pthread_cleanup_info * info)56*0ab1bfc7SKonstantin Belousov __thr_cleanup_push_imp(void (*routine)(void *), void *arg,
5783a07587SDavid Xu     struct _pthread_cleanup_info *info)
58bb535300SJeff Roberson {
59a091d823SDavid Xu 	struct pthread	*curthread = _get_curthread();
6083a07587SDavid Xu 	struct pthread_cleanup *newbuf;
61bb535300SJeff Roberson 
6283a07587SDavid Xu 	newbuf = (void *)info;
6383a07587SDavid Xu 	newbuf->routine = routine;
6483a07587SDavid Xu 	newbuf->routine_arg = arg;
6583a07587SDavid Xu 	newbuf->onheap = 0;
6683a07587SDavid Xu 	newbuf->prev = curthread->cleanup;
6783a07587SDavid Xu 	curthread->cleanup = newbuf;
6883a07587SDavid Xu }
69bb535300SJeff Roberson 
7083a07587SDavid Xu void
__thr_cleanup_pop_imp(int execute)71*0ab1bfc7SKonstantin Belousov __thr_cleanup_pop_imp(int execute)
7283a07587SDavid Xu {
7383a07587SDavid Xu 	struct pthread	*curthread = _get_curthread();
7483a07587SDavid Xu 	struct pthread_cleanup *old;
7583a07587SDavid Xu 
7683a07587SDavid Xu 	if ((old = curthread->cleanup) != NULL) {
7783a07587SDavid Xu 		curthread->cleanup = old->prev;
7883a07587SDavid Xu 		if (execute)
7983a07587SDavid Xu 			old->routine(old->routine_arg);
8083a07587SDavid Xu 		if (old->onheap)
8183a07587SDavid Xu 			free(old);
8283a07587SDavid Xu 	}
8383a07587SDavid Xu }
8483a07587SDavid Xu 
8583a07587SDavid Xu void
_thr_cleanup_push(void (* routine)(void *),void * arg)86*0ab1bfc7SKonstantin Belousov _thr_cleanup_push(void (*routine)(void *), void *arg)
8783a07587SDavid Xu {
8883a07587SDavid Xu 	struct pthread	*curthread = _get_curthread();
8983a07587SDavid Xu 	struct pthread_cleanup *newbuf;
908be6abcdSDavid Xu #ifdef _PTHREAD_FORCED_UNWIND
918690b9f6SDavid Xu 	curthread->unwind_disabled = 1;
928be6abcdSDavid Xu #endif
9383a07587SDavid Xu 	if ((newbuf = (struct pthread_cleanup *)
949acf5917SPedro F. Giffuni 	    malloc(sizeof(struct pthread_cleanup))) != NULL) {
9583a07587SDavid Xu 		newbuf->routine = routine;
9683a07587SDavid Xu 		newbuf->routine_arg = arg;
9783a07587SDavid Xu 		newbuf->onheap = 1;
9883a07587SDavid Xu 		newbuf->prev = curthread->cleanup;
9983a07587SDavid Xu 		curthread->cleanup = newbuf;
100bb535300SJeff Roberson 	}
101bb535300SJeff Roberson }
102bb535300SJeff Roberson 
103bb535300SJeff Roberson void
_thr_cleanup_pop(int execute)104*0ab1bfc7SKonstantin Belousov _thr_cleanup_pop(int execute)
105bb535300SJeff Roberson {
10683a07587SDavid Xu 	__pthread_cleanup_pop_imp(execute);
107bb535300SJeff Roberson }
108