Commit | Line | Data |
---|---|---|
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> | |
6d841bc2 | 27 | #include <stdint.h> |
6a29bfc1 | 28 | #include <time.h> |
49617de1 | 29 | |
36bc70a8 MD |
30 | #ifdef __cplusplus |
31 | extern "C" { | |
67ecffc0 | 32 | #endif |
36bc70a8 | 33 | |
49617de1 MD |
34 | #define FUTEX_WAIT 0 |
35 | #define FUTEX_WAKE 1 | |
36 | ||
37 | /* | |
38 | * sys_futex compatibility header. | |
39 | * Use *only* *either of* futex_noasync OR futex_async on a given address. | |
40 | * | |
41 | * futex_noasync cannot be executed in signal handlers, but ensures that | |
42 | * it will be put in a wait queue even in compatibility mode. | |
43 | * | |
44 | * futex_async is signal-handler safe for the wakeup. It uses polling | |
45 | * on the wait-side in compatibility mode. | |
b0a841b4 MD |
46 | * |
47 | * BEWARE: sys_futex() FUTEX_WAIT may return early if interrupted | |
48 | * (returns EINTR). | |
49617de1 MD |
49 | */ |
50 | ||
42dfe454 MD |
51 | extern int compat_futex_noasync(int32_t *uaddr, int op, int32_t val, |
52 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3); | |
53 | extern int compat_futex_async(int32_t *uaddr, int op, int32_t val, | |
54 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3); | |
55 | ||
02be5561 | 56 | #ifdef CONFIG_RCU_HAVE_FUTEX |
42dfe454 MD |
57 | |
58 | #include <unistd.h> | |
59 | #include <errno.h> | |
60 | #include <urcu/compiler.h> | |
999991c6 | 61 | #include <urcu/arch.h> |
42dfe454 MD |
62 | |
63 | static inline int futex(int32_t *uaddr, int op, int32_t val, | |
64 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3) | |
65 | { | |
66 | return syscall(__NR_futex, uaddr, op, val, timeout, | |
67 | uaddr2, val3); | |
68 | } | |
69 | ||
70 | static inline int futex_noasync(int32_t *uaddr, int op, int32_t val, | |
71 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3) | |
72 | { | |
73 | int ret; | |
74 | ||
75 | ret = futex(uaddr, op, val, timeout, uaddr2, val3); | |
76 | if (caa_unlikely(ret < 0 && errno == ENOSYS)) { | |
b2633d21 MD |
77 | /* |
78 | * The fallback on ENOSYS is the async-safe version of | |
79 | * the compat futex implementation, because the | |
80 | * async-safe compat implementation allows being used | |
81 | * concurrently with calls to futex(). Indeed, sys_futex | |
82 | * FUTEX_WAIT, on some architectures (mips and parisc), | |
83 | * within a given process, spuriously return ENOSYS due | |
84 | * to signal restart bugs on some kernel versions. | |
85 | */ | |
86 | return compat_futex_async(uaddr, op, val, timeout, | |
42dfe454 MD |
87 | uaddr2, val3); |
88 | } | |
89 | return ret; | |
90 | ||
91 | } | |
92 | ||
93 | static inline int futex_async(int32_t *uaddr, int op, int32_t val, | |
94 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3) | |
95 | { | |
96 | int ret; | |
97 | ||
98 | ret = futex(uaddr, op, val, timeout, uaddr2, val3); | |
99 | if (caa_unlikely(ret < 0 && errno == ENOSYS)) { | |
100 | return compat_futex_async(uaddr, op, val, timeout, | |
101 | uaddr2, val3); | |
102 | } | |
103 | return ret; | |
104 | } | |
105 | ||
bb109fb6 MJ |
106 | #elif defined(__CYGWIN__) |
107 | ||
108 | /* | |
109 | * The futex_noasync compat code uses a weak symbol to share state across | |
110 | * different shared object which is not possible on Windows with the | |
111 | * Portable Executable format. Use the async compat code for both cases. | |
112 | */ | |
113 | static inline int futex_noasync(int32_t *uaddr, int op, int32_t val, | |
114 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3) | |
115 | { | |
116 | return compat_futex_async(uaddr, op, val, timeout, uaddr2, val3); | |
117 | } | |
118 | ||
119 | static inline int futex_async(int32_t *uaddr, int op, int32_t val, | |
120 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3) | |
121 | { | |
122 | return compat_futex_async(uaddr, op, val, timeout, uaddr2, val3); | |
123 | } | |
124 | ||
49617de1 | 125 | #else |
42dfe454 MD |
126 | |
127 | static inline int futex_noasync(int32_t *uaddr, int op, int32_t val, | |
128 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3) | |
129 | { | |
130 | return compat_futex_noasync(uaddr, op, val, timeout, uaddr2, val3); | |
131 | } | |
132 | ||
133 | static inline int futex_async(int32_t *uaddr, int op, int32_t val, | |
134 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3) | |
135 | { | |
136 | return compat_futex_async(uaddr, op, val, timeout, uaddr2, val3); | |
137 | } | |
138 | ||
49617de1 MD |
139 | #endif |
140 | ||
67ecffc0 | 141 | #ifdef __cplusplus |
36bc70a8 MD |
142 | } |
143 | #endif | |
144 | ||
49617de1 | 145 | #endif /* _URCU_FUTEX_H */ |