b219835e0925b9d5b8e3ad64cc3e288b233fac2c
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>
29 #elif defined(__FreeBSD__)
31 #include <sys/types.h>
34 #elif defined(__OpenBSD__)
37 #include <sys/futex.h>
51 * sys_futex compatibility header.
52 * Use *only* *either of* futex_noasync OR futex_async on a given address.
54 * futex_noasync cannot be executed in signal handlers, but ensures that
55 * it will be put in a wait queue even in compatibility mode.
57 * futex_async is signal-handler safe for the wakeup. It uses polling
58 * on the wait-side in compatibility mode.
60 * BEWARE: sys_futex() FUTEX_WAIT may return early if interrupted
64 extern int compat_futex_noasync(int32_t *uaddr
, int op
, int32_t val
,
65 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
);
66 extern int compat_futex_async(int32_t *uaddr
, int op
, int32_t val
,
67 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
);
69 #if (defined(__linux__) && defined(__NR_futex))
71 static inline int futex(int32_t *uaddr
, int op
, int32_t val
,
72 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
)
74 return syscall(__NR_futex
, uaddr
, op
, val
, timeout
,
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
,
118 int32_t *uaddr2
__attribute__((unused
)),
119 int32_t val3
__attribute__((unused
)))
122 void *umtx_uaddr
= NULL
, *umtx_uaddr2
= NULL
;
123 struct _umtx_time umtx_timeout
= {
124 ._flags
= UMTX_ABSTIME
,
125 ._clockid
= CLOCK_MONOTONIC
,
130 /* On FreeBSD, a "u_int" is a 32-bit integer. */
131 umtx_op
= UMTX_OP_WAIT_UINT
;
132 if (timeout
!= NULL
) {
133 umtx_timeout
._timeout
= *timeout
;
134 umtx_uaddr
= (void *) sizeof(umtx_timeout
);
135 umtx_uaddr2
= (void *) &umtx_timeout
;
139 umtx_op
= UMTX_OP_WAKE
;
146 return _umtx_op(uaddr
, umtx_op
, (uint32_t) val
, umtx_uaddr
,
150 static inline int futex_noasync(int32_t *uaddr
, int op
, int32_t val
,
151 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
)
153 return futex_async(uaddr
, op
, val
, timeout
, uaddr2
, val3
);
156 #elif defined(__OpenBSD__)
158 static inline int futex_noasync(int32_t *uaddr
, int op
, int32_t val
,
159 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
)
163 ret
= futex((volatile uint32_t *) uaddr
, op
, val
, timeout
,
164 (volatile uint32_t *) uaddr2
);
165 if (caa_unlikely(ret
< 0 && errno
== ENOSYS
)) {
166 return compat_futex_noasync(uaddr
, op
, val
, timeout
,
172 static inline int futex_async(int32_t *uaddr
, int op
, int32_t val
,
173 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
)
177 ret
= futex((volatile uint32_t *) uaddr
, op
, val
, timeout
,
178 (volatile uint32_t *) uaddr2
);
179 if (caa_unlikely(ret
< 0 && errno
== ENOSYS
)) {
180 return compat_futex_async(uaddr
, op
, val
, timeout
,
186 #elif defined(__CYGWIN__)
189 * The futex_noasync compat code uses a weak symbol to share state across
190 * different shared object which is not possible on Windows with the
191 * Portable Executable format. Use the async compat code for both cases.
193 static inline int futex_noasync(int32_t *uaddr
, int op
, int32_t val
,
194 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
)
196 return compat_futex_async(uaddr
, op
, val
, timeout
, uaddr2
, val3
);
199 static inline int futex_async(int32_t *uaddr
, int op
, int32_t val
,
200 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
)
202 return compat_futex_async(uaddr
, op
, val
, timeout
, uaddr2
, val3
);
207 static inline int futex_noasync(int32_t *uaddr
, int op
, int32_t val
,
208 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
)
210 return compat_futex_noasync(uaddr
, op
, val
, timeout
, uaddr2
, val3
);
213 static inline int futex_async(int32_t *uaddr
, int op
, int32_t val
,
214 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
)
216 return compat_futex_async(uaddr
, op
, val
, timeout
, uaddr2
, val3
);
225 #endif /* _URCU_FUTEX_H */
This page took 0.052674 seconds and 4 git commands to generate.