urcu/arch/generic: Use atomic builtins if configured
[urcu.git] / include / urcu / futex.h
CommitLineData
d3d3857f
MJ
1// SPDX-FileCopyrightText: 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4
49617de1
MD
5#ifndef _URCU_FUTEX_H
6#define _URCU_FUTEX_H
7
8/*
49617de1 9 * Userspace RCU - sys_futex/compat_futex header.
49617de1
MD
10 */
11
12#include <urcu/config.h>
ca91060e
MJ
13#include <urcu/syscall-compat.h>
14
32146086 15#include <errno.h>
6d841bc2 16#include <stdint.h>
6a29bfc1 17#include <time.h>
49617de1 18
d428afc4
MD
19#if (defined(__linux__) && defined(__NR_futex))
20
21/* For backwards compat */
22#define CONFIG_RCU_HAVE_FUTEX 1
23
24#include <unistd.h>
25#include <errno.h>
26#include <urcu/compiler.h>
27#include <urcu/arch.h>
28
29#elif defined(__FreeBSD__)
30
31#include <sys/types.h>
32#include <sys/umtx.h>
33
34#endif
35
36bc70a8
MD
36#ifdef __cplusplus
37extern "C" {
67ecffc0 38#endif
36bc70a8 39
49617de1
MD
40#define FUTEX_WAIT 0
41#define FUTEX_WAKE 1
42
43/*
44 * sys_futex compatibility header.
45 * Use *only* *either of* futex_noasync OR futex_async on a given address.
46 *
47 * futex_noasync cannot be executed in signal handlers, but ensures that
48 * it will be put in a wait queue even in compatibility mode.
49 *
50 * futex_async is signal-handler safe for the wakeup. It uses polling
51 * on the wait-side in compatibility mode.
b0a841b4
MD
52 *
53 * BEWARE: sys_futex() FUTEX_WAIT may return early if interrupted
54 * (returns EINTR).
49617de1
MD
55 */
56
42dfe454
MD
57extern int compat_futex_noasync(int32_t *uaddr, int op, int32_t val,
58 const struct timespec *timeout, int32_t *uaddr2, int32_t val3);
59extern int compat_futex_async(int32_t *uaddr, int op, int32_t val,
60 const struct timespec *timeout, int32_t *uaddr2, int32_t val3);
61
0afb29b2 62#if (defined(__linux__) && defined(__NR_futex))
ca91060e 63
42dfe454
MD
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
32146086 109static inline int futex_async(int32_t *uaddr, int op, int32_t val,
a142df4e
MJ
110 const struct timespec *timeout,
111 int32_t *uaddr2 __attribute__((unused)),
112 int32_t val3 __attribute__((unused)))
32146086
AX
113{
114 int umtx_op;
115 void *umtx_uaddr = NULL, *umtx_uaddr2 = NULL;
116 struct _umtx_time umtx_timeout = {
117 ._flags = UMTX_ABSTIME,
118 ._clockid = CLOCK_MONOTONIC,
119 };
120
121 switch (op) {
122 case FUTEX_WAIT:
123 /* On FreeBSD, a "u_int" is a 32-bit integer. */
124 umtx_op = UMTX_OP_WAIT_UINT;
125 if (timeout != NULL) {
126 umtx_timeout._timeout = *timeout;
127 umtx_uaddr = (void *) sizeof(umtx_timeout);
128 umtx_uaddr2 = (void *) &umtx_timeout;
129 }
130 break;
131 case FUTEX_WAKE:
132 umtx_op = UMTX_OP_WAKE;
133 break;
134 default:
135 errno = EINVAL;
136 return -1;
137 }
138
139 return _umtx_op(uaddr, umtx_op, (uint32_t) val, umtx_uaddr,
140 umtx_uaddr2);
141}
142
143static inline int futex_noasync(int32_t *uaddr, int op, int32_t val,
144 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
145{
146 return futex_async(uaddr, op, val, timeout, uaddr2, val3);
147}
148
bb109fb6
MJ
149#elif defined(__CYGWIN__)
150
151/*
152 * The futex_noasync compat code uses a weak symbol to share state across
153 * different shared object which is not possible on Windows with the
154 * Portable Executable format. Use the async compat code for both cases.
155 */
156static inline int futex_noasync(int32_t *uaddr, int op, int32_t val,
157 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
158{
159 return compat_futex_async(uaddr, op, val, timeout, uaddr2, val3);
160}
161
162static inline int futex_async(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
49617de1 168#else
42dfe454
MD
169
170static 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_noasync(uaddr, op, val, timeout, uaddr2, val3);
174}
175
176static 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
MD
182#endif
183
67ecffc0 184#ifdef __cplusplus
36bc70a8
MD
185}
186#endif
187
49617de1 188#endif /* _URCU_FUTEX_H */
This page took 0.058503 seconds and 4 git commands to generate.