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