15e53a4f9SPedro F. Giffuni /*- 25e53a4f9SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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 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 41*0ab1bfc7SKonstantin Belousov __weak_reference(_thr_detach, pthread_detach); 42*0ab1bfc7SKonstantin Belousov __weak_reference(_thr_detach, _pthread_detach); 43bb535300SJeff Roberson 44bb535300SJeff Roberson int 45*0ab1bfc7SKonstantin Belousov _thr_detach(pthread_t pthread) 46bb535300SJeff Roberson { 47a091d823SDavid Xu struct pthread *curthread = _get_curthread(); 48a091d823SDavid Xu int rval; 494cd18a22SMike Makonnen 50a091d823SDavid Xu if (pthread == NULL) 51bb535300SJeff Roberson return (EINVAL); 52bb535300SJeff Roberson 53a091d823SDavid Xu if ((rval = _thr_find_thread(curthread, pthread, 54a091d823SDavid Xu /*include dead*/1)) != 0) { 55a091d823SDavid Xu return (rval); 56cff6c3caSMike Makonnen } 57bb535300SJeff Roberson 58a091d823SDavid Xu /* Check if the thread is already detached or has a joiner. */ 59a9b764e2SDavid Xu if ((pthread->flags & THR_FLAGS_DETACHED) != 0 || 60a091d823SDavid Xu (pthread->joiner != NULL)) { 61a9b764e2SDavid Xu THR_THREAD_UNLOCK(curthread, pthread); 62a091d823SDavid Xu return (EINVAL); 63bb535300SJeff Roberson } 64bb535300SJeff Roberson 65a091d823SDavid Xu /* Flag the thread as detached. */ 66a9b764e2SDavid Xu pthread->flags |= THR_FLAGS_DETACHED; 67a9b764e2SDavid Xu _thr_try_gc(curthread, pthread); /* thread lock released */ 68bb535300SJeff Roberson 69bb535300SJeff Roberson return (0); 70bb535300SJeff Roberson } 71