1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright © International Business Machines Corp., 2009
4*49cdfc7eSAndroid Build Coastguard Worker * Copyright (C) 2015 Cyril Hrubis <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker *
6*49cdfc7eSAndroid Build Coastguard Worker * DESCRIPTION
7*49cdfc7eSAndroid Build Coastguard Worker * Glibc independent futex library for testing kernel functionality.
8*49cdfc7eSAndroid Build Coastguard Worker *
9*49cdfc7eSAndroid Build Coastguard Worker * AUTHOR
10*49cdfc7eSAndroid Build Coastguard Worker * Darren Hart <[email protected]>
11*49cdfc7eSAndroid Build Coastguard Worker */
12*49cdfc7eSAndroid Build Coastguard Worker
13*49cdfc7eSAndroid Build Coastguard Worker #ifndef FUTEXTEST_H
14*49cdfc7eSAndroid Build Coastguard Worker #define FUTEXTEST_H
15*49cdfc7eSAndroid Build Coastguard Worker
16*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <sys/syscall.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/futex.h"
20*49cdfc7eSAndroid Build Coastguard Worker #include "tst_timer.h"
21*49cdfc7eSAndroid Build Coastguard Worker
22*49cdfc7eSAndroid Build Coastguard Worker #define FUTEX_INITIALIZER 0
23*49cdfc7eSAndroid Build Coastguard Worker
24*49cdfc7eSAndroid Build Coastguard Worker enum futex_fn_type {
25*49cdfc7eSAndroid Build Coastguard Worker FUTEX_FN_FUTEX,
26*49cdfc7eSAndroid Build Coastguard Worker FUTEX_FN_FUTEX64,
27*49cdfc7eSAndroid Build Coastguard Worker };
28*49cdfc7eSAndroid Build Coastguard Worker
29*49cdfc7eSAndroid Build Coastguard Worker struct futex_test_variants {
30*49cdfc7eSAndroid Build Coastguard Worker enum futex_fn_type fntype;
31*49cdfc7eSAndroid Build Coastguard Worker enum tst_ts_type tstype;
32*49cdfc7eSAndroid Build Coastguard Worker int (*gettime)(clockid_t clk_id, void *ts);
33*49cdfc7eSAndroid Build Coastguard Worker char *desc;
34*49cdfc7eSAndroid Build Coastguard Worker };
35*49cdfc7eSAndroid Build Coastguard Worker
futex_supported_by_kernel(enum futex_fn_type fntype)36*49cdfc7eSAndroid Build Coastguard Worker static inline void futex_supported_by_kernel(enum futex_fn_type fntype)
37*49cdfc7eSAndroid Build Coastguard Worker {
38*49cdfc7eSAndroid Build Coastguard Worker if (fntype != FUTEX_FN_FUTEX64)
39*49cdfc7eSAndroid Build Coastguard Worker return;
40*49cdfc7eSAndroid Build Coastguard Worker
41*49cdfc7eSAndroid Build Coastguard Worker /* Check if the syscall is implemented on the platform */
42*49cdfc7eSAndroid Build Coastguard Worker TEST(sys_futex_time64(NULL, 0, 0, NULL, NULL, 0));
43*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET == -1 && TST_ERR == ENOSYS)
44*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TCONF, "Test not supported on kernel/platform");
45*49cdfc7eSAndroid Build Coastguard Worker }
46*49cdfc7eSAndroid Build Coastguard Worker
47*49cdfc7eSAndroid Build Coastguard Worker /**
48*49cdfc7eSAndroid Build Coastguard Worker * futex_syscall() - futex syscall wrapper
49*49cdfc7eSAndroid Build Coastguard Worker * @fntype: Futex function type
50*49cdfc7eSAndroid Build Coastguard Worker * @uaddr: address of first futex
51*49cdfc7eSAndroid Build Coastguard Worker * @op: futex op code
52*49cdfc7eSAndroid Build Coastguard Worker * @val: typically expected value of uaddr, but varies by op
53*49cdfc7eSAndroid Build Coastguard Worker * @timeout: typically an absolute struct tst_ts (except where noted
54*49cdfc7eSAndroid Build Coastguard Worker * otherwise). Overloaded by some ops
55*49cdfc7eSAndroid Build Coastguard Worker * @uaddr2: address of second futex for some ops\
56*49cdfc7eSAndroid Build Coastguard Worker * @val3: varies by op
57*49cdfc7eSAndroid Build Coastguard Worker * @opflags: flags to be bitwise OR'd with op, such as FUTEX_PRIVATE_FLAG
58*49cdfc7eSAndroid Build Coastguard Worker *
59*49cdfc7eSAndroid Build Coastguard Worker * futex_syscall() is used by all the following futex op wrappers. It can also be
60*49cdfc7eSAndroid Build Coastguard Worker * used for misuse and abuse testing. Generally, the specific op wrappers
61*49cdfc7eSAndroid Build Coastguard Worker * should be used instead. It is a macro instead of an static inline function as
62*49cdfc7eSAndroid Build Coastguard Worker * some of the types over overloaded (timeout is used for nr_requeue for
63*49cdfc7eSAndroid Build Coastguard Worker * example).
64*49cdfc7eSAndroid Build Coastguard Worker *
65*49cdfc7eSAndroid Build Coastguard Worker * These argument descriptions are the defaults for all
66*49cdfc7eSAndroid Build Coastguard Worker * like-named arguments in the following wrappers except where noted below.
67*49cdfc7eSAndroid Build Coastguard Worker */
futex_syscall(enum futex_fn_type fntype,futex_t * uaddr,int futex_op,futex_t val,void * timeout,futex_t * uaddr2,int val3,int opflags)68*49cdfc7eSAndroid Build Coastguard Worker static inline int futex_syscall(enum futex_fn_type fntype, futex_t *uaddr,
69*49cdfc7eSAndroid Build Coastguard Worker int futex_op, futex_t val, void *timeout,
70*49cdfc7eSAndroid Build Coastguard Worker futex_t *uaddr2, int val3, int opflags)
71*49cdfc7eSAndroid Build Coastguard Worker {
72*49cdfc7eSAndroid Build Coastguard Worker int (*func)(int *uaddr, int futex_op, int val, void *to, int *uaddr2, int val3);
73*49cdfc7eSAndroid Build Coastguard Worker
74*49cdfc7eSAndroid Build Coastguard Worker if (fntype == FUTEX_FN_FUTEX)
75*49cdfc7eSAndroid Build Coastguard Worker func = sys_futex;
76*49cdfc7eSAndroid Build Coastguard Worker else
77*49cdfc7eSAndroid Build Coastguard Worker func = sys_futex_time64;
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker return func((int *)uaddr, futex_op | opflags, val, timeout, (int *)uaddr2, val3);
80*49cdfc7eSAndroid Build Coastguard Worker }
81*49cdfc7eSAndroid Build Coastguard Worker
82*49cdfc7eSAndroid Build Coastguard Worker /**
83*49cdfc7eSAndroid Build Coastguard Worker * futex_wait() - block on uaddr with optional timeout
84*49cdfc7eSAndroid Build Coastguard Worker * @timeout: relative timeout
85*49cdfc7eSAndroid Build Coastguard Worker */
86*49cdfc7eSAndroid Build Coastguard Worker static inline int
futex_wait(enum futex_fn_type fntype,futex_t * uaddr,futex_t val,struct tst_ts * timeout,int opflags)87*49cdfc7eSAndroid Build Coastguard Worker futex_wait(enum futex_fn_type fntype, futex_t *uaddr, futex_t val,
88*49cdfc7eSAndroid Build Coastguard Worker struct tst_ts *timeout, int opflags)
89*49cdfc7eSAndroid Build Coastguard Worker {
90*49cdfc7eSAndroid Build Coastguard Worker return futex_syscall(fntype, uaddr, FUTEX_WAIT, val,
91*49cdfc7eSAndroid Build Coastguard Worker tst_ts_get(timeout), NULL, 0, opflags);
92*49cdfc7eSAndroid Build Coastguard Worker }
93*49cdfc7eSAndroid Build Coastguard Worker
94*49cdfc7eSAndroid Build Coastguard Worker /**
95*49cdfc7eSAndroid Build Coastguard Worker * futex_wake() - wake one or more tasks blocked on uaddr
96*49cdfc7eSAndroid Build Coastguard Worker * @nr_wake: wake up to this many tasks
97*49cdfc7eSAndroid Build Coastguard Worker */
98*49cdfc7eSAndroid Build Coastguard Worker static inline int
futex_wake(enum futex_fn_type fntype,futex_t * uaddr,int nr_wake,int opflags)99*49cdfc7eSAndroid Build Coastguard Worker futex_wake(enum futex_fn_type fntype, futex_t *uaddr, int nr_wake, int opflags)
100*49cdfc7eSAndroid Build Coastguard Worker {
101*49cdfc7eSAndroid Build Coastguard Worker return futex_syscall(fntype, uaddr, FUTEX_WAKE, nr_wake, NULL, NULL, 0,
102*49cdfc7eSAndroid Build Coastguard Worker opflags);
103*49cdfc7eSAndroid Build Coastguard Worker }
104*49cdfc7eSAndroid Build Coastguard Worker
105*49cdfc7eSAndroid Build Coastguard Worker /**
106*49cdfc7eSAndroid Build Coastguard Worker * futex_wait_bitset() - block on uaddr with bitset
107*49cdfc7eSAndroid Build Coastguard Worker * @bitset: bitset to be used with futex_wake_bitset
108*49cdfc7eSAndroid Build Coastguard Worker */
109*49cdfc7eSAndroid Build Coastguard Worker static inline int
futex_wait_bitset(enum futex_fn_type fntype,futex_t * uaddr,futex_t val,struct tst_ts * timeout,u_int32_t bitset,int opflags)110*49cdfc7eSAndroid Build Coastguard Worker futex_wait_bitset(enum futex_fn_type fntype, futex_t *uaddr, futex_t val,
111*49cdfc7eSAndroid Build Coastguard Worker struct tst_ts *timeout, u_int32_t bitset, int opflags)
112*49cdfc7eSAndroid Build Coastguard Worker {
113*49cdfc7eSAndroid Build Coastguard Worker return futex_syscall(fntype, uaddr, FUTEX_WAIT_BITSET, val,
114*49cdfc7eSAndroid Build Coastguard Worker tst_ts_get(timeout), NULL, bitset, opflags);
115*49cdfc7eSAndroid Build Coastguard Worker }
116*49cdfc7eSAndroid Build Coastguard Worker
117*49cdfc7eSAndroid Build Coastguard Worker /**
118*49cdfc7eSAndroid Build Coastguard Worker * futex_wake_bitset() - wake one or more tasks blocked on uaddr with bitset
119*49cdfc7eSAndroid Build Coastguard Worker * @bitset: bitset to compare with that used in futex_wait_bitset
120*49cdfc7eSAndroid Build Coastguard Worker */
121*49cdfc7eSAndroid Build Coastguard Worker static inline int
futex_wake_bitset(enum futex_fn_type fntype,futex_t * uaddr,int nr_wake,u_int32_t bitset,int opflags)122*49cdfc7eSAndroid Build Coastguard Worker futex_wake_bitset(enum futex_fn_type fntype, futex_t *uaddr, int nr_wake,
123*49cdfc7eSAndroid Build Coastguard Worker u_int32_t bitset, int opflags)
124*49cdfc7eSAndroid Build Coastguard Worker {
125*49cdfc7eSAndroid Build Coastguard Worker return futex_syscall(fntype, uaddr, FUTEX_WAKE_BITSET, nr_wake, NULL,
126*49cdfc7eSAndroid Build Coastguard Worker NULL, bitset, opflags);
127*49cdfc7eSAndroid Build Coastguard Worker }
128*49cdfc7eSAndroid Build Coastguard Worker
129*49cdfc7eSAndroid Build Coastguard Worker /**
130*49cdfc7eSAndroid Build Coastguard Worker * futex_lock_pi() - block on uaddr as a PI mutex
131*49cdfc7eSAndroid Build Coastguard Worker * @detect: whether (1) or not (0) to perform deadlock detection
132*49cdfc7eSAndroid Build Coastguard Worker */
133*49cdfc7eSAndroid Build Coastguard Worker static inline int
futex_lock_pi(enum futex_fn_type fntype,futex_t * uaddr,struct tst_ts * timeout,int detect,int opflags)134*49cdfc7eSAndroid Build Coastguard Worker futex_lock_pi(enum futex_fn_type fntype, futex_t *uaddr, struct tst_ts *timeout,
135*49cdfc7eSAndroid Build Coastguard Worker int detect, int opflags)
136*49cdfc7eSAndroid Build Coastguard Worker {
137*49cdfc7eSAndroid Build Coastguard Worker return futex_syscall(fntype, uaddr, FUTEX_LOCK_PI, detect,
138*49cdfc7eSAndroid Build Coastguard Worker tst_ts_get(timeout), NULL, 0, opflags);
139*49cdfc7eSAndroid Build Coastguard Worker }
140*49cdfc7eSAndroid Build Coastguard Worker
141*49cdfc7eSAndroid Build Coastguard Worker /**
142*49cdfc7eSAndroid Build Coastguard Worker * futex_unlock_pi() - release uaddr as a PI mutex, waking the top waiter
143*49cdfc7eSAndroid Build Coastguard Worker */
144*49cdfc7eSAndroid Build Coastguard Worker static inline int
futex_unlock_pi(enum futex_fn_type fntype,futex_t * uaddr,int opflags)145*49cdfc7eSAndroid Build Coastguard Worker futex_unlock_pi(enum futex_fn_type fntype, futex_t *uaddr, int opflags)
146*49cdfc7eSAndroid Build Coastguard Worker {
147*49cdfc7eSAndroid Build Coastguard Worker return futex_syscall(fntype, uaddr, FUTEX_UNLOCK_PI, 0, NULL, NULL, 0,
148*49cdfc7eSAndroid Build Coastguard Worker opflags); }
149*49cdfc7eSAndroid Build Coastguard Worker
150*49cdfc7eSAndroid Build Coastguard Worker /**
151*49cdfc7eSAndroid Build Coastguard Worker * futex_wake_op() - FIXME: COME UP WITH A GOOD ONE LINE DESCRIPTION
152*49cdfc7eSAndroid Build Coastguard Worker */
153*49cdfc7eSAndroid Build Coastguard Worker static inline int
futex_wake_op(enum futex_fn_type fntype,futex_t * uaddr,futex_t * uaddr2,int nr_wake,int nr_wake2,int wake_op,int opflags)154*49cdfc7eSAndroid Build Coastguard Worker futex_wake_op(enum futex_fn_type fntype, futex_t *uaddr, futex_t *uaddr2,
155*49cdfc7eSAndroid Build Coastguard Worker int nr_wake, int nr_wake2, int wake_op, int opflags)
156*49cdfc7eSAndroid Build Coastguard Worker {
157*49cdfc7eSAndroid Build Coastguard Worker return futex_syscall(fntype, uaddr, FUTEX_WAKE_OP, nr_wake,
158*49cdfc7eSAndroid Build Coastguard Worker (void *)((unsigned long)nr_wake2), uaddr2, wake_op,
159*49cdfc7eSAndroid Build Coastguard Worker opflags);
160*49cdfc7eSAndroid Build Coastguard Worker }
161*49cdfc7eSAndroid Build Coastguard Worker
162*49cdfc7eSAndroid Build Coastguard Worker /**
163*49cdfc7eSAndroid Build Coastguard Worker * futex_requeue() - requeue without expected value comparison, deprecated
164*49cdfc7eSAndroid Build Coastguard Worker * @nr_wake: wake up to this many tasks
165*49cdfc7eSAndroid Build Coastguard Worker * @nr_requeue: requeue up to this many tasks
166*49cdfc7eSAndroid Build Coastguard Worker *
167*49cdfc7eSAndroid Build Coastguard Worker * Due to its inherently racy implementation, futex_requeue() is deprecated in
168*49cdfc7eSAndroid Build Coastguard Worker * favor of futex_cmp_requeue().
169*49cdfc7eSAndroid Build Coastguard Worker */
170*49cdfc7eSAndroid Build Coastguard Worker static inline int
futex_requeue(enum futex_fn_type fntype,futex_t * uaddr,futex_t * uaddr2,int nr_wake,int nr_requeue,int opflags)171*49cdfc7eSAndroid Build Coastguard Worker futex_requeue(enum futex_fn_type fntype, futex_t *uaddr, futex_t *uaddr2,
172*49cdfc7eSAndroid Build Coastguard Worker int nr_wake, int nr_requeue, int opflags)
173*49cdfc7eSAndroid Build Coastguard Worker {
174*49cdfc7eSAndroid Build Coastguard Worker return futex_syscall(fntype, uaddr, FUTEX_REQUEUE, nr_wake,
175*49cdfc7eSAndroid Build Coastguard Worker (void *)((unsigned long)nr_requeue), uaddr2, 0,
176*49cdfc7eSAndroid Build Coastguard Worker opflags);
177*49cdfc7eSAndroid Build Coastguard Worker }
178*49cdfc7eSAndroid Build Coastguard Worker
179*49cdfc7eSAndroid Build Coastguard Worker /**
180*49cdfc7eSAndroid Build Coastguard Worker * futex_cmp_requeue() - requeue tasks from uaddr to uaddr2
181*49cdfc7eSAndroid Build Coastguard Worker * @nr_wake: wake up to this many tasks
182*49cdfc7eSAndroid Build Coastguard Worker * @nr_requeue: requeue up to this many tasks
183*49cdfc7eSAndroid Build Coastguard Worker */
184*49cdfc7eSAndroid Build Coastguard Worker static inline int
futex_cmp_requeue(enum futex_fn_type fntype,futex_t * uaddr,futex_t val,futex_t * uaddr2,int nr_wake,int nr_requeue,int opflags)185*49cdfc7eSAndroid Build Coastguard Worker futex_cmp_requeue(enum futex_fn_type fntype, futex_t *uaddr, futex_t val,
186*49cdfc7eSAndroid Build Coastguard Worker futex_t *uaddr2, int nr_wake, int nr_requeue, int opflags)
187*49cdfc7eSAndroid Build Coastguard Worker {
188*49cdfc7eSAndroid Build Coastguard Worker return futex_syscall(fntype, uaddr, FUTEX_CMP_REQUEUE, nr_wake,
189*49cdfc7eSAndroid Build Coastguard Worker (void *)((unsigned long)nr_requeue), uaddr2, val,
190*49cdfc7eSAndroid Build Coastguard Worker opflags);
191*49cdfc7eSAndroid Build Coastguard Worker }
192*49cdfc7eSAndroid Build Coastguard Worker
193*49cdfc7eSAndroid Build Coastguard Worker /**
194*49cdfc7eSAndroid Build Coastguard Worker * futex_wait_requeue_pi() - block on uaddr and prepare to requeue to uaddr2
195*49cdfc7eSAndroid Build Coastguard Worker * @uaddr: non-PI futex source
196*49cdfc7eSAndroid Build Coastguard Worker * @uaddr2: PI futex target
197*49cdfc7eSAndroid Build Coastguard Worker *
198*49cdfc7eSAndroid Build Coastguard Worker * This is the first half of the requeue_pi mechanism. It shall always be
199*49cdfc7eSAndroid Build Coastguard Worker * paired with futex_cmp_requeue_pi().
200*49cdfc7eSAndroid Build Coastguard Worker */
201*49cdfc7eSAndroid Build Coastguard Worker static inline int
futex_wait_requeue_pi(enum futex_fn_type fntype,futex_t * uaddr,futex_t val,futex_t * uaddr2,struct tst_ts * timeout,int opflags)202*49cdfc7eSAndroid Build Coastguard Worker futex_wait_requeue_pi(enum futex_fn_type fntype, futex_t *uaddr, futex_t val,
203*49cdfc7eSAndroid Build Coastguard Worker futex_t *uaddr2, struct tst_ts *timeout, int opflags)
204*49cdfc7eSAndroid Build Coastguard Worker {
205*49cdfc7eSAndroid Build Coastguard Worker return futex_syscall(fntype, uaddr, FUTEX_WAIT_REQUEUE_PI, val,
206*49cdfc7eSAndroid Build Coastguard Worker tst_ts_get(timeout), uaddr2, 0, opflags);
207*49cdfc7eSAndroid Build Coastguard Worker }
208*49cdfc7eSAndroid Build Coastguard Worker
209*49cdfc7eSAndroid Build Coastguard Worker /**
210*49cdfc7eSAndroid Build Coastguard Worker * futex_cmp_requeue_pi() - requeue tasks from uaddr to uaddr2 (PI aware)
211*49cdfc7eSAndroid Build Coastguard Worker * @uaddr: non-PI futex source
212*49cdfc7eSAndroid Build Coastguard Worker * @uaddr2: PI futex target
213*49cdfc7eSAndroid Build Coastguard Worker * @nr_wake: wake up to this many tasks
214*49cdfc7eSAndroid Build Coastguard Worker * @nr_requeue: requeue up to this many tasks
215*49cdfc7eSAndroid Build Coastguard Worker */
216*49cdfc7eSAndroid Build Coastguard Worker static inline int
futex_cmp_requeue_pi(enum futex_fn_type fntype,futex_t * uaddr,futex_t val,futex_t * uaddr2,int nr_wake,int nr_requeue,int opflags)217*49cdfc7eSAndroid Build Coastguard Worker futex_cmp_requeue_pi(enum futex_fn_type fntype, futex_t *uaddr, futex_t val,
218*49cdfc7eSAndroid Build Coastguard Worker futex_t *uaddr2, int nr_wake, int nr_requeue, int opflags)
219*49cdfc7eSAndroid Build Coastguard Worker {
220*49cdfc7eSAndroid Build Coastguard Worker return futex_syscall(fntype, uaddr, FUTEX_CMP_REQUEUE_PI, nr_wake,
221*49cdfc7eSAndroid Build Coastguard Worker (void *)((unsigned long)nr_requeue), uaddr2, val,
222*49cdfc7eSAndroid Build Coastguard Worker opflags);
223*49cdfc7eSAndroid Build Coastguard Worker }
224*49cdfc7eSAndroid Build Coastguard Worker
225*49cdfc7eSAndroid Build Coastguard Worker /**
226*49cdfc7eSAndroid Build Coastguard Worker * futex_cmpxchg() - atomic compare and exchange
227*49cdfc7eSAndroid Build Coastguard Worker * @uaddr: The address of the futex to be modified
228*49cdfc7eSAndroid Build Coastguard Worker * @oldval: The expected value of the futex
229*49cdfc7eSAndroid Build Coastguard Worker * @newval: The new value to try and assign the futex
230*49cdfc7eSAndroid Build Coastguard Worker *
231*49cdfc7eSAndroid Build Coastguard Worker * Implement cmpxchg using gcc atomic builtins.
232*49cdfc7eSAndroid Build Coastguard Worker * http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Atomic-Builtins.html
233*49cdfc7eSAndroid Build Coastguard Worker *
234*49cdfc7eSAndroid Build Coastguard Worker * Return the old futex value.
235*49cdfc7eSAndroid Build Coastguard Worker */
236*49cdfc7eSAndroid Build Coastguard Worker static inline u_int32_t
futex_cmpxchg(futex_t * uaddr,u_int32_t oldval,u_int32_t newval)237*49cdfc7eSAndroid Build Coastguard Worker futex_cmpxchg(futex_t *uaddr, u_int32_t oldval, u_int32_t newval)
238*49cdfc7eSAndroid Build Coastguard Worker {
239*49cdfc7eSAndroid Build Coastguard Worker return __sync_val_compare_and_swap(uaddr, oldval, newval);
240*49cdfc7eSAndroid Build Coastguard Worker }
241*49cdfc7eSAndroid Build Coastguard Worker
242*49cdfc7eSAndroid Build Coastguard Worker /**
243*49cdfc7eSAndroid Build Coastguard Worker * futex_dec() - atomic decrement of the futex value
244*49cdfc7eSAndroid Build Coastguard Worker * @uaddr: The address of the futex to be modified
245*49cdfc7eSAndroid Build Coastguard Worker *
246*49cdfc7eSAndroid Build Coastguard Worker * Return the new futex value.
247*49cdfc7eSAndroid Build Coastguard Worker */
248*49cdfc7eSAndroid Build Coastguard Worker static inline u_int32_t
futex_dec(futex_t * uaddr)249*49cdfc7eSAndroid Build Coastguard Worker futex_dec(futex_t *uaddr)
250*49cdfc7eSAndroid Build Coastguard Worker {
251*49cdfc7eSAndroid Build Coastguard Worker return __sync_sub_and_fetch(uaddr, 1);
252*49cdfc7eSAndroid Build Coastguard Worker }
253*49cdfc7eSAndroid Build Coastguard Worker
254*49cdfc7eSAndroid Build Coastguard Worker /**
255*49cdfc7eSAndroid Build Coastguard Worker * futex_inc() - atomic increment of the futex value
256*49cdfc7eSAndroid Build Coastguard Worker * @uaddr: the address of the futex to be modified
257*49cdfc7eSAndroid Build Coastguard Worker *
258*49cdfc7eSAndroid Build Coastguard Worker * Return the new futex value.
259*49cdfc7eSAndroid Build Coastguard Worker */
260*49cdfc7eSAndroid Build Coastguard Worker static inline u_int32_t
futex_inc(futex_t * uaddr)261*49cdfc7eSAndroid Build Coastguard Worker futex_inc(futex_t *uaddr)
262*49cdfc7eSAndroid Build Coastguard Worker {
263*49cdfc7eSAndroid Build Coastguard Worker return __sync_add_and_fetch(uaddr, 1);
264*49cdfc7eSAndroid Build Coastguard Worker }
265*49cdfc7eSAndroid Build Coastguard Worker
266*49cdfc7eSAndroid Build Coastguard Worker /**
267*49cdfc7eSAndroid Build Coastguard Worker * futex_set() - atomic decrement of the futex value
268*49cdfc7eSAndroid Build Coastguard Worker * @uaddr: the address of the futex to be modified
269*49cdfc7eSAndroid Build Coastguard Worker * @newval: New value for the atomic_t
270*49cdfc7eSAndroid Build Coastguard Worker *
271*49cdfc7eSAndroid Build Coastguard Worker * Return the new futex value.
272*49cdfc7eSAndroid Build Coastguard Worker */
273*49cdfc7eSAndroid Build Coastguard Worker static inline u_int32_t
futex_set(futex_t * uaddr,u_int32_t newval)274*49cdfc7eSAndroid Build Coastguard Worker futex_set(futex_t *uaddr, u_int32_t newval)
275*49cdfc7eSAndroid Build Coastguard Worker {
276*49cdfc7eSAndroid Build Coastguard Worker *uaddr = newval;
277*49cdfc7eSAndroid Build Coastguard Worker return newval;
278*49cdfc7eSAndroid Build Coastguard Worker }
279*49cdfc7eSAndroid Build Coastguard Worker
280*49cdfc7eSAndroid Build Coastguard Worker /**
281*49cdfc7eSAndroid Build Coastguard Worker * futex_waked_someone() - ECHCK func for TST_RETRY_FUNC
282*49cdfc7eSAndroid Build Coastguard Worker * @ret: return value of futex_wake
283*49cdfc7eSAndroid Build Coastguard Worker *
284*49cdfc7eSAndroid Build Coastguard Worker * Return value drives TST_RETRY_FUNC macro.
285*49cdfc7eSAndroid Build Coastguard Worker */
286*49cdfc7eSAndroid Build Coastguard Worker static inline int
futex_waked_someone(int ret)287*49cdfc7eSAndroid Build Coastguard Worker futex_waked_someone(int ret)
288*49cdfc7eSAndroid Build Coastguard Worker {
289*49cdfc7eSAndroid Build Coastguard Worker if (ret < 0)
290*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TERRNO, "futex_wake returned: %d", ret);
291*49cdfc7eSAndroid Build Coastguard Worker
292*49cdfc7eSAndroid Build Coastguard Worker return ret;
293*49cdfc7eSAndroid Build Coastguard Worker }
294*49cdfc7eSAndroid Build Coastguard Worker
295*49cdfc7eSAndroid Build Coastguard Worker #endif /* _FUTEXTEST_H */
296