Add support for custom memory allocators for rculfhash
[urcu.git] / include / urcu / futex.h
1 // SPDX-FileCopyrightText: 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4
5 #ifndef _URCU_FUTEX_H
6 #define _URCU_FUTEX_H
7
8 /*
9 * Userspace RCU - sys_futex/compat_futex header.
10 */
11
12 #include <urcu/config.h>
13 #include <urcu/syscall-compat.h>
14
15 #include <errno.h>
16 #include <stdint.h>
17 #include <time.h>
18
19 #if (defined(__linux__) && defined(__NR_futex))
20
21 /* For backwards compat */
22 #define CONFIG_RCU_HAVE_FUTEX 1
23
24 #include <unistd.h>
25 #include <errno.h>
26 #include <urcu/compiler.h>
27 #include <urcu/arch.h>
28
29 #elif defined(__FreeBSD__)
30
31 #include <sys/types.h>
32 #include <sys/umtx.h>
33
34 #endif
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39
40 #define FUTEX_WAIT 0
41 #define FUTEX_WAKE 1
42
43 /*
44 * sys_futex compatibility header.
45 * Use *only* *either of* futex_noasync OR futex_async on a given address.
46 *
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.
49 *
50 * futex_async is signal-handler safe for the wakeup. It uses polling
51 * on the wait-side in compatibility mode.
52 *
53 * BEWARE: sys_futex() FUTEX_WAIT may return early if interrupted
54 * (returns EINTR).
55 */
56
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);
61
62 #if (defined(__linux__) && defined(__NR_futex))
63
64 static inline int futex(int32_t *uaddr, int op, int32_t val,
65 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
66 {
67 return syscall(__NR_futex, uaddr, op, val, timeout,
68 uaddr2, val3);
69 }
70
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)
73 {
74 int ret;
75
76 ret = futex(uaddr, op, val, timeout, uaddr2, val3);
77 if (caa_unlikely(ret < 0 && errno == ENOSYS)) {
78 /*
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.
86 */
87 return compat_futex_async(uaddr, op, val, timeout,
88 uaddr2, val3);
89 }
90 return ret;
91
92 }
93
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)
96 {
97 int ret;
98
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,
102 uaddr2, val3);
103 }
104 return ret;
105 }
106
107 #elif defined(__FreeBSD__)
108
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)))
113 {
114 int umtx_op;
115 void *umtx_uaddr = NULL, *umtx_uaddr2 = NULL;
116 struct _umtx_time umtx_timeout = {
117 ._flags = UMTX_ABSTIME,
118 ._clockid = CLOCK_MONOTONIC,
119 };
120
121 switch (op) {
122 case FUTEX_WAIT:
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;
129 }
130 break;
131 case FUTEX_WAKE:
132 umtx_op = UMTX_OP_WAKE;
133 break;
134 default:
135 errno = EINVAL;
136 return -1;
137 }
138
139 return _umtx_op(uaddr, umtx_op, (uint32_t) val, umtx_uaddr,
140 umtx_uaddr2);
141 }
142
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)
145 {
146 return futex_async(uaddr, op, val, timeout, uaddr2, val3);
147 }
148
149 #elif defined(__CYGWIN__)
150
151 /*
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.
155 */
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)
158 {
159 return compat_futex_async(uaddr, op, val, timeout, uaddr2, val3);
160 }
161
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)
164 {
165 return compat_futex_async(uaddr, op, val, timeout, uaddr2, val3);
166 }
167
168 #else
169
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)
172 {
173 return compat_futex_noasync(uaddr, op, val, timeout, uaddr2, val3);
174 }
175
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)
178 {
179 return compat_futex_async(uaddr, op, val, timeout, uaddr2, val3);
180 }
181
182 #endif
183
184 #ifdef __cplusplus
185 }
186 #endif
187
188 #endif /* _URCU_FUTEX_H */
This page took 0.03237 seconds and 4 git commands to generate.