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