source: mainline/uspace/lib/c/generic/private/futex.h@ 8591b31

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8591b31 was 8591b31, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

Fix most clang issues

Build error in futex.c remains because the proper solution of using
type _Atomic(fibril_t *) can't be parsed by sycek.

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