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