Static arch and uatomic headers
[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,
113 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
114{
115 int umtx_op;
116 void *umtx_uaddr = NULL, *umtx_uaddr2 = NULL;
117 struct _umtx_time umtx_timeout = {
118 ._flags = UMTX_ABSTIME,
119 ._clockid = CLOCK_MONOTONIC,
120 };
121
122 switch (op) {
123 case FUTEX_WAIT:
124 /* On FreeBSD, a "u_int" is a 32-bit integer. */
125 umtx_op = UMTX_OP_WAIT_UINT;
126 if (timeout != NULL) {
127 umtx_timeout._timeout = *timeout;
128 umtx_uaddr = (void *) sizeof(umtx_timeout);
129 umtx_uaddr2 = (void *) &umtx_timeout;
130 }
131 break;
132 case FUTEX_WAKE:
133 umtx_op = UMTX_OP_WAKE;
134 break;
135 default:
136 errno = EINVAL;
137 return -1;
138 }
139
140 return _umtx_op(uaddr, umtx_op, (uint32_t) val, umtx_uaddr,
141 umtx_uaddr2);
142}
143
144static inline int futex_noasync(int32_t *uaddr, int op, int32_t val,
145 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
146{
147 return futex_async(uaddr, op, val, timeout, uaddr2, val3);
148}
149
bb109fb6
MJ
150#elif defined(__CYGWIN__)
151
152/*
153 * The futex_noasync compat code uses a weak symbol to share state across
154 * different shared object which is not possible on Windows with the
155 * Portable Executable format. Use the async compat code for both cases.
156 */
157static 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 compat_futex_async(uaddr, op, val, timeout, uaddr2, val3);
161}
162
163static inline int futex_async(int32_t *uaddr, int op, int32_t val,
164 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
165{
166 return compat_futex_async(uaddr, op, val, timeout, uaddr2, val3);
167}
168
49617de1 169#else
42dfe454
MD
170
171static inline int futex_noasync(int32_t *uaddr, int op, int32_t val,
172 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
173{
174 return compat_futex_noasync(uaddr, op, val, timeout, uaddr2, val3);
175}
176
177static inline int futex_async(int32_t *uaddr, int op, int32_t val,
178 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
179{
180 return compat_futex_async(uaddr, op, val, timeout, uaddr2, val3);
181}
182
49617de1
MD
183#endif
184
67ecffc0 185#ifdef __cplusplus
36bc70a8
MD
186}
187#endif
188
49617de1 189#endif /* _URCU_FUTEX_H */
This page took 0.045865 seconds and 4 git commands to generate.