xref: /freebsd/lib/libthr/thread/thr_clean.c (revision 8be6abcdc6d278b89e90980c3fc4d97c0301f9c9)
1bb535300SJeff Roberson /*
2bb535300SJeff Roberson  * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>.
3bb535300SJeff Roberson  * All rights reserved.
4bb535300SJeff Roberson  *
5bb535300SJeff Roberson  * Redistribution and use in source and binary forms, with or without
6bb535300SJeff Roberson  * modification, are permitted provided that the following conditions
7bb535300SJeff Roberson  * are met:
8bb535300SJeff Roberson  * 1. Redistributions of source code must retain the above copyright
9bb535300SJeff Roberson  *    notice, this list of conditions and the following disclaimer.
10bb535300SJeff Roberson  * 2. Redistributions in binary form must reproduce the above copyright
11bb535300SJeff Roberson  *    notice, this list of conditions and the following disclaimer in the
12bb535300SJeff Roberson  *    documentation and/or other materials provided with the distribution.
13fed32d75SWarner Losh  * 3. Neither the name of the author nor the names of any co-contributors
14bb535300SJeff Roberson  *    may be used to endorse or promote products derived from this software
15bb535300SJeff Roberson  *    without specific prior written permission.
16bb535300SJeff Roberson  *
17bb535300SJeff Roberson  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
18bb535300SJeff Roberson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19bb535300SJeff Roberson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20bb535300SJeff Roberson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21bb535300SJeff Roberson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22bb535300SJeff Roberson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23bb535300SJeff Roberson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24bb535300SJeff Roberson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25bb535300SJeff Roberson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26bb535300SJeff Roberson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27bb535300SJeff Roberson  * SUCH DAMAGE.
28bb535300SJeff Roberson  *
29bb535300SJeff Roberson  * $FreeBSD$
30bb535300SJeff Roberson  */
31a091d823SDavid Xu 
3237a6356bSDavid Xu #include "namespace.h"
33bb535300SJeff Roberson #include <signal.h>
34bb535300SJeff Roberson #include <errno.h>
35bb535300SJeff Roberson #include <stdlib.h>
36bb535300SJeff Roberson #include <pthread.h>
3737a6356bSDavid Xu #include "un-namespace.h"
38a091d823SDavid Xu 
39bb535300SJeff Roberson #include "thr_private.h"
40bb535300SJeff Roberson 
4183a07587SDavid Xu #undef pthread_cleanup_push
4283a07587SDavid Xu #undef pthread_cleanup_pop
4383a07587SDavid Xu 
4483a07587SDavid Xu /* old binary compatible interfaces */
45bb535300SJeff Roberson __weak_reference(_pthread_cleanup_push, pthread_cleanup_push);
46bb535300SJeff Roberson __weak_reference(_pthread_cleanup_pop, pthread_cleanup_pop);
47bb535300SJeff Roberson 
48bb535300SJeff Roberson void
4983a07587SDavid Xu __pthread_cleanup_push_imp(void (*routine)(void *), void *arg,
5083a07587SDavid Xu 	struct _pthread_cleanup_info *info)
51bb535300SJeff Roberson {
52a091d823SDavid Xu 	struct pthread	*curthread = _get_curthread();
5383a07587SDavid Xu 	struct pthread_cleanup *newbuf;
54bb535300SJeff Roberson 
5583a07587SDavid Xu 	newbuf = (void *)info;
5683a07587SDavid Xu 	newbuf->routine = routine;
5783a07587SDavid Xu 	newbuf->routine_arg = arg;
5883a07587SDavid Xu 	newbuf->onheap = 0;
5983a07587SDavid Xu 	newbuf->prev = curthread->cleanup;
6083a07587SDavid Xu 	curthread->cleanup = newbuf;
6183a07587SDavid Xu }
62bb535300SJeff Roberson 
6383a07587SDavid Xu void
6483a07587SDavid Xu __pthread_cleanup_pop_imp(int execute)
6583a07587SDavid Xu {
6683a07587SDavid Xu 	struct pthread	*curthread = _get_curthread();
6783a07587SDavid Xu 	struct pthread_cleanup *old;
6883a07587SDavid Xu 
6983a07587SDavid Xu 	if ((old = curthread->cleanup) != NULL) {
7083a07587SDavid Xu 		curthread->cleanup = old->prev;
7183a07587SDavid Xu 		if (execute)
7283a07587SDavid Xu 			old->routine(old->routine_arg);
7383a07587SDavid Xu 		if (old->onheap)
7483a07587SDavid Xu 			free(old);
7583a07587SDavid Xu 	}
7683a07587SDavid Xu }
7783a07587SDavid Xu 
7883a07587SDavid Xu void
7983a07587SDavid Xu _pthread_cleanup_push(void (*routine) (void *), void *arg)
8083a07587SDavid Xu {
8183a07587SDavid Xu 	struct pthread	*curthread = _get_curthread();
8283a07587SDavid Xu 	struct pthread_cleanup *newbuf;
83*8be6abcdSDavid Xu #ifdef _PTHREAD_FORCED_UNWIND
848690b9f6SDavid Xu 	curthread->unwind_disabled = 1;
85*8be6abcdSDavid Xu #endif
8683a07587SDavid Xu 	if ((newbuf = (struct pthread_cleanup *)
8783a07587SDavid Xu 	    malloc(sizeof(struct _pthread_cleanup_info))) != NULL) {
8883a07587SDavid Xu 		newbuf->routine = routine;
8983a07587SDavid Xu 		newbuf->routine_arg = arg;
9083a07587SDavid Xu 		newbuf->onheap = 1;
9183a07587SDavid Xu 		newbuf->prev = curthread->cleanup;
9283a07587SDavid Xu 		curthread->cleanup = newbuf;
93bb535300SJeff Roberson 	}
94bb535300SJeff Roberson }
95bb535300SJeff Roberson 
96bb535300SJeff Roberson void
97bb535300SJeff Roberson _pthread_cleanup_pop(int execute)
98bb535300SJeff Roberson {
9983a07587SDavid Xu 	__pthread_cleanup_pop_imp(execute);
100bb535300SJeff Roberson }
101