MySQL 8.4.2
Source Code Documentation
lock0guards.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2020, 2024, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is designed to work with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation. The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have either included with
15the program or referenced in the documentation.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20for more details.
21
22You should have received a copy of the GNU General Public License along with
23this program; if not, write to the Free Software Foundation, Inc.,
2451 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
34namespace locksys {
35/**
36A RAII helper which latches global_latch in exclusive mode during constructor,
37and unlatches it during destruction, preventing any other threads from activity
38within lock_sys for it's entire scope.
39*/
41 public:
44};
45
46/**
47A RAII helper which tries to exclusively latch the global_lach in constructor
48and unlatches it, if needed, during destruction, preventing any other threads
49from activity within lock_sys for it's entire scope, if owns_lock().
50*/
52 public:
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? */
63};
64
65/**
66A RAII helper which latches global_latch in shared mode during constructor,
67and unlatches it during destruction, preventing any other thread from acquiring
68exclusive latch. This should be used in combination Shard_naked_latch_guard,
69preferably by simply using Shard_latch_guard which combines the two for you.
70*/
72 public:
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/**
82A RAII helper which latches the mutex protecting given shard during constructor,
83and unlatches it during destruction.
84You quite probably don't want to use this class, which only takes a shard's
85latch, without acquiring global_latch - which gives no protection from threads
86which latch only the global_latch exclusively to prevent any activity.
87You should use it in combination with Global_shared_latch_guard, so that you
88first obtain an s-latch on the global_latch, or simply use the Shard_latch_guard
89class which already combines the two for you.
90*/
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
103
104 private:
105 /** The mutex protecting the shard requested in constructor */
107};
108
109/**
110A RAII wrapper class which combines Global_shared_latch_guard and
111Shard_naked_latch_guard to s-latch the global lock_sys latch and latch the mutex
112protecting the specified shard for the duration of its scope.
113The order of initialization is important: we have to take shared global latch
114BEFORE we attempt to use hash function to compute correct shard and latch it. */
118
119 public:
121 : m_global_shared_latch_guard{location},
123
124 explicit Shard_latch_guard(ut::Location location, const page_id_t &page_id)
125 : m_global_shared_latch_guard{location},
126 m_shard_naked_latch_guard{location, page_id} {}
127};
128
129/**
130A RAII helper which latches the mutexes protecting specified shards for the
131duration of its scope.
132It makes sure to take the latches in correct order and handles the case where
133both pages are in the same shard correctly.
134You quite probably don't want to use this class, which only takes a shard's
135latch, without acquiring global_latch - which gives no protection from threads
136which latch only the global_latch exclusively to prevent any activity.
137You should use it in combination with Global_shared_latch_guard, so that you
138first obtain an s-latch on the global_latch, or simply use the
139Shard_latches_guard class which already combines the two for you.
140*/
142 explicit Shard_naked_latches_guard(Lock_mutex &shard_mutex_a,
143 Lock_mutex &shard_mutex_b);
144
145 public:
146 explicit Shard_naked_latches_guard(const buf_block_t &block_a,
147 const buf_block_t &block_b);
148
150
151 private:
152 /** The "smallest" of the two shards' mutexes in the latching order */
154 /** The "largest" of the two shards' mutexes in the latching order */
156 /** The ordering on shard mutexes used to avoid deadlocks */
157 static constexpr std::less<Lock_mutex *> MUTEX_ORDER{};
158};
159
160/**
161A RAII wrapper class which s-latches the global lock_sys shard, and mutexes
162protecting specified shards for the duration of its scope.
163It makes sure to take the latches in correct order and handles the case where
164both pages are in the same shard correctly.
165The order of initialization is important: we have to take shared global latch
166BEFORE we attempt to use hash function to compute correct shard and latch it.
167*/
169 public:
171 const buf_block_t &block_a,
172 const buf_block_t &block_b)
173 : m_global_shared_latch_guard{location},
174 m_shard_naked_latches_guard{block_a, block_b} {}
175
176 private:
179};
180
181} // namespace locksys
182
183#endif /* lock0guards_h */
A RAII helper which latches global_latch in exclusive mode during constructor, and unlatches it durin...
Definition: lock0guards.h:40
Global_exclusive_latch_guard(ut::Location location)
Definition: lock0guards.cc:38
~Global_exclusive_latch_guard()
Definition: lock0guards.cc:43
A RAII helper which tries to exclusively latch the global_lach in constructor and unlatches it,...
Definition: lock0guards.h:51
bool owns_lock() const noexcept
Checks if succeeded to latch the global_latch during construction.
Definition: lock0guards.h:58
bool m_owns_exclusive_global_latch
Did the constructor succeed to acquire exclusive global lock_sys latch?
Definition: lock0guards.h:62
~Global_exclusive_try_latch()
Definition: lock0guards.cc:54
Global_exclusive_try_latch(ut::Location location)
Definition: lock0guards.cc:49
A RAII helper which latches global_latch in shared mode during constructor, and unlatches it during d...
Definition: lock0guards.h:71
Global_shared_latch_guard(ut::Location location)
Definition: lock0guards.cc:86
bool is_x_blocked_by_us()
Checks if there is a thread requesting the global_latch in exclusive mode blocked by our thread.
Definition: lock0guards.cc:93
~Global_shared_latch_guard()
Definition: lock0guards.cc:90
A RAII wrapper class which combines Global_shared_latch_guard and Shard_naked_latch_guard to s-latch ...
Definition: lock0guards.h:115
Shard_latch_guard(ut::Location location, const page_id_t &page_id)
Definition: lock0guards.h:124
Shard_latch_guard(ut::Location location, const dict_table_t &table)
Definition: lock0guards.h:120
Global_shared_latch_guard m_global_shared_latch_guard
Definition: lock0guards.h:116
Shard_naked_latch_guard m_shard_naked_latch_guard
Definition: lock0guards.h:117
A RAII wrapper class which s-latches the global lock_sys shard, and mutexes protecting specified shar...
Definition: lock0guards.h:168
Shard_latches_guard(ut::Location location, const buf_block_t &block_a, const buf_block_t &block_b)
Definition: lock0guards.h:170
Global_shared_latch_guard m_global_shared_latch_guard
Definition: lock0guards.h:177
Shard_naked_latches_guard m_shard_naked_latches_guard
Definition: lock0guards.h:178
A RAII helper which latches the mutex protecting given shard during constructor, and unlatches it dur...
Definition: lock0guards.h:91
~Shard_naked_latch_guard()
Definition: lock0guards.cc:80
Lock_mutex & m_shard_mutex
The mutex protecting the shard requested in constructor.
Definition: lock0guards.h:106
Shard_naked_latch_guard(ut::Location location, Lock_mutex &shard_mutex)
Definition: lock0guards.cc:63
A RAII helper which latches the mutexes protecting specified shards for the duration of its scope.
Definition: lock0guards.h:141
~Shard_naked_latches_guard()
Definition: lock0guards.cc:116
static constexpr std::less< Lock_mutex * > MUTEX_ORDER
The ordering on shard mutexes used to avoid deadlocks.
Definition: lock0guards.h:157
Lock_mutex & m_shard_mutex_1
The "smallest" of the two shards' mutexes in the latching order.
Definition: lock0guards.h:153
Shard_naked_latches_guard(Lock_mutex &shard_mutex_a, Lock_mutex &shard_mutex_b)
Definition: lock0guards.cc:99
Lock_mutex & m_shard_mutex_2
The "largest" of the two shards' mutexes in the latching order.
Definition: lock0guards.h:155
Page identifier.
Definition: buf0types.h:207
A utility class which, if inherited from, prevents the descendant class from being copied,...
Definition: ut0class_life_cycle.h:41
ib_id_t table_id_t
Table or partition identifier (unique within an InnoDB instance).
Definition: dict0types.h:232
The transaction lock system.
ib_mutex_t Lock_mutex
Definition: lock0lock.h:1017
static PFS_engine_table_share_proxy table
Definition: pfs.cc:61
Definition: lock0guards.h:34
The buffer control block structure.
Definition: buf0buf.h:1747
Data structure for a database table.
Definition: dict0mem.h:1909
Definition: ut0core.h:36
Utilities related to class lifecycle.
unsigned long id[MAX_DEAD]
Definition: xcom_base.cc:510