hurd: Bump remaining LGPL2+ htl licences to LGPL 2.1+
[glibc.git] / sysdeps / htl / pt-cond-timedwait.c
blob7784a4a271ee7c408198c3d57a3e0c28c3cd5f8d
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/>. */
19 #include <pthread.h>
21 #include <pt-internal.h>
23 extern int __pthread_cond_timedwait_internal (pthread_cond_t *cond,
24 pthread_mutex_t *mutex,
25 const struct timespec *abstime);
27 int
28 __pthread_cond_timedwait (pthread_cond_t *cond,
29 pthread_mutex_t *mutex,
30 const struct timespec *abstime)
32 return __pthread_cond_timedwait_internal (cond, mutex, abstime);
35 strong_alias (__pthread_cond_timedwait, pthread_cond_timedwait);
37 struct cancel_ctx
39 struct __pthread *wakeup;
40 pthread_cond_t *cond;
43 static void
44 cancel_hook (void *arg)
46 struct cancel_ctx *ctx = arg;
47 struct __pthread *wakeup = ctx->wakeup;
48 pthread_cond_t *cond = ctx->cond;
49 int unblock;
51 __pthread_spin_lock (&cond->__lock);
52 /* The thread only needs to be awaken if it's blocking or about to block.
53 If it was already unblocked, it's not queued any more. */
54 unblock = wakeup->prevp != NULL;
55 if (unblock)
56 __pthread_dequeue (wakeup);
57 __pthread_spin_unlock (&cond->__lock);
59 if (unblock)
60 __pthread_wakeup (wakeup);
63 /* Block on condition variable COND until ABSTIME. As a GNU
64 extension, if ABSTIME is NULL, then wait forever. MUTEX should be
65 held by the calling thread. On return, MUTEX will be held by the
66 calling thread. */
67 int
68 __pthread_cond_timedwait_internal (pthread_cond_t *cond,
69 pthread_mutex_t *mutex,
70 const struct timespec *abstime)
72 error_t err;
73 int cancelled, oldtype, drain;
74 clockid_t clock_id = __pthread_default_condattr.__clock;
76 if (abstime && (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000))
77 return EINVAL;
79 struct __pthread *self = _pthread_self ();
80 struct cancel_ctx ctx;
81 ctx.wakeup = self;
82 ctx.cond = cond;
84 /* Test for a pending cancellation request, switch to deferred mode for
85 safer resource handling, and prepare the hook to call in case we're
86 cancelled while blocking. Once CANCEL_LOCK is released, the cancellation
87 hook can be called by another thread at any time. Whatever happens,
88 this function must exit with MUTEX locked.
90 This function contains inline implementations of pthread_testcancel and
91 pthread_setcanceltype to reduce locking overhead. */
92 __pthread_mutex_lock (&self->cancel_lock);
93 cancelled = (self->cancel_state == PTHREAD_CANCEL_ENABLE)
94 && self->cancel_pending;
96 if (!cancelled)
98 self->cancel_hook = cancel_hook;
99 self->cancel_hook_arg = &ctx;
100 oldtype = self->cancel_type;
102 if (oldtype != PTHREAD_CANCEL_DEFERRED)
103 self->cancel_type = PTHREAD_CANCEL_DEFERRED;
105 /* Add ourselves to the list of waiters. This is done while setting
106 the cancellation hook to simplify the cancellation procedure, i.e.
107 if the thread is queued, it can be cancelled, otherwise it is
108 already unblocked, progressing on the return path. */
109 __pthread_spin_lock (&cond->__lock);
110 __pthread_enqueue (&cond->__queue, self);
111 if (cond->__attr != NULL)
112 clock_id = cond->__attr->__clock;
113 __pthread_spin_unlock (&cond->__lock);
115 __pthread_mutex_unlock (&self->cancel_lock);
117 if (cancelled)
118 pthread_exit (PTHREAD_CANCELED);
120 /* Release MUTEX before blocking. */
121 __pthread_mutex_unlock (mutex);
123 /* Block the thread. */
124 if (abstime != NULL)
125 err = __pthread_timedblock (self, abstime, clock_id);
126 else
128 err = 0;
129 __pthread_block (self);
132 __pthread_spin_lock (&cond->__lock);
133 if (self->prevp == NULL)
135 /* Another thread removed us from the list of waiters, which means a
136 wakeup message has been sent. It was either consumed while we were
137 blocking, or queued after we timed out and before we acquired the
138 condition lock, in which case the message queue must be drained. */
139 if (!err)
140 drain = 0;
141 else
143 assert (err == ETIMEDOUT);
144 drain = 1;
147 else
149 /* We're still in the list of waiters. Noone attempted to wake us up,
150 i.e. we timed out. */
151 assert (err == ETIMEDOUT);
152 __pthread_dequeue (self);
153 drain = 0;
155 __pthread_spin_unlock (&cond->__lock);
157 if (drain)
158 __pthread_block (self);
160 /* We're almost done. Remove the unblock hook, restore the previous
161 cancellation type, and check for a pending cancellation request. */
162 __pthread_mutex_lock (&self->cancel_lock);
163 self->cancel_hook = NULL;
164 self->cancel_hook_arg = NULL;
165 self->cancel_type = oldtype;
166 cancelled = (self->cancel_state == PTHREAD_CANCEL_ENABLE)
167 && self->cancel_pending;
168 __pthread_mutex_unlock (&self->cancel_lock);
170 /* Reacquire MUTEX before returning/cancelling. */
171 __pthread_mutex_lock (mutex);
173 if (cancelled)
174 pthread_exit (PTHREAD_CANCELED);
176 return err;