initial commit
[glibc.git] / .pc / hurd-i386 / git-cond-destroy.diff / sysdeps / mach / hurd / htl / pt-hurd-cond-timedwait.c
1 /* pthread_hurd_cond_timedwait_np. Hurd-specific wait on a condition.
2 Copyright (C) 2012-2022 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 <https://www.gnu.org/licenses/>. */
18
19 #include <pthread.h>
20 #include <assert.h>
21 #include <hurd/signal.h>
22 #include <time.h>
23
24 #include <pt-internal.h>
25
26 extern int __pthread_hurd_cond_timedwait_internal (pthread_cond_t *cond,
27 pthread_mutex_t *mutex,
28 const struct timespec
29 *abstime);
30
31 int
32 __pthread_hurd_cond_timedwait_np (pthread_cond_t *cond,
33 pthread_mutex_t *mutex,
34 const struct timespec *abstime)
35 {
36 return __pthread_hurd_cond_timedwait_internal (cond, mutex, abstime);
37 }
38
39 strong_alias (__pthread_hurd_cond_timedwait_np, pthread_hurd_cond_timedwait_np);
40
41 int
42 __pthread_hurd_cond_timedwait_internal (pthread_cond_t *cond,
43 pthread_mutex_t *mutex,
44 const struct timespec *abstime)
45 {
46 struct hurd_sigstate *ss = _hurd_self_sigstate ();
47 struct __pthread *self = _pthread_self ();
48 error_t err = 0;
49 int cancel, drain;
50 clockid_t clock_id = __pthread_default_condattr.__clock;
51
52 /* This function will be called by hurd_thread_cancel while we are blocked
53 We wake up our thread if it's still blocking or about to block, so it will
54 progress and notice the cancellation flag. */
55 void cancel_me (void)
56 {
57 int unblock;
58
59 __pthread_spin_wait (&cond->__lock);
60 /* The thread only needs to be awaken if it's blocking or about to block.
61 If it was already unblocked, it's not queued any more. */
62 unblock = self->prevp != NULL;
63 if (unblock)
64 __pthread_dequeue (self);
65 __pthread_spin_unlock (&cond->__lock);
66
67 if (unblock)
68 __pthread_wakeup (self);
69 }
70
71 assert (ss->intr_port == MACH_PORT_NULL); /* Sanity check for signal bugs. */
72
73 if (abstime != NULL && ! valid_nanoseconds (abstime->tv_nsec))
74 return EINVAL;
75
76 /* Atomically enqueue our thread on the condition variable's queue of
77 waiters, and mark our sigstate to indicate that `cancel_me' must be
78 called to wake us up. We must hold the sigstate lock while acquiring
79 the condition variable's lock and tweaking it, so that
80 hurd_thread_cancel can never suspend us and then deadlock waiting for
81 the condition variable's lock. */
82
83 __spin_lock (&ss->lock);
84 __pthread_spin_wait (&cond->__lock);
85 cancel = ss->cancel;
86 if (cancel)
87 /* We were cancelled before doing anything. Don't block at all. */
88 ss->cancel = 0;
89 else
90 {
91 /* Put us on the queue so that pthread_cond_broadcast will know to wake
92 us up. */
93 __pthread_enqueue (&cond->__queue, self);
94 if (cond->__attr)
95 clock_id = cond->__attr->__clock;
96 /* Tell hurd_thread_cancel how to unblock us. */
97 ss->cancel_hook = &cancel_me;
98 }
99 __pthread_spin_unlock (&cond->__lock);
100 __spin_unlock (&ss->lock);
101
102 if (cancel)
103 {
104 /* Cancelled on entry. Just leave the mutex locked. */
105 mutex = NULL;
106
107 __spin_lock (&ss->lock);
108 }
109 else
110 {
111 /* Release MUTEX before blocking. */
112 __pthread_mutex_unlock (mutex);
113
114 /* Increase the waiter reference count. Relaxed MO is sufficient because
115 we only need to synchronize when decrementing the reference count. */
116 atomic_fetch_add_relaxed (&cond->__wrefs, 2);
117
118 /* Block the thread. */
119 if (abstime != NULL)
120 err = __pthread_timedblock (self, abstime, clock_id);
121 else
122 {
123 err = 0;
124 __pthread_block (self);
125 }
126
127 /* As it was done when enqueueing, prevent hurd_thread_cancel from
128 suspending us while the condition lock is held. */
129 __spin_lock (&ss->lock);
130 __pthread_spin_wait (&cond->__lock);
131 if (self->prevp == NULL)
132 /* Another thread removed us from the list of waiters, which means
133 a wakeup message has been sent. It was either consumed while
134 we were blocking, or queued after we timed out and before we
135 acquired the condition lock, in which case the message queue
136 must be drained. */
137 drain = err ? 1 : 0;
138 else
139 {
140 /* We're still in the list of waiters. Noone attempted to wake us
141 up, i.e. we timed out. */
142 __pthread_dequeue (self);
143 drain = 0;
144 }
145 __pthread_spin_unlock (&cond->__lock);
146
147 if (drain)
148 __pthread_block (self);
149 }
150
151 /* If destruction is pending (i.e., the wake-request flag is nonzero) and we
152 are the last waiter (prior value of __wrefs was 1 << 1), then wake any
153 threads waiting in pthread_cond_destroy. Release MO to synchronize with
154 these threads. Don't bother clearing the wake-up request flag. */
155 if ((atomic_fetch_add_release (&cond->__wrefs, -2)) == 3)
156 __gsync_wake (__mach_task_self (), (vm_offset_t) &cond->__wrefs, 0, 0);
157
158 /* Clear the hook, now that we are done blocking. */
159 ss->cancel_hook = NULL;
160 /* Check the cancellation flag; we might have unblocked due to
161 cancellation rather than a normal pthread_cond_signal or
162 pthread_cond_broadcast (or we might have just happened to get cancelled
163 right after waking up). */
164 cancel |= ss->cancel;
165 ss->cancel = 0;
166 __spin_unlock (&ss->lock);
167
168 if (mutex != NULL)
169 /* Reacquire the mutex and return. */
170 __pthread_mutex_lock (mutex);
171
172 if (cancel)
173 return EINTR;
174 else if (err)
175 {
176 assert (err == ETIMEDOUT);
177 return err;
178 }
179
180 return 0;
181 }