1 /* Common definition for pthread_{timed,try}join{_np}.
2 Copyright (C) 2017-2020 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>
23 #include <futex-internal.h>
28 /* If we already changed the waiter ID, reset it. The call cannot
29 fail for any reason but the thread not having done that yet so
30 there is no reason for a loop. */
31 struct pthread
*self
= THREAD_SELF
;
32 atomic_compare_exchange_weak_acquire (&arg
, &self
, NULL
);
35 /* The kernel notifies a process which uses CLONE_CHILD_CLEARTID via futex
36 wake-up when the clone terminates. The memory location contains the
37 thread ID while the clone is running and is reset to zero by the kernel
38 afterwards. The kernel up to version 3.16.3 does not use the private futex
39 operations for futex wake-up when the clone terminates. */
41 clockwait_tid (pid_t
*tidp
, clockid_t clockid
,
42 const struct __timespec64
*abstime
)
47 if (! valid_nanoseconds (abstime
->tv_nsec
))
50 /* Repeat until thread terminated. */
51 while ((tid
= *tidp
) != 0)
53 struct __timespec64 rt
;
55 /* Get the current time. This can only fail if clockid is
57 if (__glibc_unlikely (__clock_gettime64 (clockid
, &rt
)))
60 /* Compute relative timeout. */
61 rt
.tv_sec
= abstime
->tv_sec
- rt
.tv_sec
;
62 rt
.tv_nsec
= abstime
->tv_nsec
- rt
.tv_nsec
;
65 rt
.tv_nsec
+= 1000000000;
69 /* Already timed out? */
73 /* If *tidp == tid, wait until thread terminates or the wait times out.
74 The kernel up to version 3.16.3 does not use the private futex
75 operations for futex wake-up when the clone terminates. */
76 ret
= futex_timed_wait_cancel64 (tidp
, tid
, &rt
, LLL_SHARED
);
77 if (ret
== -ETIMEDOUT
|| ret
== -EOVERFLOW
)
85 __pthread_clockjoin_ex (pthread_t threadid
, void **thread_return
,
87 const struct __timespec64
*abstime
, bool block
)
89 struct pthread
*pd
= (struct pthread
*) threadid
;
91 /* Make sure the descriptor is valid. */
92 if (INVALID_NOT_TERMINATED_TD_P (pd
))
93 /* Not a valid thread handle. */
96 /* Is the thread joinable?. */
98 /* We cannot wait for the thread. */
101 struct pthread
*self
= THREAD_SELF
;
104 LIBC_PROBE (pthread_join
, 1, threadid
);
107 || (self
->joinid
== pd
108 && (pd
->cancelhandling
109 & (CANCELING_BITMASK
| CANCELED_BITMASK
| EXITING_BITMASK
110 | TERMINATED_BITMASK
)) == 0))
111 && !CANCEL_ENABLED_AND_CANCELED (self
->cancelhandling
))
112 /* This is a deadlock situation. The threads are waiting for each
113 other to finish. Note that this is a "may" error. To be 100%
114 sure we catch this error we would have to lock the data
115 structures but it is not necessary. In the unlikely case that
116 two threads are really caught in this situation they will
117 deadlock. It is the programmer's problem to figure this
121 /* Wait for the thread to finish. If it is already locked something
122 is wrong. There can only be one waiter. */
123 else if (__glibc_unlikely (atomic_compare_exchange_weak_acquire (&pd
->joinid
,
126 /* There is already somebody waiting for the thread. */
129 /* BLOCK waits either indefinitely or based on an absolute time. POSIX also
130 states a cancellation point shall occur for pthread_join, and we use the
131 same rationale for posix_timedjoin_np. Both clockwait_tid and the futex
132 call use the cancellable variant. */
135 /* During the wait we change to asynchronous cancellation. If we
136 are cancelled the thread we are waiting for must be marked as
137 un-wait-ed for again. */
138 pthread_cleanup_push (cleanup
, &pd
->joinid
);
141 result
= clockwait_tid (&pd
->tid
, clockid
, abstime
);
145 /* We need acquire MO here so that we synchronize with the
146 kernel's store to 0 when the clone terminates. (see above) */
147 while ((tid
= atomic_load_acquire (&pd
->tid
)) != 0)
148 lll_futex_wait_cancel (&pd
->tid
, tid
, LLL_SHARED
);
151 pthread_cleanup_pop (0);
154 void *pd_result
= pd
->result
;
155 if (__glibc_likely (result
== 0))
157 /* We mark the thread as terminated and as joined. */
160 /* Store the return value if the caller is interested. */
161 if (thread_return
!= NULL
)
162 *thread_return
= pd_result
;
170 LIBC_PROBE (pthread_join_ret
, 3, threadid
, result
, pd_result
);