MySQL 8.4.2
Source Code Documentation
sync0debug.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2013, 2024, Oracle and/or its affiliates.
4
5Portions of this file contain modifications contributed and copyrighted by
6Google, Inc. Those modifications are gratefully acknowledged and are described
7briefly in the InnoDB documentation. The contributions by Google are
8incorporated with their permission, and subject to the conditions contained in
9the file COPYING.Google.
10
11This program is free software; you can redistribute it and/or modify it under
12the terms of the GNU General Public License, version 2.0, as published by the
13Free Software Foundation.
14
15This program is designed to work with certain software (including
16but not limited to OpenSSL) that is licensed under separate terms,
17as designated in a particular file or component or in included license
18documentation. The authors of MySQL hereby grant you an additional
19permission to link the program and your derivative works with the
20separately licensed software that they have either included with
21the program or referenced in the documentation.
22
23This program is distributed in the hope that it will be useful, but WITHOUT
24ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
25FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
26for more details.
27
28You should have received a copy of the GNU General Public License along with
29this program; if not, write to the Free Software Foundation, Inc.,
3051 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. */
51void sync_check_init(size_t max_threads);
52
53/** Frees the resources in synchronization data structures. */
54void sync_check_close();
55
56#ifdef UNIV_DEBUG
57/** Enable sync order checking. */
59
60/** Check if it is OK to acquire the latch.
61@param[in] latch latch type */
62void sync_check_lock_validate(const latch_t *latch);
63
64/** Note that the lock has been granted
65@param[in] latch latch type */
66void 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 */
71void sync_check_lock(const latch_t *latch, latch_level_t level);
72
73/**
74Check if it is OK to re-acquire the lock. */
75void 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 */
79void sync_check_unlock(const latch_t *latch);
80
81/** Checks if the level array for the current thread contains a
82mutex or rw-latch at the specified level.
83@param[in] level to find
84@return a matching latch, or NULL if not found */
86
87/** Checks that the level array for the current thread is empty.
88Terminate iteration if the functor returns true.
89@param[in,out] functor called for each element.
90@return true if the functor returns true */
92
93/** Acquires the debug mutex. We cannot use the mutex defined in sync0sync,
94because the debug mutex is also acquired in sync0arr while holding the OS
95mutex protecting the sync array, and the ordinary mutex_enter might
96recursively call routines in sync0arr, leading to a deadlock on the OS
97mutex. */
99
100/** Releases the debug mutex. */
102
103/** For handling sync points in child threads spawned by a foreground thread. */
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. */
147
148 /** Server connection/session context. */
149 const THD *m_thd{};
150
151 /** List of enabled targets. */
153};
154#endif /* UNIV_DEBUG */
155
156#endif /* !UNIV_LIBRARY */
157#endif /* !sync0debug_h */
For handling sync points in child threads spawned by a foreground thread.
Definition: sync0debug.h:104
std::vector< Sync_point, ut::allocator< Sync_point > > Sync_points
Definition: sync0debug.h:140
const THD * m_thd
Server connection/session context.
Definition: sync0debug.h:149
Sync_point(const THD *thd) noexcept
Constructor.
Definition: sync0debug.h:108
std::vector< std::string, ut::allocator< std::string > > Targets
Definition: sync0debug.h:139
static void add(const THD *thd, const std::string &target) noexcept
Add a target to the list of sync points, nop for duplicates.
Definition: sync0debug.cc:1711
~Sync_point()=default
Destructor.
Sync_point(const Sync_point &)=default
static Sync_points s_sync_points
Sync points.
Definition: sync0debug.h:146
static bool enabled(const THD *thd, const std::string &target) noexcept
Check if a target is enabled.
Definition: sync0debug.cc:1732
static void erase(const THD *thd, const std::string &target) noexcept
Clear the named target.
Definition: sync0debug.cc:1758
Sync_point & operator=(const Sync_point &)=default
static std::mutex s_mutex
Mutex protecting access to Sync_point infrastructure.
Definition: sync0debug.h:143
Targets m_targets
List of enabled targets.
Definition: sync0debug.h:152
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
All (ordered) latches, used in debugging, must derive from this class.
Definition: sync0types.h:962
Subclass this to iterate over a thread's acquired latch levels.
Definition: sync0types.h:1043
void sync_check_relock(const latch_t *latch)
Check if it is OK to re-acquire the lock.
Definition: sync0debug.cc:1052
const latch_t * sync_check_find(latch_level_t level)
Checks if the level array for the current thread contains a mutex or rw-latch at the specified level.
Definition: sync0debug.cc:1070
bool sync_check_iterate(sync_check_functor_t &functor)
Checks that the level array for the current thread is empty.
Definition: sync0debug.cc:1082
void sync_check_unlock(const latch_t *latch)
Removes a latch from the thread level array if it is found there.
Definition: sync0debug.cc:1060
void sync_check_close()
Frees the resources in synchronization data structures.
Definition: sync0debug.cc:1689
void sync_check_lock_validate(const latch_t *latch)
Check if it is OK to acquire the latch.
Definition: sync0debug.cc:1023
void sync_check_lock_granted(const latch_t *latch)
Note that the lock has been granted.
Definition: sync0debug.cc:1031
void rw_lock_debug_mutex_enter()
Acquires the debug mutex.
Definition: sync0debug.cc:1146
void rw_lock_debug_mutex_exit()
Releases the debug mutex.
Definition: sync0debug.cc:1172
void sync_check_enable()
Enable sync order checking.
Definition: sync0debug.cc:1094
void sync_check_lock(const latch_t *latch, latch_level_t level)
Check if it is OK to acquire the latch.
Definition: sync0debug.cc:1040
void sync_check_init(size_t max_threads)
Initializes the synchronization data structures.
Definition: sync0debug.cc:1666
Global types for sync.
latch_level_t
Latching order levels.
Definition: sync0types.h:201