fix: warnings on non-Linux platforms
[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>
32146086 27#include <errno.h>
6d841bc2 28#include <stdint.h>
6a29bfc1 29#include <time.h>
49617de1 30
36bc70a8
MD
31#ifdef __cplusplus
32extern "C" {
67ecffc0 33#endif
36bc70a8 34
49617de1
MD
35#define FUTEX_WAIT 0
36#define FUTEX_WAKE 1
37
38/*
39 * sys_futex compatibility header.
40 * Use *only* *either of* futex_noasync OR futex_async on a given address.
41 *
42 * futex_noasync cannot be executed in signal handlers, but ensures that
43 * it will be put in a wait queue even in compatibility mode.
44 *
45 * futex_async is signal-handler safe for the wakeup. It uses polling
46 * on the wait-side in compatibility mode.
b0a841b4
MD
47 *
48 * BEWARE: sys_futex() FUTEX_WAIT may return early if interrupted
49 * (returns EINTR).
49617de1
MD
50 */
51
42dfe454
MD
52extern int compat_futex_noasync(int32_t *uaddr, int op, int32_t val,
53 const struct timespec *timeout, int32_t *uaddr2, int32_t val3);
54extern int compat_futex_async(int32_t *uaddr, int op, int32_t val,
55 const struct timespec *timeout, int32_t *uaddr2, int32_t val3);
56
02be5561 57#ifdef CONFIG_RCU_HAVE_FUTEX
42dfe454
MD
58
59#include <unistd.h>
60#include <errno.h>
61#include <urcu/compiler.h>
999991c6 62#include <urcu/arch.h>
42dfe454
MD
63
64static 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
71static 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)) {
b2633d21
MD
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,
42dfe454
MD
88 uaddr2, val3);
89 }
90 return ret;
91
92}
93
94static 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
32146086
AX
107#elif defined(__FreeBSD__)
108
109#include <sys/types.h>
110#include <sys/umtx.h>
111
112static inline int futex_async(int32_t *uaddr, int op, int32_t val,
0d657320
MJ
113 const struct timespec *timeout,
114 int32_t *uaddr2 __attribute__((unused)),
115 int32_t val3 __attribute__((unused)))
32146086
AX
116{
117 int umtx_op;
118 void *umtx_uaddr = NULL, *umtx_uaddr2 = NULL;
119 struct _umtx_time umtx_timeout = {
120 ._flags = UMTX_ABSTIME,
121 ._clockid = CLOCK_MONOTONIC,
122 };
123
124 switch (op) {
125 case FUTEX_WAIT:
126 /* On FreeBSD, a "u_int" is a 32-bit integer. */
127 umtx_op = UMTX_OP_WAIT_UINT;
128 if (timeout != NULL) {
129 umtx_timeout._timeout = *timeout;
130 umtx_uaddr = (void *) sizeof(umtx_timeout);
131 umtx_uaddr2 = (void *) &umtx_timeout;
132 }
133 break;
134 case FUTEX_WAKE:
135 umtx_op = UMTX_OP_WAKE;
136 break;
137 default:
138 errno = EINVAL;
139 return -1;
140 }
141
142 return _umtx_op(uaddr, umtx_op, (uint32_t) val, umtx_uaddr,
143 umtx_uaddr2);
144}
145
146static inline int futex_noasync(int32_t *uaddr, int op, int32_t val,
147 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
148{
149 return futex_async(uaddr, op, val, timeout, uaddr2, val3);
150}
151
bb109fb6
MJ
152#elif defined(__CYGWIN__)
153
154/*
155 * The futex_noasync compat code uses a weak symbol to share state across
156 * different shared object which is not possible on Windows with the
157 * Portable Executable format. Use the async compat code for both cases.
158 */
159static inline int futex_noasync(int32_t *uaddr, int op, int32_t val,
160 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
161{
162 return compat_futex_async(uaddr, op, val, timeout, uaddr2, val3);
163}
164
165static inline int futex_async(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
49617de1 171#else
42dfe454
MD
172
173static inline int futex_noasync(int32_t *uaddr, int op, int32_t val,
174 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
175{
176 return compat_futex_noasync(uaddr, op, val, timeout, uaddr2, val3);
177}
178
179static inline int futex_async(int32_t *uaddr, int op, int32_t val,
180 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
181{
182 return compat_futex_async(uaddr, op, val, timeout, uaddr2, val3);
183}
184
49617de1
MD
185#endif
186
67ecffc0 187#ifdef __cplusplus
36bc70a8
MD
188}
189#endif
190
49617de1 191#endif /* _URCU_FUTEX_H */
This page took 0.047791 seconds and 4 git commands to generate.