thr_clean.c revision 157457
1219820Sjeff/*
2219820Sjeff * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>.
3219820Sjeff * All rights reserved.
4219820Sjeff *
5219820Sjeff * Redistribution and use in source and binary forms, with or without
6219820Sjeff * modification, are permitted provided that the following conditions
7219820Sjeff * are met:
8219820Sjeff * 1. Redistributions of source code must retain the above copyright
9219820Sjeff *    notice, this list of conditions and the following disclaimer.
10219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright
11219820Sjeff *    notice, this list of conditions and the following disclaimer in the
12219820Sjeff *    documentation and/or other materials provided with the distribution.
13219820Sjeff * 3. All advertising materials mentioning features or use of this software
14219820Sjeff *    must display the following acknowledgement:
15219820Sjeff *	This product includes software developed by John Birrell.
16219820Sjeff * 4. Neither the name of the author nor the names of any co-contributors
17219820Sjeff *    may be used to endorse or promote products derived from this software
18219820Sjeff *    without specific prior written permission.
19219820Sjeff *
20219820Sjeff * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
21219820Sjeff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22219820Sjeff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23219820Sjeff * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24219820Sjeff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25219820Sjeff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26219820Sjeff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27219820Sjeff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28219820Sjeff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29219820Sjeff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30219820Sjeff * SUCH DAMAGE.
31219820Sjeff *
32219820Sjeff * $FreeBSD: head/lib/libthr/thread/thr_clean.c 157457 2006-04-04 02:57:49Z davidxu $
33219820Sjeff */
34219820Sjeff
35219820Sjeff#include "namespace.h"
36219820Sjeff#include <signal.h>
37219820Sjeff#include <errno.h>
38219820Sjeff#include <stdlib.h>
39219820Sjeff#include <pthread.h>
40219820Sjeff#include "un-namespace.h"
41219820Sjeff
42219820Sjeff#include "thr_private.h"
43219820Sjeff
44219820Sjeff__weak_reference(_pthread_cleanup_push, pthread_cleanup_push);
45219820Sjeff__weak_reference(_pthread_cleanup_pop, pthread_cleanup_pop);
46219820Sjeff
47219820Sjeffvoid
48219820Sjeff_pthread_cleanup_push(void (*routine) (void *), void *routine_arg)
49219820Sjeff{
50219820Sjeff	struct pthread	*curthread = _get_curthread();
51219820Sjeff	struct pthread_cleanup *new;
52219820Sjeff
53219820Sjeff	if ((new = (struct pthread_cleanup *)
54219820Sjeff	    malloc(sizeof(struct pthread_cleanup))) != NULL) {
55219820Sjeff		new->routine = routine;
56219820Sjeff		new->routine_arg = routine_arg;
57219820Sjeff		new->onstack = 0;
58219820Sjeff		new->next = curthread->cleanup;
59219820Sjeff
60219820Sjeff		curthread->cleanup = new;
61219820Sjeff	}
62219820Sjeff}
63219820Sjeff
64219820Sjeffvoid
65219820Sjeff_pthread_cleanup_pop(int execute)
66219820Sjeff{
67219820Sjeff	struct pthread	*curthread = _get_curthread();
68219820Sjeff	struct pthread_cleanup *old;
69219820Sjeff
70219820Sjeff	if ((old = curthread->cleanup) != NULL) {
71219820Sjeff		curthread->cleanup = old->next;
72219820Sjeff		if (execute) {
73219820Sjeff			old->routine(old->routine_arg);
74219820Sjeff		}
75219820Sjeff		if (old->onstack == 0)
76219820Sjeff			free(old);
77219820Sjeff	}
78219820Sjeff}
79219820Sjeff