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>
44 * sys_futex compatibility header.
45 * Use *only* *either of* futex_noasync OR futex_async on a given address.
47 * futex_noasync cannot be executed in signal handlers, but ensures that
48 * it will be put in a wait queue even in compatibility mode.
50 * futex_async is signal-handler safe for the wakeup. It uses polling
51 * on the wait-side in compatibility mode.
53 * BEWARE: sys_futex() FUTEX_WAIT may return early if interrupted
57 extern int compat_futex_noasync(int32_t *uaddr
, int op
, int32_t val
,
58 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
);
59 extern int compat_futex_async(int32_t *uaddr
, int op
, int32_t val
,
60 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
);
62 #if (defined(__linux__) && defined(__NR_futex))
64 static inline int futex(int32_t *uaddr
, int op
, int32_t val
,
65 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
)
67 return syscall(__NR_futex
, uaddr
, op
, val
, timeout
,
71 static inline int futex_noasync(int32_t *uaddr
, int op
, int32_t val
,
72 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
)
76 ret
= futex(uaddr
, op
, val
, timeout
, uaddr2
, val3
);
77 if (caa_unlikely(ret
< 0 && errno
== ENOSYS
)) {
79 * The fallback on ENOSYS is the async-safe version of
80 * the compat futex implementation, because the
81 * async-safe compat implementation allows being used
82 * concurrently with calls to futex(). Indeed, sys_futex
83 * FUTEX_WAIT, on some architectures (mips and parisc),
84 * within a given process, spuriously return ENOSYS due
85 * to signal restart bugs on some kernel versions.
87 return compat_futex_async(uaddr
, op
, val
, timeout
,
94 static inline int futex_async(int32_t *uaddr
, int op
, int32_t val
,
95 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
)
99 ret
= futex(uaddr
, op
, val
, timeout
, uaddr2
, val3
);
100 if (caa_unlikely(ret
< 0 && errno
== ENOSYS
)) {
101 return compat_futex_async(uaddr
, op
, val
, timeout
,
107 #elif defined(__FreeBSD__)
109 static inline int futex_async(int32_t *uaddr
, int op
, int32_t val
,
110 const struct timespec
*timeout
,
111 int32_t *uaddr2
__attribute__((unused
)),
112 int32_t val3
__attribute__((unused
)))
115 void *umtx_uaddr
= NULL
, *umtx_uaddr2
= NULL
;
116 struct _umtx_time umtx_timeout
= {
117 ._flags
= UMTX_ABSTIME
,
118 ._clockid
= CLOCK_MONOTONIC
,
123 /* On FreeBSD, a "u_int" is a 32-bit integer. */
124 umtx_op
= UMTX_OP_WAIT_UINT
;
125 if (timeout
!= NULL
) {
126 umtx_timeout
._timeout
= *timeout
;
127 umtx_uaddr
= (void *) sizeof(umtx_timeout
);
128 umtx_uaddr2
= (void *) &umtx_timeout
;
132 umtx_op
= UMTX_OP_WAKE
;
139 return _umtx_op(uaddr
, umtx_op
, (uint32_t) val
, umtx_uaddr
,
143 static inline int futex_noasync(int32_t *uaddr
, int op
, int32_t val
,
144 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
)
146 return futex_async(uaddr
, op
, val
, timeout
, uaddr2
, val3
);
149 #elif defined(__CYGWIN__)
152 * The futex_noasync compat code uses a weak symbol to share state across
153 * different shared object which is not possible on Windows with the
154 * Portable Executable format. Use the async compat code for both cases.
156 static inline int futex_noasync(int32_t *uaddr
, int op
, int32_t val
,
157 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
)
159 return compat_futex_async(uaddr
, op
, val
, timeout
, uaddr2
, val3
);
162 static inline int futex_async(int32_t *uaddr
, int op
, int32_t val
,
163 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
)
165 return compat_futex_async(uaddr
, op
, val
, timeout
, uaddr2
, val3
);
170 static inline int futex_noasync(int32_t *uaddr
, int op
, int32_t val
,
171 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
)
173 return compat_futex_noasync(uaddr
, op
, val
, timeout
, uaddr2
, val3
);
176 static inline int futex_async(int32_t *uaddr
, int op
, int32_t val
,
177 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
)
179 return compat_futex_async(uaddr
, op
, val
, timeout
, uaddr2
, val3
);
188 #endif /* _URCU_FUTEX_H */
This page took 0.03237 seconds and 4 git commands to generate.