]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/nptl/lowlevellock.h
6f017afdd5c17a046e4b9c254fdbffb985068a65
[thirdparty/glibc.git] / sysdeps / nptl / lowlevellock.h
1 /* Low-level lock implementation. Generic futex-based version.
2 Copyright (C) 2005-2019 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 <http://www.gnu.org/licenses/>. */
18
19 #ifndef _LOWLEVELLOCK_H
20 #define _LOWLEVELLOCK_H 1
21
22 #include <atomic.h>
23 #include <lowlevellock-futex.h>
24
25 /* Low-level locks use a combination of atomic operations (to acquire and
26 release lock ownership) and futex operations (to block until the state
27 of a lock changes). A lock can be in one of three states:
28 0: not acquired,
29 1: acquired with no waiters; no other threads are blocked or about to block
30 for changes to the lock state,
31 >1: acquired, possibly with waiters; there may be other threads blocked or
32 about to block for changes to the lock state.
33
34 We expect that the common case is an uncontended lock, so we just need
35 to transition the lock between states 0 and 1; releasing the lock does
36 not need to wake any other blocked threads. If the lock is contended
37 and a thread decides to block using a futex operation, then this thread
38 needs to first change the state to >1; if this state is observed during
39 lock release, the releasing thread will wake one of the potentially
40 blocked threads.
41
42 Much of this code takes a 'private' parameter. This may be:
43 LLL_PRIVATE: lock only shared within a process
44 LLL_SHARED: lock may be shared across processes.
45
46 Condition variables contain an optimization for broadcasts that requeues
47 waiting threads on a lock's futex. Therefore, there is a special
48 variant of the locks (whose name contains "cond") that makes sure to
49 always set the lock state to >1 and not just 1.
50
51 Robust locks set the lock to the id of the owner. This allows detection
52 of the case where the owner exits without releasing the lock. Flags are
53 OR'd with the owner id to record additional information about lock state.
54 Therefore the states of robust locks are:
55 0: not acquired
56 id: acquired (by user identified by id & FUTEX_TID_MASK)
57
58 The following flags may be set in the robust lock value:
59 FUTEX_WAITERS - possibly has waiters
60 FUTEX_OWNER_DIED - owning user has exited without releasing the futex. */
61
62
63 /* If LOCK is 0 (not acquired), set to 1 (acquired with no waiters) and return
64 0. Otherwise leave lock unchanged and return non-zero to indicate that the
65 lock was not acquired. */
66 #define lll_trylock(lock) \
67 __glibc_unlikely (atomic_compare_and_exchange_bool_acq (&(lock), 1, 0))
68
69 /* If LOCK is 0 (not acquired), set to 2 (acquired, possibly with waiters) and
70 return 0. Otherwise leave lock unchanged and return non-zero to indicate
71 that the lock was not acquired. */
72 #define lll_cond_trylock(lock) \
73 __glibc_unlikely (atomic_compare_and_exchange_bool_acq (&(lock), 2, 0))
74
75 extern void __lll_lock_wait_private (int *futex) attribute_hidden;
76 extern void __lll_lock_wait (int *futex, int private) attribute_hidden;
77
78 /* This is an expression rather than a statement even though its value is
79 void, so that it can be used in a comma expression or as an expression
80 that's cast to void. */
81 /* The inner conditional compiles to a call to __lll_lock_wait_private if
82 private is known at compile time to be LLL_PRIVATE, and to a call to
83 __lll_lock_wait otherwise. */
84 /* If FUTEX is 0 (not acquired), set to 1 (acquired with no waiters) and
85 return. Otherwise, ensure that it is >1 (acquired, possibly with waiters)
86 and then block until we acquire the lock, at which point FUTEX will still be
87 >1. The lock is always acquired on return. */
88 #define __lll_lock(futex, private) \
89 ((void) \
90 ({ \
91 int *__futex = (futex); \
92 if (__glibc_unlikely \
93 (atomic_compare_and_exchange_bool_acq (__futex, 1, 0))) \
94 { \
95 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
96 __lll_lock_wait_private (__futex); \
97 else \
98 __lll_lock_wait (__futex, private); \
99 } \
100 }))
101 #define lll_lock(futex, private) \
102 __lll_lock (&(futex), private)
103
104
105 /* This is an expression rather than a statement even though its value is
106 void, so that it can be used in a comma expression or as an expression
107 that's cast to void. */
108 /* Unconditionally set FUTEX to 2 (acquired, possibly with waiters). If FUTEX
109 was 0 (not acquired) then return. Otherwise, block until the lock is
110 acquired, at which point FUTEX is 2 (acquired, possibly with waiters). The
111 lock is always acquired on return. */
112 #define __lll_cond_lock(futex, private) \
113 ((void) \
114 ({ \
115 int *__futex = (futex); \
116 if (__glibc_unlikely (atomic_exchange_acq (__futex, 2) != 0)) \
117 __lll_lock_wait (__futex, private); \
118 }))
119 #define lll_cond_lock(futex, private) __lll_cond_lock (&(futex), private)
120
121
122 extern int __lll_timedlock_wait (int *futex, const struct timespec *,
123 int private) attribute_hidden;
124
125
126 /* As __lll_lock, but with a timeout. If the timeout occurs then return
127 ETIMEDOUT. If ABSTIME is invalid, return EINVAL. */
128 #define __lll_timedlock(futex, abstime, private) \
129 ({ \
130 int *__futex = (futex); \
131 int __val = 0; \
132 \
133 if (__glibc_unlikely \
134 (atomic_compare_and_exchange_bool_acq (__futex, 1, 0))) \
135 __val = __lll_timedlock_wait (__futex, abstime, private); \
136 __val; \
137 })
138 #define lll_timedlock(futex, abstime, private) \
139 __lll_timedlock (&(futex), abstime, private)
140
141
142 /* This is an expression rather than a statement even though its value is
143 void, so that it can be used in a comma expression or as an expression
144 that's cast to void. */
145 /* Unconditionally set FUTEX to 0 (not acquired), releasing the lock. If FUTEX
146 was >1 (acquired, possibly with waiters), then wake any waiters. The waiter
147 that acquires the lock will set FUTEX to >1.
148 Evaluate PRIVATE before releasing the lock so that we do not violate the
149 mutex destruction requirements. Specifically, we need to ensure that
150 another thread can destroy the mutex (and reuse its memory) once it
151 acquires the lock and when there will be no further lock acquisitions;
152 thus, we must not access the lock after releasing it, or those accesses
153 could be concurrent with mutex destruction or reuse of the memory. */
154 #define __lll_unlock(futex, private) \
155 ((void) \
156 ({ \
157 int *__futex = (futex); \
158 int __private = (private); \
159 int __oldval = atomic_exchange_rel (__futex, 0); \
160 if (__glibc_unlikely (__oldval > 1)) \
161 lll_futex_wake (__futex, 1, __private); \
162 }))
163 #define lll_unlock(futex, private) \
164 __lll_unlock (&(futex), private)
165
166
167 #define lll_islocked(futex) \
168 ((futex) != LLL_LOCK_INITIALIZER)
169
170
171 /* Our internal lock implementation is identical to the binary-compatible
172 mutex implementation. */
173
174 /* Initializers for lock. */
175 #define LLL_LOCK_INITIALIZER (0)
176 #define LLL_LOCK_INITIALIZER_LOCKED (1)
177
178 #endif /* lowlevellock.h */