]>
git.ipfire.org Git - thirdparty/glibc.git/blob - nptl/pthread_join_common.c
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>
29 /* If we already changed the waiter ID, reset it. The call cannot
30 fail for any reason but the thread not having done that yet so
31 there is no reason for a loop. */
32 struct pthread
*self
= THREAD_SELF
;
33 atomic_compare_exchange_weak_acquire (&arg
, &self
, NULL
);
36 /* The kernel notifies a process which uses CLONE_CHILD_CLEARTID via futex
37 wake-up when the clone terminates. The memory location contains the
38 thread ID while the clone is running and is reset to zero by the kernel
39 afterwards. The kernel up to version 3.16.3 does not use the private futex
40 operations for futex wake-up when the clone terminates. */
42 clockwait_tid (pid_t
*tidp
, clockid_t clockid
, const struct timespec
*abstime
)
46 if (! valid_nanoseconds (abstime
->tv_nsec
))
49 /* Repeat until thread terminated. */
50 while ((tid
= *tidp
) != 0)
54 /* Get the current time. This can only fail if clockid is
56 if (__glibc_unlikely (__clock_gettime (clockid
, &rt
)))
59 /* Compute relative timeout. */
60 rt
.tv_sec
= abstime
->tv_sec
- rt
.tv_sec
;
61 rt
.tv_nsec
= abstime
->tv_nsec
- rt
.tv_nsec
;
64 rt
.tv_nsec
+= 1000000000;
68 /* Already timed out? */
72 /* If *tidp == tid, wait until thread terminates or the wait times out.
73 The kernel up to version 3.16.3 does not use the private futex
74 operations for futex wake-up when the clone terminates. */
75 if (lll_futex_timed_wait_cancel (tidp
, tid
, &rt
, LLL_SHARED
)
84 __pthread_clockjoin_ex (pthread_t threadid
, void **thread_return
,
86 const struct timespec
*abstime
, bool block
)
88 struct pthread
*pd
= (struct pthread
*) threadid
;
90 /* Make sure the descriptor is valid. */
91 if (INVALID_NOT_TERMINATED_TD_P (pd
))
92 /* Not a valid thread handle. */
95 /* Is the thread joinable?. */
97 /* We cannot wait for the thread. */
100 struct pthread
*self
= THREAD_SELF
;
103 LIBC_PROBE (pthread_join
, 1, threadid
);
106 || (self
->joinid
== pd
107 && (pd
->cancelhandling
108 & (CANCELING_BITMASK
| CANCELED_BITMASK
| EXITING_BITMASK
109 | TERMINATED_BITMASK
)) == 0))
110 && !CANCEL_ENABLED_AND_CANCELED (self
->cancelhandling
))
111 /* This is a deadlock situation. The threads are waiting for each
112 other to finish. Note that this is a "may" error. To be 100%
113 sure we catch this error we would have to lock the data
114 structures but it is not necessary. In the unlikely case that
115 two threads are really caught in this situation they will
116 deadlock. It is the programmer's problem to figure this
120 /* Wait for the thread to finish. If it is already locked something
121 is wrong. There can only be one waiter. */
122 else if (__glibc_unlikely (atomic_compare_exchange_weak_acquire (&pd
->joinid
,
125 /* There is already somebody waiting for the thread. */
128 /* BLOCK waits either indefinitely or based on an absolute time. POSIX also
129 states a cancellation point shall occur for pthread_join, and we use the
130 same rationale for posix_timedjoin_np. Both clockwait_tid and the futex
131 call use the cancellable variant. */
134 /* During the wait we change to asynchronous cancellation. If we
135 are cancelled the thread we are waiting for must be marked as
136 un-wait-ed for again. */
137 pthread_cleanup_push (cleanup
, &pd
->joinid
);
140 result
= clockwait_tid (&pd
->tid
, clockid
, abstime
);
144 /* We need acquire MO here so that we synchronize with the
145 kernel's store to 0 when the clone terminates. (see above) */
146 while ((tid
= atomic_load_acquire (&pd
->tid
)) != 0)
147 lll_futex_wait_cancel (&pd
->tid
, tid
, LLL_SHARED
);
150 pthread_cleanup_pop (0);
153 void *pd_result
= pd
->result
;
154 if (__glibc_likely (result
== 0))
156 /* We mark the thread as terminated and as joined. */
159 /* Store the return value if the caller is interested. */
160 if (thread_return
!= NULL
)
161 *thread_return
= pd_result
;
169 LIBC_PROBE (pthread_join_ret
, 3, threadid
, result
, pd_result
);