]>
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-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 timedwait_tid (pid_t
*tidp
, 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. */
53 __clock_gettime (CLOCK_REALTIME
, &rt
);
55 /* Compute relative timeout. */
56 rt
.tv_sec
= abstime
->tv_sec
- rt
.tv_sec
;
57 rt
.tv_nsec
= abstime
->tv_nsec
- rt
.tv_nsec
;
60 rt
.tv_nsec
+= 1000000000;
64 /* Already timed out? */
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
)
80 __pthread_timedjoin_ex (pthread_t threadid
, void **thread_return
,
81 const struct timespec
*abstime
, bool block
)
83 struct pthread
*pd
= (struct pthread
*) threadid
;
85 /* Make sure the descriptor is valid. */
86 if (INVALID_NOT_TERMINATED_TD_P (pd
))
87 /* Not a valid thread handle. */
90 /* Is the thread joinable?. */
92 /* We cannot wait for the thread. */
95 struct pthread
*self
= THREAD_SELF
;
98 LIBC_PROBE (pthread_join
, 1, threadid
);
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
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
,
120 /* There is already somebody waiting for the thread. */
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. */
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
);
135 result
= timedwait_tid (&pd
->tid
, abstime
);
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
);
145 pthread_cleanup_pop (0);
148 void *pd_result
= pd
->result
;
149 if (__glibc_likely (result
== 0))
151 /* We mark the thread as terminated and as joined. */
154 /* Store the return value if the caller is interested. */
155 if (thread_return
!= NULL
)
156 *thread_return
= pd_result
;
164 LIBC_PROBE (pthread_join_ret
, 3, threadid
, result
, pd_result
);
168 hidden_def (__pthread_timedjoin_ex
)