xref: /freebsd/lib/libthr/thread/thr_detach.c (revision f8bbbce458194ff4312c610d32a64ff4a3a71d45)
15e53a4f9SPedro F. Giffuni /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
35e53a4f9SPedro 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 
3037a6356bSDavid Xu #include "namespace.h"
31a091d823SDavid Xu #include <sys/types.h>
32bb535300SJeff Roberson #include <errno.h>
33bb535300SJeff Roberson #include <pthread.h>
3437a6356bSDavid Xu #include "un-namespace.h"
35a091d823SDavid Xu 
36bb535300SJeff Roberson #include "thr_private.h"
37bb535300SJeff Roberson 
380ab1bfc7SKonstantin Belousov __weak_reference(_thr_detach, pthread_detach);
390ab1bfc7SKonstantin Belousov __weak_reference(_thr_detach, _pthread_detach);
40bb535300SJeff Roberson 
41bb535300SJeff Roberson int
_thr_detach(pthread_t pthread)420ab1bfc7SKonstantin Belousov _thr_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 	if ((rval = _thr_find_thread(curthread, pthread,
51a091d823SDavid Xu 			/*include dead*/1)) != 0) {
52a091d823SDavid Xu 		return (rval);
53cff6c3caSMike Makonnen 	}
54bb535300SJeff Roberson 
55a091d823SDavid Xu 	/* Check if the thread is already detached or has a joiner. */
56a9b764e2SDavid Xu 	if ((pthread->flags & THR_FLAGS_DETACHED) != 0 ||
57a091d823SDavid Xu 	    (pthread->joiner != NULL)) {
58a9b764e2SDavid Xu 		THR_THREAD_UNLOCK(curthread, pthread);
59a091d823SDavid Xu 		return (EINVAL);
60bb535300SJeff Roberson 	}
61bb535300SJeff Roberson 
62a091d823SDavid Xu 	/* Flag the thread as detached. */
63a9b764e2SDavid Xu 	pthread->flags |= THR_FLAGS_DETACHED;
64a9b764e2SDavid Xu 	_thr_try_gc(curthread, pthread); /* thread lock released */
65bb535300SJeff Roberson 
66bb535300SJeff Roberson 	return (0);
67bb535300SJeff Roberson }
68