1/* Common definition for pthread_{timed,try}join{_np}.
2 Copyright (C) 2017-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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 "pthreadP.h"
20#include <atomic.h>
21#include <stap-probe.h>
22
23static void
24cleanup (void *arg)
25{
26 /* If we already changed the waiter ID, reset it. The call cannot
27 fail for any reason but the thread not having done that yet so
28 there is no reason for a loop. */
29 struct pthread *self = THREAD_SELF;
30 atomic_compare_exchange_weak_acquire (&arg, &self, NULL);
31}
32
33/* The kernel notifies a process which uses CLONE_CHILD_CLEARTID via futex
34 wake-up when the clone terminates. The memory location contains the
35 thread ID while the clone is running and is reset to zero by the kernel
36 afterwards. The kernel up to version 3.16.3 does not use the private futex
37 operations for futex wake-up when the clone terminates. */
38static int
39timedwait_tid (pid_t *tidp, const struct timespec *abstime)
40{
41 pid_t tid;
42
43 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
44 return EINVAL;
45
46 /* Repeat until thread terminated. */
47 while ((tid = *tidp) != 0)
48 {
49 struct timeval tv;
50 struct timespec rt;
51
52 /* Get the current time. */
53 __gettimeofday (&tv, NULL);
54
55 /* Compute relative timeout. */
56 rt.tv_sec = abstime->tv_sec - tv.tv_sec;
57 rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
58 if (rt.tv_nsec < 0)
59 {
60 rt.tv_nsec += 1000000000;
61 --rt.tv_sec;
62 }
63
64 /* Already timed out? */
65 if (rt.tv_sec < 0)
66 return ETIMEDOUT;
67
68 /* If *tidp == tid, wait until thread terminates or the wait times out.
69 The kernel up to version 3.16.3 does not use the private futex
70 operations for futex wake-up when the clone terminates. */
71 if (lll_futex_timed_wait_cancel (tidp, tid, &rt, LLL_SHARED)
72 == -ETIMEDOUT)
73 return ETIMEDOUT;
74 }
75
76 return 0;
77}
78
79int
80__pthread_timedjoin_ex (pthread_t threadid, void **thread_return,
81 const struct timespec *abstime, bool block)
82{
83 struct pthread *pd = (struct pthread *) threadid;
84
85 /* Make sure the descriptor is valid. */
86 if (INVALID_NOT_TERMINATED_TD_P (pd))
87 /* Not a valid thread handle. */
88 return ESRCH;
89
90 /* Is the thread joinable?. */
91 if (IS_DETACHED (pd))
92 /* We cannot wait for the thread. */
93 return EINVAL;
94
95 struct pthread *self = THREAD_SELF;
96 int result = 0;
97
98 LIBC_PROBE (pthread_join, 1, threadid);
99
100 if ((pd == self
101 || (self->joinid == pd
102 && (pd->cancelhandling
103 & (CANCELING_BITMASK | CANCELED_BITMASK | EXITING_BITMASK
104 | TERMINATED_BITMASK)) == 0))
105 && !CANCEL_ENABLED_AND_CANCELED (self->cancelhandling))
106 /* This is a deadlock situation. The threads are waiting for each
107 other to finish. Note that this is a "may" error. To be 100%
108 sure we catch this error we would have to lock the data
109 structures but it is not necessary. In the unlikely case that
110 two threads are really caught in this situation they will
111 deadlock. It is the programmer's problem to figure this
112 out. */
113 return EDEADLK;
114
115 /* Wait for the thread to finish. If it is already locked something
116 is wrong. There can only be one waiter. */
117 else if (__glibc_unlikely (atomic_compare_exchange_weak_acquire (&pd->joinid,
118 &self,
119 NULL)))
120 /* There is already somebody waiting for the thread. */
121 return EINVAL;
122
123 /* BLOCK waits either indefinitely or based on an absolute time. POSIX also
124 states a cancellation point shall occur for pthread_join, and we use the
125 same rationale for posix_timedjoin_np. Both timedwait_tid and the futex
126 call use the cancellable variant. */
127 if (block)
128 {
129 /* During the wait we change to asynchronous cancellation. If we
130 are cancelled the thread we are waiting for must be marked as
131 un-wait-ed for again. */
132 pthread_cleanup_push (cleanup, &pd->joinid);
133
134 if (abstime != NULL)
135 result = timedwait_tid (&pd->tid, abstime);
136 else
137 {
138 pid_t tid;
139 /* We need acquire MO here so that we synchronize with the
140 kernel's store to 0 when the clone terminates. (see above) */
141 while ((tid = atomic_load_acquire (&pd->tid)) != 0)
142 lll_futex_wait_cancel (&pd->tid, tid, LLL_SHARED);
143 }
144
145 pthread_cleanup_pop (0);
146 }
147
148 void *pd_result = pd->result;
149 if (__glibc_likely (result == 0))
150 {
151 /* We mark the thread as terminated and as joined. */
152 pd->tid = -1;
153
154 /* Store the return value if the caller is interested. */
155 if (thread_return != NULL)
156 *thread_return = pd_result;
157
158 /* Free the TCB. */
159 __free_tcb (pd);
160 }
161 else
162 pd->joinid = NULL;
163
164 LIBC_PROBE (pthread_join_ret, 3, threadid, result, pd_result);
165
166 return result;
167}
168hidden_def (__pthread_timedjoin_ex)
169