source: mainline/uspace/lib/c/generic/private/futex.h@ 45c8eea

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 45c8eea was 45c8eea, checked in by Jakub Jermar <jakub@…>, 7 years ago

Preallocate waitq handle during initialization

Do not clutter futex_down_composable() with the preallocation of the
wait queue handle and do it single-threadedly in futex_initialize().

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