1*7adb4107Sriastradh /* $NetBSD: pthread_cond.c,v 1.77 2022/02/12 14:59:32 riastradh Exp $ */
2c62a74e6Sthorpej
3c62a74e6Sthorpej /*-
406d492d1Sad * Copyright (c) 2001, 2006, 2007, 2008, 2020 The NetBSD Foundation, Inc.
5c62a74e6Sthorpej * All rights reserved.
6c62a74e6Sthorpej *
7c62a74e6Sthorpej * This code is derived from software contributed to The NetBSD Foundation
8fe47a5c7Sad * by Nathan J. Williams and Andrew Doran.
9c62a74e6Sthorpej *
10c62a74e6Sthorpej * Redistribution and use in source and binary forms, with or without
11c62a74e6Sthorpej * modification, are permitted provided that the following conditions
12c62a74e6Sthorpej * are met:
13c62a74e6Sthorpej * 1. Redistributions of source code must retain the above copyright
14c62a74e6Sthorpej * notice, this list of conditions and the following disclaimer.
15c62a74e6Sthorpej * 2. Redistributions in binary form must reproduce the above copyright
16c62a74e6Sthorpej * notice, this list of conditions and the following disclaimer in the
17c62a74e6Sthorpej * documentation and/or other materials provided with the distribution.
18c62a74e6Sthorpej *
19c62a74e6Sthorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20c62a74e6Sthorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21c62a74e6Sthorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22c62a74e6Sthorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23c62a74e6Sthorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24c62a74e6Sthorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25c62a74e6Sthorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26c62a74e6Sthorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27c62a74e6Sthorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28c62a74e6Sthorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29c62a74e6Sthorpej * POSSIBILITY OF SUCH DAMAGE.
30c62a74e6Sthorpej */
31c62a74e6Sthorpej
32c62a74e6Sthorpej #include <sys/cdefs.h>
33*7adb4107Sriastradh __RCSID("$NetBSD: pthread_cond.c,v 1.77 2022/02/12 14:59:32 riastradh Exp $");
34*7adb4107Sriastradh
35*7adb4107Sriastradh /* Need to use libc-private names for atomic operations. */
36*7adb4107Sriastradh #include "../../common/lib/libc/atomic/atomic_op_namespace.h"
37f043c0fbSlukem
3871d484f9Schristos #include <stdlib.h>
39f043c0fbSlukem #include <errno.h>
40947e41dfSnathanw #include <sys/time.h>
41947e41dfSnathanw #include <sys/types.h>
42c62a74e6Sthorpej
43c62a74e6Sthorpej #include "pthread.h"
44c62a74e6Sthorpej #include "pthread_int.h"
4571d484f9Schristos #include "reentrant.h"
46c62a74e6Sthorpej
471982972aSdrochner int _sys___nanosleep50(const struct timespec *, struct timespec *);
48947e41dfSnathanw
496ebb8696Spooka int _pthread_cond_has_waiters_np(pthread_cond_t *);
506ebb8696Spooka
51639a0868Syamt __weak_alias(pthread_cond_has_waiters_np,_pthread_cond_has_waiters_np)
526ebb8696Spooka
53c62a74e6Sthorpej __strong_alias(__libc_cond_init,pthread_cond_init)
54c62a74e6Sthorpej __strong_alias(__libc_cond_signal,pthread_cond_signal)
55c62a74e6Sthorpej __strong_alias(__libc_cond_broadcast,pthread_cond_broadcast)
56c62a74e6Sthorpej __strong_alias(__libc_cond_wait,pthread_cond_wait)
57c62a74e6Sthorpej __strong_alias(__libc_cond_timedwait,pthread_cond_timedwait)
58c62a74e6Sthorpej __strong_alias(__libc_cond_destroy,pthread_cond_destroy)
59c62a74e6Sthorpej
607a60fa0aSad /*
617a60fa0aSad * A dummy waiter that's used to flag that pthread_cond_signal() is in
627a60fa0aSad * progress and nobody else should try to modify the waiter list until
637a60fa0aSad * it completes.
647a60fa0aSad */
657a60fa0aSad static struct pthread__waiter pthread__cond_dummy;
667a60fa0aSad
671f8d3d6eSchristos static clockid_t
pthread_cond_getclock(const pthread_cond_t * cond)681f8d3d6eSchristos pthread_cond_getclock(const pthread_cond_t *cond)
691f8d3d6eSchristos {
70e06a99c9Skamil
71e06a99c9Skamil pthread__error(EINVAL, "Invalid condition variable",
72e06a99c9Skamil cond->ptc_magic == _PT_COND_MAGIC);
73e06a99c9Skamil
741f8d3d6eSchristos return cond->ptc_private ?
751f8d3d6eSchristos *(clockid_t *)cond->ptc_private : CLOCK_REALTIME;
761f8d3d6eSchristos }
771f8d3d6eSchristos
78c62a74e6Sthorpej int
pthread_cond_init(pthread_cond_t * cond,const pthread_condattr_t * attr)79c62a74e6Sthorpej pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
80c62a74e6Sthorpej {
8171d484f9Schristos if (__predict_false(__uselibcstub))
8271d484f9Schristos return __libc_cond_init_stub(cond, attr);
83c62a74e6Sthorpej
843f6de8d8Snathanw pthread__error(EINVAL, "Invalid condition variable attribute",
853f6de8d8Snathanw (attr == NULL) || (attr->ptca_magic == _PT_CONDATTR_MAGIC));
86c62a74e6Sthorpej
87c62a74e6Sthorpej cond->ptc_magic = _PT_COND_MAGIC;
8806d492d1Sad cond->ptc_waiters = NULL;
89c62a74e6Sthorpej cond->ptc_mutex = NULL;
902a29ccd3Schristos if (attr && attr->ptca_private) {
912a29ccd3Schristos cond->ptc_private = malloc(sizeof(clockid_t));
922a29ccd3Schristos if (cond->ptc_private == NULL)
932a29ccd3Schristos return errno;
942a29ccd3Schristos *(clockid_t *)cond->ptc_private =
952a29ccd3Schristos *(clockid_t *)attr->ptca_private;
962a29ccd3Schristos } else
972a29ccd3Schristos cond->ptc_private = NULL;
98c62a74e6Sthorpej
99c62a74e6Sthorpej return 0;
100c62a74e6Sthorpej }
101c62a74e6Sthorpej
102c62a74e6Sthorpej
103c62a74e6Sthorpej int
pthread_cond_destroy(pthread_cond_t * cond)104c62a74e6Sthorpej pthread_cond_destroy(pthread_cond_t *cond)
105c62a74e6Sthorpej {
10671d484f9Schristos if (__predict_false(__uselibcstub))
10771d484f9Schristos return __libc_cond_destroy_stub(cond);
108c62a74e6Sthorpej
1093f6de8d8Snathanw pthread__error(EINVAL, "Invalid condition variable",
1103f6de8d8Snathanw cond->ptc_magic == _PT_COND_MAGIC);
1113f6de8d8Snathanw pthread__error(EBUSY, "Destroying condition variable in use",
11206d492d1Sad cond->ptc_waiters == NULL);
113c62a74e6Sthorpej
114c62a74e6Sthorpej cond->ptc_magic = _PT_COND_DEAD;
1152a29ccd3Schristos free(cond->ptc_private);
116c62a74e6Sthorpej
117c62a74e6Sthorpej return 0;
118c62a74e6Sthorpej }
119c62a74e6Sthorpej
12074a7f3e6Sjoerg int
pthread_cond_timedwait(pthread_cond_t * cond,pthread_mutex_t * mutex,const struct timespec * abstime)121c62a74e6Sthorpej pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
122c62a74e6Sthorpej const struct timespec *abstime)
123c62a74e6Sthorpej {
1247a60fa0aSad struct pthread__waiter waiter, *next, *head;
12562e0939eSad pthread_t self;
12662e0939eSad int error, cancel;
127cdce479aSchristos clockid_t clkid = pthread_cond_getclock(cond);
128c62a74e6Sthorpej
12971d484f9Schristos if (__predict_false(__uselibcstub))
13071d484f9Schristos return __libc_cond_timedwait_stub(cond, mutex, abstime);
13171d484f9Schristos
1323f6de8d8Snathanw pthread__error(EINVAL, "Invalid condition variable",
1333f6de8d8Snathanw cond->ptc_magic == _PT_COND_MAGIC);
1343f6de8d8Snathanw pthread__error(EINVAL, "Invalid mutex",
1353f6de8d8Snathanw mutex->ptm_magic == _PT_MUTEX_MAGIC);
1363f6de8d8Snathanw pthread__error(EPERM, "Mutex not locked in condition wait",
1378ccc6e06Sad mutex->ptm_owner != NULL);
138b7ac97b8Snathanw
139c62a74e6Sthorpej self = pthread__self();
14062e0939eSad pthread__assert(self->pt_lid != 0);
141947e41dfSnathanw
1422bcb8bf1Sad if (__predict_false(self->pt_cancel)) {
1432bcb8bf1Sad pthread__cancelled();
1442bcb8bf1Sad }
1452bcb8bf1Sad
1462bcb8bf1Sad /* Note this thread as waiting on the CV. */
147f4fd6b79Sad cond->ptc_mutex = mutex;
1487a60fa0aSad for (head = cond->ptc_waiters;; head = next) {
1497a60fa0aSad /* Wait while pthread_cond_signal() in progress. */
1507a60fa0aSad if (__predict_false(head == &pthread__cond_dummy)) {
1517a60fa0aSad sched_yield();
1527a60fa0aSad next = cond->ptc_waiters;
1537a60fa0aSad continue;
1547a60fa0aSad }
15562e0939eSad waiter.lid = self->pt_lid;
1567a60fa0aSad waiter.next = head;
15706d492d1Sad #ifndef PTHREAD__ATOMIC_IS_MEMBAR
15806d492d1Sad membar_producer();
15906d492d1Sad #endif
1607a60fa0aSad next = atomic_cas_ptr(&cond->ptc_waiters, head, &waiter);
1617a60fa0aSad if (__predict_true(next == head)) {
16206d492d1Sad break;
16306d492d1Sad }
16462e0939eSad }
16550fa8db4Sad
1667a60fa0aSad /* Drop the interlock and wait. */
16762e0939eSad error = 0;
1687a60fa0aSad pthread_mutex_unlock(mutex);
16962e0939eSad while (waiter.lid && !(cancel = self->pt_cancel)) {
17062e0939eSad int rv = _lwp_park(clkid, TIMER_ABSTIME, __UNCONST(abstime),
17162e0939eSad 0, NULL, NULL);
17262e0939eSad if (rv == 0) {
17362e0939eSad continue;
17462e0939eSad }
1757a60fa0aSad if (errno != EINTR && errno != EALREADY) {
17662e0939eSad error = errno;
17762e0939eSad break;
17806d492d1Sad }
17906d492d1Sad }
1807a60fa0aSad pthread_mutex_lock(mutex);
18150fa8db4Sad
18250fa8db4Sad /*
183051faad4Sad * If this thread absorbed a wakeup from pthread_cond_signal() and
18462e0939eSad * cannot take the wakeup, we should ensure that another thread does.
18550fa8db4Sad *
186051faad4Sad * And if awoken early, we may still be on the waiter list and must
187051faad4Sad * remove self.
188051faad4Sad */
18962e0939eSad if (__predict_false(cancel | error)) {
190051faad4Sad pthread_cond_broadcast(cond);
191051faad4Sad
192051faad4Sad /*
193051faad4Sad * Might have raced with another thread to do the wakeup.
19462e0939eSad * Wait until released, otherwise "waiter" is still globally
19562e0939eSad * visible.
196051faad4Sad */
1977a60fa0aSad pthread_mutex_unlock(mutex);
19862e0939eSad while (__predict_false(waiter.lid)) {
19962e0939eSad (void)_lwp_park(CLOCK_MONOTONIC, 0, NULL, 0, NULL,
20062e0939eSad NULL);
201b81fe3a2Sad }
2027a60fa0aSad pthread_mutex_lock(mutex);
20362e0939eSad } else {
20462e0939eSad pthread__assert(!waiter.lid);
205051faad4Sad }
206051faad4Sad
207051faad4Sad /*
208051faad4Sad * If cancelled then exit. POSIX dictates that the mutex must be
209051faad4Sad * held if this happens.
21050fa8db4Sad */
211051faad4Sad if (cancel) {
212622bbc50Sad pthread__cancelled();
213b5a5e72aSad }
214c62a74e6Sthorpej
21562e0939eSad return error;
216c62a74e6Sthorpej }
217c62a74e6Sthorpej
218c62a74e6Sthorpej int
pthread_cond_wait(pthread_cond_t * cond,pthread_mutex_t * mutex)2192bcb8bf1Sad pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
2202bcb8bf1Sad {
22171d484f9Schristos if (__predict_false(__uselibcstub))
22271d484f9Schristos return __libc_cond_wait_stub(cond, mutex);
2232bcb8bf1Sad
2242bcb8bf1Sad return pthread_cond_timedwait(cond, mutex, NULL);
2252bcb8bf1Sad }
2262bcb8bf1Sad
227c62a74e6Sthorpej int
pthread_cond_signal(pthread_cond_t * cond)22810ba2675Sad pthread_cond_signal(pthread_cond_t *cond)
22910ba2675Sad {
2307a60fa0aSad struct pthread__waiter *head, *next;
23106d492d1Sad pthread_mutex_t *mutex;
23262e0939eSad pthread_t self;
23310ba2675Sad
23471d484f9Schristos if (__predict_false(__uselibcstub))
23571d484f9Schristos return __libc_cond_signal_stub(cond);
23671d484f9Schristos
237e06a99c9Skamil pthread__error(EINVAL, "Invalid condition variable",
238e06a99c9Skamil cond->ptc_magic == _PT_COND_MAGIC);
239e06a99c9Skamil
24006d492d1Sad /* Take ownership of one waiter. */
24106d492d1Sad self = pthread_self();
2422bcb8bf1Sad mutex = cond->ptc_mutex;
2437a60fa0aSad for (head = cond->ptc_waiters;; head = next) {
2447a60fa0aSad /* Wait while pthread_cond_signal() in progress. */
2457a60fa0aSad if (__predict_false(head == &pthread__cond_dummy)) {
2467a60fa0aSad sched_yield();
2477a60fa0aSad next = cond->ptc_waiters;
2487a60fa0aSad continue;
2497a60fa0aSad }
2507a60fa0aSad if (head == NULL) {
25106d492d1Sad return 0;
25206d492d1Sad }
2537a60fa0aSad /* Block concurrent access to the waiter list. */
2547a60fa0aSad next = atomic_cas_ptr(&cond->ptc_waiters, head,
2557a60fa0aSad &pthread__cond_dummy);
2567a60fa0aSad if (__predict_true(next == head)) {
25706d492d1Sad break;
25806d492d1Sad }
25906d492d1Sad }
26062e0939eSad
2617a60fa0aSad /* Now that list is locked, read pointer to next and then unlock. */
2627a60fa0aSad membar_enter();
2637a60fa0aSad cond->ptc_waiters = head->next;
2647a60fa0aSad membar_producer();
2657a60fa0aSad head->next = NULL;
2667a60fa0aSad
26762e0939eSad /* Now transfer waiter to the mutex. */
2687a60fa0aSad pthread__mutex_deferwake(self, mutex, head);
269c62a74e6Sthorpej return 0;
270c62a74e6Sthorpej }
271c62a74e6Sthorpej
27210ba2675Sad int
pthread_cond_broadcast(pthread_cond_t * cond)27310ba2675Sad pthread_cond_broadcast(pthread_cond_t *cond)
27410ba2675Sad {
2757a60fa0aSad struct pthread__waiter *head, *next;
27606d492d1Sad pthread_mutex_t *mutex;
27762e0939eSad pthread_t self;
27806d492d1Sad
27971d484f9Schristos if (__predict_false(__uselibcstub))
28071d484f9Schristos return __libc_cond_broadcast_stub(cond);
28110ba2675Sad
282e06a99c9Skamil pthread__error(EINVAL, "Invalid condition variable",
283e06a99c9Skamil cond->ptc_magic == _PT_COND_MAGIC);
284e06a99c9Skamil
28506d492d1Sad if (cond->ptc_waiters == NULL)
28610ba2675Sad return 0;
28706d492d1Sad
2887a60fa0aSad /* Take ownership of current set of waiters. */
28906d492d1Sad self = pthread_self();
29006d492d1Sad mutex = cond->ptc_mutex;
2917a60fa0aSad for (head = cond->ptc_waiters;; head = next) {
2927a60fa0aSad /* Wait while pthread_cond_signal() in progress. */
2937a60fa0aSad if (__predict_false(head == &pthread__cond_dummy)) {
2947a60fa0aSad sched_yield();
2957a60fa0aSad next = cond->ptc_waiters;
2967a60fa0aSad continue;
2977a60fa0aSad }
29862e0939eSad if (head == NULL) {
29962e0939eSad return 0;
30062e0939eSad }
3017a60fa0aSad next = atomic_cas_ptr(&cond->ptc_waiters, head, NULL);
3027a60fa0aSad if (__predict_true(next == head)) {
3037a60fa0aSad break;
3047a60fa0aSad }
3057a60fa0aSad }
3067a60fa0aSad membar_enter();
30706d492d1Sad
30862e0939eSad /* Now transfer waiters to the mutex. */
30962e0939eSad pthread__mutex_deferwake(self, mutex, head);
31006d492d1Sad return 0;
31110ba2675Sad }
312c62a74e6Sthorpej
313c62a74e6Sthorpej int
_pthread_cond_has_waiters_np(pthread_cond_t * cond)3146ebb8696Spooka _pthread_cond_has_waiters_np(pthread_cond_t *cond)
3156ebb8696Spooka {
3166ebb8696Spooka
31706d492d1Sad return cond->ptc_waiters != NULL;
3186ebb8696Spooka }
3196ebb8696Spooka
3206ebb8696Spooka int
pthread_condattr_init(pthread_condattr_t * attr)321c62a74e6Sthorpej pthread_condattr_init(pthread_condattr_t *attr)
322c62a74e6Sthorpej {
323c62a74e6Sthorpej
324c62a74e6Sthorpej attr->ptca_magic = _PT_CONDATTR_MAGIC;
3252a29ccd3Schristos attr->ptca_private = NULL;
326c62a74e6Sthorpej
327c62a74e6Sthorpej return 0;
328c62a74e6Sthorpej }
329c62a74e6Sthorpej
330c62a74e6Sthorpej int
pthread_condattr_setclock(pthread_condattr_t * attr,clockid_t clck)3312a29ccd3Schristos pthread_condattr_setclock(pthread_condattr_t *attr, clockid_t clck)
3322a29ccd3Schristos {
333e06a99c9Skamil
334e06a99c9Skamil pthread__error(EINVAL, "Invalid condition variable attribute",
335e06a99c9Skamil attr->ptca_magic == _PT_CONDATTR_MAGIC);
336e06a99c9Skamil
3372a29ccd3Schristos switch (clck) {
3382a29ccd3Schristos case CLOCK_MONOTONIC:
3392a29ccd3Schristos case CLOCK_REALTIME:
3402a29ccd3Schristos if (attr->ptca_private == NULL)
3412a29ccd3Schristos attr->ptca_private = malloc(sizeof(clockid_t));
3422a29ccd3Schristos if (attr->ptca_private == NULL)
3432a29ccd3Schristos return errno;
3442a29ccd3Schristos *(clockid_t *)attr->ptca_private = clck;
3452a29ccd3Schristos return 0;
3462a29ccd3Schristos default:
3472a29ccd3Schristos return EINVAL;
3482a29ccd3Schristos }
3492a29ccd3Schristos }
3502a29ccd3Schristos
3512a29ccd3Schristos int
pthread_condattr_getclock(const pthread_condattr_t * __restrict attr,clockid_t * __restrict clock_id)3527cf7644fSchristos pthread_condattr_getclock(const pthread_condattr_t *__restrict attr,
3537cf7644fSchristos clockid_t *__restrict clock_id)
3547cf7644fSchristos {
355e06a99c9Skamil
356e06a99c9Skamil pthread__error(EINVAL, "Invalid condition variable attribute",
357e06a99c9Skamil attr->ptca_magic == _PT_CONDATTR_MAGIC);
358e06a99c9Skamil
3597cf7644fSchristos if (attr == NULL || attr->ptca_private == NULL)
3607cf7644fSchristos return EINVAL;
3617cf7644fSchristos *clock_id = *(clockid_t *)attr->ptca_private;
3627cf7644fSchristos return 0;
3637cf7644fSchristos }
3647cf7644fSchristos
3657cf7644fSchristos int
pthread_condattr_destroy(pthread_condattr_t * attr)366c62a74e6Sthorpej pthread_condattr_destroy(pthread_condattr_t *attr)
367c62a74e6Sthorpej {
368c62a74e6Sthorpej
3693f6de8d8Snathanw pthread__error(EINVAL, "Invalid condition variable attribute",
3703f6de8d8Snathanw attr->ptca_magic == _PT_CONDATTR_MAGIC);
371c62a74e6Sthorpej
372c62a74e6Sthorpej attr->ptca_magic = _PT_CONDATTR_DEAD;
3732a29ccd3Schristos free(attr->ptca_private);
374c62a74e6Sthorpej
375c62a74e6Sthorpej return 0;
376c62a74e6Sthorpej }
377947e41dfSnathanw
3787cf7644fSchristos #ifdef _PTHREAD_PSHARED
3797cf7644fSchristos int
pthread_condattr_getpshared(const pthread_condattr_t * __restrict attr,int * __restrict pshared)3807cf7644fSchristos pthread_condattr_getpshared(const pthread_condattr_t * __restrict attr,
3817cf7644fSchristos int * __restrict pshared)
3827cf7644fSchristos {
3837cf7644fSchristos
384e06a99c9Skamil pthread__error(EINVAL, "Invalid condition variable attribute",
385e06a99c9Skamil attr->ptca_magic == _PT_CONDATTR_MAGIC);
386e06a99c9Skamil
3877cf7644fSchristos *pshared = PTHREAD_PROCESS_PRIVATE;
3887cf7644fSchristos return 0;
3897cf7644fSchristos }
3907cf7644fSchristos
3917cf7644fSchristos int
pthread_condattr_setpshared(pthread_condattr_t * attr,int pshared)3927cf7644fSchristos pthread_condattr_setpshared(pthread_condattr_t *attr, int pshared)
3937cf7644fSchristos {
3947cf7644fSchristos
395e06a99c9Skamil pthread__error(EINVAL, "Invalid condition variable attribute",
396e06a99c9Skamil attr->ptca_magic == _PT_CONDATTR_MAGIC);
397e06a99c9Skamil
3987cf7644fSchristos switch(pshared) {
3997cf7644fSchristos case PTHREAD_PROCESS_PRIVATE:
4007cf7644fSchristos return 0;
4017cf7644fSchristos case PTHREAD_PROCESS_SHARED:
4027cf7644fSchristos return ENOSYS;
4037cf7644fSchristos }
4047cf7644fSchristos return EINVAL;
4057cf7644fSchristos }
4067cf7644fSchristos #endif
407