1/* Low-level locking access to futex facilities. Stub version.
2 Copyright (C) 2014-2020 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library. If not, see
17 <https://www.gnu.org/licenses/>. */
18
19#ifndef _LOWLEVELLOCK_FUTEX_H
20#define _LOWLEVELLOCK_FUTEX_H 1
21
22#ifndef __ASSEMBLER__
23# include <sysdep.h>
24# include <sysdep-cancel.h>
25# include <kernel-features.h>
26#endif
27
28#define FUTEX_WAIT 0
29#define FUTEX_WAKE 1
30#define FUTEX_REQUEUE 3
31#define FUTEX_CMP_REQUEUE 4
32#define FUTEX_WAKE_OP 5
33#define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1)
34#define FUTEX_LOCK_PI 6
35#define FUTEX_UNLOCK_PI 7
36#define FUTEX_TRYLOCK_PI 8
37#define FUTEX_WAIT_BITSET 9
38#define FUTEX_WAKE_BITSET 10
39#define FUTEX_WAIT_REQUEUE_PI 11
40#define FUTEX_CMP_REQUEUE_PI 12
41#define FUTEX_PRIVATE_FLAG 128
42#define FUTEX_CLOCK_REALTIME 256
43
44#define FUTEX_BITSET_MATCH_ANY 0xffffffff
45
46/* Values for 'private' parameter of locking macros. Yes, the
47 definition seems to be backwards. But it is not. The bit will be
48 reversed before passing to the system call. */
49#define LLL_PRIVATE 0
50#define LLL_SHARED FUTEX_PRIVATE_FLAG
51
52#ifndef __ASSEMBLER__
53
54# if IS_IN (libc) || IS_IN (rtld)
55/* In libc.so or ld.so all futexes are private. */
56# define __lll_private_flag(fl, private) \
57 ({ \
58 /* Prevent warnings in callers of this macro. */ \
59 int __lll_private_flag_priv __attribute__ ((unused)); \
60 __lll_private_flag_priv = (private); \
61 ((fl) | FUTEX_PRIVATE_FLAG); \
62 })
63# else
64# define __lll_private_flag(fl, private) \
65 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
66# endif
67
68# define lll_futex_syscall(nargs, futexp, op, ...) \
69 ({ \
70 INTERNAL_SYSCALL_DECL (__err); \
71 long int __ret = INTERNAL_SYSCALL (futex, __err, nargs, futexp, op, \
72 __VA_ARGS__); \
73 (__glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (__ret, __err)) \
74 ? -INTERNAL_SYSCALL_ERRNO (__ret, __err) : 0); \
75 })
76
77/* For most of these macros, the return value is never really used.
78 Nevertheless, the protocol is that each one returns a negated errno
79 code for failure or zero for success. (Note that the corresponding
80 Linux system calls can sometimes return positive values for success
81 cases too. We never use those values.) */
82
83
84/* Wait while *FUTEXP == VAL for an lll_futex_wake call on FUTEXP. */
85# define lll_futex_wait(futexp, val, private) \
86 lll_futex_timed_wait (futexp, val, NULL, private)
87
88# define lll_futex_timed_wait(futexp, val, timeout, private) \
89 lll_futex_syscall (4, futexp, \
90 __lll_private_flag (FUTEX_WAIT, private), \
91 val, timeout)
92
93/* Verify whether the supplied clockid is supported by
94 lll_futex_clock_wait_bitset. */
95# define lll_futex_supported_clockid(clockid) \
96 ((clockid) == CLOCK_REALTIME || (clockid) == CLOCK_MONOTONIC)
97
98/* The kernel currently only supports CLOCK_MONOTONIC or
99 CLOCK_REALTIME timeouts for FUTEX_WAIT_BITSET. We could attempt to
100 convert others here but currently do not. */
101# define lll_futex_clock_wait_bitset(futexp, val, clockid, timeout, private) \
102 ({ \
103 long int __ret; \
104 if (lll_futex_supported_clockid (clockid)) \
105 { \
106 const unsigned int clockbit = \
107 (clockid == CLOCK_REALTIME) ? FUTEX_CLOCK_REALTIME : 0; \
108 const int op = \
109 __lll_private_flag (FUTEX_WAIT_BITSET | clockbit, private); \
110 \
111 __ret = lll_futex_syscall (6, futexp, op, val, \
112 timeout, NULL /* Unused. */, \
113 FUTEX_BITSET_MATCH_ANY); \
114 } \
115 else \
116 __ret = -EINVAL; \
117 __ret; \
118 })
119
120/* Wake up up to NR waiters on FUTEXP. */
121# define lll_futex_wake(futexp, nr, private) \
122 lll_futex_syscall (4, futexp, \
123 __lll_private_flag (FUTEX_WAKE, private), nr, 0)
124
125/* Wake up up to NR_WAKE waiters on FUTEXP. Move up to NR_MOVE of the
126 rest from waiting on FUTEXP to waiting on MUTEX (a different futex).
127 Returns non-zero if error happened, zero if success. */
128# define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val, private) \
129 lll_futex_syscall (6, futexp, \
130 __lll_private_flag (FUTEX_CMP_REQUEUE, private), \
131 nr_wake, nr_move, mutex, val)
132
133/* Wake up up to NR_WAKE waiters on FUTEXP and NR_WAKE2 on FUTEXP2.
134 Returns non-zero if error happened, zero if success. */
135# define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) \
136 lll_futex_syscall (6, futexp, \
137 __lll_private_flag (FUTEX_WAKE_OP, private), \
138 nr_wake, nr_wake2, futexp2, \
139 FUTEX_OP_CLEAR_WAKE_IF_GT_ONE)
140
141
142/* Priority Inheritance support. */
143#define lll_futex_timed_lock_pi(futexp, abstime, private) \
144 lll_futex_syscall (4, futexp, \
145 __lll_private_flag (FUTEX_LOCK_PI, private), \
146 0, abstime)
147
148#define lll_futex_timed_unlock_pi(futexp, private) \
149 lll_futex_syscall (4, futexp, \
150 __lll_private_flag (FUTEX_UNLOCK_PI, private), \
151 0, 0)
152
153/* Like lll_futex_wait (FUTEXP, VAL, PRIVATE) but with the expectation
154 that lll_futex_cmp_requeue_pi (FUTEXP, _, _, MUTEX, _, PRIVATE) will
155 be used to do the wakeup. Confers priority-inheritance behavior on
156 the waiter. */
157# define lll_futex_wait_requeue_pi(futexp, val, mutex, private) \
158 lll_futex_timed_wait_requeue_pi (futexp, val, NULL, 0, mutex, private)
159
160/* Like lll_futex_wait_requeue_pi, but with a timeout. */
161# define lll_futex_timed_wait_requeue_pi(futexp, val, timeout, clockbit, \
162 mutex, private) \
163 lll_futex_syscall (5, futexp, \
164 __lll_private_flag (FUTEX_WAIT_REQUEUE_PI \
165 | (clockbit), private), \
166 val, timeout, mutex)
167
168/* Like lll_futex_requeue, but pairs with lll_futex_wait_requeue_pi
169 and inherits priority from the waiter. */
170# define lll_futex_cmp_requeue_pi(futexp, nr_wake, nr_move, mutex, \
171 val, private) \
172 lll_futex_syscall (6, futexp, \
173 __lll_private_flag (FUTEX_CMP_REQUEUE_PI, \
174 private), \
175 nr_wake, nr_move, mutex, val)
176
177/* Like lll_futex_wait, but acting as a cancellable entrypoint. */
178# define lll_futex_wait_cancel(futexp, val, private) \
179 ({ \
180 int __oldtype = CANCEL_ASYNC (); \
181 long int __err = lll_futex_wait (futexp, val, LLL_SHARED); \
182 CANCEL_RESET (__oldtype); \
183 __err; \
184 })
185
186/* Like lll_futex_timed_wait, but acting as a cancellable entrypoint. */
187# define lll_futex_timed_wait_cancel(futexp, val, timeout, private) \
188 ({ \
189 int __oldtype = CANCEL_ASYNC (); \
190 long int __err = lll_futex_timed_wait (futexp, val, timeout, private); \
191 CANCEL_RESET (__oldtype); \
192 __err; \
193 })
194
195#endif /* !__ASSEMBLER__ */
196
197#endif /* lowlevellock-futex.h */
198