xref: /freebsd/lib/libthr/thread/thr_clean.c (revision 8aac90f18aef7c9eea906c3ff9a001ca7b94f375)
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 "namespace.h"
34 #include <signal.h>
35 #include <errno.h>
36 #include <stdlib.h>
37 #include <pthread.h>
38 #include "un-namespace.h"
39 
40 #include "thr_private.h"
41 
42 #undef pthread_cleanup_push
43 #undef pthread_cleanup_pop
44 
45 /* old binary compatible interfaces */
46 __weak_reference(_thr_cleanup_pop, pthread_cleanup_pop);
47 __weak_reference(_thr_cleanup_pop, _pthread_cleanup_pop);
48 __weak_reference(_thr_cleanup_push, pthread_cleanup_push);
49 __weak_reference(_thr_cleanup_push, _pthread_cleanup_push);
50 
51 /* help static linking when libc symbols have preference */
52 __weak_reference(__thr_cleanup_push_imp, __pthread_cleanup_push_imp);
53 __weak_reference(__thr_cleanup_pop_imp, __pthread_cleanup_pop_imp);
54 
55 void
56 __thr_cleanup_push_imp(void (*routine)(void *), void *arg,
57     struct _pthread_cleanup_info *info)
58 {
59 	struct pthread	*curthread = _get_curthread();
60 	struct pthread_cleanup *newbuf;
61 
62 	newbuf = (void *)info;
63 	newbuf->routine = routine;
64 	newbuf->routine_arg = arg;
65 	newbuf->onheap = 0;
66 	newbuf->prev = curthread->cleanup;
67 	curthread->cleanup = newbuf;
68 }
69 
70 void
71 __thr_cleanup_pop_imp(int execute)
72 {
73 	struct pthread	*curthread = _get_curthread();
74 	struct pthread_cleanup *old;
75 
76 	if ((old = curthread->cleanup) != NULL) {
77 		curthread->cleanup = old->prev;
78 		if (execute)
79 			old->routine(old->routine_arg);
80 		if (old->onheap)
81 			free(old);
82 	}
83 }
84 
85 void
86 _thr_cleanup_push(void (*routine)(void *), void *arg)
87 {
88 	struct pthread	*curthread = _get_curthread();
89 	struct pthread_cleanup *newbuf;
90 #ifdef _PTHREAD_FORCED_UNWIND
91 	curthread->unwind_disabled = 1;
92 #endif
93 	if ((newbuf = (struct pthread_cleanup *)
94 	    malloc(sizeof(struct pthread_cleanup))) != NULL) {
95 		newbuf->routine = routine;
96 		newbuf->routine_arg = arg;
97 		newbuf->onheap = 1;
98 		newbuf->prev = curthread->cleanup;
99 		curthread->cleanup = newbuf;
100 	}
101 }
102 
103 void
104 _thr_cleanup_pop(int execute)
105 {
106 	__pthread_cleanup_pop_imp(execute);
107 }
108