1 /* Wait on a condition. Generic version.
2 Copyright (C) 2000-2022 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 <pt-internal.h>
25 extern int __pthread_cond_timedwait_internal (pthread_cond_t
*cond
,
26 pthread_mutex_t
*mutex
,
28 const struct timespec
*abstime
);
31 __pthread_cond_timedwait (pthread_cond_t
*cond
,
32 pthread_mutex_t
*mutex
,
33 const struct timespec
*abstime
)
35 return __pthread_cond_timedwait_internal (cond
, mutex
, -1, abstime
);
38 weak_alias (__pthread_cond_timedwait
, pthread_cond_timedwait
);
41 __pthread_cond_clockwait (pthread_cond_t
*cond
,
42 pthread_mutex_t
*mutex
,
44 const struct timespec
*abstime
)
46 return __pthread_cond_timedwait_internal (cond
, mutex
, clockid
, abstime
);
49 weak_alias (__pthread_cond_clockwait
, pthread_cond_clockwait
);
53 struct __pthread
*wakeup
;
58 cancel_hook (void *arg
)
60 struct cancel_ctx
*ctx
= arg
;
61 struct __pthread
*wakeup
= ctx
->wakeup
;
62 pthread_cond_t
*cond
= ctx
->cond
;
65 __pthread_spin_wait (&cond
->__lock
);
66 /* The thread only needs to be awaken if it's blocking or about to block.
67 If it was already unblocked, it's not queued any more. */
68 unblock
= wakeup
->prevp
!= NULL
;
70 __pthread_dequeue (wakeup
);
71 __pthread_spin_unlock (&cond
->__lock
);
74 __pthread_wakeup (wakeup
);
77 /* Block on condition variable COND until ABSTIME. As a GNU
78 extension, if ABSTIME is NULL, then wait forever. MUTEX should be
79 held by the calling thread. On return, MUTEX will be held by the
82 __pthread_cond_timedwait_internal (pthread_cond_t
*cond
,
83 pthread_mutex_t
*mutex
,
85 const struct timespec
*abstime
)
88 int cancelled
, oldtype
, drain
;
94 clock_id
= __pthread_default_condattr
.__clock
;
96 if (abstime
&& ! valid_nanoseconds (abstime
->tv_nsec
))
99 err
= __pthread_mutex_checklocked (mutex
);
103 struct __pthread
*self
= _pthread_self ();
104 struct cancel_ctx ctx
;
108 /* Test for a pending cancellation request, switch to deferred mode for
109 safer resource handling, and prepare the hook to call in case we're
110 cancelled while blocking. Once CANCEL_LOCK is released, the cancellation
111 hook can be called by another thread at any time. Whatever happens,
112 this function must exit with MUTEX locked.
114 This function contains inline implementations of pthread_testcancel and
115 pthread_setcanceltype to reduce locking overhead. */
116 __pthread_mutex_lock (&self
->cancel_lock
);
117 cancelled
= (self
->cancel_state
== PTHREAD_CANCEL_ENABLE
)
118 && self
->cancel_pending
;
122 __pthread_mutex_unlock (&self
->cancel_lock
);
123 __pthread_exit (PTHREAD_CANCELED
);
126 self
->cancel_hook
= cancel_hook
;
127 self
->cancel_hook_arg
= &ctx
;
128 oldtype
= self
->cancel_type
;
130 if (oldtype
!= PTHREAD_CANCEL_DEFERRED
)
131 self
->cancel_type
= PTHREAD_CANCEL_DEFERRED
;
133 /* Add ourselves to the list of waiters. This is done while setting
134 the cancellation hook to simplify the cancellation procedure, i.e.
135 if the thread is queued, it can be cancelled, otherwise it is
136 already unblocked, progressing on the return path. */
137 __pthread_spin_wait (&cond
->__lock
);
138 __pthread_enqueue (&cond
->__queue
, self
);
139 if (cond
->__attr
!= NULL
&& clockid
== -1)
140 clock_id
= cond
->__attr
->__clock
;
141 __pthread_spin_unlock (&cond
->__lock
);
143 __pthread_mutex_unlock (&self
->cancel_lock
);
145 /* Release MUTEX before blocking. */
146 __pthread_mutex_unlock (mutex
);
148 /* Increase the waiter reference count. Relaxed MO is sufficient because
149 we only need to synchronize when decrementing the reference count. */
150 atomic_fetch_add_relaxed (&cond
->__wrefs
, 2);
152 /* Block the thread. */
154 err
= __pthread_timedblock (self
, abstime
, clock_id
);
158 __pthread_block (self
);
161 __pthread_spin_wait (&cond
->__lock
);
162 if (self
->prevp
== NULL
)
164 /* Another thread removed us from the list of waiters, which means a
165 wakeup message has been sent. It was either consumed while we were
166 blocking, or queued after we timed out and before we acquired the
167 condition lock, in which case the message queue must be drained. */
172 assert (err
== ETIMEDOUT
);
178 /* We're still in the list of waiters. Noone attempted to wake us up,
179 i.e. we timed out. */
180 assert (err
== ETIMEDOUT
);
181 __pthread_dequeue (self
);
184 __pthread_spin_unlock (&cond
->__lock
);
186 /* If destruction is pending (i.e., the wake-request flag is nonzero) and we
187 are the last waiter (prior value of __wrefs was 1 << 1), then wake any
188 threads waiting in pthread_cond_destroy. Release MO to synchronize with
189 these threads. Don't bother clearing the wake-up request flag. */
190 if ((atomic_fetch_add_release (&cond
->__wrefs
, -2)) == 3)
191 __gsync_wake (__mach_task_self (), (vm_offset_t
) &cond
->__wrefs
, 0, 0);
194 __pthread_block (self
);
196 /* We're almost done. Remove the unblock hook, restore the previous
197 cancellation type, and check for a pending cancellation request. */
198 __pthread_mutex_lock (&self
->cancel_lock
);
199 self
->cancel_hook
= NULL
;
200 self
->cancel_hook_arg
= NULL
;
201 self
->cancel_type
= oldtype
;
202 cancelled
= (self
->cancel_state
== PTHREAD_CANCEL_ENABLE
)
203 && self
->cancel_pending
;
204 __pthread_mutex_unlock (&self
->cancel_lock
);
206 /* Reacquire MUTEX before returning/cancelling. */
207 __pthread_mutex_lock (mutex
);
210 __pthread_exit (PTHREAD_CANCELED
);