[1cef26f] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Jakub Jermar
|
---|
[1cef26f] | 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 |
|
---|
[fadd381] | 29 | /** @addtogroup libc
|
---|
[b2951e2] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[fadd381] | 35 | #ifndef LIBC_FUTEX_H_
|
---|
| 36 | #define LIBC_FUTEX_H_
|
---|
[1cef26f] | 37 |
|
---|
[b59318e] | 38 | #include <assert.h>
|
---|
[508b0df1] | 39 | #include <stdatomic.h>
|
---|
[897fd8f1] | 40 | #include <errno.h>
|
---|
[cb10bc9] | 41 | #include <libc.h>
|
---|
[f6372be9] | 42 | #include <time.h>
|
---|
[8591b31] | 43 | #include <fibril.h>
|
---|
[9e889f6] | 44 | #include <abi/cap.h>
|
---|
[cb10bc9] | 45 |
|
---|
[927a181e] | 46 | typedef struct futex {
|
---|
[508b0df1] | 47 | volatile atomic_int val;
|
---|
[8a18d5b] | 48 | volatile atomic_int lock;
|
---|
| 49 | volatile cap_waitq_handle_t whandle;
|
---|
[9e889f6] | 50 |
|
---|
[f6372be9] | 51 | #ifdef CONFIG_DEBUG_FUTEX
|
---|
[42da5ed] | 52 | _Atomic(fibril_t *) owner;
|
---|
[f6372be9] | 53 | #endif
|
---|
[927a181e] | 54 | } futex_t;
|
---|
[4db6eaf] | 55 |
|
---|
| 56 | extern void futex_initialize(futex_t *futex, int value);
|
---|
[cb10bc9] | 57 |
|
---|
[f6372be9] | 58 | #ifdef CONFIG_DEBUG_FUTEX
|
---|
| 59 |
|
---|
[9e889f6] | 60 | #define FUTEX_INITIALIZE(val) { (val), 0, CAP_NIL, NULL }
|
---|
[f6372be9] | 61 | #define FUTEX_INITIALIZER FUTEX_INITIALIZE(1)
|
---|
| 62 |
|
---|
| 63 | void __futex_assert_is_locked(futex_t *, const char *);
|
---|
| 64 | void __futex_assert_is_not_locked(futex_t *, const char *);
|
---|
| 65 | void __futex_lock(futex_t *, const char *);
|
---|
| 66 | void __futex_unlock(futex_t *, const char *);
|
---|
| 67 | bool __futex_trylock(futex_t *, const char *);
|
---|
| 68 | void __futex_give_to(futex_t *, void *, const char *);
|
---|
| 69 |
|
---|
| 70 | #define futex_lock(futex) __futex_lock((futex), #futex)
|
---|
| 71 | #define futex_unlock(futex) __futex_unlock((futex), #futex)
|
---|
| 72 | #define futex_trylock(futex) __futex_trylock((futex), #futex)
|
---|
| 73 |
|
---|
| 74 | #define futex_give_to(futex, new_owner) __futex_give_to((futex), (new_owner), #futex)
|
---|
| 75 | #define futex_assert_is_locked(futex) __futex_assert_is_locked((futex), #futex)
|
---|
| 76 | #define futex_assert_is_not_locked(futex) __futex_assert_is_not_locked((futex), #futex)
|
---|
| 77 |
|
---|
| 78 | #else
|
---|
| 79 |
|
---|
[9e889f6] | 80 | #define FUTEX_INITIALIZE(val) { (val), 0, CAP_NIL }
|
---|
[3679f51a] | 81 | #define FUTEX_INITIALIZER FUTEX_INITIALIZE(1)
|
---|
[d54b303] | 82 |
|
---|
[156b6406] | 83 | #define futex_lock(fut) (void) futex_down((fut))
|
---|
| 84 | #define futex_trylock(fut) futex_trydown((fut))
|
---|
| 85 | #define futex_unlock(fut) (void) futex_up((fut))
|
---|
[a35b458] | 86 |
|
---|
[f6372be9] | 87 | #define futex_give_to(fut, owner) ((void)0)
|
---|
[5f1d850] | 88 | #define futex_assert_is_locked(fut) assert(atomic_load_explicit(&(fut)->val, memory_order_relaxed) <= 0)
|
---|
[f6372be9] | 89 | #define futex_assert_is_not_locked(fut) ((void)0)
|
---|
| 90 |
|
---|
| 91 | #endif
|
---|
| 92 |
|
---|
[8a18d5b] | 93 | static errno_t allocate_waitq(futex_t *futex)
|
---|
| 94 | {
|
---|
| 95 | int expected = 0;
|
---|
| 96 | while (!atomic_compare_exchange_weak_explicit(&futex->lock, &expected,
|
---|
| 97 | 1, memory_order_acquire, memory_order_relaxed))
|
---|
| 98 | expected = 0;
|
---|
| 99 |
|
---|
| 100 | if (futex->whandle == CAP_NIL) {
|
---|
| 101 | errno_t rc = __SYSCALL1(SYS_WAITQ_CREATE,
|
---|
| 102 | (sysarg_t) &futex->whandle);
|
---|
| 103 | if (rc != EOK) {
|
---|
| 104 | atomic_store_explicit(&futex->lock, 0,
|
---|
| 105 | memory_order_release);
|
---|
| 106 | return rc;
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | atomic_store_explicit(&futex->lock, 0, memory_order_release);
|
---|
| 111 | return EOK;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[b59318e] | 114 | /** Down the futex with timeout, composably.
|
---|
| 115 | *
|
---|
| 116 | * This means that when the operation fails due to a timeout or being
|
---|
| 117 | * interrupted, the next futex_up() is ignored, which allows certain kinds of
|
---|
| 118 | * composition of synchronization primitives.
|
---|
| 119 | *
|
---|
| 120 | * In most other circumstances, regular futex_down_timeout() is a better choice.
|
---|
[cb10bc9] | 121 | *
|
---|
| 122 | * @param futex Futex.
|
---|
| 123 | *
|
---|
| 124 | * @return ENOENT if there is no such virtual address.
|
---|
[b59318e] | 125 | * @return ETIMEOUT if timeout expires.
|
---|
[897fd8f1] | 126 | * @return EOK on success.
|
---|
| 127 | * @return Error code from <errno.h> otherwise.
|
---|
[cb10bc9] | 128 | *
|
---|
| 129 | */
|
---|
[bd41ac52] | 130 | static inline errno_t futex_down_composable(futex_t *futex,
|
---|
| 131 | const struct timespec *expires)
|
---|
[cb10bc9] | 132 | {
|
---|
[b59318e] | 133 | // TODO: Add tests for this.
|
---|
| 134 |
|
---|
[8a18d5b] | 135 | // Preallocate the waitq handle so that we don't need to risk a failure
|
---|
| 136 | // during wakeup
|
---|
| 137 | if (futex->whandle == CAP_NIL) {
|
---|
| 138 | errno_t rc = allocate_waitq(futex);
|
---|
| 139 | if (rc != EOK)
|
---|
| 140 | return rc;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[508b0df1] | 143 | if (atomic_fetch_sub_explicit(&futex->val, 1, memory_order_acquire) > 0)
|
---|
[710c1e9] | 144 | return EOK;
|
---|
| 145 |
|
---|
[508b0df1] | 146 | /* There wasn't any token. We must defer to the underlying semaphore. */
|
---|
| 147 |
|
---|
[bd41ac52] | 148 | usec_t timeout;
|
---|
[b59318e] | 149 |
|
---|
[710c1e9] | 150 | if (!expires) {
|
---|
| 151 | /* No timeout. */
|
---|
| 152 | timeout = 0;
|
---|
| 153 | } else {
|
---|
| 154 | if (expires->tv_sec == 0) {
|
---|
[b59318e] | 155 | /* We can't just return ETIMEOUT. That wouldn't be composable. */
|
---|
| 156 | timeout = 1;
|
---|
| 157 | } else {
|
---|
[bd41ac52] | 158 | struct timespec tv;
|
---|
[710c1e9] | 159 | getuptime(&tv);
|
---|
[bd41ac52] | 160 | timeout = ts_gteq(&tv, expires) ? 1 :
|
---|
| 161 | NSEC2USEC(ts_sub_diff(expires, &tv));
|
---|
[b59318e] | 162 | }
|
---|
| 163 |
|
---|
| 164 | assert(timeout > 0);
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[9e889f6] | 167 | return __SYSCALL2(SYS_WAITQ_SLEEP, (sysarg_t) futex->whandle,
|
---|
| 168 | (sysarg_t) timeout);
|
---|
[cb10bc9] | 169 | }
|
---|
| 170 |
|
---|
| 171 | /** Up the futex.
|
---|
| 172 | *
|
---|
| 173 | * @param futex Futex.
|
---|
| 174 | *
|
---|
| 175 | * @return ENOENT if there is no such virtual address.
|
---|
[897fd8f1] | 176 | * @return EOK on success.
|
---|
| 177 | * @return Error code from <errno.h> otherwise.
|
---|
[cb10bc9] | 178 | *
|
---|
| 179 | */
|
---|
[b7fd2a0] | 180 | static inline errno_t futex_up(futex_t *futex)
|
---|
[cb10bc9] | 181 | {
|
---|
[508b0df1] | 182 | if (atomic_fetch_add_explicit(&futex->val, 1, memory_order_release) < 0)
|
---|
[9e889f6] | 183 | return __SYSCALL1(SYS_WAITQ_WAKEUP, (sysarg_t) futex->whandle);
|
---|
[a35b458] | 184 |
|
---|
[897fd8f1] | 185 | return EOK;
|
---|
[cb10bc9] | 186 | }
|
---|
[1cef26f] | 187 |
|
---|
[bd41ac52] | 188 | static inline errno_t futex_down_timeout(futex_t *futex,
|
---|
| 189 | const struct timespec *expires)
|
---|
[b59318e] | 190 | {
|
---|
| 191 | /*
|
---|
| 192 | * This combination of a "composable" sleep followed by futex_up() on
|
---|
| 193 | * failure is necessary to prevent breakage due to certain race
|
---|
| 194 | * conditions.
|
---|
| 195 | */
|
---|
| 196 | errno_t rc = futex_down_composable(futex, expires);
|
---|
| 197 | if (rc != EOK)
|
---|
| 198 | futex_up(futex);
|
---|
| 199 | return rc;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[710c1e9] | 202 | /** Try to down the futex.
|
---|
| 203 | *
|
---|
| 204 | * @param futex Futex.
|
---|
| 205 | *
|
---|
| 206 | * @return true if the futex was acquired.
|
---|
| 207 | * @return false if the futex was not acquired.
|
---|
| 208 | *
|
---|
| 209 | */
|
---|
| 210 | static inline bool futex_trydown(futex_t *futex)
|
---|
| 211 | {
|
---|
| 212 | /*
|
---|
[508b0df1] | 213 | * We can't just use CAS here.
|
---|
| 214 | * If we don't succeed with CAS, we can't return failure
|
---|
| 215 | * because that would lead to spurious failures where
|
---|
| 216 | * futex_down_timeout returns ETIMEOUT despite there being
|
---|
| 217 | * available tokens. That would break some algorithms.
|
---|
| 218 | * We also don't want to loop on CAS indefinitely, because
|
---|
| 219 | * that would make the semaphore not wait-free, even when all
|
---|
| 220 | * atomic operations and the underlying base semaphore are all
|
---|
| 221 | * wait-free.
|
---|
| 222 | * It's much less trouble (and code bloat) to just do regular
|
---|
| 223 | * down_timeout(), with an already expired deadline.
|
---|
[710c1e9] | 224 | */
|
---|
[bd41ac52] | 225 | struct timespec tv = { .tv_sec = 0, .tv_nsec = 0 };
|
---|
[710c1e9] | 226 | return futex_down_timeout(futex, &tv) == EOK;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
[b59318e] | 229 | /** Down the futex.
|
---|
| 230 | *
|
---|
| 231 | * @param futex Futex.
|
---|
| 232 | *
|
---|
| 233 | * @return ENOENT if there is no such virtual address.
|
---|
| 234 | * @return EOK on success.
|
---|
| 235 | * @return Error code from <errno.h> otherwise.
|
---|
| 236 | *
|
---|
| 237 | */
|
---|
| 238 | static inline errno_t futex_down(futex_t *futex)
|
---|
| 239 | {
|
---|
| 240 | return futex_down_timeout(futex, NULL);
|
---|
| 241 | }
|
---|
| 242 |
|
---|
[1cef26f] | 243 | #endif
|
---|
[b2951e2] | 244 |
|
---|
[fadd381] | 245 | /** @}
|
---|
[b2951e2] | 246 | */
|
---|