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