source: mainline/uspace/lib/c/generic/private/futex.h@ 269bc459

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 269bc459 was 269bc459, checked in by Jakub Jermar <jakub@…>, 7 years ago

Add SYS_WAITQ_DESTROY

  • Property mode set to 100644
File size: 7.2 KB
Line 
1/*
2 * Copyright (c) 2006 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libc
30 * @{
31 */
32/** @file
33 */
34
35#ifndef LIBC_FUTEX_H_
36#define LIBC_FUTEX_H_
37
38#include <assert.h>
39#include <stdatomic.h>
40#include <errno.h>
41#include <libc.h>
42#include <time.h>
43#include <fibril.h>
44#include <abi/cap.h>
45
46typedef struct futex {
47 volatile atomic_int val;
48 volatile atomic_int lock;
49 volatile cap_waitq_handle_t whandle;
50
51#ifdef CONFIG_DEBUG_FUTEX
52 _Atomic(fibril_t *) owner;
53#endif
54} futex_t;
55
56extern void futex_initialize(futex_t *futex, int value);
57
58static inline errno_t futex_destroy(futex_t *futex)
59{
60 if (futex->whandle)
61 return __SYSCALL1(SYS_WAITQ_DESTROY, (sysarg_t) futex->whandle);
62 return EOK;
63}
64
65#ifdef CONFIG_DEBUG_FUTEX
66
67#define FUTEX_INITIALIZE(val) { (val), 0, CAP_NIL, NULL }
68#define FUTEX_INITIALIZER FUTEX_INITIALIZE(1)
69
70void __futex_assert_is_locked(futex_t *, const char *);
71void __futex_assert_is_not_locked(futex_t *, const char *);
72void __futex_lock(futex_t *, const char *);
73void __futex_unlock(futex_t *, const char *);
74bool __futex_trylock(futex_t *, const char *);
75void __futex_give_to(futex_t *, void *, const char *);
76
77#define futex_lock(futex) __futex_lock((futex), #futex)
78#define futex_unlock(futex) __futex_unlock((futex), #futex)
79#define futex_trylock(futex) __futex_trylock((futex), #futex)
80
81#define futex_give_to(futex, new_owner) __futex_give_to((futex), (new_owner), #futex)
82#define futex_assert_is_locked(futex) __futex_assert_is_locked((futex), #futex)
83#define futex_assert_is_not_locked(futex) __futex_assert_is_not_locked((futex), #futex)
84
85#else
86
87#define FUTEX_INITIALIZE(val) { (val), 0, CAP_NIL }
88#define FUTEX_INITIALIZER FUTEX_INITIALIZE(1)
89
90#define futex_lock(fut) (void) futex_down((fut))
91#define futex_trylock(fut) futex_trydown((fut))
92#define futex_unlock(fut) (void) futex_up((fut))
93
94#define futex_give_to(fut, owner) ((void)0)
95#define futex_assert_is_locked(fut) assert(atomic_load_explicit(&(fut)->val, memory_order_relaxed) <= 0)
96#define futex_assert_is_not_locked(fut) ((void)0)
97
98#endif
99
100static errno_t allocate_waitq(futex_t *futex)
101{
102 int expected = 0;
103 while (!atomic_compare_exchange_weak_explicit(&futex->lock, &expected,
104 1, memory_order_acquire, memory_order_relaxed))
105 expected = 0;
106
107 if (futex->whandle == CAP_NIL) {
108 errno_t rc = __SYSCALL1(SYS_WAITQ_CREATE,
109 (sysarg_t) &futex->whandle);
110 if (rc != EOK) {
111 atomic_store_explicit(&futex->lock, 0,
112 memory_order_release);
113 return rc;
114 }
115 }
116
117 atomic_store_explicit(&futex->lock, 0, memory_order_release);
118 return EOK;
119}
120
121/** Down the futex with timeout, composably.
122 *
123 * This means that when the operation fails due to a timeout or being
124 * interrupted, the next futex_up() is ignored, which allows certain kinds of
125 * composition of synchronization primitives.
126 *
127 * In most other circumstances, regular futex_down_timeout() is a better choice.
128 *
129 * @param futex Futex.
130 *
131 * @return ENOENT if there is no such virtual address.
132 * @return ETIMEOUT if timeout expires.
133 * @return EOK on success.
134 * @return Error code from <errno.h> otherwise.
135 *
136 */
137static inline errno_t futex_down_composable(futex_t *futex,
138 const struct timespec *expires)
139{
140 // TODO: Add tests for this.
141
142 // Preallocate the waitq handle so that we don't need to risk a failure
143 // during wakeup
144 if (futex->whandle == CAP_NIL) {
145 errno_t rc = allocate_waitq(futex);
146 if (rc != EOK)
147 return rc;
148 }
149
150 if (atomic_fetch_sub_explicit(&futex->val, 1, memory_order_acquire) > 0)
151 return EOK;
152
153 /* There wasn't any token. We must defer to the underlying semaphore. */
154
155 usec_t timeout;
156
157 if (!expires) {
158 /* No timeout. */
159 timeout = 0;
160 } else {
161 if (expires->tv_sec == 0) {
162 /* We can't just return ETIMEOUT. That wouldn't be composable. */
163 timeout = 1;
164 } else {
165 struct timespec tv;
166 getuptime(&tv);
167 timeout = ts_gteq(&tv, expires) ? 1 :
168 NSEC2USEC(ts_sub_diff(expires, &tv));
169 }
170
171 assert(timeout > 0);
172 }
173
174 return __SYSCALL2(SYS_WAITQ_SLEEP, (sysarg_t) futex->whandle,
175 (sysarg_t) timeout);
176}
177
178/** Up the futex.
179 *
180 * @param futex Futex.
181 *
182 * @return ENOENT if there is no such virtual address.
183 * @return EOK on success.
184 * @return Error code from <errno.h> otherwise.
185 *
186 */
187static inline errno_t futex_up(futex_t *futex)
188{
189 if (atomic_fetch_add_explicit(&futex->val, 1, memory_order_release) < 0)
190 return __SYSCALL1(SYS_WAITQ_WAKEUP, (sysarg_t) futex->whandle);
191
192 return EOK;
193}
194
195static inline errno_t futex_down_timeout(futex_t *futex,
196 const struct timespec *expires)
197{
198 /*
199 * This combination of a "composable" sleep followed by futex_up() on
200 * failure is necessary to prevent breakage due to certain race
201 * conditions.
202 */
203 errno_t rc = futex_down_composable(futex, expires);
204 if (rc != EOK)
205 futex_up(futex);
206 return rc;
207}
208
209/** Try to down the futex.
210 *
211 * @param futex Futex.
212 *
213 * @return true if the futex was acquired.
214 * @return false if the futex was not acquired.
215 *
216 */
217static inline bool futex_trydown(futex_t *futex)
218{
219 /*
220 * We can't just use CAS here.
221 * If we don't succeed with CAS, we can't return failure
222 * because that would lead to spurious failures where
223 * futex_down_timeout returns ETIMEOUT despite there being
224 * available tokens. That would break some algorithms.
225 * We also don't want to loop on CAS indefinitely, because
226 * that would make the semaphore not wait-free, even when all
227 * atomic operations and the underlying base semaphore are all
228 * wait-free.
229 * It's much less trouble (and code bloat) to just do regular
230 * down_timeout(), with an already expired deadline.
231 */
232 struct timespec tv = { .tv_sec = 0, .tv_nsec = 0 };
233 return futex_down_timeout(futex, &tv) == EOK;
234}
235
236/** Down the futex.
237 *
238 * @param futex Futex.
239 *
240 * @return ENOENT if there is no such virtual address.
241 * @return EOK on success.
242 * @return Error code from <errno.h> otherwise.
243 *
244 */
245static inline errno_t futex_down(futex_t *futex)
246{
247 return futex_down_timeout(futex, NULL);
248}
249
250#endif
251
252/** @}
253 */
Note: See TracBrowser for help on using the repository browser.