Debian Codegraph @

storage/innobase/include/lock0guards.h

raw
1 /*****************************************************************************
2
3 Copyright (c) 2020, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 #ifndef lock0guards_h
29 #define lock0guards_h
30
31 #include "lock0lock.h"
32 #include "ut0class_life_cycle.h"
33
34 namespace locksys {
35 /**
36 A RAII helper which latches global_latch in exclusive mode during constructor,
37 and unlatches it during destruction, preventing any other threads from activity
38 within lock_sys for it's entire scope.
39 */
40 class Global_exclusive_latch_guard : private ut::Non_copyable {
41 public:
42 Global_exclusive_latch_guard(ut::Location location);
43 ~Global_exclusive_latch_guard();
44 };
45
46 /**
47 A RAII helper which tries to exclusively latch the global_lach in constructor
48 and unlatches it, if needed, during destruction, preventing any other threads
49 from activity within lock_sys for it's entire scope, if owns_lock().
50 */
51 class Global_exclusive_try_latch : private ut::Non_copyable {
52 public:
53 Global_exclusive_try_latch(ut::Location location);
54 ~Global_exclusive_try_latch();
55 /** Checks if succeeded to latch the global_latch during construction.
56 @return true iff the current thread owns (through this instance) the exclusive
57 global lock_sys latch */
58 bool owns_lock() const noexcept { return m_owns_exclusive_global_latch; }
59
60 private:
61 /** Did the constructor succeed to acquire exclusive global lock_sys latch? */
62 bool m_owns_exclusive_global_latch;
63 };
64
65 /**
66 A RAII helper which latches global_latch in shared mode during constructor,
67 and unlatches it during destruction, preventing any other thread from acquiring
68 exclusive latch. This should be used in combination Shard_naked_latch_guard,
69 preferably by simply using Shard_latch_guard which combines the two for you.
70 */
71 class Global_shared_latch_guard : private ut::Non_copyable {
72 public:
73 Global_shared_latch_guard(ut::Location location);
74 ~Global_shared_latch_guard();
75 /** Checks if there is a thread requesting the global_latch in exclusive mode
76 blocked by our thread.
77 @return true iff there is an x-latcher blocked by our s-latch. */
78 bool is_x_blocked_by_us();
79 };
80
81 /**
82 A RAII helper which latches the mutex protecting given shard during constructor,
83 and unlatches it during destruction.
84 You quite probably don't want to use this class, which only takes a shard's
85 latch, without acquiring global_latch - which gives no protection from threads
86 which latch only the global_latch exclusively to prevent any activity.
87 You should use it in combination with Global_shared_latch_guard, so that you
88 first obtain an s-latch on the global_latch, or simply use the Shard_latch_guard
89 class which already combines the two for you.
90 */
91 class Shard_naked_latch_guard : private ut::Non_copyable {
92 explicit Shard_naked_latch_guard(ut::Location location,
93 Lock_mutex &shard_mutex);
94
95 public:
96 explicit Shard_naked_latch_guard(ut::Location location,
97 const table_id_t &table_id);
98
99 explicit Shard_naked_latch_guard(ut::Location location,
100 const page_id_t &page_id);
101
102 explicit Shard_naked_latch_guard(ut::Location location,
103 hash_table_t *hash_table, uint64_t cell_id);
104
105 ~Shard_naked_latch_guard();
106
107 private:
108 /** The mutex protecting the shard requested in constructor */
109 Lock_mutex &m_shard_mutex;
110 };
111
112 /**
113 A RAII wrapper class which combines Global_shared_latch_guard and
114 Shard_naked_latch_guard to s-latch the global lock_sys latch and latch the mutex
115 protecting the specified shard for the duration of its scope.
116 The order of initialization is important: we have to take shared global latch
117 BEFORE we attempt to use hash function to compute correct shard and latch it. */
118 class Shard_latch_guard {
119 Global_shared_latch_guard m_global_shared_latch_guard;
120 Shard_naked_latch_guard m_shard_naked_latch_guard;
121
122 public:
123 explicit Shard_latch_guard(ut::Location location, const dict_table_t &table)
124 : m_global_shared_latch_guard{location},
125 m_shard_naked_latch_guard{location, table.id} {}
126
127 explicit Shard_latch_guard(ut::Location location, const page_id_t &page_id)
128 : m_global_shared_latch_guard{location},
129 m_shard_naked_latch_guard{location, page_id} {}
130 };
131
132 /**
133 A RAII helper which latches the mutexes protecting specified shards for the
134 duration of its scope.
135 It makes sure to take the latches in correct order and handles the case where
136 both pages are in the same shard correctly.
137 You quite probably don't want to use this class, which only takes a shard's
138 latch, without acquiring global_latch - which gives no protection from threads
139 which latch only the global_latch exclusively to prevent any activity.
140 You should use it in combination with Global_shared_latch_guard, so that you
141 first obtain an s-latch on the global_latch, or simply use the
142 Shard_latches_guard class which already combines the two for you.
143 */
144 class Shard_naked_latches_guard {
145 explicit Shard_naked_latches_guard(Lock_mutex &shard_mutex_a,
146 Lock_mutex &shard_mutex_b);
147
148 public:
149 explicit Shard_naked_latches_guard(const buf_block_t &block_a,
150 const buf_block_t &block_b);
151
152 ~Shard_naked_latches_guard();
153
154 private:
155 /** The "smallest" of the two shards' mutexes in the latching order */
156 Lock_mutex &m_shard_mutex_1;
157 /** The "largest" of the two shards' mutexes in the latching order */
158 Lock_mutex &m_shard_mutex_2;
159 /** The ordering on shard mutexes used to avoid deadlocks */
160 static constexpr std::less<Lock_mutex *> MUTEX_ORDER{};
161 };
162
163 /**
164 A RAII wrapper class which s-latches the global lock_sys shard, and mutexes
165 protecting specified shards for the duration of its scope.
166 It makes sure to take the latches in correct order and handles the case where
167 both pages are in the same shard correctly.
168 The order of initialization is important: we have to take shared global latch
169 BEFORE we attempt to use hash function to compute correct shard and latch it.
170 */
171 class Shard_latches_guard {
172 public:
173 explicit Shard_latches_guard(ut::Location location,
174 const buf_block_t &block_a,
175 const buf_block_t &block_b)
176 : m_global_shared_latch_guard{location},
177 m_shard_naked_latches_guard{block_a, block_b} {}
178
179 private:
180 Global_shared_latch_guard m_global_shared_latch_guard;
181 Shard_naked_latches_guard m_shard_naked_latches_guard;
182 };
183
184 } // namespace locksys
185
186 #endif /* lock0guards_h */