1 /* Wait on a condition. Generic version.
2 Copyright (C) 2000-2018 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 <http://www.gnu.org/licenses/>. */
21 #include <pt-internal.h>
24 extern int __pthread_cond_timedwait_internal (pthread_cond_t
*cond
,
25 pthread_mutex_t
*mutex
,
26 const struct timespec
*abstime
);
29 __pthread_cond_timedwait (pthread_cond_t
*cond
,
30 pthread_mutex_t
*mutex
,
31 const struct timespec
*abstime
)
33 return __pthread_cond_timedwait_internal (cond
, mutex
, abstime
);
36 strong_alias (__pthread_cond_timedwait
, pthread_cond_timedwait
);
40 struct __pthread
*wakeup
;
45 cancel_hook (void *arg
)
47 struct cancel_ctx
*ctx
= arg
;
48 struct __pthread
*wakeup
= ctx
->wakeup
;
49 pthread_cond_t
*cond
= ctx
->cond
;
52 __pthread_spin_lock (&cond
->__lock
);
53 /* The thread only needs to be awaken if it's blocking or about to block.
54 If it was already unblocked, it's not queued any more. */
55 unblock
= wakeup
->prevp
!= NULL
;
57 __pthread_dequeue (wakeup
);
58 __pthread_spin_unlock (&cond
->__lock
);
61 __pthread_wakeup (wakeup
);
64 /* Block on condition variable COND until ABSTIME. As a GNU
65 extension, if ABSTIME is NULL, then wait forever. MUTEX should be
66 held by the calling thread. On return, MUTEX will be held by the
69 __pthread_cond_timedwait_internal (pthread_cond_t
*cond
,
70 pthread_mutex_t
*mutex
,
71 const struct timespec
*abstime
)
74 int cancelled
, oldtype
, drain
;
75 clockid_t clock_id
= __pthread_default_condattr
.__clock
;
77 if (abstime
&& (abstime
->tv_nsec
< 0 || abstime
->tv_nsec
>= 1000000000))
80 struct __pthread
*self
= _pthread_self ();
81 struct cancel_ctx ctx
;
85 /* Test for a pending cancellation request, switch to deferred mode for
86 safer resource handling, and prepare the hook to call in case we're
87 cancelled while blocking. Once CANCEL_LOCK is released, the cancellation
88 hook can be called by another thread at any time. Whatever happens,
89 this function must exit with MUTEX locked.
91 This function contains inline implementations of pthread_testcancel and
92 pthread_setcanceltype to reduce locking overhead. */
93 __pthread_mutex_lock (&self
->cancel_lock
);
94 cancelled
= (self
->cancel_state
== PTHREAD_CANCEL_ENABLE
)
95 && self
->cancel_pending
;
99 self
->cancel_hook
= cancel_hook
;
100 self
->cancel_hook_arg
= &ctx
;
101 oldtype
= self
->cancel_type
;
103 if (oldtype
!= PTHREAD_CANCEL_DEFERRED
)
104 self
->cancel_type
= PTHREAD_CANCEL_DEFERRED
;
106 /* Add ourselves to the list of waiters. This is done while setting
107 the cancellation hook to simplify the cancellation procedure, i.e.
108 if the thread is queued, it can be cancelled, otherwise it is
109 already unblocked, progressing on the return path. */
110 __pthread_spin_lock (&cond
->__lock
);
111 __pthread_enqueue (&cond
->__queue
, self
);
112 if (cond
->__attr
!= NULL
)
113 clock_id
= cond
->__attr
->__clock
;
114 __pthread_spin_unlock (&cond
->__lock
);
116 __pthread_mutex_unlock (&self
->cancel_lock
);
119 __pthread_exit (PTHREAD_CANCELED
);
121 /* Release MUTEX before blocking. */
122 __pthread_mutex_unlock (mutex
);
124 /* Block the thread. */
126 err
= __pthread_timedblock (self
, abstime
, clock_id
);
130 __pthread_block (self
);
133 __pthread_spin_lock (&cond
->__lock
);
134 if (self
->prevp
== NULL
)
136 /* Another thread removed us from the list of waiters, which means a
137 wakeup message has been sent. It was either consumed while we were
138 blocking, or queued after we timed out and before we acquired the
139 condition lock, in which case the message queue must be drained. */
144 assert (err
== ETIMEDOUT
);
150 /* We're still in the list of waiters. Noone attempted to wake us up,
151 i.e. we timed out. */
152 assert (err
== ETIMEDOUT
);
153 __pthread_dequeue (self
);
156 __pthread_spin_unlock (&cond
->__lock
);
159 __pthread_block (self
);
161 /* We're almost done. Remove the unblock hook, restore the previous
162 cancellation type, and check for a pending cancellation request. */
163 __pthread_mutex_lock (&self
->cancel_lock
);
164 self
->cancel_hook
= NULL
;
165 self
->cancel_hook_arg
= NULL
;
166 self
->cancel_type
= oldtype
;
167 cancelled
= (self
->cancel_state
== PTHREAD_CANCEL_ENABLE
)
168 && self
->cancel_pending
;
169 __pthread_mutex_unlock (&self
->cancel_lock
);
171 /* Reacquire MUTEX before returning/cancelling. */
172 __pthread_mutex_lock (mutex
);
175 __pthread_exit (PTHREAD_CANCELED
);