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