1 /* Copyright (C) 2002-2022 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3 
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8 
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <https://www.gnu.org/licenses/>.  */
17 
18 #include <errno.h>
19 #include "pthreadP.h"
20 #include <atomic.h>
21 #include <shlib-compat.h>
22 
23 int
___pthread_detach(pthread_t th)24 ___pthread_detach (pthread_t th)
25 {
26   struct pthread *pd = (struct pthread *) th;
27 
28   /* Make sure the descriptor is valid.  */
29   if (INVALID_NOT_TERMINATED_TD_P (pd))
30     /* Not a valid thread handle.  */
31     return ESRCH;
32 
33   int result = 0;
34 
35   /* Mark the thread as detached.  */
36   if (atomic_compare_and_exchange_bool_acq (&pd->joinid, pd, NULL))
37     {
38       /* There are two possibilities here.  First, the thread might
39 	 already be detached.  In this case we return EINVAL.
40 	 Otherwise there might already be a waiter.  The standard does
41 	 not mention what happens in this case.  */
42       if (IS_DETACHED (pd))
43 	result = EINVAL;
44     }
45   else
46     /* Check whether the thread terminated meanwhile.  In this case we
47        will just free the TCB.  */
48     if ((pd->cancelhandling & EXITING_BITMASK) != 0)
49       /* Note that the code in __free_tcb makes sure each thread
50 	 control block is freed only once.  */
51       __nptl_free_tcb (pd);
52 
53   return result;
54 }
55 versioned_symbol (libc, ___pthread_detach, pthread_detach, GLIBC_2_34);
56 libc_hidden_ver (___pthread_detach, __pthread_detach)
57 #ifndef SHARED
58 strong_alias (___pthread_detach, __pthread_detach)
59 #endif
60 
61 #if OTHER_SHLIB_COMPAT (libpthread, GLIBC_2_0, GLIBC_2_34)
62 compat_symbol (libc, ___pthread_detach, pthread_detach, GLIBC_2_0);
63 #endif
64