MySQL 9.1.0
Source Code Documentation
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} // namespace locksys
55/** Iterates over all locks in the lock sys in a manner which guarantees that
56all locks from the same lock queue are processed in a single critical section.*/
58 public:
59 /** Processes a batch of one or more non-empty lock queues, calling the
60 provided function f for each lock in the queue, making sure that the queue is
61 not being modified during processing it.
62 Please note, that this means that the locks from a single lock queue visited
63 by f() present a consistent snapshot of this queue, however locks which reside
64 in different queues, may be inconsistent with each other, as they are observed
65 at different "times".
66 Also, this iterator does not guarantee reporting all locks in case the
67 lock-sys is being resized in parallel by lock_sys_resize() - resizing causes
68 the iterator to stop processing to avoid double-reporting.
69 @return true iff the iterator is done, and calling it again will not provide
70 any further results */
72 const std::function<void(const lock_t &lock)> &f);
73
74 private:
75 /** This iterator moves through the following stages, where the move to next
76 stage occurs when all locks from previous stage were reported. */
77 enum class stage_t {
78 /** iterator was just created (which does not cost much) */
80 /** iterating over LOCK_TABLE locks for tables from m_table_ids */
82 /** iterating over LOCK_PRDT_PAGE in lock_sys->prdt_page_hash */
84 /** iterating over LOCK_PREDICATE locks in lock_sys->prdt_hash */
86 /** iterating over other (non-predicate) LOCK_RECORD locks in
87 lock_sys->rec_hash */
89 /** finished iterating, nothing more to see */
90 DONE,
91 };
92
93 /** The current stage this iterator is in. */
95
96 /** List of ids of all tables found in dict sys which are candidates for
97 inspection in TABLE_LOCKS stage */
98 std::vector<table_id_t> m_table_ids;
99
100 /** Tracks progress within a single stage: index of table in m_table_ids for
101 the TABLE_LOCKS stage, and cell of the hash_table for record locks.
102 It is reset to 0 at the beginning of each stage. */
103 size_t m_bucket_id{0};
104
105 /** The value of lock_sys->n_resizes is stored in this field at the begging
106 of stages involving iterating over lock sys hash tables so that we can spot
107 if the hash table got resized during our iteration and invalidate the iterator
108 */
110
111 private:
112 /** Helper function for TABLE_LOCKS stage.
113 Calls f for all locks associated with m_table_ids[m_bucket_id].
114 @param[in] f function to apply to each lock
115 @return true iff it succeeded */
116 template <typename F>
117 bool iterate_over_current_table(F &&f);
118
119 /** Helper function for PRDT_PAGE_LOCKS, PRDT_LOCKS and REC_LOCKS stages.
120 Calls f for all locks associated with hash_table m_bucket_id-th cell.
121 @param[in] hash_table hash_table to inspect
122 @param[in] f function to apply to each lock
123 @return true iff it succeeded */
124 template <typename F>
125 bool iterate_over_current_cell(struct Locks_hashtable &hash_table, F &&f);
126};
127
128#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:57
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:98
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:66
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:94
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:103
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:121
stage_t
This iterator moves through the following stages, where the move to next stage occurs when all locks ...
Definition: lock0iter.h:77
@ 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:109
Data dictionary global types.
The transaction lock system global types.
Provides atomic access in shared-exclusive modes.
Definition: shared_spin_lock.h:79
Definition: lock0guards.h:34
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:191
A hashmap used by lock sys, to organize locks by page (block), so that it is easy to maintain a list ...
Definition: lock0lock.h:1008
Lock struct; protected by lock_sys latches.
Definition: lock0priv.h:137
Version control for database, common definitions, and include files.