113546Sjulian/*
213546Sjulian * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>.
313546Sjulian * All rights reserved.
413546Sjulian *
513546Sjulian * Redistribution and use in source and binary forms, with or without
613546Sjulian * modification, are permitted provided that the following conditions
713546Sjulian * are met:
813546Sjulian * 1. Redistributions of source code must retain the above copyright
913546Sjulian *    notice, this list of conditions and the following disclaimer.
1013546Sjulian * 2. Redistributions in binary form must reproduce the above copyright
1113546Sjulian *    notice, this list of conditions and the following disclaimer in the
1213546Sjulian *    documentation and/or other materials provided with the distribution.
13165967Simp * 3. Neither the name of the author nor the names of any co-contributors
1413546Sjulian *    may be used to endorse or promote products derived from this software
1513546Sjulian *    without specific prior written permission.
1613546Sjulian *
1713546Sjulian * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
1813546Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1913546Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2049439Sdeischen * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2113546Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2213546Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2313546Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2413546Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2513546Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2613546Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2713546Sjulian * SUCH DAMAGE.
2813546Sjulian *
2950476Speter * $FreeBSD$
3013546Sjulian */
31174112Sdeischen
32174112Sdeischen#include "namespace.h"
3313546Sjulian#include <signal.h>
3413546Sjulian#include <errno.h>
3513546Sjulian#include <stdlib.h>
3613546Sjulian#include <pthread.h>
37174112Sdeischen#include "un-namespace.h"
38103388Smini#include "thr_private.h"
3913546Sjulian
4075369Sdeischen__weak_reference(_pthread_cleanup_push, pthread_cleanup_push);
4175369Sdeischen__weak_reference(_pthread_cleanup_pop, pthread_cleanup_pop);
4271581Sdeischen
4322315Sjulianvoid
4471581Sdeischen_pthread_cleanup_push(void (*routine) (void *), void *routine_arg)
4513546Sjulian{
4671581Sdeischen	struct pthread	*curthread = _get_curthread();
4713546Sjulian	struct pthread_cleanup *new;
4813546Sjulian
49113658Sdeischen	if ((new = (struct pthread_cleanup *)
50113658Sdeischen	    malloc(sizeof(struct pthread_cleanup))) != NULL) {
5113546Sjulian		new->routine = routine;
5213546Sjulian		new->routine_arg = routine_arg;
53139023Sdeischen		new->onstack = 0;
5471581Sdeischen		new->next = curthread->cleanup;
5513546Sjulian
5671581Sdeischen		curthread->cleanup = new;
5713546Sjulian	}
5813546Sjulian}
5913546Sjulian
6013546Sjulianvoid
6171581Sdeischen_pthread_cleanup_pop(int execute)
6213546Sjulian{
6371581Sdeischen	struct pthread	*curthread = _get_curthread();
6413546Sjulian	struct pthread_cleanup *old;
6513546Sjulian
6671581Sdeischen	if ((old = curthread->cleanup) != NULL) {
6771581Sdeischen		curthread->cleanup = old->next;
6813546Sjulian		if (execute) {
6913546Sjulian			old->routine(old->routine_arg);
7013546Sjulian		}
71139023Sdeischen		if (old->onstack == 0)
72139023Sdeischen			free(old);
7313546Sjulian	}
7413546Sjulian}
75