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