| 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 |
#define LOCK_MODULE_IMPLEMENTATION |
| 29 |
|
| 30 |
#include "lock0guards.h" |
| 31 |
#include "lock0priv.h" |
| 32 |
#include "sync0rw.h" |
| 33 |
|
| 34 |
namespace locksys { |
| 35 |
|
| 36 |
/* Global_exclusive_latch_guard */ |
| 37 |
|
| 38 |
Global_exclusive_latch_guard::Global_exclusive_latch_guard( |
| 39 |
ut::Location location) { |
| 40 |
lock_sys->latches.global_latch.x_lock(location); |
| 41 |
} |
| 42 |
|
| 43 |
Global_exclusive_latch_guard::~Global_exclusive_latch_guard() { |
| 44 |
lock_sys->latches.global_latch.x_unlock(); |
| 45 |
} |
| 46 |
|
| 47 |
/* Global_exclusive_try_latch */ |
| 48 |
|
| 49 |
Global_exclusive_try_latch::Global_exclusive_try_latch(ut::Location location) { |
| 50 |
m_owns_exclusive_global_latch = |
| 51 |
lock_sys->latches.global_latch.try_x_lock(location); |
| 52 |
} |
| 53 |
|
| 54 |
Global_exclusive_try_latch::~Global_exclusive_try_latch() { |
| 55 |
if (m_owns_exclusive_global_latch) { |
| 56 |
lock_sys->latches.global_latch.x_unlock(); |
| 57 |
m_owns_exclusive_global_latch = false; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
/* Shard_naked_latch_guard */ |
| 62 |
|
| 63 |
Shard_naked_latch_guard::Shard_naked_latch_guard(ut::Location location, |
| 64 |
Lock_mutex &shard_mutex) |
| 65 |
: m_shard_mutex{shard_mutex} { |
| 66 |
ut_ad(owns_shared_global_latch()); |
| 67 |
mutex_enter_inline(&m_shard_mutex, location); |
| 68 |
} |
| 69 |
|
| 70 |
Shard_naked_latch_guard::Shard_naked_latch_guard(ut::Location location, |
| 71 |
const table_id_t &table_id) |
| 72 |
: Shard_naked_latch_guard{ |
| 73 |
location, lock_sys->latches.table_shards.get_mutex(table_id)} {} |
| 74 |
|
| 75 |
Shard_naked_latch_guard::Shard_naked_latch_guard(ut::Location location, |
| 76 |
const page_id_t &page_id) |
| 77 |
: Shard_naked_latch_guard{ |
| 78 |
location, lock_sys->latches.page_shards.get_mutex(page_id)} {} |
| 79 |
|
| 80 |
// Please note, that the hash_table argument is ignored, but differentiates this |
| 81 |
// overload from the one for table_id_t which is uint64_t as well. |
| 82 |
Shard_naked_latch_guard::Shard_naked_latch_guard(ut::Location location, |
| 83 |
hash_table_t *, |
| 84 |
uint64_t cell_id) |
| 85 |
: Shard_naked_latch_guard{ |
| 86 |
location, lock_sys->latches.page_shards.get_mutex(cell_id)} {} |
| 87 |
|
| 88 |
Shard_naked_latch_guard::~Shard_naked_latch_guard() { |
| 89 |
mutex_exit(&m_shard_mutex); |
| 90 |
} |
| 91 |
|
| 92 |
/* Global_shared_latch_guard */ |
| 93 |
|
| 94 |
Global_shared_latch_guard::Global_shared_latch_guard(ut::Location location) { |
| 95 |
lock_sys->latches.global_latch.s_lock(location); |
| 96 |
} |
| 97 |
|
| 98 |
Global_shared_latch_guard::~Global_shared_latch_guard() { |
| 99 |
lock_sys->latches.global_latch.s_unlock(); |
| 100 |
} |
| 101 |
bool Global_shared_latch_guard::is_x_blocked_by_us() { |
| 102 |
return lock_sys->latches.global_latch.is_x_blocked_by_our_s(); |
| 103 |
} |
| 104 |
|
| 105 |
/* Shard_naked_latches_guard */ |
| 106 |
|
| 107 |
Shard_naked_latches_guard::Shard_naked_latches_guard(Lock_mutex &shard_mutex_a, |
| 108 |
Lock_mutex &shard_mutex_b) |
| 109 |
: m_shard_mutex_1{*std::min(&shard_mutex_a, &shard_mutex_b, MUTEX_ORDER)}, |
| 110 |
m_shard_mutex_2{*std::max(&shard_mutex_a, &shard_mutex_b, MUTEX_ORDER)} { |
| 111 |
ut_ad(owns_shared_global_latch()); |
| 112 |
if (&m_shard_mutex_1 != &m_shard_mutex_2) { |
| 113 |
mutex_enter(&m_shard_mutex_1); |
| 114 |
} |
| 115 |
mutex_enter(&m_shard_mutex_2); |
| 116 |
} |
| 117 |
|
| 118 |
Shard_naked_latches_guard::Shard_naked_latches_guard(const buf_block_t &block_a, |
| 119 |
const buf_block_t &block_b) |
| 120 |
: Shard_naked_latches_guard{ |
| 121 |
lock_sys->latches.page_shards.get_mutex(block_a.get_page_id()), |
| 122 |
lock_sys->latches.page_shards.get_mutex(block_b.get_page_id())} {} |
| 123 |
|
| 124 |
Shard_naked_latches_guard::~Shard_naked_latches_guard() { |
| 125 |
mutex_exit(&m_shard_mutex_2); |
| 126 |
if (&m_shard_mutex_1 != &m_shard_mutex_2) { |
| 127 |
mutex_exit(&m_shard_mutex_1); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
} // namespace locksys |