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
Line 
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
45typedef struct futex {
46 volatile atomic_int val;
47#ifdef CONFIG_DEBUG_FUTEX
48 // FIXME: Should be _Atomic(fibril_t *)
49 fibril_t *owner;
50#endif
51} futex_t;
52
53extern void futex_initialize(futex_t *futex, int value);
54
55#ifdef CONFIG_DEBUG_FUTEX
56
57#define FUTEX_INITIALIZE(val) { (val) , NULL }
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
77#define FUTEX_INITIALIZE(val) { (val) }
78#define FUTEX_INITIALIZER FUTEX_INITIALIZE(1)
79
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))
83
84#define futex_give_to(fut, owner) ((void)0)
85#define futex_assert_is_locked(fut) assert(atomic_load_explicit(&(fut)->val, memory_order_relaxed) <= 0)
86#define futex_assert_is_not_locked(fut) ((void)0)
87
88#endif
89
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.
97 *
98 * @param futex Futex.
99 *
100 * @return ENOENT if there is no such virtual address.
101 * @return ETIMEOUT if timeout expires.
102 * @return EOK on success.
103 * @return Error code from <errno.h> otherwise.
104 *
105 */
106static inline errno_t futex_down_composable(futex_t *futex,
107 const struct timespec *expires)
108{
109 // TODO: Add tests for this.
110
111 if (atomic_fetch_sub_explicit(&futex->val, 1, memory_order_acquire) > 0)
112 return EOK;
113
114 /* There wasn't any token. We must defer to the underlying semaphore. */
115
116 usec_t timeout;
117
118 if (!expires) {
119 /* No timeout. */
120 timeout = 0;
121 } else {
122 if (expires->tv_sec == 0) {
123 /* We can't just return ETIMEOUT. That wouldn't be composable. */
124 timeout = 1;
125 } else {
126 struct timespec tv;
127 getuptime(&tv);
128 timeout = ts_gteq(&tv, expires) ? 1 :
129 NSEC2USEC(ts_sub_diff(expires, &tv));
130 }
131
132 assert(timeout > 0);
133 }
134
135 return __SYSCALL2(SYS_FUTEX_SLEEP, (sysarg_t) futex, (sysarg_t) timeout);
136}
137
138/** Up the futex.
139 *
140 * @param futex Futex.
141 *
142 * @return ENOENT if there is no such virtual address.
143 * @return EOK on success.
144 * @return Error code from <errno.h> otherwise.
145 *
146 */
147static inline errno_t futex_up(futex_t *futex)
148{
149 if (atomic_fetch_add_explicit(&futex->val, 1, memory_order_release) < 0)
150 return __SYSCALL1(SYS_FUTEX_WAKEUP, (sysarg_t) futex);
151
152 return EOK;
153}
154
155static inline errno_t futex_down_timeout(futex_t *futex,
156 const struct timespec *expires)
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
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 /*
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.
191 */
192 struct timespec tv = { .tv_sec = 0, .tv_nsec = 0 };
193 return futex_down_timeout(futex, &tv) == EOK;
194}
195
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
210#endif
211
212/** @}
213 */
Note: See TracBrowser for help on using the repository browser.