| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2007, 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 |
/** @file include/lock0priv.ic |
| 29 |
Lock module internal inline methods. |
| 30 |
|
| 31 |
Created July 16, 2007 Vasil Dimov |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
/* This file contains only methods which are used in |
| 35 |
lock/lock0* files, other than lock/lock0lock.cc. |
| 36 |
I.e. lock/lock0lock.cc contains more internal inline |
| 37 |
methods but they are used only in that file. */ |
| 38 |
|
| 39 |
#ifndef LOCK_MODULE_IMPLEMENTATION |
| 40 |
#error Do not include lock0priv.ic outside of the lock/ module |
| 41 |
#endif |
| 42 |
|
| 43 |
/** Gets the type of a lock. |
| 44 |
@return LOCK_TABLE or LOCK_REC */ |
| 45 |
static inline uint32_t lock_get_type_low(const lock_t *lock) /*!< in: lock */ |
| 46 |
{ |
| 47 |
ut_ad(lock); |
| 48 |
|
| 49 |
return (lock->type_mode & LOCK_TYPE_MASK); |
| 50 |
} |
| 51 |
|
| 52 |
static inline trx_id_t lock_clust_rec_some_has_impl(const rec_t *rec, |
| 53 |
const dict_index_t *index, |
| 54 |
const ulint *offsets) { |
| 55 |
ut_ad(index->is_clustered()); |
| 56 |
ut_ad(page_rec_is_user_rec(rec)); |
| 57 |
|
| 58 |
return (row_get_rec_trx_id(rec, index, offsets)); |
| 59 |
} |
| 60 |
|
| 61 |
static inline uint32_t lock_rec_get_n_bits(const lock_t *lock) { |
| 62 |
return (lock->rec_lock.n_bits); |
| 63 |
} |
| 64 |
|
| 65 |
/** Sets the nth bit of a record lock to true. */ |
| 66 |
static inline void lock_rec_set_nth_bit(lock_t *lock, /*!< in: record lock */ |
| 67 |
ulint i) /*!< in: index of the bit */ |
| 68 |
{ |
| 69 |
ut_ad(lock); |
| 70 |
auto bitset = lock->bitset(); |
| 71 |
ut_ad(!bitset.test(i)); |
| 72 |
bitset.set(i); |
| 73 |
lock->trx->lock.n_rec_locks.fetch_add(1, std::memory_order_relaxed); |
| 74 |
} |
| 75 |
|
| 76 |
/** Gets the nth bit of a record lock. |
| 77 |
@param[in] lock Record lock |
| 78 |
@param[in] i Index of the bit to check |
| 79 |
@return true if bit set also if i == ULINT_UNDEFINED return false */ |
| 80 |
static inline bool lock_rec_get_nth_bit(const lock_t *lock, ulint i) { |
| 81 |
ut_ad(lock); |
| 82 |
ut_ad(lock_get_type_low(lock) == LOCK_REC); |
| 83 |
/* When allocating the bitmap we've chosen size good enough at that moment, |
| 84 |
to fit all heap_no's on the page, plus some margin, but the page can allocate |
| 85 |
new heap_no's over time, and we should treat bits at these positions as 0 */ |
| 86 |
if (i >= lock->rec_lock.n_bits) { |
| 87 |
return (false); |
| 88 |
} |
| 89 |
return lock->bitset().test(i); |
| 90 |
} |
| 91 |
|
| 92 |
/** Gets the mode of a lock. |
| 93 |
@return mode */ |
| 94 |
static inline enum lock_mode lock_get_mode(const lock_t *lock) /*!< in: lock */ |
| 95 |
{ |
| 96 |
ut_ad(lock); |
| 97 |
|
| 98 |
return (static_cast<enum lock_mode>(lock->type_mode & LOCK_MODE_MASK)); |
| 99 |
} |
| 100 |
|
| 101 |
/** Calculates if lock mode 1 is compatible with lock mode 2. |
| 102 |
@return nonzero if mode1 compatible with mode2 */ |
| 103 |
static inline ulint lock_mode_compatible( |
| 104 |
enum lock_mode mode1, /*!< in: lock mode */ |
| 105 |
enum lock_mode mode2) /*!< in: lock mode */ |
| 106 |
{ |
| 107 |
ut_ad((ulint)mode1 < lock_types); |
| 108 |
ut_ad((ulint)mode2 < lock_types); |
| 109 |
|
| 110 |
return (lock_compatibility_matrix[mode1][mode2]); |
| 111 |
} |
| 112 |
|
| 113 |
static inline bool lock_mode_stronger_or_eq(enum lock_mode mode1, |
| 114 |
enum lock_mode mode2) { |
| 115 |
ut_ad((ulint)mode1 < lock_types); |
| 116 |
ut_ad((ulint)mode2 < lock_types); |
| 117 |
|
| 118 |
return (lock_strength_matrix[mode1][mode2] != 0); |
| 119 |
} |
| 120 |
|
| 121 |
/** Gets the wait flag of a lock. |
| 122 |
@return LOCK_WAIT if waiting, 0 if not */ |
| 123 |
static inline ulint lock_get_wait(const lock_t *lock) /*!< in: lock */ |
| 124 |
{ |
| 125 |
ut_ad(lock); |
| 126 |
|
| 127 |
return (lock->type_mode & LOCK_WAIT); |
| 128 |
} |
| 129 |
|
| 130 |
/** The back pointer to a waiting lock request in the transaction is set to NULL |
| 131 |
and the wait bit in lock type_mode is reset. */ |
| 132 |
static inline void lock_reset_lock_and_trx_wait( |
| 133 |
lock_t *lock) /*!< in/out: record lock */ |
| 134 |
{ |
| 135 |
ut_ad(locksys::owns_lock_shard(lock)); |
| 136 |
ut_ad(lock_get_wait(lock)); |
| 137 |
ut_ad(lock->trx->lock.wait_lock == lock); |
| 138 |
/* We intentionally don't clear trx->lock.blocking_trx here, as |
| 139 |
lock_reset_lock_and_trx_wait() is called also during movements of locks from |
| 140 |
one page to another, which does not really change the structure of the |
| 141 |
wait-for graph. Instead the lock_reset_wait_and_release_thread_if_suspended() |
| 142 |
is responsible for clearing the blocking_trx field once it is sure that |
| 143 |
we really want to remove the edge from the wait-for graph.*/ |
| 144 |
lock->trx->lock.wait_lock = nullptr; |
| 145 |
|
| 146 |
/* We intentionally don't clear lock->trx->lock.wait_lock_type here, to make |
| 147 |
it easier to obtain stats about the last wait in lock_wait_suspend_thread(). |
| 148 |
@see trx_lock_t::wait_lock_type for more detailed explanation. */ |
| 149 |
lock->type_mode &= ~LOCK_WAIT; |
| 150 |
} |
| 151 |
|
| 152 |
static inline bool lock_table_has(const trx_t *trx, const dict_table_t *table, |
| 153 |
lock_mode in_mode) { |
| 154 |
ut_ad(!trx_mutex_own(trx)); |
| 155 |
trx_mutex_enter(trx); |
| 156 |
ut_ad(trx_can_be_handled_by_current_thread(trx)); |
| 157 |
|
| 158 |
/* Look for stronger locks the same trx already has on the table */ |
| 159 |
for (const lock_t *lock = UT_LIST_GET_FIRST(trx->lock.trx_locks); |
| 160 |
lock != nullptr && lock_get_type(lock) == LOCK_TABLE; |
| 161 |
lock = UT_LIST_GET_NEXT(trx_locks, lock)) { |
| 162 |
lock_mode mode = lock_get_mode(lock); |
| 163 |
|
| 164 |
ut_ad(trx == lock->trx); |
| 165 |
ut_ad(lock->tab_lock.table != nullptr); |
| 166 |
|
| 167 |
if (table == lock->tab_lock.table && |
| 168 |
lock_mode_stronger_or_eq(mode, in_mode)) { |
| 169 |
// we know it can't be waiting because we are the thread *running* the trx |
| 170 |
ut_ad(!lock_get_wait(lock)); |
| 171 |
|
| 172 |
trx_mutex_exit(trx); |
| 173 |
return (true); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
trx_mutex_exit(trx); |
| 178 |
return (false); |
| 179 |
} |
| 180 |
|
| 181 |
/* Check if the rec id matches the lock instance. |
| 182 |
@param[i] lock Lock to compare with |
| 183 |
@return true if <space, page_no, heap_no> matches the lock. */ |
| 184 |
bool RecID::matches(const lock_t *lock) const { |
| 185 |
return (lock->rec_lock.page_id == get_page_id() && |
| 186 |
lock_rec_get_nth_bit(lock, m_heap_no)); |
| 187 |
} |
| 188 |
|
| 189 |
/* vim: set filetype=c: */ |