]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/htl/pt-cond-timedwait.c
hurd: Bump remaining LGPL2+ htl licences to LGPL 2.1+
[thirdparty/glibc.git] / sysdeps / htl / pt-cond-timedwait.c
CommitLineData
33574c17
ST
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.
4
5 The GNU C Library is free software; you can redistribute it and/or
ad2b41bf
ST
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.
33574c17
ST
9
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
ad2b41bf 13 Lesser General Public License for more details.
33574c17 14
ad2b41bf
ST
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/>. */
33574c17
ST
18
19#include <pthread.h>
20
21#include <pt-internal.h>
22
23extern int __pthread_cond_timedwait_internal (pthread_cond_t *cond,
24 pthread_mutex_t *mutex,
25 const struct timespec *abstime);
26
27int
28__pthread_cond_timedwait (pthread_cond_t *cond,
29 pthread_mutex_t *mutex,
30 const struct timespec *abstime)
31{
32 return __pthread_cond_timedwait_internal (cond, mutex, abstime);
33}
34
35strong_alias (__pthread_cond_timedwait, pthread_cond_timedwait);
36
37struct cancel_ctx
38{
39 struct __pthread *wakeup;
40 pthread_cond_t *cond;
41};
42
43static void
44cancel_hook (void *arg)
45{
46 struct cancel_ctx *ctx = arg;
47 struct __pthread *wakeup = ctx->wakeup;
48 pthread_cond_t *cond = ctx->cond;
49 int unblock;
50
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);
58
59 if (unblock)
60 __pthread_wakeup (wakeup);
61}
62
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. */
67int
68__pthread_cond_timedwait_internal (pthread_cond_t *cond,
69 pthread_mutex_t *mutex,
70 const struct timespec *abstime)
71{
72 error_t err;
73 int cancelled, oldtype, drain;
74 clockid_t clock_id = __pthread_default_condattr.__clock;
75
76 if (abstime && (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000))
77 return EINVAL;
78
79 struct __pthread *self = _pthread_self ();
80 struct cancel_ctx ctx;
81 ctx.wakeup = self;
82 ctx.cond = cond;
83
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.
89
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;
95
96 if (!cancelled)
97 {
98 self->cancel_hook = cancel_hook;
99 self->cancel_hook_arg = &ctx;
100 oldtype = self->cancel_type;
101
102 if (oldtype != PTHREAD_CANCEL_DEFERRED)
103 self->cancel_type = PTHREAD_CANCEL_DEFERRED;
104
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);
114 }
115 __pthread_mutex_unlock (&self->cancel_lock);
116
117 if (cancelled)
118 pthread_exit (PTHREAD_CANCELED);
119
120 /* Release MUTEX before blocking. */
121 __pthread_mutex_unlock (mutex);
122
123 /* Block the thread. */
124 if (abstime != NULL)
125 err = __pthread_timedblock (self, abstime, clock_id);
126 else
127 {
128 err = 0;
129 __pthread_block (self);
130 }
131
132 __pthread_spin_lock (&cond->__lock);
133 if (self->prevp == NULL)
134 {
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
142 {
143 assert (err == ETIMEDOUT);
144 drain = 1;
145 }
146 }
147 else
148 {
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;
154 }
155 __pthread_spin_unlock (&cond->__lock);
156
157 if (drain)
158 __pthread_block (self);
159
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);
169
170 /* Reacquire MUTEX before returning/cancelling. */
171 __pthread_mutex_lock (mutex);
172
173 if (cancelled)
174 pthread_exit (PTHREAD_CANCELED);
175
176 return err;
177}