1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#ifndef _FUTEXTEST_H
22#define _FUTEXTEST_H
23
24#include <unistd.h>
25#include <sys/syscall.h>
26#include <sys/types.h>
27#include <linux/futex.h>
28
29typedef volatile u_int32_t futex_t;
30#define FUTEX_INITIALIZER 0
31
32
33#ifndef FUTEX_WAIT_BITSET
34#define FUTEX_WAIT_BITSET 9
35#endif
36#ifndef FUTEX_WAKE_BITSET
37#define FUTEX_WAKE_BITSET 10
38#endif
39#ifndef FUTEX_WAIT_REQUEUE_PI
40#define FUTEX_WAIT_REQUEUE_PI 11
41#endif
42#ifndef FUTEX_CMP_REQUEUE_PI
43#define FUTEX_CMP_REQUEUE_PI 12
44#endif
45#ifndef FUTEX_WAIT_REQUEUE_PI_PRIVATE
46#define FUTEX_WAIT_REQUEUE_PI_PRIVATE (FUTEX_WAIT_REQUEUE_PI | \
47 FUTEX_PRIVATE_FLAG)
48#endif
49#ifndef FUTEX_REQUEUE_PI_PRIVATE
50#define FUTEX_CMP_REQUEUE_PI_PRIVATE (FUTEX_CMP_REQUEUE_PI | \
51 FUTEX_PRIVATE_FLAG)
52#endif
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74#define futex(uaddr, op, val, timeout, uaddr2, val3, opflags) \
75 syscall(SYS_futex, uaddr, op | opflags, val, timeout, uaddr2, val3)
76
77
78
79
80
81static inline int
82futex_wait(futex_t *uaddr, futex_t val, struct timespec *timeout, int opflags)
83{
84 return futex(uaddr, FUTEX_WAIT, val, timeout, NULL, 0, opflags);
85}
86
87
88
89
90
91static inline int
92futex_wake(futex_t *uaddr, int nr_wake, int opflags)
93{
94 return futex(uaddr, FUTEX_WAKE, nr_wake, NULL, NULL, 0, opflags);
95}
96
97
98
99
100
101static inline int
102futex_wait_bitset(futex_t *uaddr, futex_t val, struct timespec *timeout,
103 u_int32_t bitset, int opflags)
104{
105 return futex(uaddr, FUTEX_WAIT_BITSET, val, timeout, NULL, bitset,
106 opflags);
107}
108
109
110
111
112
113static inline int
114futex_wake_bitset(futex_t *uaddr, int nr_wake, u_int32_t bitset, int opflags)
115{
116 return futex(uaddr, FUTEX_WAKE_BITSET, nr_wake, NULL, NULL, bitset,
117 opflags);
118}
119
120
121
122
123
124static inline int
125futex_lock_pi(futex_t *uaddr, struct timespec *timeout, int detect,
126 int opflags)
127{
128 return futex(uaddr, FUTEX_LOCK_PI, detect, timeout, NULL, 0, opflags);
129}
130
131
132
133
134static inline int
135futex_unlock_pi(futex_t *uaddr, int opflags)
136{
137 return futex(uaddr, FUTEX_UNLOCK_PI, 0, NULL, NULL, 0, opflags);
138}
139
140
141
142
143static inline int
144futex_wake_op(futex_t *uaddr, futex_t *uaddr2, int nr_wake, int nr_wake2,
145 int wake_op, int opflags)
146{
147 return futex(uaddr, FUTEX_WAKE_OP, nr_wake, nr_wake2, uaddr2, wake_op,
148 opflags);
149}
150
151
152
153
154
155
156
157
158
159static inline int
160futex_requeue(futex_t *uaddr, futex_t *uaddr2, int nr_wake, int nr_requeue,
161 int opflags)
162{
163 return futex(uaddr, FUTEX_REQUEUE, nr_wake, nr_requeue, uaddr2, 0,
164 opflags);
165}
166
167
168
169
170
171
172static inline int
173futex_cmp_requeue(futex_t *uaddr, futex_t val, futex_t *uaddr2, int nr_wake,
174 int nr_requeue, int opflags)
175{
176 return futex(uaddr, FUTEX_CMP_REQUEUE, nr_wake, nr_requeue, uaddr2,
177 val, opflags);
178}
179
180
181
182
183
184
185
186
187
188static inline int
189futex_wait_requeue_pi(futex_t *uaddr, futex_t val, futex_t *uaddr2,
190 struct timespec *timeout, int opflags)
191{
192 return futex(uaddr, FUTEX_WAIT_REQUEUE_PI, val, timeout, uaddr2, 0,
193 opflags);
194}
195
196
197
198
199
200
201
202
203static inline int
204futex_cmp_requeue_pi(futex_t *uaddr, futex_t val, futex_t *uaddr2, int nr_wake,
205 int nr_requeue, int opflags)
206{
207 return futex(uaddr, FUTEX_CMP_REQUEUE_PI, nr_wake, nr_requeue, uaddr2,
208 val, opflags);
209}
210
211
212
213
214
215
216
217
218
219
220
221
222static inline u_int32_t
223futex_cmpxchg(futex_t *uaddr, u_int32_t oldval, u_int32_t newval)
224{
225 return __sync_val_compare_and_swap(uaddr, oldval, newval);
226}
227
228
229
230
231
232
233
234static inline u_int32_t
235futex_dec(futex_t *uaddr)
236{
237 return __sync_sub_and_fetch(uaddr, 1);
238}
239
240
241
242
243
244
245
246static inline u_int32_t
247futex_inc(futex_t *uaddr)
248{
249 return __sync_add_and_fetch(uaddr, 1);
250}
251
252
253
254
255
256
257
258
259static inline u_int32_t
260futex_set(futex_t *uaddr, u_int32_t newval)
261{
262 *uaddr = newval;
263 return newval;
264}
265
266#endif
267