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. |
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, see |
17 | <http://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <errno.h> |
20 | #include <stdlib.h> |
21 | #include <atomic.h> |
22 | #include "pthreadP.h" |
23 | |
24 | |
25 | static void |
26 | cleanup (void *arg) |
27 | { |
28 | *(void **) arg = NULL; |
29 | } |
30 | |
31 | |
32 | int |
33 | pthread_timedjoin_np (pthread_t threadid, void **thread_return, |
34 | const struct timespec *abstime) |
35 | { |
36 | struct pthread *self; |
37 | struct pthread *pd = (struct pthread *) threadid; |
38 | int result; |
39 | |
40 | /* Make sure the descriptor is valid. */ |
41 | if (INVALID_NOT_TERMINATED_TD_P (pd)) |
42 | /* Not a valid thread handle. */ |
43 | return ESRCH; |
44 | |
45 | /* Is the thread joinable?. */ |
46 | if (IS_DETACHED (pd)) |
47 | /* We cannot wait for the thread. */ |
48 | return EINVAL; |
49 | |
50 | self = THREAD_SELF; |
51 | if (pd == self || self->joinid == pd) |
52 | /* This is a deadlock situation. The threads are waiting for each |
53 | other to finish. Note that this is a "may" error. To be 100% |
54 | sure we catch this error we would have to lock the data |
55 | structures but it is not necessary. In the unlikely case that |
56 | two threads are really caught in this situation they will |
57 | deadlock. It is the programmer's problem to figure this |
58 | out. */ |
59 | return EDEADLK; |
60 | |
61 | /* Wait for the thread to finish. If it is already locked something |
62 | is wrong. There can only be one waiter. */ |
63 | if (__builtin_expect (atomic_compare_and_exchange_bool_acq (&pd->joinid, |
64 | self, NULL), 0)) |
65 | /* There is already somebody waiting for the thread. */ |
66 | return EINVAL; |
67 | |
68 | |
69 | /* During the wait we change to asynchronous cancellation. If we |
70 | are cancelled the thread we are waiting for must be marked as |
71 | un-wait-ed for again. */ |
72 | pthread_cleanup_push (cleanup, &pd->joinid); |
73 | |
74 | /* Switch to asynchronous cancellation. */ |
75 | int oldtype = CANCEL_ASYNC (); |
76 | |
77 | |
78 | /* Wait for the child. */ |
79 | result = lll_timedwait_tid (pd->tid, abstime); |
80 | |
81 | |
82 | /* Restore cancellation mode. */ |
83 | CANCEL_RESET (oldtype); |
84 | |
85 | /* Remove the handler. */ |
86 | pthread_cleanup_pop (0); |
87 | |
88 | |
89 | /* We might have timed out. */ |
90 | if (result == 0) |
91 | { |
92 | /* Store the return value if the caller is interested. */ |
93 | if (thread_return != NULL) |
94 | *thread_return = pd->result; |
95 | |
96 | |
97 | /* Free the TCB. */ |
98 | __free_tcb (pd); |
99 | } |
100 | else |
101 | pd->joinid = NULL; |
102 | |
103 | return result; |
104 | } |
105 | |