| 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 <atomic.h>
|
|---|
| 40 | #include <errno.h>
|
|---|
| 41 | #include <libc.h>
|
|---|
| 42 | #include <time.h>
|
|---|
| 43 |
|
|---|
| 44 | typedef struct futex {
|
|---|
| 45 | atomic_t val;
|
|---|
| 46 | #ifdef CONFIG_DEBUG_FUTEX
|
|---|
| 47 | void *owner;
|
|---|
| 48 | #endif
|
|---|
| 49 | } futex_t;
|
|---|
| 50 |
|
|---|
| 51 | extern void futex_initialize(futex_t *futex, int value);
|
|---|
| 52 |
|
|---|
| 53 | #ifdef CONFIG_DEBUG_FUTEX
|
|---|
| 54 |
|
|---|
| 55 | #define FUTEX_INITIALIZE(val) {{ (val) }, NULL }
|
|---|
| 56 | #define FUTEX_INITIALIZER FUTEX_INITIALIZE(1)
|
|---|
| 57 |
|
|---|
| 58 | void __futex_assert_is_locked(futex_t *, const char *);
|
|---|
| 59 | void __futex_assert_is_not_locked(futex_t *, const char *);
|
|---|
| 60 | void __futex_lock(futex_t *, const char *);
|
|---|
| 61 | void __futex_unlock(futex_t *, const char *);
|
|---|
| 62 | bool __futex_trylock(futex_t *, const char *);
|
|---|
| 63 | void __futex_give_to(futex_t *, void *, const char *);
|
|---|
| 64 |
|
|---|
| 65 | #define futex_lock(futex) __futex_lock((futex), #futex)
|
|---|
| 66 | #define futex_unlock(futex) __futex_unlock((futex), #futex)
|
|---|
| 67 | #define futex_trylock(futex) __futex_trylock((futex), #futex)
|
|---|
| 68 |
|
|---|
| 69 | #define futex_give_to(futex, new_owner) __futex_give_to((futex), (new_owner), #futex)
|
|---|
| 70 | #define futex_assert_is_locked(futex) __futex_assert_is_locked((futex), #futex)
|
|---|
| 71 | #define futex_assert_is_not_locked(futex) __futex_assert_is_not_locked((futex), #futex)
|
|---|
| 72 |
|
|---|
| 73 | #else
|
|---|
| 74 |
|
|---|
| 75 | #define FUTEX_INITIALIZE(val) {{ (val) }}
|
|---|
| 76 | #define FUTEX_INITIALIZER FUTEX_INITIALIZE(1)
|
|---|
| 77 |
|
|---|
| 78 | #define futex_lock(fut) (void) futex_down((fut))
|
|---|
| 79 | #define futex_trylock(fut) futex_trydown((fut))
|
|---|
| 80 | #define futex_unlock(fut) (void) futex_up((fut))
|
|---|
| 81 |
|
|---|
| 82 | #define futex_give_to(fut, owner) ((void)0)
|
|---|
| 83 | #define futex_assert_is_locked(fut) assert((atomic_signed_t) (fut)->val.count <= 0)
|
|---|
| 84 | #define futex_assert_is_not_locked(fut) ((void)0)
|
|---|
| 85 |
|
|---|
| 86 | #endif
|
|---|
| 87 |
|
|---|
| 88 | /** Try to down the futex.
|
|---|
| 89 | *
|
|---|
| 90 | * @param futex Futex.
|
|---|
| 91 | *
|
|---|
| 92 | * @return true if the futex was acquired.
|
|---|
| 93 | * @return false if the futex was not acquired.
|
|---|
| 94 | *
|
|---|
| 95 | */
|
|---|
| 96 | static inline bool futex_trydown(futex_t *futex)
|
|---|
| 97 | {
|
|---|
| 98 | return cas(&futex->val, 1, 0);
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | /** Down the futex with timeout, composably.
|
|---|
| 102 | *
|
|---|
| 103 | * This means that when the operation fails due to a timeout or being
|
|---|
| 104 | * interrupted, the next futex_up() is ignored, which allows certain kinds of
|
|---|
| 105 | * composition of synchronization primitives.
|
|---|
| 106 | *
|
|---|
| 107 | * In most other circumstances, regular futex_down_timeout() is a better choice.
|
|---|
| 108 | *
|
|---|
| 109 | * @param futex Futex.
|
|---|
| 110 | *
|
|---|
| 111 | * @return ENOENT if there is no such virtual address.
|
|---|
| 112 | * @return ETIMEOUT if timeout expires.
|
|---|
| 113 | * @return EOK on success.
|
|---|
| 114 | * @return Error code from <errno.h> otherwise.
|
|---|
| 115 | *
|
|---|
| 116 | */
|
|---|
| 117 | static inline errno_t futex_down_composable(futex_t *futex, struct timeval *expires)
|
|---|
| 118 | {
|
|---|
| 119 | // TODO: Add tests for this.
|
|---|
| 120 |
|
|---|
| 121 | /* No timeout by default. */
|
|---|
| 122 | suseconds_t timeout = 0;
|
|---|
| 123 |
|
|---|
| 124 | if (expires) {
|
|---|
| 125 | struct timeval tv;
|
|---|
| 126 | getuptime(&tv);
|
|---|
| 127 | if (tv_gteq(&tv, expires)) {
|
|---|
| 128 | /* We can't just return ETIMEOUT. That wouldn't be composable. */
|
|---|
| 129 | timeout = 1;
|
|---|
| 130 | } else {
|
|---|
| 131 | timeout = tv_sub_diff(expires, &tv);
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | assert(timeout > 0);
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | if ((atomic_signed_t) atomic_predec(&futex->val) < 0)
|
|---|
| 138 | return (errno_t) __SYSCALL2(SYS_FUTEX_SLEEP, (sysarg_t) &futex->val.count, (sysarg_t) timeout);
|
|---|
| 139 |
|
|---|
| 140 | return EOK;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | /** Up the futex.
|
|---|
| 144 | *
|
|---|
| 145 | * @param futex Futex.
|
|---|
| 146 | *
|
|---|
| 147 | * @return ENOENT if there is no such virtual address.
|
|---|
| 148 | * @return EOK on success.
|
|---|
| 149 | * @return Error code from <errno.h> otherwise.
|
|---|
| 150 | *
|
|---|
| 151 | */
|
|---|
| 152 | static inline errno_t futex_up(futex_t *futex)
|
|---|
| 153 | {
|
|---|
| 154 | if ((atomic_signed_t) atomic_postinc(&futex->val) < 0)
|
|---|
| 155 | return (errno_t) __SYSCALL1(SYS_FUTEX_WAKEUP, (sysarg_t) &futex->val.count);
|
|---|
| 156 |
|
|---|
| 157 | return EOK;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | static inline errno_t futex_down_timeout(futex_t *futex, struct timeval *expires)
|
|---|
| 161 | {
|
|---|
| 162 | /*
|
|---|
| 163 | * This combination of a "composable" sleep followed by futex_up() on
|
|---|
| 164 | * failure is necessary to prevent breakage due to certain race
|
|---|
| 165 | * conditions.
|
|---|
| 166 | */
|
|---|
| 167 | errno_t rc = futex_down_composable(futex, expires);
|
|---|
| 168 | if (rc != EOK)
|
|---|
| 169 | futex_up(futex);
|
|---|
| 170 | return rc;
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | /** Down the futex.
|
|---|
| 174 | *
|
|---|
| 175 | * @param futex Futex.
|
|---|
| 176 | *
|
|---|
| 177 | * @return ENOENT if there is no such virtual address.
|
|---|
| 178 | * @return EOK on success.
|
|---|
| 179 | * @return Error code from <errno.h> otherwise.
|
|---|
| 180 | *
|
|---|
| 181 | */
|
|---|
| 182 | static inline errno_t futex_down(futex_t *futex)
|
|---|
| 183 | {
|
|---|
| 184 | return futex_down_timeout(futex, NULL);
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | #endif
|
|---|
| 188 |
|
|---|
| 189 | /** @}
|
|---|
| 190 | */
|
|---|