1 /* $NetBSD: pthread_cond.c,v 1.77 2022/02/12 14:59:32 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2001, 2006, 2007, 2008, 2020 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nathan J. Williams and Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: pthread_cond.c,v 1.77 2022/02/12 14:59:32 riastradh Exp $");
34
35 /* Need to use libc-private names for atomic operations. */
36 #include "../../common/lib/libc/atomic/atomic_op_namespace.h"
37
38 #include <stdlib.h>
39 #include <errno.h>
40 #include <sys/time.h>
41 #include <sys/types.h>
42
43 #include "pthread.h"
44 #include "pthread_int.h"
45 #include "reentrant.h"
46
47 int _sys___nanosleep50(const struct timespec *, struct timespec *);
48
49 int _pthread_cond_has_waiters_np(pthread_cond_t *);
50
51 __weak_alias(pthread_cond_has_waiters_np,_pthread_cond_has_waiters_np)
52
53 __strong_alias(__libc_cond_init,pthread_cond_init)
54 __strong_alias(__libc_cond_signal,pthread_cond_signal)
55 __strong_alias(__libc_cond_broadcast,pthread_cond_broadcast)
56 __strong_alias(__libc_cond_wait,pthread_cond_wait)
57 __strong_alias(__libc_cond_timedwait,pthread_cond_timedwait)
58 __strong_alias(__libc_cond_destroy,pthread_cond_destroy)
59
60 /*
61 * A dummy waiter that's used to flag that pthread_cond_signal() is in
62 * progress and nobody else should try to modify the waiter list until
63 * it completes.
64 */
65 static struct pthread__waiter pthread__cond_dummy;
66
67 static clockid_t
pthread_cond_getclock(const pthread_cond_t * cond)68 pthread_cond_getclock(const pthread_cond_t *cond)
69 {
70
71 pthread__error(EINVAL, "Invalid condition variable",
72 cond->ptc_magic == _PT_COND_MAGIC);
73
74 return cond->ptc_private ?
75 *(clockid_t *)cond->ptc_private : CLOCK_REALTIME;
76 }
77
78 int
pthread_cond_init(pthread_cond_t * cond,const pthread_condattr_t * attr)79 pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
80 {
81 if (__predict_false(__uselibcstub))
82 return __libc_cond_init_stub(cond, attr);
83
84 pthread__error(EINVAL, "Invalid condition variable attribute",
85 (attr == NULL) || (attr->ptca_magic == _PT_CONDATTR_MAGIC));
86
87 cond->ptc_magic = _PT_COND_MAGIC;
88 cond->ptc_waiters = NULL;
89 cond->ptc_mutex = NULL;
90 if (attr && attr->ptca_private) {
91 cond->ptc_private = malloc(sizeof(clockid_t));
92 if (cond->ptc_private == NULL)
93 return errno;
94 *(clockid_t *)cond->ptc_private =
95 *(clockid_t *)attr->ptca_private;
96 } else
97 cond->ptc_private = NULL;
98
99 return 0;
100 }
101
102
103 int
pthread_cond_destroy(pthread_cond_t * cond)104 pthread_cond_destroy(pthread_cond_t *cond)
105 {
106 if (__predict_false(__uselibcstub))
107 return __libc_cond_destroy_stub(cond);
108
109 pthread__error(EINVAL, "Invalid condition variable",
110 cond->ptc_magic == _PT_COND_MAGIC);
111 pthread__error(EBUSY, "Destroying condition variable in use",
112 cond->ptc_waiters == NULL);
113
114 cond->ptc_magic = _PT_COND_DEAD;
115 free(cond->ptc_private);
116
117 return 0;
118 }
119
120 int
pthread_cond_timedwait(pthread_cond_t * cond,pthread_mutex_t * mutex,const struct timespec * abstime)121 pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
122 const struct timespec *abstime)
123 {
124 struct pthread__waiter waiter, *next, *head;
125 pthread_t self;
126 int error, cancel;
127 clockid_t clkid = pthread_cond_getclock(cond);
128
129 if (__predict_false(__uselibcstub))
130 return __libc_cond_timedwait_stub(cond, mutex, abstime);
131
132 pthread__error(EINVAL, "Invalid condition variable",
133 cond->ptc_magic == _PT_COND_MAGIC);
134 pthread__error(EINVAL, "Invalid mutex",
135 mutex->ptm_magic == _PT_MUTEX_MAGIC);
136 pthread__error(EPERM, "Mutex not locked in condition wait",
137 mutex->ptm_owner != NULL);
138
139 self = pthread__self();
140 pthread__assert(self->pt_lid != 0);
141
142 if (__predict_false(self->pt_cancel)) {
143 pthread__cancelled();
144 }
145
146 /* Note this thread as waiting on the CV. */
147 cond->ptc_mutex = mutex;
148 for (head = cond->ptc_waiters;; head = next) {
149 /* Wait while pthread_cond_signal() in progress. */
150 if (__predict_false(head == &pthread__cond_dummy)) {
151 sched_yield();
152 next = cond->ptc_waiters;
153 continue;
154 }
155 waiter.lid = self->pt_lid;
156 waiter.next = head;
157 #ifndef PTHREAD__ATOMIC_IS_MEMBAR
158 membar_producer();
159 #endif
160 next = atomic_cas_ptr(&cond->ptc_waiters, head, &waiter);
161 if (__predict_true(next == head)) {
162 break;
163 }
164 }
165
166 /* Drop the interlock and wait. */
167 error = 0;
168 pthread_mutex_unlock(mutex);
169 while (waiter.lid && !(cancel = self->pt_cancel)) {
170 int rv = _lwp_park(clkid, TIMER_ABSTIME, __UNCONST(abstime),
171 0, NULL, NULL);
172 if (rv == 0) {
173 continue;
174 }
175 if (errno != EINTR && errno != EALREADY) {
176 error = errno;
177 break;
178 }
179 }
180 pthread_mutex_lock(mutex);
181
182 /*
183 * If this thread absorbed a wakeup from pthread_cond_signal() and
184 * cannot take the wakeup, we should ensure that another thread does.
185 *
186 * And if awoken early, we may still be on the waiter list and must
187 * remove self.
188 */
189 if (__predict_false(cancel | error)) {
190 pthread_cond_broadcast(cond);
191
192 /*
193 * Might have raced with another thread to do the wakeup.
194 * Wait until released, otherwise "waiter" is still globally
195 * visible.
196 */
197 pthread_mutex_unlock(mutex);
198 while (__predict_false(waiter.lid)) {
199 (void)_lwp_park(CLOCK_MONOTONIC, 0, NULL, 0, NULL,
200 NULL);
201 }
202 pthread_mutex_lock(mutex);
203 } else {
204 pthread__assert(!waiter.lid);
205 }
206
207 /*
208 * If cancelled then exit. POSIX dictates that the mutex must be
209 * held if this happens.
210 */
211 if (cancel) {
212 pthread__cancelled();
213 }
214
215 return error;
216 }
217
218 int
pthread_cond_wait(pthread_cond_t * cond,pthread_mutex_t * mutex)219 pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
220 {
221 if (__predict_false(__uselibcstub))
222 return __libc_cond_wait_stub(cond, mutex);
223
224 return pthread_cond_timedwait(cond, mutex, NULL);
225 }
226
227 int
pthread_cond_signal(pthread_cond_t * cond)228 pthread_cond_signal(pthread_cond_t *cond)
229 {
230 struct pthread__waiter *head, *next;
231 pthread_mutex_t *mutex;
232 pthread_t self;
233
234 if (__predict_false(__uselibcstub))
235 return __libc_cond_signal_stub(cond);
236
237 pthread__error(EINVAL, "Invalid condition variable",
238 cond->ptc_magic == _PT_COND_MAGIC);
239
240 /* Take ownership of one waiter. */
241 self = pthread_self();
242 mutex = cond->ptc_mutex;
243 for (head = cond->ptc_waiters;; head = next) {
244 /* Wait while pthread_cond_signal() in progress. */
245 if (__predict_false(head == &pthread__cond_dummy)) {
246 sched_yield();
247 next = cond->ptc_waiters;
248 continue;
249 }
250 if (head == NULL) {
251 return 0;
252 }
253 /* Block concurrent access to the waiter list. */
254 next = atomic_cas_ptr(&cond->ptc_waiters, head,
255 &pthread__cond_dummy);
256 if (__predict_true(next == head)) {
257 break;
258 }
259 }
260
261 /* Now that list is locked, read pointer to next and then unlock. */
262 membar_enter();
263 cond->ptc_waiters = head->next;
264 membar_producer();
265 head->next = NULL;
266
267 /* Now transfer waiter to the mutex. */
268 pthread__mutex_deferwake(self, mutex, head);
269 return 0;
270 }
271
272 int
pthread_cond_broadcast(pthread_cond_t * cond)273 pthread_cond_broadcast(pthread_cond_t *cond)
274 {
275 struct pthread__waiter *head, *next;
276 pthread_mutex_t *mutex;
277 pthread_t self;
278
279 if (__predict_false(__uselibcstub))
280 return __libc_cond_broadcast_stub(cond);
281
282 pthread__error(EINVAL, "Invalid condition variable",
283 cond->ptc_magic == _PT_COND_MAGIC);
284
285 if (cond->ptc_waiters == NULL)
286 return 0;
287
288 /* Take ownership of current set of waiters. */
289 self = pthread_self();
290 mutex = cond->ptc_mutex;
291 for (head = cond->ptc_waiters;; head = next) {
292 /* Wait while pthread_cond_signal() in progress. */
293 if (__predict_false(head == &pthread__cond_dummy)) {
294 sched_yield();
295 next = cond->ptc_waiters;
296 continue;
297 }
298 if (head == NULL) {
299 return 0;
300 }
301 next = atomic_cas_ptr(&cond->ptc_waiters, head, NULL);
302 if (__predict_true(next == head)) {
303 break;
304 }
305 }
306 membar_enter();
307
308 /* Now transfer waiters to the mutex. */
309 pthread__mutex_deferwake(self, mutex, head);
310 return 0;
311 }
312
313 int
_pthread_cond_has_waiters_np(pthread_cond_t * cond)314 _pthread_cond_has_waiters_np(pthread_cond_t *cond)
315 {
316
317 return cond->ptc_waiters != NULL;
318 }
319
320 int
pthread_condattr_init(pthread_condattr_t * attr)321 pthread_condattr_init(pthread_condattr_t *attr)
322 {
323
324 attr->ptca_magic = _PT_CONDATTR_MAGIC;
325 attr->ptca_private = NULL;
326
327 return 0;
328 }
329
330 int
pthread_condattr_setclock(pthread_condattr_t * attr,clockid_t clck)331 pthread_condattr_setclock(pthread_condattr_t *attr, clockid_t clck)
332 {
333
334 pthread__error(EINVAL, "Invalid condition variable attribute",
335 attr->ptca_magic == _PT_CONDATTR_MAGIC);
336
337 switch (clck) {
338 case CLOCK_MONOTONIC:
339 case CLOCK_REALTIME:
340 if (attr->ptca_private == NULL)
341 attr->ptca_private = malloc(sizeof(clockid_t));
342 if (attr->ptca_private == NULL)
343 return errno;
344 *(clockid_t *)attr->ptca_private = clck;
345 return 0;
346 default:
347 return EINVAL;
348 }
349 }
350
351 int
pthread_condattr_getclock(const pthread_condattr_t * __restrict attr,clockid_t * __restrict clock_id)352 pthread_condattr_getclock(const pthread_condattr_t *__restrict attr,
353 clockid_t *__restrict clock_id)
354 {
355
356 pthread__error(EINVAL, "Invalid condition variable attribute",
357 attr->ptca_magic == _PT_CONDATTR_MAGIC);
358
359 if (attr == NULL || attr->ptca_private == NULL)
360 return EINVAL;
361 *clock_id = *(clockid_t *)attr->ptca_private;
362 return 0;
363 }
364
365 int
pthread_condattr_destroy(pthread_condattr_t * attr)366 pthread_condattr_destroy(pthread_condattr_t *attr)
367 {
368
369 pthread__error(EINVAL, "Invalid condition variable attribute",
370 attr->ptca_magic == _PT_CONDATTR_MAGIC);
371
372 attr->ptca_magic = _PT_CONDATTR_DEAD;
373 free(attr->ptca_private);
374
375 return 0;
376 }
377
378 #ifdef _PTHREAD_PSHARED
379 int
pthread_condattr_getpshared(const pthread_condattr_t * __restrict attr,int * __restrict pshared)380 pthread_condattr_getpshared(const pthread_condattr_t * __restrict attr,
381 int * __restrict pshared)
382 {
383
384 pthread__error(EINVAL, "Invalid condition variable attribute",
385 attr->ptca_magic == _PT_CONDATTR_MAGIC);
386
387 *pshared = PTHREAD_PROCESS_PRIVATE;
388 return 0;
389 }
390
391 int
pthread_condattr_setpshared(pthread_condattr_t * attr,int pshared)392 pthread_condattr_setpshared(pthread_condattr_t *attr, int pshared)
393 {
394
395 pthread__error(EINVAL, "Invalid condition variable attribute",
396 attr->ptca_magic == _PT_CONDATTR_MAGIC);
397
398 switch(pshared) {
399 case PTHREAD_PROCESS_PRIVATE:
400 return 0;
401 case PTHREAD_PROCESS_SHARED:
402 return ENOSYS;
403 }
404 return EINVAL;
405 }
406 #endif
407