xref: /freebsd/lib/libthr/thread/thr_detach.c (revision 37a6356bbed1e94fd2b0d9d02a29508465584c19)
1bb535300SJeff Roberson /*
2df2cf821SDavid Xu  * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
3df2cf821SDavid Xu  * Copyright (C) 2003 Daniel M. Eischen <deischen@freebsd.org>
4bb535300SJeff Roberson  * All rights reserved.
5bb535300SJeff Roberson  *
6bb535300SJeff Roberson  * Redistribution and use in source and binary forms, with or without
7bb535300SJeff Roberson  * modification, are permitted provided that the following conditions
8bb535300SJeff Roberson  * are met:
9bb535300SJeff Roberson  * 1. Redistributions of source code must retain the above copyright
10df2cf821SDavid Xu  *    notice unmodified, this list of conditions, and the following
11df2cf821SDavid Xu  *    disclaimer.
12bb535300SJeff Roberson  * 2. Redistributions in binary form must reproduce the above copyright
13bb535300SJeff Roberson  *    notice, this list of conditions and the following disclaimer in the
14bb535300SJeff Roberson  *    documentation and/or other materials provided with the distribution.
15bb535300SJeff Roberson  *
16df2cf821SDavid Xu  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17df2cf821SDavid Xu  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18df2cf821SDavid Xu  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19df2cf821SDavid Xu  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20df2cf821SDavid Xu  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21df2cf821SDavid Xu  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22df2cf821SDavid Xu  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23df2cf821SDavid Xu  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24df2cf821SDavid Xu  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25df2cf821SDavid Xu  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26bb535300SJeff Roberson  *
27bb535300SJeff Roberson  * $FreeBSD$
28df2cf821SDavid Xu  *
29bb535300SJeff Roberson  */
30a091d823SDavid Xu 
3137a6356bSDavid Xu #include "namespace.h"
32a091d823SDavid Xu #include <sys/types.h>
33bb535300SJeff Roberson #include <errno.h>
34bb535300SJeff Roberson #include <pthread.h>
3537a6356bSDavid Xu #include "un-namespace.h"
36a091d823SDavid Xu 
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 {
44a091d823SDavid Xu 	struct pthread *curthread = _get_curthread();
45a091d823SDavid Xu 	int rval;
464cd18a22SMike Makonnen 
47a091d823SDavid Xu 	if (pthread == NULL)
48bb535300SJeff Roberson 		return (EINVAL);
49bb535300SJeff Roberson 
50a091d823SDavid Xu 	THREAD_LIST_LOCK(curthread);
51a091d823SDavid Xu 	if ((rval = _thr_find_thread(curthread, pthread,
52a091d823SDavid Xu 			/*include dead*/1)) != 0) {
53a091d823SDavid Xu 		THREAD_LIST_UNLOCK(curthread);
54a091d823SDavid Xu 		return (rval);
55cff6c3caSMike Makonnen 	}
56bb535300SJeff Roberson 
57a091d823SDavid Xu 	/* Check if the thread is already detached or has a joiner. */
58a091d823SDavid Xu 	if ((pthread->tlflags & TLFLAGS_DETACHED) != 0 ||
59a091d823SDavid Xu 	    (pthread->joiner != NULL)) {
60a091d823SDavid Xu 		THREAD_LIST_UNLOCK(curthread);
61a091d823SDavid Xu 		return (EINVAL);
62bb535300SJeff Roberson 	}
63bb535300SJeff Roberson 
64a091d823SDavid Xu 	/* Flag the thread as detached. */
65a091d823SDavid Xu 	pthread->tlflags |= TLFLAGS_DETACHED;
66a091d823SDavid Xu 	if (pthread->state == PS_DEAD)
67a091d823SDavid Xu 		THR_GCLIST_ADD(pthread);
68a091d823SDavid Xu 	THREAD_LIST_UNLOCK(curthread);
69bb535300SJeff Roberson 
70bb535300SJeff Roberson 	return (0);
71bb535300SJeff Roberson }
72