thr_detach.c revision 174112
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: head/lib/libkse/thread/thr_detach.c 174112 2007-11-30 17:20:29Z deischen $
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
39156611SdeischenLT10_COMPAT_PRIVATE(_pthread_detach);
40156611SdeischenLT10_COMPAT_DEFAULT(pthread_detach);
41156611Sdeischen
4275369Sdeischen__weak_reference(_pthread_detach, pthread_detach);
4371581Sdeischen
4413546Sjulianint
4571581Sdeischen_pthread_detach(pthread_t pthread)
4613546Sjulian{
47113661Sdeischen	struct pthread *curthread = _get_curthread();
48117907Sdeischen	struct kse_mailbox *kmbx = NULL;
49113661Sdeischen	struct pthread *joiner;
50113658Sdeischen	int rval = 0;
5113546Sjulian
5213546Sjulian	/* Check for invalid calling parameters: */
53113658Sdeischen	if (pthread == NULL || pthread->magic != THR_MAGIC)
5413546Sjulian		/* Return an invalid argument error: */
5531402Salex		rval = EINVAL;
5635509Sjb
57113661Sdeischen	else if ((rval = _thr_ref_add(curthread, pthread,
58113661Sdeischen	    /*include dead*/1)) != 0) {
59113661Sdeischen		/* Return an error: */
60113661Sdeischen	}
61113661Sdeischen
62113658Sdeischen	/* Check if the thread is already detached: */
63113661Sdeischen	else if ((pthread->attr.flags & PTHREAD_DETACHED) != 0) {
64113658Sdeischen		/* Return an error: */
65113661Sdeischen		_thr_ref_delete(curthread, pthread);
66113658Sdeischen		rval = EINVAL;
67113661Sdeischen	} else {
68113658Sdeischen		/* Lock the detached thread: */
69113658Sdeischen		THR_SCHED_LOCK(curthread, pthread);
70113658Sdeischen
7113546Sjulian		/* Flag the thread as detached: */
7213546Sjulian		pthread->attr.flags |= PTHREAD_DETACHED;
7313546Sjulian
74113658Sdeischen		/* Retrieve any joining thread and remove it: */
75113658Sdeischen		joiner = pthread->joiner;
76115278Sdeischen		if ((joiner != NULL) && (joiner->kseg == pthread->kseg)) {
77113661Sdeischen			/*
78113661Sdeischen			 * We already own the scheduler lock for the joiner.
79113661Sdeischen			 * Take advantage of that and make the joiner runnable.
80113661Sdeischen			 */
81113661Sdeischen			if (joiner->join_status.thread == pthread) {
82113661Sdeischen				/*
83113661Sdeischen				 * Set the return value for the woken thread:
84113661Sdeischen				 */
85113661Sdeischen				joiner->join_status.error = ESRCH;
86113661Sdeischen				joiner->join_status.ret = NULL;
87113661Sdeischen				joiner->join_status.thread = NULL;
8844963Sjb
89117907Sdeischen				kmbx = _thr_setrunnable_unlocked(joiner);
90113661Sdeischen			}
91113661Sdeischen			joiner = NULL;
92113661Sdeischen		}
93113661Sdeischen		THR_SCHED_UNLOCK(curthread, pthread);
94113658Sdeischen		/* See if there is a thread waiting in pthread_join(): */
95115278Sdeischen		if ((joiner != NULL) &&
96115278Sdeischen		    (_thr_ref_add(curthread, joiner, 0) == 0)) {
97113658Sdeischen			/* Lock the joiner before fiddling with it. */
98113658Sdeischen			THR_SCHED_LOCK(curthread, joiner);
99113658Sdeischen			if (joiner->join_status.thread == pthread) {
100113658Sdeischen				/*
101113658Sdeischen				 * Set the return value for the woken thread:
102113658Sdeischen				 */
103113658Sdeischen				joiner->join_status.error = ESRCH;
104113658Sdeischen				joiner->join_status.ret = NULL;
105113658Sdeischen				joiner->join_status.thread = NULL;
10676909Sjasone
107117907Sdeischen				kmbx = _thr_setrunnable_unlocked(joiner);
108113658Sdeischen			}
109113658Sdeischen			THR_SCHED_UNLOCK(curthread, joiner);
110114187Sdeischen			_thr_ref_delete(curthread, joiner);
11113546Sjulian		}
112115278Sdeischen		_thr_ref_delete(curthread, pthread);
113117907Sdeischen		if (kmbx != NULL)
114117907Sdeischen			kse_wakeup(kmbx);
115113658Sdeischen	}
11644963Sjb
11713546Sjulian	/* Return the completion status: */
11813546Sjulian	return (rval);
11913546Sjulian}
120