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#include "namespace.h"
32113658Sdeischen#include <sys/types.h>
33113658Sdeischen#include <machine/atomic.h>
3413546Sjulian#include <errno.h>
3513546Sjulian#include <pthread.h>
36174112Sdeischen#include "un-namespace.h"
37103388Smini#include "thr_private.h"
3813546Sjulian
3975369Sdeischen__weak_reference(_pthread_detach, pthread_detach);
4071581Sdeischen
4113546Sjulianint
4271581Sdeischen_pthread_detach(pthread_t pthread)
4313546Sjulian{
44113661Sdeischen	struct pthread *curthread = _get_curthread();
45117907Sdeischen	struct kse_mailbox *kmbx = NULL;
46113661Sdeischen	struct pthread *joiner;
47113658Sdeischen	int rval = 0;
4813546Sjulian
4913546Sjulian	/* Check for invalid calling parameters: */
50113658Sdeischen	if (pthread == NULL || pthread->magic != THR_MAGIC)
5113546Sjulian		/* Return an invalid argument error: */
5231402Salex		rval = EINVAL;
5335509Sjb
54113661Sdeischen	else if ((rval = _thr_ref_add(curthread, pthread,
55113661Sdeischen	    /*include dead*/1)) != 0) {
56113661Sdeischen		/* Return an error: */
57113661Sdeischen	}
58113661Sdeischen
59113658Sdeischen	/* Check if the thread is already detached: */
60113661Sdeischen	else if ((pthread->attr.flags & PTHREAD_DETACHED) != 0) {
61113658Sdeischen		/* Return an error: */
62113661Sdeischen		_thr_ref_delete(curthread, pthread);
63113658Sdeischen		rval = EINVAL;
64113661Sdeischen	} else {
65113658Sdeischen		/* Lock the detached thread: */
66113658Sdeischen		THR_SCHED_LOCK(curthread, pthread);
67113658Sdeischen
6813546Sjulian		/* Flag the thread as detached: */
6913546Sjulian		pthread->attr.flags |= PTHREAD_DETACHED;
7013546Sjulian
71113658Sdeischen		/* Retrieve any joining thread and remove it: */
72113658Sdeischen		joiner = pthread->joiner;
73115278Sdeischen		if ((joiner != NULL) && (joiner->kseg == pthread->kseg)) {
74113661Sdeischen			/*
75113661Sdeischen			 * We already own the scheduler lock for the joiner.
76113661Sdeischen			 * Take advantage of that and make the joiner runnable.
77113661Sdeischen			 */
78113661Sdeischen			if (joiner->join_status.thread == pthread) {
79113661Sdeischen				/*
80113661Sdeischen				 * Set the return value for the woken thread:
81113661Sdeischen				 */
82113661Sdeischen				joiner->join_status.error = ESRCH;
83113661Sdeischen				joiner->join_status.ret = NULL;
84113661Sdeischen				joiner->join_status.thread = NULL;
8544963Sjb
86117907Sdeischen				kmbx = _thr_setrunnable_unlocked(joiner);
87113661Sdeischen			}
88113661Sdeischen			joiner = NULL;
89113661Sdeischen		}
90113661Sdeischen		THR_SCHED_UNLOCK(curthread, pthread);
91113658Sdeischen		/* See if there is a thread waiting in pthread_join(): */
92115278Sdeischen		if ((joiner != NULL) &&
93115278Sdeischen		    (_thr_ref_add(curthread, joiner, 0) == 0)) {
94113658Sdeischen			/* Lock the joiner before fiddling with it. */
95113658Sdeischen			THR_SCHED_LOCK(curthread, joiner);
96113658Sdeischen			if (joiner->join_status.thread == pthread) {
97113658Sdeischen				/*
98113658Sdeischen				 * Set the return value for the woken thread:
99113658Sdeischen				 */
100113658Sdeischen				joiner->join_status.error = ESRCH;
101113658Sdeischen				joiner->join_status.ret = NULL;
102113658Sdeischen				joiner->join_status.thread = NULL;
10376909Sjasone
104117907Sdeischen				kmbx = _thr_setrunnable_unlocked(joiner);
105113658Sdeischen			}
106113658Sdeischen			THR_SCHED_UNLOCK(curthread, joiner);
107114187Sdeischen			_thr_ref_delete(curthread, joiner);
10813546Sjulian		}
109115278Sdeischen		_thr_ref_delete(curthread, pthread);
110117907Sdeischen		if (kmbx != NULL)
111117907Sdeischen			kse_wakeup(kmbx);
112113658Sdeischen	}
11344963Sjb
11413546Sjulian	/* Return the completion status: */
11513546Sjulian	return (rval);
11613546Sjulian}
117