127837Sdavidn/*
266830Sobrien * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
366830Sobrien * All rights reserved.
466830Sobrien *
566830Sobrien * Redistribution and use in source and binary forms, with or without
666830Sobrien * modification, are permitted provided that the following conditions
766830Sobrien * are met:
866830Sobrien * 1. Redistributions of source code must retain the above copyright
966830Sobrien *    notice, this list of conditions and the following disclaimer.
1066830Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1166830Sobrien *    notice, this list of conditions and the following disclaimer in the
1266830Sobrien *    documentation and/or other materials provided with the distribution.
1366830Sobrien * 3. Neither the name of the author nor the names of any co-contributors
1466830Sobrien *    may be used to endorse or promote products derived from this software
1566830Sobrien *    without specific prior written permission.
1666830Sobrien *
1766830Sobrien * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
1866830Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1966830Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2066830Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2166830Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2266830Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2366830Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2466830Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2566830Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2666830Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2750472Speter * SUCH DAMAGE.
2866830Sobrien *
2927837Sdavidn * $FreeBSD$
3051231Ssheldonh */
3127837Sdavidn
3227837Sdavidn#include "namespace.h"
3327837Sdavidn#include <errno.h>
3427837Sdavidn#include <unistd.h>
3527837Sdavidn#include <fcntl.h>
3627837Sdavidn#include <stdio.h>
3727837Sdavidn#include <stdlib.h>
3827837Sdavidn#include <string.h>
3927837Sdavidn#include <pthread.h>
4027837Sdavidn#include "un-namespace.h"
4127837Sdavidn#include "thr_private.h"
4251231Ssheldonh
4327837Sdavidnvoid	_pthread_exit(void *status);
4451231Ssheldonh
4527837Sdavidn__weak_reference(_pthread_exit, pthread_exit);
46117324Smtm
4762640Stgvoid
48117324Smtm_thr_exit(const char *fname, int lineno, const char *msg)
4998189Sgordon{
5096830Sgordon
5196830Sgordon	/* Write an error message to the standard error file descriptor: */
5296830Sgordon	_thread_printf(2,
5396830Sgordon	    "Fatal error '%s' at line %d in file %s (errno = %d)\n",
5496830Sgordon	    msg, lineno, fname, errno);
5596830Sgordon
5696830Sgordon	abort();
5796830Sgordon}
5896830Sgordon
5996830Sgordon/*
6096830Sgordon * Only called when a thread is cancelled.  It may be more useful
6196830Sgordon * to call it from pthread_exit() if other ways of asynchronous or
62117324Smtm * abnormal thread termination can be found.
63117324Smtm */
64117324Smtmvoid
6563307Smarkm_thr_exit_cleanup(void)
66117324Smtm{
67117324Smtm	struct pthread	*curthread = _get_curthread();
68117324Smtm
69130151Sschweikh	/*
70136615Sschweikh	 * POSIX states that cancellation/termination of a thread should
71136615Sschweikh	 * not release any visible resources (such as mutexes) and that
72117324Smtm	 * it is the applications responsibility.  Resources that are
73117324Smtm	 * internal to the threads library, including file and fd locks,
74117324Smtm	 * are not visible to the application and need to be released.
75117324Smtm	 */
76117324Smtm	/* Unlock all private mutexes: */
77117324Smtm	_mutex_unlock_private(curthread);
78117324Smtm
7963307Smarkm	/*
80117324Smtm	 * This still isn't quite correct because we don't account
81117324Smtm	 * for held spinlocks (see libc/stdlib/malloc.c).
82117324Smtm	 */
83138847Srse}
84138847Srse
8553550Sdillonvoid
86153027Sdougb_pthread_exit(void *status)
87153027Sdougb{
88153027Sdougb	struct pthread *curthread = _get_curthread();
89153027Sdougb	kse_critical_t crit;
90153027Sdougb	struct kse *curkse;
91153027Sdougb
92153027Sdougb	/* Check if this thread is already in the process of exiting: */
93117324Smtm	if ((curthread->flags & THR_FLAGS_EXITING) != 0) {
94131135Smtm		char msg[128];
95131135Smtm		snprintf(msg, sizeof(msg), "Thread %p has called "
96117324Smtm		    "pthread_exit() from a destructor. POSIX 1003.1 "
9727837Sdavidn		    "1996 s16.2.5.2 does not allow this!", curthread);
98117324Smtm		PANIC(msg);
99117324Smtm	}
100117324Smtm
101187685Sbz	/*
102117324Smtm	 * Flag this thread as exiting.  Threads should now be prevented
10327837Sdavidn	 * from joining to this thread.
10486856Sdarrenr	 */
10585219Sdarrenr	THR_SCHED_LOCK(curthread, curthread);
10686856Sdarrenr	curthread->flags |= THR_FLAGS_EXITING;
10727837Sdavidn	THR_SCHED_UNLOCK(curthread, curthread);
10827837Sdavidn
109	/*
110	 * To avoid signal-lost problem, if signals had already been
111	 * delivered to us, handle it. we have already set EXITING flag
112	 * so no new signals should be delivered to us.
113	 * XXX this is not enough if signal was delivered just before
114	 * thread called sigprocmask and masked it! in this case, we
115	 * might have to re-post the signal by kill() if the signal
116	 * is targeting process (not for a specified thread).
117	 * Kernel has same signal-lost problem, a signal may be delivered
118	 * to a thread which is on the way to call sigprocmask or thr_exit()!
119	 */
120	if (curthread->check_pending)
121		_thr_sig_check_pending(curthread);
122	/* Save the return value: */
123	curthread->ret = status;
124	while (curthread->cleanup != NULL) {
125		_pthread_cleanup_pop(1);
126	}
127	if (curthread->attr.cleanup_attr != NULL) {
128		curthread->attr.cleanup_attr(curthread->attr.arg_attr);
129	}
130	/* Check if there is thread specific data: */
131	if (curthread->specific != NULL) {
132		/* Run the thread-specific data destructors: */
133		_thread_cleanupspecific();
134	}
135	if (!_kse_isthreaded())
136		exit(0);
137	crit = _kse_critical_enter();
138	curkse = _get_curkse();
139	KSE_LOCK_ACQUIRE(curkse, &_thread_list_lock);
140	/* Use thread_list_lock */
141	_thread_active_threads--;
142	if ((_thread_scope_system <= 0 && _thread_active_threads == 1) ||
143	    (_thread_scope_system > 0 && _thread_active_threads == 0)) {
144		KSE_LOCK_RELEASE(curkse, &_thread_list_lock);
145		_kse_critical_leave(crit);
146		exit(0);
147		/* Never reach! */
148	}
149	KSE_LOCK_RELEASE(curkse, &_thread_list_lock);
150
151	/* This thread will never be re-scheduled. */
152	KSE_LOCK(curkse);
153	THR_SET_STATE(curthread, PS_DEAD);
154	_thr_sched_switch_unlocked(curthread);
155	/* Never reach! */
156
157	/* This point should not be reached. */
158	PANIC("Dead thread has resumed");
159}
160