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.
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 <https://www.gnu.org/licenses/>. */
21 #include <stap-probe.h>
27 /* If we already changed the waiter ID, reset it. The call cannot
28 fail for any reason but the thread not having done that yet so
29 there is no reason for a loop. */
30 struct pthread
*self
= THREAD_SELF
;
31 atomic_compare_exchange_weak_acquire (&arg
, &self
, NULL
);
34 /* The kernel notifies a process which uses CLONE_CHILD_CLEARTID via futex
35 wake-up when the clone terminates. The memory location contains the
36 thread ID while the clone is running and is reset to zero by the kernel
37 afterwards. The kernel up to version 3.16.3 does not use the private futex
38 operations for futex wake-up when the clone terminates. */
40 clockwait_tid (pid_t
*tidp
, clockid_t clockid
, const struct timespec
*abstime
)
44 if (! valid_nanoseconds (abstime
->tv_nsec
))
47 /* Repeat until thread terminated. */
48 while ((tid
= *tidp
) != 0)
52 /* Get the current time. This can only fail if clockid is
54 if (__glibc_unlikely (__clock_gettime (clockid
, &rt
)))
57 /* Compute relative timeout. */
58 rt
.tv_sec
= abstime
->tv_sec
- rt
.tv_sec
;
59 rt
.tv_nsec
= abstime
->tv_nsec
- rt
.tv_nsec
;
62 rt
.tv_nsec
+= 1000000000;
66 /* Already timed out? */
70 /* If *tidp == tid, wait until thread terminates or the wait times out.
71 The kernel up to version 3.16.3 does not use the private futex
72 operations for futex wake-up when the clone terminates. */
73 if (lll_futex_timed_wait_cancel (tidp
, tid
, &rt
, LLL_SHARED
)
82 __pthread_clockjoin_ex (pthread_t threadid
, void **thread_return
,
84 const struct timespec
*abstime
, bool block
)
86 struct pthread
*pd
= (struct pthread
*) threadid
;
88 /* Make sure the descriptor is valid. */
89 if (INVALID_NOT_TERMINATED_TD_P (pd
))
90 /* Not a valid thread handle. */
93 /* Is the thread joinable?. */
95 /* We cannot wait for the thread. */
98 struct pthread
*self
= THREAD_SELF
;
101 LIBC_PROBE (pthread_join
, 1, threadid
);
104 || (self
->joinid
== pd
105 && (pd
->cancelhandling
106 & (CANCELING_BITMASK
| CANCELED_BITMASK
| EXITING_BITMASK
107 | TERMINATED_BITMASK
)) == 0))
108 && !CANCEL_ENABLED_AND_CANCELED (self
->cancelhandling
))
109 /* This is a deadlock situation. The threads are waiting for each
110 other to finish. Note that this is a "may" error. To be 100%
111 sure we catch this error we would have to lock the data
112 structures but it is not necessary. In the unlikely case that
113 two threads are really caught in this situation they will
114 deadlock. It is the programmer's problem to figure this
118 /* Wait for the thread to finish. If it is already locked something
119 is wrong. There can only be one waiter. */
120 else if (__glibc_unlikely (atomic_compare_exchange_weak_acquire (&pd
->joinid
,
123 /* There is already somebody waiting for the thread. */
126 /* BLOCK waits either indefinitely or based on an absolute time. POSIX also
127 states a cancellation point shall occur for pthread_join, and we use the
128 same rationale for posix_timedjoin_np. Both clockwait_tid and the futex
129 call use the cancellable variant. */
132 /* During the wait we change to asynchronous cancellation. If we
133 are cancelled the thread we are waiting for must be marked as
134 un-wait-ed for again. */
135 pthread_cleanup_push (cleanup
, &pd
->joinid
);
138 result
= clockwait_tid (&pd
->tid
, clockid
, abstime
);
142 /* We need acquire MO here so that we synchronize with the
143 kernel's store to 0 when the clone terminates. (see above) */
144 while ((tid
= atomic_load_acquire (&pd
->tid
)) != 0)
145 lll_futex_wait_cancel (&pd
->tid
, tid
, LLL_SHARED
);
148 pthread_cleanup_pop (0);
151 void *pd_result
= pd
->result
;
152 if (__glibc_likely (result
== 0))
154 /* We mark the thread as terminated and as joined. */
157 /* Store the return value if the caller is interested. */
158 if (thread_return
!= NULL
)
159 *thread_return
= pd_result
;
167 LIBC_PROBE (pthread_join_ret
, 3, threadid
, result
, pd_result
);