Debian Codegraph @

storage/innobase/include/sync0debug.h

raw
1 /*****************************************************************************
2
3 Copyright (c) 2013, 2026, Oracle and/or its affiliates.
4
5 Portions of this file contain modifications contributed and copyrighted by
6 Google, Inc. Those modifications are gratefully acknowledged and are described
7 briefly in the InnoDB documentation. The contributions by Google are
8 incorporated with their permission, and subject to the conditions contained in
9 the file COPYING.Google.
10
11 This program is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License, version 2.0, as published by the
13 Free Software Foundation.
14
15 This program is designed to work with certain software (including
16 but not limited to OpenSSL) that is licensed under separate terms,
17 as designated in a particular file or component or in included license
18 documentation. The authors of MySQL hereby grant you an additional
19 permission to link the program and your derivative works with the
20 separately licensed software that they have either included with
21 the program or referenced in the documentation.
22
23 This program is distributed in the hope that it will be useful, but WITHOUT
24 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
25 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
26 for more details.
27
28 You should have received a copy of the GNU General Public License along with
29 this program; if not, write to the Free Software Foundation, Inc.,
30 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31
32 *****************************************************************************/
33
34 /** @file include/sync0debug.h
35 Debug checks for latches, header file
36
37 Created 2012-08-21 Sunny Bains
38 *******************************************************/
39
40 #ifndef sync0debug_h
41 #define sync0debug_h
42
43 #ifndef UNIV_LIBRARY
44 #include "sync0types.h"
45
46 #include <string>
47 #include <vector>
48
49 /** Initializes the synchronization data structures.
50 @param[in] max_threads Maximum threads that can be created. */
51 void sync_check_init(size_t max_threads);
52
53 /** Frees the resources in synchronization data structures. */
54 void sync_check_close();
55
56 #ifdef UNIV_DEBUG
57 /** Enable sync order checking. */
58 void sync_check_enable();
59
60 /** Check if it is OK to acquire the latch.
61 @param[in] latch latch type */
62 void sync_check_lock_validate(const latch_t *latch);
63
64 /** Note that the lock has been granted
65 @param[in] latch latch type */
66 void sync_check_lock_granted(const latch_t *latch);
67
68 /** Check if it is OK to acquire the latch.
69 @param[in] latch latch type
70 @param[in] level the level of the mutex */
71 void sync_check_lock(const latch_t *latch, latch_level_t level);
72
73 /**
74 Check if it is OK to re-acquire the lock. */
75 void sync_check_relock(const latch_t *latch);
76
77 /** Removes a latch from the thread level array if it is found there.
78 @param[in] latch The latch to unlock */
79 void sync_check_unlock(const latch_t *latch);
80
81 /** Checks if the level array for the current thread contains a
82 mutex or rw-latch at the specified level.
83 @param[in] level to find
84 @return a matching latch, or NULL if not found */
85 const latch_t *sync_check_find(latch_level_t level);
86
87 /** Checks that the level array for the current thread is empty.
88 Terminate iteration if the functor returns true.
89 @param[in,out] functor called for each element.
90 @return true if the functor returns true */
91 bool sync_check_iterate(sync_check_functor_t &functor);
92
93 /** Acquires the debug mutex. We cannot use the mutex defined in sync0sync,
94 because the debug mutex is also acquired in sync0arr while holding the OS
95 mutex protecting the sync array, and the ordinary mutex_enter might
96 recursively call routines in sync0arr, leading to a deadlock on the OS
97 mutex. */
98 void rw_lock_debug_mutex_enter();
99
100 /** Releases the debug mutex. */
101 void rw_lock_debug_mutex_exit();
102
103 /** For handling sync points in child threads spawned by a foreground thread. */
104 class Sync_point {
105 public:
106 /** Constructor.
107 @param[in,out] thd Server connection/session context. */
108 explicit Sync_point(const THD *thd) noexcept : m_thd(thd) {}
109
110 Sync_point(const Sync_point &) = default;
111
112 Sync_point &operator=(const Sync_point &) = default;
113
114 /** Destructor. */
115 ~Sync_point() = default;
116
117 /** Add a target to the list of sync points, nop for duplicates.
118 @param[in] thd Server connection/session context.
119 @param[in] target Target to add. */
120 static void add(const THD *thd, const std::string &target) noexcept;
121
122 /** Check if a target is enabled. Disable it if found.
123 @param[in] thd Server connection/session context.
124 @param[in] target Check if target is enabled.
125 @return true if was enabled. */
126 static bool enabled(const THD *thd, const std::string &target) noexcept;
127
128 /** Check if a target is enabled. Disable it if found.
129 @param[in] target Check if target is enabled.
130 @return true if was enabled. */
131 static bool enabled(const std::string &target) noexcept;
132
133 /** Clear the named target.
134 @param[in] thd Server connection/session context.
135 @param[in] target Check if target is enabled. */
136 static void erase(const THD *thd, const std::string &target) noexcept;
137
138 private:
139 using Targets = std::vector<std::string, ut::allocator<std::string>>;
140 using Sync_points = std::vector<Sync_point, ut::allocator<Sync_point>>;
141
142 /** Mutex protecting access to Sync_point infrastructure. */
143 static std::mutex s_mutex;
144
145 /** Sync points. */
146 static Sync_points s_sync_points;
147
148 /** Server connection/session context. */
149 const THD *m_thd{};
150
151 /** List of enabled targets. */
152 Targets m_targets{};
153 };
154 #endif /* UNIV_DEBUG */
155
156 #endif /* !UNIV_LIBRARY */
157 #endif /* !sync0debug_h */