MySQL 26.7.0
Source Code Documentation
thd_backup_and_restore.h
Go to the documentation of this file.
1/* Copyright (c) 2025, 2026, 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#ifndef THD_BACKUP_AND_RESTORE_INCLUDED
24#define THD_BACKUP_AND_RESTORE_INCLUDED
25
27#include "sql/sql_class.h"
28
29/**
30 Helper class to switch to a new thread and then go back to the previous one,
31 when the object is destroyed using RAII.
32
33 This class is used to temporarily switch to another session (THD
34 structure). It will set up thread specific "globals" correctly
35 so that the POSIX thread looks exactly like the session attached to.
36 However, PSI_thread info is not touched as it is required to show
37 the actual physical view in PFS instrumentation i.e., it should
38 depict as the real thread doing the work instead of thread it switched
39 to.
40
41 On destruction, the original session (which is supplied to the
42 constructor) will be re-attached automatically. For example, with
43 this code, the value of @c current_thd will be the same before and
44 after execution of the code.
45
46 @code
47 {
48 for (int i = 0 ; i < count ; ++i)
49 {
50 // here we are attached to current_thd
51 // [...]
52 Thd_backup_and_restore switch_thd(current_thd, other_thd[i]);
53 // [...]
54 // here we are attached to other_thd[i]
55 // [...]
56 }
57 // here we are attached to current_thd
58 }
59 @endcode
60
61 @warning The class is not designed to be inherited from.
62 */
63
65 public:
66 /**
67 Try to attach the POSIX thread to a session.
68
69 @param[in] backup_thd The thd to restore to when object is destructed.
70 @param[in] new_thd The thd to attach to.
71 */
72 Thd_backup_and_restore(THD *backup_thd, THD *new_thd);
73
74 /**
75 Restores to previous thd.
76 */
78
79 private:
86};
87
88#endif /* THD_BACKUP_AND_RESTORE_INCLUDED */
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Helper class to switch to a new thread and then go back to the previous one, when the object is destr...
Definition: thd_backup_and_restore.h:64
Thd_backup_and_restore(THD *backup_thd, THD *new_thd)
Try to attach the POSIX thread to a session.
Definition: thd_backup_and_restore.cc:26
THD * m_backup_thd
Definition: thd_backup_and_restore.h:80
my_thread_t m_new_thd_old_real_id
Definition: thd_backup_and_restore.h:84
THD * m_dummy_cnt_thd
Definition: thd_backup_and_restore.h:83
THD * m_new_thd
Definition: thd_backup_and_restore.h:81
~Thd_backup_and_restore()
Restores to previous thd.
Definition: thd_backup_and_restore.cc:45
THD * m_backup_cnt_thd
Definition: thd_backup_and_restore.h:82
const char * m_new_thd_old_thread_stack
Definition: thd_backup_and_restore.h:85
Types to make different thread packages compatible.
pthread_t my_thread_t
Definition: my_thread_bits.h:48