Debian Codegraph @

storage/innobase/include/ut0mutex.ic

raw
1 /*****************************************************************************
2
3 Copyright (c) 2013, 2026, Oracle and/or its affiliates.
4
5 Portions of this file contain modifications contributed and copyrighted by
6 Google, Inc. Those modifications are gratefully acknowledged and are described
7 briefly in the InnoDB documentation. The contributions by Google are
8 incorporated with their permission, and subject to the conditions contained in
9 the file COPYING.Google.
10
11 This program is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License, version 2.0, as published by the
13 Free Software Foundation.
14
15 This program is designed to work with certain software (including
16 but not limited to OpenSSL) that is licensed under separate terms,
17 as designated in a particular file or component or in included license
18 documentation. The authors of MySQL hereby grant you an additional
19 permission to link the program and your derivative works with the
20 separately licensed software that they have either included with
21 the program or referenced in the documentation.
22
23 This program is distributed in the hope that it will be useful, but WITHOUT
24 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
25 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
26 for more details.
27
28 You should have received a copy of the GNU General Public License along with
29 this program; if not, write to the Free Software Foundation, Inc.,
30 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31
32 *****************************************************************************/
33
34 /** @file include/ut0mutex.ic
35 Mutex implementation include file
36
37 Created 2012/08/21 Sunny Bains
38 *******************************************************/
39
40 #include "sync0arr.h"
41 #include "sync0debug.h"
42
43 /**
44 Wait in the sync array.
45 @return true if the mutex acquisition was successful. */
46
47 template <template <typename> class Policy>
48 bool TTASEventMutex<Policy>::wait(const char *filename, uint32_t line,
49 uint32_t spin) UNIV_NOTHROW {
50 sync_cell_t *cell;
51 sync_array_t *sync_arr;
52
53 /* We shall prove that in C++14 as defined by the draft N4140 following holds:
54
55 Theorem 1. If thread X decides to call os_event_wait(), and each thread which
56 calls enter() eventually calls exit(), then X will eventually be woken up.
57
58 Proof: By contradiction.
59 We assume the contrary, that thread X got to endless sleep, so the following
60 operations are in `sequenced-before` relation in X:
61
62 # inside sync_array_get_and_reserve_cell(..) => sync_array_reserve_cell(..):
63 X1 cell->signal_count = os_event_reset(..)
64 # inside set_waiters():
65 X2 m_waiters.store(true)
66 # inside try_lock():
67 X3 unsuccessful m_lock_word.compare_exchange_strong(false,true)
68 # inside sync_array_wait_event(..):
69 X4 os_event_wait_low(...,cell->signal_count)
70
71 -----
72 Lemma 1. All os_event_set()s `happen-before` X1.
73 Proof of Lemma 1: By contradiction.
74 The functions os_event_reset(), os_event_set() and os_event_wait_low() use
75 internally the same mutex, so there must be a single objective order in which
76 calls to them `synchronize-with` each other, and thus for each two such calls
77 exactly one has to `happens-before` the other.
78
79 If there is at least one call to os_event_set() which `happens-after` X1, then
80 we can show that X will not sleep indefinitely, as either:
81 a) os_event_set() `happens-before` X4, in which case it increments the event's
82 signal_count, and os_event_wait_low() will notice that event's signal_count
83 doesn't match the one from X1 and will not even start the sleep,
84 b) X4 `happens-before` the os_event_set() in which case by the specification
85 of os_event_set() it will wake up thread X.
86 Contradiction with assumption that thread X goes to endless sleep ends the
87 proof of Lemma 1.
88 -----
89
90 In what follows we will often refer to following rule from the standard:
91
92 29.3.3. There shall be a single total order S on all memory_order_seq_cst
93 operations, consistent with the `happens before` order and modification orders
94 for all affected locations such that each memory_order_seq_cst operation B
95 that loads a value from an atomic object M observes one of the following
96 values:
97 (3.1) the result of the last modification A of M that precedes B in S, if it
98 exists, or
99 (3.2) if A exists, the result of some modification of M that is not
100 memory_order_seq_cst and that does not happen before A, or
101 (3.3) if A does not exist, the result of some modification of M that is not
102 memory_order_seq_cst.
103
104 IMPORTANT: all modifications of `m_lock_word` and `m_waiters` in the current
105 implementation use `memory_order_seq_cst`, so thankfully we can ignore cases
106 (3.2) and (3.3) from considerations.
107 IMPORTANT: In C++14 a failed compare_exchange_strong is considered a `load`,
108 NOT a `read-modify-write`.
109
110 X4 is only executed if X3 loaded m_lock_word==true, and according to 29.3.3.1
111 this `true` must be a result of most recent modification of m_lock_word in S.
112
113 We only write `true` to m_lock_word using compare_exchange_strong in enter(),
114 so let Y be the last thread which performed this operation in try_lock():
115
116 Y0 successful m_lock_word.compare_exchange_strong(false,true)
117
118 before X3 in S. We've assumed Y must eventually call exit(), which involves:
119
120 Y1 m_lock_word.store(false)
121 Y2 if (m_waiters.load()) {
122 Y3 m_waiters.store(false)
123 Y4 os_event_set(..) }
124
125 We will now show, that X2 is before Y2 in S.
126
127 IMPORTANT: this is not a trivial thing: even though X3 apparently has not seen
128 results of Y1 it does not follow immediately that X3 `happens-before` Y1. The
129 C++14 standard deliberately doesn't make it easy to deduce `happens-before`
130 from *absence* of evidence. We will only show that X3 is before Y1 in S, and
131 leverage that to show that X2 is before Y2 in S, which among other things,
132 requires X2, X3, Y1 and Y2 to use memory_order_seq_cst to be part of S at all.
133
134 As Y0 is `sequenced-before` Y1, then Y0 also `happens-before` Y1 and thus
135 Y1 must be after Y0 in S (29.3.3). As Y0 is the last modification of
136 m_lock_word before X3 in S, it means that Y1 must be also after X3 in S.
137
138 Y1 is `sequenced-before` Y2, thus Y1 `happens-before` Y2, thus Y1 is before
139 Y2 in S.
140 X2 is `sequenced-before` X3, thus X2 `happens-before` X3, thus X2 is before
141 X3 in S.
142
143 Combining all these, we get that X2 is before Y2 in S.
144
145 Now, we will show that Y2 can not read `true` nor `false` as both lead to
146 contradiction.
147
148 If Y2 loads `true`, then Y will proceed to call os_event_set() during Y4.
149 By Lemma 1 Y4 `happens-before` X1. But, Y2 is `sequenced-before` Y4,
150 and X1 is `sequenced-before` X2, which together would give that Y2
151 `happens-before` X2 which contradicts the rule that S is consistent with
152 `happens-before`, and we've already established that X2 is before Y2 in S.
153
154 So, it must be that Y2 loads `false`. There is only one place in code which
155 writes `false` to this variable: during exit(). By 1.10.19 Y2 can not read
156 value stored by itself in Y3 which `happens-after`, so this `false` must be
157 stored by some other thread, Z, which was executing:
158
159 Z3 m_waiters.store(false)
160 Z4 os_event_set(..)
161
162 By Lemma 1, Z4 `happens-before` X1, and since Z3 is `sequenced-before` Z4, and
163 X1 is `sequenced-before` X2, we get Z3 `happens-before` X2.
164 Thus, we get, that in S the order is: Z3, X2, Y2.
165
166 We will now use 29.3.3.1 rule again for Y2=B, X2=A, M=m_waiters:
167 Y2 must read he result of the last modification of m_waiters that
168 precedes Y2 in S. This can not be Z3, because it is not the last modification
169 before Y2, as we know X2 must appear somewhere between Z3 and Y2 in S.
170
171 So, Y2 can not load `false` neither. Contradiction ends the proof.
172 */
173
174 sync_arr = sync_array_get_and_reserve_cell(
175 this,
176 (m_policy.get_id() == LATCH_ID_BUF_BLOCK_MUTEX ||
177 m_policy.get_id() == LATCH_ID_BUF_POOL_ZIP)
178 ? SYNC_BUF_BLOCK
179 : SYNC_MUTEX,
180 {filename, line}, &cell);
181
182 /* The memory order of the array reservation and
183 the change in the waiters field is important: when
184 we suspend a thread, we first reserve the cell and
185 then set waiters field to 1. When threads are released
186 in mutex_exit, the waiters field is first set to zero
187 and then the event is set to the signaled state. */
188
189 set_waiters();
190
191 /* Try to reserve still a few times. */
192
193 for (uint32_t i = 0; i < spin; ++i) {
194 if (try_lock()) {
195 sync_array_free_cell(sync_arr, cell);
196
197 /* Note that in this case we leave
198 the waiters field set to 1. We cannot
199 reset it to zero, as we do not know if
200 there are other waiters. */
201
202 return (true);
203 }
204 }
205
206 /* Now we know that there has been some thread
207 holding the mutex after the change in the wait
208 array and the waiters field was made. Now there
209 is no risk of infinite wait on the event. */
210
211 sync_array_wait_event(sync_arr, cell);
212
213 return (false);
214 }
215
216 /** Wakeup any waiting thread(s). */
217
218 template <template <typename> class Policy>
219 void TTASEventMutex<Policy>::signal() UNIV_NOTHROW {
220 clear_waiters();
221
222 /* The memory order of resetting the waiters field and
223 signaling the object is important. See Theorem 1 above. */
224 os_event_set(m_event);
225
226 sync_array_object_signalled();
227 }