]>
Commit | Line | Data |
---|---|---|
d4697bc9 | 1 | /* Copyright (C) 2002-2014 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 | |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
76a50749 UD |
18 | |
19 | #include <errno.h> | |
20 | #include <stdlib.h> | |
21 | ||
8dea90aa | 22 | #include <atomic.h> |
76a50749 UD |
23 | #include "pthreadP.h" |
24 | ||
25 | ||
26 | int | |
27 | pthread_tryjoin_np (threadid, thread_return) | |
28 | pthread_t threadid; | |
29 | void **thread_return; | |
30 | { | |
31 | struct pthread *self; | |
32 | struct pthread *pd = (struct pthread *) threadid; | |
33 | ||
34 | /* Make sure the descriptor is valid. */ | |
35 | if (DEBUGGING_P && __find_in_stack_list (pd) == NULL) | |
36 | /* Not a valid thread handle. */ | |
37 | return ESRCH; | |
38 | ||
39 | /* Is the thread joinable?. */ | |
40 | if (IS_DETACHED (pd)) | |
41 | /* We cannot wait for the thread. */ | |
42 | return EINVAL; | |
43 | ||
44 | self = THREAD_SELF; | |
45 | if (pd == self || self->joinid == pd) | |
46 | /* This is a deadlock situation. The threads are waiting for each | |
47 | other to finish. Note that this is a "may" error. To be 100% | |
48 | sure we catch this error we would have to lock the data | |
49 | structures but it is not necessary. In the unlikely case that | |
50 | two threads are really caught in this situation they will | |
51 | deadlock. It is the programmer's problem to figure this | |
52 | out. */ | |
53 | return EDEADLK; | |
54 | ||
55 | /* Return right away if the thread hasn't terminated yet. */ | |
56 | if (pd->tid != 0) | |
57 | return EBUSY; | |
58 | ||
59 | /* Wait for the thread to finish. If it is already locked something | |
60 | is wrong. There can only be one waiter. */ | |
5a3ab2fc | 61 | if (atomic_compare_and_exchange_bool_acq (&pd->joinid, self, NULL)) |
76a50749 UD |
62 | /* There is already somebody waiting for the thread. */ |
63 | return EINVAL; | |
64 | ||
65 | /* Store the return value if the caller is interested. */ | |
66 | if (thread_return != NULL) | |
67 | *thread_return = pd->result; | |
68 | ||
69 | ||
70 | /* Free the TCB. */ | |
71 | __free_tcb (pd); | |
72 | ||
73 | return 0; | |
74 | } |