]> git.ipfire.org Git - thirdparty/glibc.git/blame - nptl/pthread_tryjoin.c
Use <> for include of kernel-features.h.
[thirdparty/glibc.git] / nptl / pthread_tryjoin.c
CommitLineData
8dea90aa 1/* Copyright (C) 2002, 2003, 2005 Free Software Foundation, Inc.
76a50749
UD
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
4
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.
9
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.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
20#include <errno.h>
21#include <stdlib.h>
22
8dea90aa 23#include <atomic.h>
76a50749
UD
24#include "pthreadP.h"
25
26
27int
28pthread_tryjoin_np (threadid, thread_return)
29 pthread_t threadid;
30 void **thread_return;
31{
32 struct pthread *self;
33 struct pthread *pd = (struct pthread *) threadid;
34
35 /* Make sure the descriptor is valid. */
36 if (DEBUGGING_P && __find_in_stack_list (pd) == NULL)
37 /* Not a valid thread handle. */
38 return ESRCH;
39
40 /* Is the thread joinable?. */
41 if (IS_DETACHED (pd))
42 /* We cannot wait for the thread. */
43 return EINVAL;
44
45 self = THREAD_SELF;
46 if (pd == self || self->joinid == pd)
47 /* This is a deadlock situation. The threads are waiting for each
48 other to finish. Note that this is a "may" error. To be 100%
49 sure we catch this error we would have to lock the data
50 structures but it is not necessary. In the unlikely case that
51 two threads are really caught in this situation they will
52 deadlock. It is the programmer's problem to figure this
53 out. */
54 return EDEADLK;
55
56 /* Return right away if the thread hasn't terminated yet. */
57 if (pd->tid != 0)
58 return EBUSY;
59
60 /* Wait for the thread to finish. If it is already locked something
61 is wrong. There can only be one waiter. */
5a3ab2fc 62 if (atomic_compare_and_exchange_bool_acq (&pd->joinid, self, NULL))
76a50749
UD
63 /* There is already somebody waiting for the thread. */
64 return EINVAL;
65
66 /* Store the return value if the caller is interested. */
67 if (thread_return != NULL)
68 *thread_return = pd->result;
69
70
71 /* Free the TCB. */
72 __free_tcb (pd);
73
74 return 0;
75}