1 // SPDX-FileCopyrightText: 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 // SPDX-License-Identifier: LGPL-2.1-or-later
9 * Userspace RCU - sys_futex/compat_futex header.
12 #include <urcu/config.h>
13 #include <urcu/syscall-compat.h>
19 #if (defined(__linux__) && defined(__NR_futex))
21 /* For backwards compat */
22 # define CONFIG_RCU_HAVE_FUTEX 1
26 # include <urcu/compiler.h>
27 # include <urcu/arch.h>
28 # include <urcu/assert.h>
30 #elif defined(__FreeBSD__)
32 # include <sys/types.h>
33 # include <sys/umtx.h>
35 #elif defined(__OpenBSD__)
37 # include <sys/time.h>
38 # include <sys/futex.h>
52 * sys_futex compatibility header.
53 * Use *only* *either of* futex_noasync OR futex_async on a given address.
55 * futex_noasync cannot be executed in signal handlers, but ensures that
56 * it will be put in a wait queue even in compatibility mode.
58 * futex_async is signal-handler safe for the wakeup. It uses polling
59 * on the wait-side in compatibility mode.
61 * BEWARE: sys_futex() FUTEX_WAIT may return early if interrupted
65 extern int compat_futex_noasync(int32_t *uaddr
, int op
, int32_t val
,
66 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
);
67 extern int compat_futex_async(int32_t *uaddr
, int op
, int32_t val
,
68 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
);
70 #if (defined(__linux__) && defined(__NR_futex))
72 static inline int futex(int32_t *uaddr
, int op
, int32_t val
,
73 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
)
75 return syscall(__NR_futex
, uaddr
, op
, val
, timeout
, uaddr2
, val3
);
78 static inline int futex_noasync(int32_t *uaddr
, int op
, int32_t val
,
79 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
)
83 ret
= futex(uaddr
, op
, val
, timeout
, uaddr2
, val3
);
84 if (caa_unlikely(ret
< 0 && errno
== ENOSYS
)) {
86 * The fallback on ENOSYS is the async-safe version of
87 * the compat futex implementation, because the
88 * async-safe compat implementation allows being used
89 * concurrently with calls to futex(). Indeed, sys_futex
90 * FUTEX_WAIT, on some architectures (mips and parisc),
91 * within a given process, spuriously return ENOSYS due
92 * to signal restart bugs on some kernel versions.
94 return compat_futex_async(uaddr
, op
, val
, timeout
,
101 static inline int futex_async(int32_t *uaddr
, int op
, int32_t val
,
102 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
)
106 ret
= futex(uaddr
, op
, val
, timeout
, uaddr2
, val3
);
107 if (caa_unlikely(ret
< 0 && errno
== ENOSYS
)) {
108 return compat_futex_async(uaddr
, op
, val
, timeout
,
114 #elif defined(__FreeBSD__)
116 static inline int futex_async(int32_t *uaddr
, int op
, int32_t val
,
117 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
)
120 void *umtx_uaddr
= NULL
, *umtx_uaddr2
= NULL
;
121 struct _umtx_time umtx_timeout
= {
122 ._flags
= UMTX_ABSTIME
,
123 ._clockid
= CLOCK_MONOTONIC
,
127 * Check if NULL or zero. Don't let users expect that they are
128 * taken into account.
130 urcu_posix_assert(!uaddr2
);
131 urcu_posix_assert(!val3
);
135 /* On FreeBSD, a "u_int" is a 32-bit integer. */
136 umtx_op
= UMTX_OP_WAIT_UINT
;
137 if (timeout
!= NULL
) {
138 umtx_timeout
._timeout
= *timeout
;
139 umtx_uaddr
= (void *) sizeof(umtx_timeout
);
140 umtx_uaddr2
= (void *) &umtx_timeout
;
144 umtx_op
= UMTX_OP_WAKE
;
151 return _umtx_op(uaddr
, umtx_op
, (uint32_t) val
, umtx_uaddr
,
155 static inline int futex_noasync(int32_t *uaddr
, int op
, int32_t val
,
156 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
)
158 return futex_async(uaddr
, op
, val
, timeout
, uaddr2
, val3
);
161 #elif defined(__OpenBSD__)
163 static inline int futex_noasync(int32_t *uaddr
, int op
, int32_t val
,
164 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
)
169 * Check that val3 is zero. Don't let users expect that it is
170 * taken into account.
172 urcu_posix_assert(!val3
);
174 ret
= futex((volatile uint32_t *) uaddr
, op
, val
, timeout
,
175 (volatile uint32_t *) uaddr2
);
176 if (caa_unlikely(ret
< 0 && errno
== ENOSYS
)) {
177 return compat_futex_noasync(uaddr
, op
, val
, timeout
,
183 static inline int futex_async(int32_t *uaddr
, int op
, int32_t val
,
184 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
)
189 * Check that val3 is zero. Don't let users expect that it is
190 * taken into account.
192 urcu_posix_assert(!val3
);
194 ret
= futex((volatile uint32_t *) uaddr
, op
, val
, timeout
,
195 (volatile uint32_t *) uaddr2
);
196 if (caa_unlikely(ret
< 0 && errno
== ENOSYS
)) {
197 return compat_futex_async(uaddr
, op
, val
, timeout
,
203 #elif defined(__CYGWIN__)
206 * The futex_noasync compat code uses a weak symbol to share state across
207 * different shared object which is not possible on Windows with the
208 * Portable Executable format. Use the async compat code for both cases.
210 static inline int futex_noasync(int32_t *uaddr
, int op
, int32_t val
,
211 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
)
213 return compat_futex_async(uaddr
, op
, val
, timeout
, uaddr2
, val3
);
216 static inline int futex_async(int32_t *uaddr
, int op
, int32_t val
,
217 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
)
219 return compat_futex_async(uaddr
, op
, val
, timeout
, uaddr2
, val3
);
224 static inline int futex_noasync(int32_t *uaddr
, int op
, int32_t val
,
225 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
)
227 return compat_futex_noasync(uaddr
, op
, val
, timeout
, uaddr2
, val3
);
230 static inline int futex_async(int32_t *uaddr
, int op
, int32_t val
,
231 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
)
233 return compat_futex_async(uaddr
, op
, val
, timeout
, uaddr2
, val3
);
242 #endif /* _URCU_FUTEX_H */
This page took 0.046499 seconds and 4 git commands to generate.