1 /* Low-level locking access to futex facilities. Linux/i386 version.
2 Copyright (C) 2014-2015 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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 <http://www.gnu.org/licenses/>. */
19 #ifndef _LOWLEVELLOCK_FUTEX_H
20 #define _LOWLEVELLOCK_FUTEX_H 1
24 #define FUTEX_CMP_REQUEUE 4
25 #define FUTEX_WAKE_OP 5
26 #define FUTEX_LOCK_PI 6
27 #define FUTEX_UNLOCK_PI 7
28 #define FUTEX_TRYLOCK_PI 8
29 #define FUTEX_WAIT_BITSET 9
30 #define FUTEX_WAKE_BITSET 10
31 #define FUTEX_WAIT_REQUEUE_PI 11
32 #define FUTEX_CMP_REQUEUE_PI 12
33 #define FUTEX_PRIVATE_FLAG 128
34 #define FUTEX_CLOCK_REALTIME 256
36 #define FUTEX_BITSET_MATCH_ANY 0xffffffff
38 #define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1)
40 /* Values for 'private' parameter of locking macros. Yes, the
41 definition seems to be backwards. But it is not. The bit will be
42 reversed before passing to the system call. */
44 #define LLL_SHARED FUTEX_PRIVATE_FLAG
47 #if IS_IN (libc) || IS_IN (rtld)
48 /* In libc.so or ld.so all futexes are private. */
49 # ifdef __ASSUME_PRIVATE_FUTEX
50 # define __lll_private_flag(fl, private) \
51 ((fl) | FUTEX_PRIVATE_FLAG)
53 # define __lll_private_flag(fl, private) \
54 ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
57 # ifdef __ASSUME_PRIVATE_FUTEX
58 # define __lll_private_flag(fl, private) \
59 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
61 # define __lll_private_flag(fl, private) \
62 (__builtin_constant_p (private) \
64 ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
66 : ({ unsigned int __fl = ((private) ^ FUTEX_PRIVATE_FLAG); \
67 asm ("andl %%gs:%P1, %0" : "+r" (__fl) \
68 : "i" (offsetof (struct pthread, header.private_futex))); \
76 /* To avoid naming conflicts with lowlevellock.h, use a different prefix
79 # define LLLF_EBX_LOAD "xchgl %2, %%ebx\n"
80 # define LLLF_EBX_REG "D"
82 # define LLLF_EBX_LOAD
83 # define LLLF_EBX_REG "b"
86 #ifdef I386_USE_SYSENTER
88 # define LLLF_ENTER_KERNEL "call *%%gs:%P6\n\t"
90 # define LLLF_ENTER_KERNEL "call *_dl_sysinfo\n\t"
93 # define LLLF_ENTER_KERNEL "int $0x80\n\t"
97 #define lll_futex_wait(futex, val, private) \
98 lll_futex_timed_wait (futex, val, NULL, private)
101 #define lll_futex_timed_wait(futex, val, timeout, private) \
104 register __typeof (val) _val asm ("edx") = (val); \
105 __asm __volatile (LLLF_EBX_LOAD \
109 : "0" (SYS_futex), LLLF_EBX_REG (futex), "S" (timeout), \
110 "c" (__lll_private_flag (FUTEX_WAIT, private)), \
111 "d" (_val), "i" (offsetof (tcbhead_t, sysinfo)) \
117 #define lll_futex_wake(futex, nr, private) \
120 register __typeof (nr) _nr asm ("edx") = (nr); \
121 LIBC_PROBE (lll_futex_wake, 3, futex, nr, private); \
122 __asm __volatile (LLLF_EBX_LOAD \
126 : "0" (SYS_futex), LLLF_EBX_REG (futex), \
127 "c" (__lll_private_flag (FUTEX_WAKE, private)), \
129 "i" (0) /* phony, to align next arg's number */, \
130 "i" (offsetof (tcbhead_t, sysinfo))); \
135 #endif /* !__ASSEMBLER__ */
137 #endif /* lowlevellock-futex.h */