Update copyright dates with scripts/update-copyrights.
[glibc.git] / sysdeps / unix / sysv / linux / lowlevellock-futex.h
blob59f6627bdb916bd7e92c6b0f488feb71516472a8
1 /* Low-level locking access to futex facilities. Linux version.
2 Copyright (C) 2005-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
22 #ifndef __ASSEMBLER__
23 #include <sysdep.h>
24 #include <tls.h>
25 #include <kernel-features.h>
26 #endif
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
44 #define FUTEX_BITSET_MATCH_ANY 0xffffffff
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
52 #ifndef __ASSEMBLER__
54 #if IS_IN (libc) || IS_IN (rtld)
55 /* In libc.so or ld.so all futexes are private. */
56 # ifdef __ASSUME_PRIVATE_FUTEX
57 # define __lll_private_flag(fl, private) \
58 ((fl) | FUTEX_PRIVATE_FLAG)
59 # else
60 # define __lll_private_flag(fl, private) \
61 ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
62 # endif
63 #else
64 # ifdef __ASSUME_PRIVATE_FUTEX
65 # define __lll_private_flag(fl, private) \
66 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
67 # else
68 # define __lll_private_flag(fl, private) \
69 (__builtin_constant_p (private) \
70 ? ((private) == 0 \
71 ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
72 : (fl)) \
73 : ((fl) | (((private) ^ FUTEX_PRIVATE_FLAG) \
74 & THREAD_GETMEM (THREAD_SELF, header.private_futex))))
75 # endif
76 #endif
78 #define lll_futex_syscall(nargs, futexp, op, ...) \
79 ({ \
80 INTERNAL_SYSCALL_DECL (__err); \
81 long int __ret = INTERNAL_SYSCALL (futex, __err, nargs, futexp, op, \
82 __VA_ARGS__); \
83 (__glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (__ret, __err)) \
84 ? -INTERNAL_SYSCALL_ERRNO (__ret, __err) : 0); \
87 #define lll_futex_wait(futexp, val, private) \
88 lll_futex_timed_wait (futexp, val, NULL, private)
90 #define lll_futex_timed_wait(futexp, val, timeout, private) \
91 lll_futex_syscall (4, futexp, \
92 __lll_private_flag (FUTEX_WAIT, private), \
93 val, timeout)
95 #define lll_futex_timed_wait_bitset(futexp, val, timeout, clockbit, private) \
96 lll_futex_syscall (6, futexp, \
97 __lll_private_flag (FUTEX_WAIT_BITSET | (clockbit), \
98 private), \
99 val, timeout, NULL /* Unused. */, \
100 FUTEX_BITSET_MATCH_ANY)
102 #define lll_futex_wake(futexp, nr, private) \
103 lll_futex_syscall (4, futexp, \
104 __lll_private_flag (FUTEX_WAKE, private), nr, 0)
106 /* Returns non-zero if error happened, zero if success. */
107 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val, private) \
108 lll_futex_syscall (6, futexp, \
109 __lll_private_flag (FUTEX_CMP_REQUEUE, private), \
110 nr_wake, nr_move, mutex, val)
112 /* Returns non-zero if error happened, zero if success. */
113 #define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) \
114 lll_futex_syscall (6, futexp, \
115 __lll_private_flag (FUTEX_WAKE_OP, private), \
116 nr_wake, nr_wake2, futexp2, \
117 FUTEX_OP_CLEAR_WAKE_IF_GT_ONE)
119 /* Priority Inheritance support. */
120 #define lll_futex_wait_requeue_pi(futexp, val, mutex, private) \
121 lll_futex_timed_wait_requeue_pi (futexp, val, NULL, 0, mutex, private)
123 #define lll_futex_timed_wait_requeue_pi(futexp, val, timeout, clockbit, \
124 mutex, private) \
125 lll_futex_syscall (5, futexp, \
126 __lll_private_flag (FUTEX_WAIT_REQUEUE_PI \
127 | (clockbit), private), \
128 val, timeout, mutex)
131 #define lll_futex_cmp_requeue_pi(futexp, nr_wake, nr_move, mutex, \
132 val, private) \
133 lll_futex_syscall (6, futexp, \
134 __lll_private_flag (FUTEX_CMP_REQUEUE_PI, \
135 private), \
136 nr_wake, nr_move, mutex, val)
138 #endif /* !__ASSEMBLER__ */
140 #endif /* lowlevellock-futex.h */