thr_detach.c revision 113661
1/* 2 * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by John Birrell. 16 * 4. Neither the name of the author nor the names of any co-contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * $FreeBSD: head/lib/libkse/thread/thr_detach.c 113661 2003-04-18 07:09:43Z deischen $ 33 */ 34#include <sys/types.h> 35#include <machine/atomic.h> 36#include <errno.h> 37#include <pthread.h> 38#include "thr_private.h" 39 40__weak_reference(_pthread_detach, pthread_detach); 41 42int 43_pthread_detach(pthread_t pthread) 44{ 45 struct pthread *curthread = _get_curthread(); 46 struct pthread *joiner; 47 kse_critical_t crit; 48 int dead; 49 int rval = 0; 50 51 /* Check for invalid calling parameters: */ 52 if (pthread == NULL || pthread->magic != THR_MAGIC) 53 /* Return an invalid argument error: */ 54 rval = EINVAL; 55 56 else if ((rval = _thr_ref_add(curthread, pthread, 57 /*include dead*/1)) != 0) { 58 /* Return an error: */ 59 _thr_leave_cancellation_point(curthread); 60 } 61 62 /* Check if the thread is already detached: */ 63 else if ((pthread->attr.flags & PTHREAD_DETACHED) != 0) { 64 /* Return an error: */ 65 _thr_ref_delete(curthread, pthread); 66 rval = EINVAL; 67 } else { 68 /* Lock the detached thread: */ 69 THR_SCHED_LOCK(curthread, pthread); 70 71 /* Flag the thread as detached: */ 72 pthread->attr.flags |= PTHREAD_DETACHED; 73 74 /* Retrieve any joining thread and remove it: */ 75 joiner = pthread->joiner; 76 pthread->joiner = NULL; 77 if (joiner->kseg == pthread->kseg) { 78 /* 79 * We already own the scheduler lock for the joiner. 80 * Take advantage of that and make the joiner runnable. 81 */ 82 if (joiner->join_status.thread == pthread) { 83 /* 84 * Set the return value for the woken thread: 85 */ 86 joiner->join_status.error = ESRCH; 87 joiner->join_status.ret = NULL; 88 joiner->join_status.thread = NULL; 89 90 _thr_setrunnable_unlocked(joiner); 91 } 92 joiner = NULL; 93 } 94 dead = (pthread->flags & THR_FLAGS_GC_SAFE) != 0; 95 THR_SCHED_UNLOCK(curthread, pthread); 96 97 if (dead != 0) { 98 crit = _kse_critical_enter(); 99 KSE_LOCK_ACQUIRE(curthread->kse, &_thread_list_lock); 100 THR_GCLIST_ADD(pthread); 101 KSE_LOCK_RELEASE(curthread->kse, &_thread_list_lock); 102 _kse_critical_leave(crit); 103 } 104 _thr_ref_delete(curthread, pthread); 105 106 /* See if there is a thread waiting in pthread_join(): */ 107 if (joiner != NULL) { 108 /* Lock the joiner before fiddling with it. */ 109 THR_SCHED_LOCK(curthread, joiner); 110 if (joiner->join_status.thread == pthread) { 111 /* 112 * Set the return value for the woken thread: 113 */ 114 joiner->join_status.error = ESRCH; 115 joiner->join_status.ret = NULL; 116 joiner->join_status.thread = NULL; 117 118 _thr_setrunnable_unlocked(joiner); 119 } 120 THR_SCHED_UNLOCK(curthread, joiner); 121 } 122 } 123 124 /* Return the completion status: */ 125 return (rval); 126} 127