Debian Codegraph @

storage/innobase/include/lock0iter.h

raw
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/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"
40 namespace locksys {
41 /** Calls visitor for each lock_t object which is a reason that wait_lock has to
42 wait. It is assumed that the wait_lock is waiting, and the caller has latched
43 the 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
50 ends) or nullptr if visitor never returned true (so all waiters were visited).*/
51 // TODO: this should use ut0function_reference.h or std::function_ref
52 const 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
56 safe against the case another thread is trying to drop or truncate the table.
57 The 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 */
67 void 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
72 all locks from the same lock queue are processed in a single critical section.*/
73 class All_locks_iterator {
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 */
87 bool iterate_over_next_batch(
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) */
95 NOT_STARTED,
96 /** iterating over LOCK_TABLE locks for tables from m_table_ids */
97 TABLE_LOCKS,
98 /** iterating over LOCK_PRDT_PAGE in lock_sys->prdt_page_hash */
99 PRDT_PAGE_LOCKS,
100 /** iterating over LOCK_PREDICATE locks in lock_sys->prdt_hash */
101 PRDT_LOCKS,
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. */
110 stage_t m_stage{stage_t::NOT_STARTED};
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 */
125 uint32_t m_lock_sys_n_resizes_at_the_beginning{0};
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 */