sparc: Fix .udiv plt on libc
[glibc.git] / nptl / pthread_tryjoin.c
blob6a3b62e4099369f8b37e0c60dccf00aa0a074da6
1 /* Copyright (C) 2002-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <stdlib.h>
22 #include <atomic.h>
23 #include "pthreadP.h"
26 int
27 pthread_tryjoin_np (pthread_t threadid, void **thread_return)
29 struct pthread *self;
30 struct pthread *pd = (struct pthread *) threadid;
32 /* Make sure the descriptor is valid. */
33 if (DEBUGGING_P && __find_in_stack_list (pd) == NULL)
34 /* Not a valid thread handle. */
35 return ESRCH;
37 /* Is the thread joinable?. */
38 if (IS_DETACHED (pd))
39 /* We cannot wait for the thread. */
40 return EINVAL;
42 self = THREAD_SELF;
43 if (pd == self || self->joinid == pd)
44 /* This is a deadlock situation. The threads are waiting for each
45 other to finish. Note that this is a "may" error. To be 100%
46 sure we catch this error we would have to lock the data
47 structures but it is not necessary. In the unlikely case that
48 two threads are really caught in this situation they will
49 deadlock. It is the programmer's problem to figure this
50 out. */
51 return EDEADLK;
53 /* Return right away if the thread hasn't terminated yet. */
54 if (pd->tid != 0)
55 return EBUSY;
57 /* Wait for the thread to finish. If it is already locked something
58 is wrong. There can only be one waiter. */
59 if (atomic_compare_and_exchange_bool_acq (&pd->joinid, self, NULL))
60 /* There is already somebody waiting for the thread. */
61 return EINVAL;
63 /* Store the return value if the caller is interested. */
64 if (thread_return != NULL)
65 *thread_return = pd->result;
68 /* Free the TCB. */
69 __free_tcb (pd);
71 return 0;