]> git.ipfire.org Git - thirdparty/glibc.git/blob - nptl/pthread_join_common.c
Rename sys/ucontext.h to bits/ucontext.h.
[thirdparty/glibc.git] / 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.
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 <https://www.gnu.org/licenses/>. */
18
19 #include "pthreadP.h"
20 #include <atomic.h>
21 #include <stap-probe.h>
22 #include <time.h>
23
24 #include <sys/time.h>
25
26 static void
27 cleanup (void *arg)
28 {
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);
34 }
35
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. */
41 static int
42 clockwait_tid (pid_t *tidp, clockid_t clockid, const struct timespec *abstime)
43 {
44 pid_t tid;
45
46 if (! valid_nanoseconds (abstime->tv_nsec))
47 return EINVAL;
48
49 /* Repeat until thread terminated. */
50 while ((tid = *tidp) != 0)
51 {
52 struct timespec rt;
53
54 /* Get the current time. This can only fail if clockid is
55 invalid. */
56 if (__glibc_unlikely (__clock_gettime (clockid, &rt)))
57 return EINVAL;
58
59 /* Compute relative timeout. */
60 rt.tv_sec = abstime->tv_sec - rt.tv_sec;
61 rt.tv_nsec = abstime->tv_nsec - rt.tv_nsec;
62 if (rt.tv_nsec < 0)
63 {
64 rt.tv_nsec += 1000000000;
65 --rt.tv_sec;
66 }
67
68 /* Already timed out? */
69 if (rt.tv_sec < 0)
70 return ETIMEDOUT;
71
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)
76 == -ETIMEDOUT)
77 return ETIMEDOUT;
78 }
79
80 return 0;
81 }
82
83 int
84 __pthread_clockjoin_ex (pthread_t threadid, void **thread_return,
85 clockid_t clockid,
86 const struct timespec *abstime, bool block)
87 {
88 struct pthread *pd = (struct pthread *) threadid;
89
90 /* Make sure the descriptor is valid. */
91 if (INVALID_NOT_TERMINATED_TD_P (pd))
92 /* Not a valid thread handle. */
93 return ESRCH;
94
95 /* Is the thread joinable?. */
96 if (IS_DETACHED (pd))
97 /* We cannot wait for the thread. */
98 return EINVAL;
99
100 struct pthread *self = THREAD_SELF;
101 int result = 0;
102
103 LIBC_PROBE (pthread_join, 1, threadid);
104
105 if ((pd == self
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
117 out. */
118 return EDEADLK;
119
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,
123 &self,
124 NULL)))
125 /* There is already somebody waiting for the thread. */
126 return EINVAL;
127
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. */
132 if (block)
133 {
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);
138
139 if (abstime != NULL)
140 result = clockwait_tid (&pd->tid, clockid, abstime);
141 else
142 {
143 pid_t tid;
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);
148 }
149
150 pthread_cleanup_pop (0);
151 }
152
153 void *pd_result = pd->result;
154 if (__glibc_likely (result == 0))
155 {
156 /* We mark the thread as terminated and as joined. */
157 pd->tid = -1;
158
159 /* Store the return value if the caller is interested. */
160 if (thread_return != NULL)
161 *thread_return = pd_result;
162
163 /* Free the TCB. */
164 __free_tcb (pd);
165 }
166 else
167 pd->joinid = NULL;
168
169 LIBC_PROBE (pthread_join_ret, 3, threadid, result, pd_result);
170
171 return result;
172 }