MySQL 8.0.41
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
lock0iter.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2007, 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/** @file include/lock0iter.h
29 Lock queue iterator type and function prototypes.
30
31 Created July 16, 2007 Vasil Dimov
32 *******************************************************/
33
34#ifndef lock0iter_h
35#define lock0iter_h
36
37#include "dict0types.h"
38#include "lock0types.h"
39#include "univ.i"
40namespace locksys {
41/** Calls visitor for each lock_t object which is a reason that wait_lock has to
42wait. It is assumed that the wait_lock is waiting, and the caller has latched
43the shard which contains the wait_lock
44@param[in] wait_lock the waiting lock
45@param[in] visitor a function to be called for each lock, s.t.
46 locksys::has_to_wait(wait_lock, lock) is true.
47 To stop iteration the visitor can return true, in which
48 case the lock for which it happened will be returned.
49@return the first lock for which visitor returned true (in which case the search
50ends) or nullptr if visitor never returned true (so all waiters were visited).*/
51// TODO: this should use ut0function_reference.h or std::function_ref
52const lock_t *find_blockers(const lock_t &wait_lock,
53 std::function<bool(const lock_t &)> visitor);
54
55/** A helper method to access dict_table_t::locks list in a way which is
56safe against the case another thread is trying to drop or truncate the table.
57The main challenge it solves is how to do that without acquiring an MDL.
58@param[in] table_id The id of the table, locks of which we want to visit
59@param[in] visitor The visitor function which will be called for each lock
60 on the table.
61 It might be not called at all, in case the table is no
62 longer found in the hash, or has no locks.
63 If it is called, then it is called under protection of
64 shard mutex for this table_id.
65 To stop iteration the visitor can return true.
66*/
67void find_on_table(const table_id_t table_id,
68 std::function<bool(const lock_t &)> visitor);
69
70} // namespace locksys
71/** Iterates over all locks in the lock sys in a manner which guarantees that
72all locks from the same lock queue are processed in a single critical section.*/
74 public:
75 /** Processes a batch of one or more non-empty lock queues, calling the
76 provided function f for each lock in the queue, making sure that the queue is
77 not being modified during processing it.
78 Please note, that this means that the locks from a single lock queue visited
79 by f() present a consistent snapshot of this queue, however locks which reside
80 in different queues, may be inconsistent with each other, as they are observed
81 at different "times".
82 Also, this iterator does not guarantee reporting all locks in case the
83 lock-sys is being resized in parallel by lock_sys_resize() - resizing causes
84 the iterator to stop processing to avoid double-reporting.
85 @return true iff the iterator is done, and calling it again will not provide
86 any further results */
88 const std::function<void(const lock_t &lock)> &f);
89
90 private:
91 /** This iterator moves through the following stages, where the move to next
92 stage occurs when all locks from previous stage were reported. */
93 enum class stage_t {
94 /** iterator was just created (which does not cost much) */
96 /** iterating over LOCK_TABLE locks for tables from m_table_ids */
98 /** iterating over LOCK_PRDT_PAGE in lock_sys->prdt_page_hash */
100 /** iterating over LOCK_PREDICATE locks in lock_sys->prdt_hash */
102 /** iterating over other (non-predicate) LOCK_RECORD locks in
103 lock_sys->rec_hash */
104 REC_LOCKS,
105 /** finished iterating, nothing more to see */
106 DONE,
107 };
108
109 /** The current stage this iterator is in. */
111
112 /** List of ids of all tables found in dict sys which are candidates for
113 inspection in TABLE_LOCKS stage */
114 std::vector<table_id_t> m_table_ids;
115
116 /** Tracks progress within a single stage: index of table in m_table_ids for
117 the TABLE_LOCKS stage, and cell of the hash_table for record locks.
118 It is reset to 0 at the beginning of each stage. */
119 size_t m_bucket_id{0};
120
121 /** The value of lock_sys->n_resizes is stored in this field at the begging
122 of stages involving iterating over lock sys hash tables so that we can spot
123 if the hash table got resized during our iteration and invalidate the iterator
124 */
126
127 private:
128 /** Helper function for TABLE_LOCKS stage.
129 Calls f for all locks associated with m_table_ids[m_bucket_id].
130 @param[in] f function to apply to each lock
131 @return true iff it succeeded */
132 template <typename F>
133 bool iterate_over_current_table(F &&f);
134
135 /** Helper function for PRDT_PAGE_LOCKS, PRDT_LOCKS and REC_LOCKS stages.
136 Calls f for all locks associated with hash_table m_bucket_id-th cell.
137 @param[in] hash_table hash_table to inspect
138 @param[in] f function to apply to each lock
139 @return true iff it succeeded */
140 template <typename F>
141 bool iterate_over_current_cell(struct Locks_hashtable &hash_table, F &&f);
142};
143
144#endif /* lock0iter_h */
Iterates over all locks in the lock sys in a manner which guarantees that all locks from the same loc...
Definition: lock0iter.h:73
std::vector< table_id_t > m_table_ids
List of ids of all tables found in dict sys which are candidates for inspection in TABLE_LOCKS stage.
Definition: lock0iter.h:114
bool iterate_over_current_cell(struct Locks_hashtable &hash_table, F &&f)
Helper function for PRDT_PAGE_LOCKS, PRDT_LOCKS and REC_LOCKS stages.
Definition: lock0iter.cc:60
bool iterate_over_current_table(F &&f)
Helper function for TABLE_LOCKS stage.
Definition: lock0iter.cc:45
stage_t m_stage
The current stage this iterator is in.
Definition: lock0iter.h:110
size_t m_bucket_id
Tracks progress within a single stage: index of table in m_table_ids for the TABLE_LOCKS stage,...
Definition: lock0iter.h:119
bool iterate_over_next_batch(const std::function< void(const lock_t &lock)> &f)
Processes a batch of one or more non-empty lock queues, calling the provided function f for each lock...
Definition: lock0iter.cc:115
stage_t
This iterator moves through the following stages, where the move to next stage occurs when all locks ...
Definition: lock0iter.h:93
@ NOT_STARTED
iterator was just created (which does not cost much)
@ DONE
finished iterating, nothing more to see
@ TABLE_LOCKS
iterating over LOCK_TABLE locks for tables from m_table_ids
@ PRDT_LOCKS
iterating over LOCK_PREDICATE locks in lock_sys->prdt_hash
@ REC_LOCKS
iterating over other (non-predicate) LOCK_RECORD locks in lock_sys->rec_hash
@ PRDT_PAGE_LOCKS
iterating over LOCK_PRDT_PAGE in lock_sys->prdt_page_hash
uint32_t m_lock_sys_n_resizes_at_the_beginning
The value of lock_sys->n_resizes is stored in this field at the begging of stages involving iterating...
Definition: lock0iter.h:125
Data dictionary global types.
ib_id_t table_id_t
Table or partition identifier (unique within an InnoDB instance).
Definition: dict0types.h:232
The transaction lock system global types.
Provides atomic access in shared-exclusive modes.
Definition: shared_spin_lock.h:79
Definition: lock0guards.h:34
void find_on_table(const table_id_t table_id, std::function< bool(const lock_t &)> visitor)
A helper method to access dict_table_t::locks list in a way which is safe against the case another th...
Definition: lock0iter.cc:217
const lock_t * find_blockers(const lock_t &wait_lock, std::function< bool(const lock_t &)> visitor)
Calls visitor for each lock_t object which is a reason that wait_lock has to wait.
Definition: lock0iter.cc:185
A hashmap used by lock sys, to organize locks by page (block), so that it is easy to maintain a list ...
Definition: lock0lock.h:1018
Lock struct; protected by lock_sys latches.
Definition: lock0priv.h:137
Version control for database, common definitions, and include files.