xref: /freebsd/lib/libthr/thread/thr_detach.c (revision 4cd18a22d554e7205ddf0408badbda02411ed51e)
1bb535300SJeff Roberson /*
2bb535300SJeff Roberson  * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>.
3bb535300SJeff Roberson  * All rights reserved.
4bb535300SJeff Roberson  *
5bb535300SJeff Roberson  * Redistribution and use in source and binary forms, with or without
6bb535300SJeff Roberson  * modification, are permitted provided that the following conditions
7bb535300SJeff Roberson  * are met:
8bb535300SJeff Roberson  * 1. Redistributions of source code must retain the above copyright
9bb535300SJeff Roberson  *    notice, this list of conditions and the following disclaimer.
10bb535300SJeff Roberson  * 2. Redistributions in binary form must reproduce the above copyright
11bb535300SJeff Roberson  *    notice, this list of conditions and the following disclaimer in the
12bb535300SJeff Roberson  *    documentation and/or other materials provided with the distribution.
13bb535300SJeff Roberson  * 3. All advertising materials mentioning features or use of this software
14bb535300SJeff Roberson  *    must display the following acknowledgement:
15bb535300SJeff Roberson  *	This product includes software developed by John Birrell.
16bb535300SJeff Roberson  * 4. Neither the name of the author nor the names of any co-contributors
17bb535300SJeff Roberson  *    may be used to endorse or promote products derived from this software
18bb535300SJeff Roberson  *    without specific prior written permission.
19bb535300SJeff Roberson  *
20bb535300SJeff Roberson  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
21bb535300SJeff Roberson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22bb535300SJeff Roberson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23bb535300SJeff Roberson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24bb535300SJeff Roberson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25bb535300SJeff Roberson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26bb535300SJeff Roberson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27bb535300SJeff Roberson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28bb535300SJeff Roberson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29bb535300SJeff Roberson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30bb535300SJeff Roberson  * SUCH DAMAGE.
31bb535300SJeff Roberson  *
32bb535300SJeff Roberson  * $FreeBSD$
33bb535300SJeff Roberson  */
34bb535300SJeff Roberson #include <errno.h>
35bb535300SJeff Roberson #include <pthread.h>
367d9d7ca2SMike Makonnen #include <stdlib.h>
37bb535300SJeff Roberson #include "thr_private.h"
38bb535300SJeff Roberson 
39bb535300SJeff Roberson __weak_reference(_pthread_detach, pthread_detach);
40bb535300SJeff Roberson 
41bb535300SJeff Roberson int
42bb535300SJeff Roberson _pthread_detach(pthread_t pthread)
43bb535300SJeff Roberson {
444cd18a22SMike Makonnen 	int error;
454cd18a22SMike Makonnen 
468c223652SMike Makonnen 	if (pthread->magic != PTHREAD_MAGIC)
47bb535300SJeff Roberson 		return (EINVAL);
48bb535300SJeff Roberson 
494cd18a22SMike Makonnen 	PTHREAD_LOCK(pthread);
50cff6c3caSMike Makonnen 
518c223652SMike Makonnen 	if ((pthread->attr.flags & PTHREAD_DETACHED) != 0) {
524cd18a22SMike Makonnen 		_thread_sigblock();
534cd18a22SMike Makonnen 		DEAD_LIST_LOCK;
544cd18a22SMike Makonnen 		error = pthread->isdead ? ESRCH : EINVAL;
554cd18a22SMike Makonnen 		DEAD_LIST_UNLOCK;
564cd18a22SMike Makonnen 		_thread_sigunblock();
574cd18a22SMike Makonnen 		PTHREAD_UNLOCK(pthread);
584cd18a22SMike Makonnen 		return (error);
59cff6c3caSMike Makonnen 	}
60bb535300SJeff Roberson 
61bb535300SJeff Roberson 	pthread->attr.flags |= PTHREAD_DETACHED;
62bb535300SJeff Roberson 
63bb535300SJeff Roberson 	/* Check if there is a joiner: */
64bb535300SJeff Roberson 	if (pthread->joiner != NULL) {
65bb535300SJeff Roberson 		struct pthread	*joiner = pthread->joiner;
66bb535300SJeff Roberson 
67bb535300SJeff Roberson 		/* Set the return value for the woken thread: */
68bb535300SJeff Roberson 		joiner->join_status.error = ESRCH;
69bb535300SJeff Roberson 		joiner->join_status.ret = NULL;
70bb535300SJeff Roberson 		joiner->join_status.thread = NULL;
71bb535300SJeff Roberson 
72bb535300SJeff Roberson 		/*
73bb535300SJeff Roberson 		 * Disconnect the joiner from the thread being detached:
74bb535300SJeff Roberson 		 */
75bb535300SJeff Roberson 		pthread->joiner = NULL;
764cd18a22SMike Makonnen 		PTHREAD_WAKE(joiner);
77bb535300SJeff Roberson 	}
78bb535300SJeff Roberson 
794cd18a22SMike Makonnen 	PTHREAD_UNLOCK(pthread);
80bb535300SJeff Roberson 
81bb535300SJeff Roberson 	return (0);
82bb535300SJeff Roberson }
83