| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1996, 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/lock0types.h |
| 29 |
The transaction lock system global types |
| 30 |
|
| 31 |
Created 5/7/1996 Heikki Tuuri |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#include "univ.i" |
| 35 |
|
| 36 |
#ifndef lock0types_h |
| 37 |
#define lock0types_h |
| 38 |
|
| 39 |
#define lock_t ib_lock_t |
| 40 |
|
| 41 |
#include "trx0types.h" |
| 42 |
|
| 43 |
struct lock_t; |
| 44 |
struct lock_sys_t; |
| 45 |
struct lock_table_t; |
| 46 |
|
| 47 |
enum select_mode { |
| 48 |
SELECT_ORDINARY, /* default behaviour */ |
| 49 |
SELECT_SKIP_LOCKED, /* skip the row if row is locked */ |
| 50 |
SELECT_NOWAIT /* return immediately if row is locked */ |
| 51 |
}; |
| 52 |
|
| 53 |
/* Basic lock modes */ |
| 54 |
enum lock_mode { |
| 55 |
LOCK_IS = 0, /* intention shared */ |
| 56 |
LOCK_IX, /* intention exclusive */ |
| 57 |
LOCK_S, /* shared */ |
| 58 |
LOCK_X, /* exclusive */ |
| 59 |
LOCK_AUTO_INC, /* locks the auto-inc counter of a table |
| 60 |
in an exclusive mode */ |
| 61 |
LOCK_NONE, /* this is used elsewhere to note consistent read */ |
| 62 |
LOCK_NUM = LOCK_NONE, /* number of lock modes */ |
| 63 |
LOCK_NONE_UNSET = 255 |
| 64 |
}; |
| 65 |
|
| 66 |
/** Convert the given enum value into string. |
| 67 |
@param[in] mode the lock mode |
| 68 |
@return human readable string of the given enum value */ |
| 69 |
inline const char *lock_mode_string(enum lock_mode mode) { |
| 70 |
switch (mode) { |
| 71 |
case LOCK_IS: |
| 72 |
return ("LOCK_IS"); |
| 73 |
case LOCK_IX: |
| 74 |
return ("LOCK_IX"); |
| 75 |
case LOCK_S: |
| 76 |
return ("LOCK_S"); |
| 77 |
case LOCK_X: |
| 78 |
return ("LOCK_X"); |
| 79 |
case LOCK_AUTO_INC: |
| 80 |
return ("LOCK_AUTO_INC"); |
| 81 |
case LOCK_NONE: |
| 82 |
return ("LOCK_NONE"); |
| 83 |
case LOCK_NONE_UNSET: |
| 84 |
return ("LOCK_NONE_UNSET"); |
| 85 |
default: |
| 86 |
ut_error; |
| 87 |
} |
| 88 |
} |
| 89 |
typedef UT_LIST_BASE_NODE_T_EXTERN(lock_t, trx_locks) trx_lock_list_t; |
| 90 |
|
| 91 |
typedef uint32_t trx_schedule_weight_t; |
| 92 |
|
| 93 |
/** Used to represent locks requests uniquely over time. |
| 94 |
Please note that in case of LOCK_REC there might be actually multiple |
| 95 |
"sub-requests" for many different heap_no associated with the same lock_t object |
| 96 |
which we ignore here and use same guid for all of them. |
| 97 |
Also, the lock mode and status and other properties of the lock can change over |
| 98 |
time and/or represent more than one actual request. This is also ignored here. |
| 99 |
What we try to achieve is some way to identify lock_t struct such that: |
| 100 |
(1) we can serialize and deserialize it |
| 101 |
(2) we don't have to be afraid of dangling pointers |
| 102 |
(3) if the same lock_t gets deallocated and then allocated again, or otherwise |
| 103 |
reused by a different transaction, we will have a different guid for it. Note, |
| 104 |
that a single transaction never dealloactes a lock_t and allocates it again, as |
| 105 |
all deallocations happen during commit (lock_trx_release_locks). */ |
| 106 |
struct lock_guid_t { |
| 107 |
/** The guid of lock->trx. Used to identify ABA problems when the same lock_t |
| 108 |
struct gets reused by a new transaction. */ |
| 109 |
trx_guid_t m_trx_guid{}; |
| 110 |
|
| 111 |
/** Id of the lock_t struct such that it does not change over time, and two |
| 112 |
different lock_t structs never have the same id. However it may happen that |
| 113 |
two "different" locks at different points in time actually reuse the same |
| 114 |
lock_t struct and thus have the same immutable id - this is why we also store |
| 115 |
the transaction's guid */ |
| 116 |
uint64_t m_immutable_id{}; |
| 117 |
|
| 118 |
/** Initializes the lock_guid_t object to a value which doesn't match any real |
| 119 |
lock. */ |
| 120 |
lock_guid_t() = default; |
| 121 |
|
| 122 |
/** Initializes lock_guid_t with data uniquely identifying the lock request(s) |
| 123 |
represented by lock_t object. |
| 124 |
@param[in] lock the object representing the lock request(s) */ |
| 125 |
lock_guid_t(const lock_t &lock); |
| 126 |
|
| 127 |
/** Checks if two guids represent the same lock (conceptually): |
| 128 |
they represent the same lock_t struct in memory and it was not reused. |
| 129 |
@param[in] rhs another guid to compare against |
| 130 |
@return true iff the two guids are equal and thus represent same lock*/ |
| 131 |
bool operator==(const lock_guid_t &rhs) const { |
| 132 |
return m_trx_guid == rhs.m_trx_guid && m_immutable_id == rhs.m_immutable_id; |
| 133 |
} |
| 134 |
|
| 135 |
/** Checks if two guids represent two different locks (conceptually): |
| 136 |
they represent two different lock_t structs in memory or struct was reused. |
| 137 |
@param[in] rhs another guid to compare against |
| 138 |
@return true iff the two guids are different and thus represent different |
| 139 |
locks */ |
| 140 |
bool operator!=(const lock_guid_t &rhs) const { return !(*this == rhs); } |
| 141 |
|
| 142 |
/** Checks if the instance is non-empty, i.e. was not default-constructed, |
| 143 |
but rather initialized to correspond to a real lock_t. |
| 144 |
@return true iff this guid was initialized to match a real lock*/ |
| 145 |
operator bool() const { return m_immutable_id != 0; } |
| 146 |
}; |
| 147 |
#endif /* lock0types_h */ |