MySQL 26.7.0
Source Code Documentation
session.h
Go to the documentation of this file.
1// Copyright (c) 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
24#ifndef MYSQL_CSA_SESSION_H
25#define MYSQL_CSA_SESSION_H
26
28#include "sql/sql_class.h"
29
30namespace mysql::csa {
31
32/// Wraps up THD object and provides simple function to manage its execution
33/// within CSA
34class Session {
35 public:
36 /// Construct from configuration in parent_thd
37 /// @param parent_thd Configuration THD
38 /// @param seq_num Session sequence number
39 Session(THD *parent_thd, std::size_t seq_num);
40 /// Destructor
41 virtual ~Session();
42 /// Checks if created session is valid
43 /// True when session is valid, false otherwise
44 bool is_valid() const;
45 /// Obtains handled THD identifier
46 /// @return THD identifier
47 unsigned int get_thd_id() const;
48 /// Obtains THD handle
49 THD *get_thd();
50 /// Obtains thread identifier
52 /// Awakes the handled session
53 /// @param force_kill If true, kills session
54 void awake(bool force_kill);
55 /// Attach current thread to session. Must succeed.
56 /// @param temporary_tables Temporary tables we attach
57 void attach(TABLE *temporary_tables);
58 /// Detach current thread from session
59 void detach();
60 /// Check if CSA killed this session
61 /// @return True only if this session was killed by CSA; false otherwise
62 bool self_killed() const { return m_is_killed; }
63 /// @return True only if this session was killed; false otherwise
64 bool is_killed() const { return m_thd->is_killed() || self_killed(); }
65
66 protected:
67 /// The base server session associated with this processor.
69 /// PSI thread id
71 /// Saved THD stack
72 const char *m_saved_thread_stack{""};
73 /// Saved stack pointer
74 long m_stack_ptr{0};
75 /// Validity flag
76 bool m_is_valid{false};
77 /// Attach flag
78 bool m_is_attached{false};
79 /// We save our own kill flag and protect modifications with LOCK_thd_data
80 bool m_is_killed{false};
81 /// Session lock for protecting attach/detach operations. While LOCK_thd_data
82 /// could be used, in a large session pool we avoid its potential syscalls
83 /// by using a spinlock instead.
85};
86
87} // namespace mysql::csa
88
89#endif // MYSQL_CSA_SESSION_H
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
int is_killed() const final
Has the owner thread been killed?
Definition: sql_class.h:3137
Implementation of a spin-lock mutex.
Definition: spin_lock_mutex.h:59
Wraps up THD object and provides simple function to manage its execution within CSA.
Definition: session.h:34
void awake(bool force_kill)
Awakes the handled session.
Definition: session.cpp:122
bool self_killed() const
Check if CSA killed this session.
Definition: session.h:62
const char * m_saved_thread_stack
Saved THD stack.
Definition: session.h:72
bool is_valid() const
Checks if created session is valid True when session is valid, false otherwise.
Definition: session.cpp:84
bool m_is_killed
We save our own kill flag and protect modifications with LOCK_thd_data.
Definition: session.h:80
THD * get_thd()
Obtains THD handle.
Definition: session.cpp:88
void detach()
Detach current thread from session.
Definition: session.cpp:108
bool m_is_attached
Attach flag.
Definition: session.h:78
Session(THD *parent_thd, std::size_t seq_num)
Construct from configuration in parent_thd.
Definition: session.cpp:37
virtual ~Session()
Destructor.
Definition: session.cpp:71
long m_stack_ptr
Saved stack pointer.
Definition: session.h:74
bool m_is_valid
Validity flag.
Definition: session.h:76
concurrency::Spin_lock_mutex m_session_lock
Session lock for protecting attach/detach operations.
Definition: session.h:84
THD * m_thd
The base server session associated with this processor.
Definition: session.h:68
bool is_killed() const
Definition: session.h:64
unsigned int get_thd_id() const
Obtains handled THD identifier.
Definition: session.cpp:86
void attach(TABLE *temporary_tables)
Attach current thread to session.
Definition: session.cpp:93
ulonglong get_thread_id() const
Obtains thread identifier.
Definition: session.cpp:69
ulonglong m_psi_id
PSI thread id.
Definition: session.h:70
unsigned long long int ulonglong
Definition: my_inttypes.h:56
Definition: channel.cpp:28
Definition: table.h:1456