Warning: That file was not part of the compilation database. It may have many parsing errors.

1/* Wait on a condition. Generic version.
2 Copyright (C) 2000-2019 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
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.
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
13 Lesser General Public License for more details.
14
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/>. */
18
19#include <pthread.h>
20
21#include <pt-internal.h>
22#include <pthreadP.h>
23
24extern int __pthread_cond_timedwait_internal (pthread_cond_t *cond,
25 pthread_mutex_t *mutex,
26 const struct timespec *abstime);
27
28int
29__pthread_cond_timedwait (pthread_cond_t *cond,
30 pthread_mutex_t *mutex,
31 const struct timespec *abstime)
32{
33 return __pthread_cond_timedwait_internal (cond, mutex, abstime);
34}
35
36strong_alias (__pthread_cond_timedwait, pthread_cond_timedwait);
37
38struct cancel_ctx
39{
40 struct __pthread *wakeup;
41 pthread_cond_t *cond;
42};
43
44static void
45cancel_hook (void *arg)
46{
47 struct cancel_ctx *ctx = arg;
48 struct __pthread *wakeup = ctx->wakeup;
49 pthread_cond_t *cond = ctx->cond;
50 int unblock;
51
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;
56 if (unblock)
57 __pthread_dequeue (wakeup);
58 __pthread_spin_unlock (&cond->__lock);
59
60 if (unblock)
61 __pthread_wakeup (wakeup);
62}
63
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
67 calling thread. */
68int
69__pthread_cond_timedwait_internal (pthread_cond_t *cond,
70 pthread_mutex_t *mutex,
71 const struct timespec *abstime)
72{
73 error_t err;
74 int cancelled, oldtype, drain;
75 clockid_t clock_id = __pthread_default_condattr.__clock;
76
77 if (abstime && (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000))
78 return EINVAL;
79
80 struct __pthread *self = _pthread_self ();
81 struct cancel_ctx ctx;
82 ctx.wakeup = self;
83 ctx.cond = cond;
84
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.
90
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;
96
97 if (!cancelled)
98 {
99 self->cancel_hook = cancel_hook;
100 self->cancel_hook_arg = &ctx;
101 oldtype = self->cancel_type;
102
103 if (oldtype != PTHREAD_CANCEL_DEFERRED)
104 self->cancel_type = PTHREAD_CANCEL_DEFERRED;
105
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);
115 }
116 __pthread_mutex_unlock (&self->cancel_lock);
117
118 if (cancelled)
119 __pthread_exit (PTHREAD_CANCELED);
120
121 /* Release MUTEX before blocking. */
122 __pthread_mutex_unlock (mutex);
123
124 /* Block the thread. */
125 if (abstime != NULL)
126 err = __pthread_timedblock (self, abstime, clock_id);
127 else
128 {
129 err = 0;
130 __pthread_block (self);
131 }
132
133 __pthread_spin_lock (&cond->__lock);
134 if (self->prevp == NULL)
135 {
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. */
140 if (!err)
141 drain = 0;
142 else
143 {
144 assert (err == ETIMEDOUT);
145 drain = 1;
146 }
147 }
148 else
149 {
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);
154 drain = 0;
155 }
156 __pthread_spin_unlock (&cond->__lock);
157
158 if (drain)
159 __pthread_block (self);
160
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);
170
171 /* Reacquire MUTEX before returning/cancelling. */
172 __pthread_mutex_lock (mutex);
173
174 if (cancelled)
175 __pthread_exit (PTHREAD_CANCELED);
176
177 return err;
178}
179

Warning: That file was not part of the compilation database. It may have many parsing errors.