MySQL 8.4.0
Source Code Documentation
thread_stage_guard.h
Go to the documentation of this file.
1/* Copyright (c) 2023, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef THREAD_STAGE_GUARD_H_INCLUDED
25#define THREAD_STAGE_GUARD_H_INCLUDED
26
27#include "mysql/components/services/bits/psi_stage_bits.h" // PSI_stage_info
28#include "sql/sql_class.h" // THD
29
30namespace raii {
31
32/// RAII guard that sets a thread stage, and restores the previous
33/// stage when going out of scope.
35 public:
37
38 /// Set the given stage for the session, and remember the previous
39 /// stage in a member variable.
40 ///
41 /// @param[in,out] thd Session object that should change stage.
42 ///
43 /// @param new_stage The new stage to use for THD.
44 ///
45 /// @param func Name of the calling function.
46 ///
47 /// @param file Filename of the caller.
48 ///
49 /// @param line Line number of the caller.
50 Thread_stage_guard(THD *thd, const PSI_stage_info &new_stage,
51 const char *func, const char *file,
52 const unsigned int line)
53 : m_new_stage(new_stage),
54 m_thd(thd),
55 m_func(func),
56 m_file(file),
57 m_line(line) {
58 thd->enter_stage(&new_stage, &m_old_stage, func, file, line);
59 }
60
65
66 /// Revert back to the old stage before this object goes out of scope.
67 void set_old_stage() const {
69 }
70
71 /// Restore the new stage, in case set_old_stage was used earlier.
72 void set_new_stage() const {
74 }
75
76 /// Revert the old stage that was used before this object's
77 /// constructor was invoked.
78 ///
79 /// @note This will set the function/filename/line number relating
80 /// to where the Thread_stage_guard was created, not where it went
81 /// out of scope.
83
84 private:
85 /// The previous stage.
87 /// The new stage.
89 /// The session.
91 /// The name of the calling function.
92 const char *m_func;
93 /// The filename of the caller.
94 const char *m_file;
95 /// The Line number of the caller.
96 const unsigned int m_line;
97};
98
99// NOLINTBEGIN(cppcoreguidelines-macro-usage)
100
101/// Set the thread stage for the given thread, and make it restore the
102/// previous stage at the end of the invoking scope, using the named
103/// local RAII variable.
104///
105/// @param name A variable name. The macro will define a variable of
106/// type Thread_stage_guard with this name in the current scope where
107/// this macro is invoked.
108///
109/// @param thd The thread for which the stage should be set.
110///
111/// @param new_stage The new stage. `thd` will use this stage until
112/// the end of the scope where the macro is invoked. At that point,
113/// the stage is reverted to what it was before invoking this macro.
114#define NAMED_THD_STAGE_GUARD(name, thd, new_stage) \
115 raii::Thread_stage_guard name { \
116 (thd), (new_stage), __func__, __FILE__, __LINE__ \
117 }
118
119/// Set the thread stage for the given thread, and make it restore the
120/// previous stage at the end of the invoking scope.
121///
122/// @param thd The thread for which the stage should be set.
123///
124/// @param new_stage The new stage. `thd` will use this stage until
125/// the end of the scope where the macro is invoked. At that point,
126/// the stage is reverted to what it was before invoking this macro.
127#define THD_STAGE_GUARD(thd, new_stage) \
128 NAMED_THD_STAGE_GUARD(_thread_stage_guard_##new_stage, (thd), (new_stage))
129
130// NOLINTEND(cppcoreguidelines-macro-usage)
131
132} // namespace raii
133
134#endif /// THREAD_STAGE_GUARD_H_INCLUDED
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
void enter_stage(const PSI_stage_info *stage, PSI_stage_info *old_stage, const char *calling_func, const char *calling_file, const unsigned int calling_line) SUPPRESS_TSAN
Definition: sql_class.cc:542
RAII guard that sets a thread stage, and restores the previous stage when going out of scope.
Definition: thread_stage_guard.h:34
Thread_stage_guard & operator=(const Thread_stage_guard &)=delete
const char * m_func
The name of the calling function.
Definition: thread_stage_guard.h:92
THD * m_thd
The session.
Definition: thread_stage_guard.h:90
Thread_stage_guard(Thread_stage_guard &&)=delete
Thread_stage_guard(const Thread_stage_guard &)=delete
PSI_stage_info m_old_stage
The previous stage.
Definition: thread_stage_guard.h:86
const unsigned int m_line
The Line number of the caller.
Definition: thread_stage_guard.h:96
void set_old_stage() const
Revert back to the old stage before this object goes out of scope.
Definition: thread_stage_guard.h:67
~Thread_stage_guard()
Revert the old stage that was used before this object's constructor was invoked.
Definition: thread_stage_guard.h:82
PSI_stage_info m_new_stage
The new stage.
Definition: thread_stage_guard.h:88
void set_new_stage() const
Restore the new stage, in case set_old_stage was used earlier.
Definition: thread_stage_guard.h:72
const char * m_file
The filename of the caller.
Definition: thread_stage_guard.h:94
Thread_stage_guard & operator=(Thread_stage_guard &&)=delete
Thread_stage_guard(THD *thd, const PSI_stage_info &new_stage, const char *func, const char *file, const unsigned int line)
Set the given stage for the session, and remember the previous stage in a member variable.
Definition: thread_stage_guard.h:50
Definition: os0file.h:89
Definition: sentry.h:29
Performance schema instrumentation interface.
Stage instrument information.
Definition: psi_stage_bits.h:74