Fix: futex.h: include headers outside extern C
[urcu.git] / include / urcu / futex.h
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 *
9 * Copyright 2011-2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 *
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>
27 #include <errno.h>
28 #include <stdint.h>
29 #include <time.h>
30
31 #ifdef CONFIG_RCU_HAVE_FUTEX
32
33 #include <unistd.h>
34 #include <errno.h>
35 #include <urcu/compiler.h>
36 #include <urcu/arch.h>
37
38 #elif defined(__FreeBSD__)
39
40 #include <sys/types.h>
41 #include <sys/umtx.h>
42
43 #endif
44
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48
49 #define FUTEX_WAIT 0
50 #define FUTEX_WAKE 1
51
52 /*
53 * sys_futex compatibility header.
54 * Use *only* *either of* futex_noasync OR futex_async on a given address.
55 *
56 * futex_noasync cannot be executed in signal handlers, but ensures that
57 * it will be put in a wait queue even in compatibility mode.
58 *
59 * futex_async is signal-handler safe for the wakeup. It uses polling
60 * on the wait-side in compatibility mode.
61 *
62 * BEWARE: sys_futex() FUTEX_WAIT may return early if interrupted
63 * (returns EINTR).
64 */
65
66 extern int compat_futex_noasync(int32_t *uaddr, int op, int32_t val,
67 const struct timespec *timeout, int32_t *uaddr2, int32_t val3);
68 extern int compat_futex_async(int32_t *uaddr, int op, int32_t val,
69 const struct timespec *timeout, int32_t *uaddr2, int32_t val3);
70
71 #ifdef CONFIG_RCU_HAVE_FUTEX
72
73 static inline int futex(int32_t *uaddr, int op, int32_t val,
74 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
75 {
76 return syscall(__NR_futex, uaddr, op, val, timeout,
77 uaddr2, val3);
78 }
79
80 static inline int futex_noasync(int32_t *uaddr, int op, int32_t val,
81 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
82 {
83 int ret;
84
85 ret = futex(uaddr, op, val, timeout, uaddr2, val3);
86 if (caa_unlikely(ret < 0 && errno == ENOSYS)) {
87 /*
88 * The fallback on ENOSYS is the async-safe version of
89 * the compat futex implementation, because the
90 * async-safe compat implementation allows being used
91 * concurrently with calls to futex(). Indeed, sys_futex
92 * FUTEX_WAIT, on some architectures (mips and parisc),
93 * within a given process, spuriously return ENOSYS due
94 * to signal restart bugs on some kernel versions.
95 */
96 return compat_futex_async(uaddr, op, val, timeout,
97 uaddr2, val3);
98 }
99 return ret;
100
101 }
102
103 static inline int futex_async(int32_t *uaddr, int op, int32_t val,
104 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
105 {
106 int ret;
107
108 ret = futex(uaddr, op, val, timeout, uaddr2, val3);
109 if (caa_unlikely(ret < 0 && errno == ENOSYS)) {
110 return compat_futex_async(uaddr, op, val, timeout,
111 uaddr2, val3);
112 }
113 return ret;
114 }
115
116 #elif defined(__FreeBSD__)
117
118 static inline int futex_async(int32_t *uaddr, int op, int32_t val,
119 const struct timespec *timeout,
120 int32_t *uaddr2 __attribute__((unused)),
121 int32_t val3 __attribute__((unused)))
122 {
123 int umtx_op;
124 void *umtx_uaddr = NULL, *umtx_uaddr2 = NULL;
125 struct _umtx_time umtx_timeout = {
126 ._flags = UMTX_ABSTIME,
127 ._clockid = CLOCK_MONOTONIC,
128 };
129
130 switch (op) {
131 case FUTEX_WAIT:
132 /* On FreeBSD, a "u_int" is a 32-bit integer. */
133 umtx_op = UMTX_OP_WAIT_UINT;
134 if (timeout != NULL) {
135 umtx_timeout._timeout = *timeout;
136 umtx_uaddr = (void *) sizeof(umtx_timeout);
137 umtx_uaddr2 = (void *) &umtx_timeout;
138 }
139 break;
140 case FUTEX_WAKE:
141 umtx_op = UMTX_OP_WAKE;
142 break;
143 default:
144 errno = EINVAL;
145 return -1;
146 }
147
148 return _umtx_op(uaddr, umtx_op, (uint32_t) val, umtx_uaddr,
149 umtx_uaddr2);
150 }
151
152 static inline int futex_noasync(int32_t *uaddr, int op, int32_t val,
153 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
154 {
155 return futex_async(uaddr, op, val, timeout, uaddr2, val3);
156 }
157
158 #elif defined(__CYGWIN__)
159
160 /*
161 * The futex_noasync compat code uses a weak symbol to share state across
162 * different shared object which is not possible on Windows with the
163 * Portable Executable format. Use the async compat code for both cases.
164 */
165 static inline int futex_noasync(int32_t *uaddr, int op, int32_t val,
166 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
167 {
168 return compat_futex_async(uaddr, op, val, timeout, uaddr2, val3);
169 }
170
171 static inline int futex_async(int32_t *uaddr, int op, int32_t val,
172 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
173 {
174 return compat_futex_async(uaddr, op, val, timeout, uaddr2, val3);
175 }
176
177 #else
178
179 static inline int futex_noasync(int32_t *uaddr, int op, int32_t val,
180 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
181 {
182 return compat_futex_noasync(uaddr, op, val, timeout, uaddr2, val3);
183 }
184
185 static inline int futex_async(int32_t *uaddr, int op, int32_t val,
186 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
187 {
188 return compat_futex_async(uaddr, op, val, timeout, uaddr2, val3);
189 }
190
191 #endif
192
193 #ifdef __cplusplus
194 }
195 #endif
196
197 #endif /* _URCU_FUTEX_H */
This page took 0.032566 seconds and 4 git commands to generate.