[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 |
|
---|
| 38 | #include <atomic.h>
|
---|
[cb10bc9] | 39 | #include <libc.h>
|
---|
| 40 |
|
---|
[927a181e] | 41 | typedef struct futex {
|
---|
| 42 | atomic_t val;
|
---|
[d54b303] | 43 | #ifdef FUTEX_UPGRADABLE
|
---|
| 44 | int upgraded;
|
---|
| 45 | #endif
|
---|
[927a181e] | 46 | } futex_t;
|
---|
[4db6eaf] | 47 |
|
---|
[cb10bc9] | 48 |
|
---|
[4db6eaf] | 49 | extern void futex_initialize(futex_t *futex, int value);
|
---|
[cb10bc9] | 50 |
|
---|
[d54b303] | 51 | #ifdef FUTEX_UPGRADABLE
|
---|
| 52 | #include <rcu.h>
|
---|
| 53 |
|
---|
[32d2e60] | 54 | #define FUTEX_INITIALIZE(val) {{ (val) }, 0}
|
---|
[d54b303] | 55 |
|
---|
[156b6406] | 56 | #define futex_lock(fut) \
|
---|
[d54b303] | 57 | ({ \
|
---|
| 58 | rcu_read_lock(); \
|
---|
| 59 | (fut)->upgraded = rcu_access(_upgrade_futexes); \
|
---|
| 60 | if ((fut)->upgraded) \
|
---|
[156b6406] | 61 | (void) futex_down((fut)); \
|
---|
[d54b303] | 62 | })
|
---|
| 63 |
|
---|
[156b6406] | 64 | #define futex_trylock(fut) \
|
---|
[d54b303] | 65 | ({ \
|
---|
| 66 | rcu_read_lock(); \
|
---|
| 67 | int _upgraded = rcu_access(_upgrade_futexes); \
|
---|
| 68 | if (_upgraded) { \
|
---|
[156b6406] | 69 | int _acquired = futex_trydown((fut)); \
|
---|
[d54b303] | 70 | if (!_acquired) { \
|
---|
| 71 | rcu_read_unlock(); \
|
---|
| 72 | } else { \
|
---|
| 73 | (fut)->upgraded = true; \
|
---|
| 74 | } \
|
---|
| 75 | _acquired; \
|
---|
| 76 | } else { \
|
---|
| 77 | (fut)->upgraded = false; \
|
---|
| 78 | 1; \
|
---|
| 79 | } \
|
---|
| 80 | })
|
---|
| 81 |
|
---|
[156b6406] | 82 | #define futex_unlock(fut) \
|
---|
[d54b303] | 83 | ({ \
|
---|
| 84 | if ((fut)->upgraded) \
|
---|
[156b6406] | 85 | (void) futex_up((fut)); \
|
---|
[d54b303] | 86 | rcu_read_unlock(); \
|
---|
| 87 | })
|
---|
| 88 |
|
---|
| 89 | extern int _upgrade_futexes;
|
---|
| 90 |
|
---|
| 91 | extern void futex_upgrade_all_and_wait(void);
|
---|
| 92 |
|
---|
| 93 | #else
|
---|
| 94 |
|
---|
[32d2e60] | 95 | #define FUTEX_INITIALIZE(val) {{ (val) }}
|
---|
[d54b303] | 96 |
|
---|
[156b6406] | 97 | #define futex_lock(fut) (void) futex_down((fut))
|
---|
| 98 | #define futex_trylock(fut) futex_trydown((fut))
|
---|
| 99 | #define futex_unlock(fut) (void) futex_up((fut))
|
---|
[d54b303] | 100 |
|
---|
| 101 | #endif
|
---|
[cb10bc9] | 102 |
|
---|
[d54b303] | 103 | #define FUTEX_INITIALIZER FUTEX_INITIALIZE(1)
|
---|
[cb10bc9] | 104 |
|
---|
| 105 | /** Try to down the futex.
|
---|
| 106 | *
|
---|
| 107 | * @param futex Futex.
|
---|
| 108 | *
|
---|
| 109 | * @return Non-zero if the futex was acquired.
|
---|
| 110 | * @return Zero if the futex was not acquired.
|
---|
| 111 | *
|
---|
| 112 | */
|
---|
[156b6406] | 113 | static inline int futex_trydown(futex_t *futex)
|
---|
[cb10bc9] | 114 | {
|
---|
| 115 | return cas(&futex->val, 1, 0);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | /** Down the futex.
|
---|
| 119 | *
|
---|
| 120 | * @param futex Futex.
|
---|
| 121 | *
|
---|
| 122 | * @return ENOENT if there is no such virtual address.
|
---|
| 123 | * @return Zero in the uncontended case.
|
---|
| 124 | * @return Otherwise one of ESYNCH_OK_ATOMIC or ESYNCH_OK_BLOCKED.
|
---|
| 125 | *
|
---|
| 126 | */
|
---|
[156b6406] | 127 | static inline int futex_down(futex_t *futex)
|
---|
[cb10bc9] | 128 | {
|
---|
| 129 | if ((atomic_signed_t) atomic_predec(&futex->val) < 0)
|
---|
| 130 | return __SYSCALL1(SYS_FUTEX_SLEEP, (sysarg_t) &futex->val.count);
|
---|
| 131 |
|
---|
| 132 | return 0;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | /** Up the futex.
|
---|
| 136 | *
|
---|
| 137 | * @param futex Futex.
|
---|
| 138 | *
|
---|
| 139 | * @return ENOENT if there is no such virtual address.
|
---|
| 140 | * @return Zero in the uncontended case.
|
---|
| 141 | *
|
---|
| 142 | */
|
---|
[156b6406] | 143 | static inline int futex_up(futex_t *futex)
|
---|
[cb10bc9] | 144 | {
|
---|
| 145 | if ((atomic_signed_t) atomic_postinc(&futex->val) < 0)
|
---|
| 146 | return __SYSCALL1(SYS_FUTEX_WAKEUP, (sysarg_t) &futex->val.count);
|
---|
| 147 |
|
---|
| 148 | return 0;
|
---|
| 149 | }
|
---|
[1cef26f] | 150 |
|
---|
| 151 | #endif
|
---|
[b2951e2] | 152 |
|
---|
[fadd381] | 153 | /** @}
|
---|
[b2951e2] | 154 | */
|
---|