Commit | Line | Data |
---|---|---|
49617de1 MD |
1 | #ifndef _URCU_FUTEX_H |
2 | #define _URCU_FUTEX_H | |
3 | ||
4 | /* | |
5 | * urcu-futex.h | |
6 | * | |
7 | * Userspace RCU - sys_futex/compat_futex header. | |
8 | * | |
3282a76b MD |
9 | * Copyright 2011-2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
10 | * | |
49617de1 MD |
11 | * This library is free software; you can redistribute it and/or |
12 | * modify it under the terms of the GNU Lesser General Public | |
13 | * License as published by the Free Software Foundation; either | |
14 | * version 2.1 of the License, or (at your option) any later version. | |
15 | * | |
16 | * This library is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
19 | * Lesser General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU Lesser General Public | |
22 | * License along with this library; if not, write to the Free Software | |
23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
24 | */ | |
25 | ||
26 | #include <urcu/config.h> | |
ca91060e MJ |
27 | #include <urcu/syscall-compat.h> |
28 | ||
32146086 | 29 | #include <errno.h> |
6d841bc2 | 30 | #include <stdint.h> |
6a29bfc1 | 31 | #include <time.h> |
49617de1 | 32 | |
d428afc4 MD |
33 | #if (defined(__linux__) && defined(__NR_futex)) |
34 | ||
35 | /* For backwards compat */ | |
36 | #define CONFIG_RCU_HAVE_FUTEX 1 | |
37 | ||
38 | #include <unistd.h> | |
39 | #include <errno.h> | |
40 | #include <urcu/compiler.h> | |
41 | #include <urcu/arch.h> | |
42 | ||
43 | #elif defined(__FreeBSD__) | |
44 | ||
45 | #include <sys/types.h> | |
46 | #include <sys/umtx.h> | |
47 | ||
48 | #endif | |
49 | ||
36bc70a8 MD |
50 | #ifdef __cplusplus |
51 | extern "C" { | |
67ecffc0 | 52 | #endif |
36bc70a8 | 53 | |
49617de1 MD |
54 | #define FUTEX_WAIT 0 |
55 | #define FUTEX_WAKE 1 | |
56 | ||
57 | /* | |
58 | * sys_futex compatibility header. | |
59 | * Use *only* *either of* futex_noasync OR futex_async on a given address. | |
60 | * | |
61 | * futex_noasync cannot be executed in signal handlers, but ensures that | |
62 | * it will be put in a wait queue even in compatibility mode. | |
63 | * | |
64 | * futex_async is signal-handler safe for the wakeup. It uses polling | |
65 | * on the wait-side in compatibility mode. | |
b0a841b4 MD |
66 | * |
67 | * BEWARE: sys_futex() FUTEX_WAIT may return early if interrupted | |
68 | * (returns EINTR). | |
49617de1 MD |
69 | */ |
70 | ||
42dfe454 MD |
71 | extern int compat_futex_noasync(int32_t *uaddr, int op, int32_t val, |
72 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3); | |
73 | extern int compat_futex_async(int32_t *uaddr, int op, int32_t val, | |
74 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3); | |
75 | ||
0afb29b2 | 76 | #if (defined(__linux__) && defined(__NR_futex)) |
ca91060e | 77 | |
42dfe454 MD |
78 | static inline int futex(int32_t *uaddr, int op, int32_t val, |
79 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3) | |
80 | { | |
81 | return syscall(__NR_futex, uaddr, op, val, timeout, | |
82 | uaddr2, val3); | |
83 | } | |
84 | ||
85 | static inline int futex_noasync(int32_t *uaddr, int op, int32_t val, | |
86 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3) | |
87 | { | |
88 | int ret; | |
89 | ||
90 | ret = futex(uaddr, op, val, timeout, uaddr2, val3); | |
91 | if (caa_unlikely(ret < 0 && errno == ENOSYS)) { | |
b2633d21 MD |
92 | /* |
93 | * The fallback on ENOSYS is the async-safe version of | |
94 | * the compat futex implementation, because the | |
95 | * async-safe compat implementation allows being used | |
96 | * concurrently with calls to futex(). Indeed, sys_futex | |
97 | * FUTEX_WAIT, on some architectures (mips and parisc), | |
98 | * within a given process, spuriously return ENOSYS due | |
99 | * to signal restart bugs on some kernel versions. | |
100 | */ | |
101 | return compat_futex_async(uaddr, op, val, timeout, | |
42dfe454 MD |
102 | uaddr2, val3); |
103 | } | |
104 | return ret; | |
105 | ||
106 | } | |
107 | ||
108 | static inline int futex_async(int32_t *uaddr, int op, int32_t val, | |
109 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3) | |
110 | { | |
111 | int ret; | |
112 | ||
113 | ret = futex(uaddr, op, val, timeout, uaddr2, val3); | |
114 | if (caa_unlikely(ret < 0 && errno == ENOSYS)) { | |
115 | return compat_futex_async(uaddr, op, val, timeout, | |
116 | uaddr2, val3); | |
117 | } | |
118 | return ret; | |
119 | } | |
120 | ||
32146086 AX |
121 | #elif defined(__FreeBSD__) |
122 | ||
32146086 | 123 | static inline int futex_async(int32_t *uaddr, int op, int32_t val, |
a142df4e MJ |
124 | const struct timespec *timeout, |
125 | int32_t *uaddr2 __attribute__((unused)), | |
126 | int32_t val3 __attribute__((unused))) | |
32146086 AX |
127 | { |
128 | int umtx_op; | |
129 | void *umtx_uaddr = NULL, *umtx_uaddr2 = NULL; | |
130 | struct _umtx_time umtx_timeout = { | |
131 | ._flags = UMTX_ABSTIME, | |
132 | ._clockid = CLOCK_MONOTONIC, | |
133 | }; | |
134 | ||
135 | switch (op) { | |
136 | case FUTEX_WAIT: | |
137 | /* On FreeBSD, a "u_int" is a 32-bit integer. */ | |
138 | umtx_op = UMTX_OP_WAIT_UINT; | |
139 | if (timeout != NULL) { | |
140 | umtx_timeout._timeout = *timeout; | |
141 | umtx_uaddr = (void *) sizeof(umtx_timeout); | |
142 | umtx_uaddr2 = (void *) &umtx_timeout; | |
143 | } | |
144 | break; | |
145 | case FUTEX_WAKE: | |
146 | umtx_op = UMTX_OP_WAKE; | |
147 | break; | |
148 | default: | |
149 | errno = EINVAL; | |
150 | return -1; | |
151 | } | |
152 | ||
153 | return _umtx_op(uaddr, umtx_op, (uint32_t) val, umtx_uaddr, | |
154 | umtx_uaddr2); | |
155 | } | |
156 | ||
157 | static inline int futex_noasync(int32_t *uaddr, int op, int32_t val, | |
158 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3) | |
159 | { | |
160 | return futex_async(uaddr, op, val, timeout, uaddr2, val3); | |
161 | } | |
162 | ||
bb109fb6 MJ |
163 | #elif defined(__CYGWIN__) |
164 | ||
165 | /* | |
166 | * The futex_noasync compat code uses a weak symbol to share state across | |
167 | * different shared object which is not possible on Windows with the | |
168 | * Portable Executable format. Use the async compat code for both cases. | |
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_async(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 | ||
49617de1 | 182 | #else |
42dfe454 MD |
183 | |
184 | static inline int futex_noasync(int32_t *uaddr, int op, int32_t val, | |
185 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3) | |
186 | { | |
187 | return compat_futex_noasync(uaddr, op, val, timeout, uaddr2, val3); | |
188 | } | |
189 | ||
190 | static inline int futex_async(int32_t *uaddr, int op, int32_t val, | |
191 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3) | |
192 | { | |
193 | return compat_futex_async(uaddr, op, val, timeout, uaddr2, val3); | |
194 | } | |
195 | ||
49617de1 MD |
196 | #endif |
197 | ||
67ecffc0 | 198 | #ifdef __cplusplus |
36bc70a8 MD |
199 | } |
200 | #endif | |
201 | ||
49617de1 | 202 | #endif /* _URCU_FUTEX_H */ |