MySQL 26.7.0
Source Code Documentation
module.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_MODULE_H
25#define MYSQL_CSA_MODULE_H
26
29#include "my_sys.h"
31
36
37namespace mysql::csa {
38
39/// @brief Represents a lockable module
40class Module : public Initable {
41 public:
42 /// @brief Enum defining possible states of the module
43 enum class State { off = 0, on, start, stop };
44
45 friend class Lock_guard_object<Module>;
47
48 /// @brief Constructs the module
49 /// @param mutex_key PSI mutex key
50 /// @param rwlock_key PSI rwlock key
51 Module(PSI_mutex_key mutex_key, PSI_rwlock_key rwlock_key);
52
53 /// @brief Destructor
54 virtual ~Module() override;
55
56 /// @brief Initializes internal locking
57 void init_locking();
58
59 /// @brief Initializes the module
60 /// @return True on error, false on success
61 bool init() override;
62
63 /// @brief Acquires a read lock on this object
64 /// @return Scope guard that unlocks at the end of the scope
65 [[nodiscard]] Lock_guard rdlock();
66
67 /// @brief Acquires a write lock on this object
68 /// @return Scope guard that unlocks at the end of the scope
69 [[nodiscard]] Lock_guard wrlock();
70
71 /// @brief Deinitializes the module
72 /// @return True on error, false on success
73 bool deinit() override;
74
75 /// @brief Checks if the module is initialized
76 /// @return True if initialized, false otherwise
77 bool is_inited();
78
79 protected:
80 /// @brief Unlocks this module
81 void unlock();
82
83 /// @brief Initialization hook for derived classes
84 /// @return True on error, false on success
85 virtual bool do_init();
86 /// @brief Deinitialization hook for derived classes
87 /// @return True on error, false on success
88 virtual bool do_deinit();
89
90 private:
91 /// @brief PSI key for the read-write lock
93 /// @brief Read-write lock for the module
95 /// @brief State of the module
96 std::atomic<State> m_inited{State::off};
97};
98
99} // namespace mysql::csa
100
101#endif
Interface for initable components.
Definition: initable.h:32
Scope guard to unlock an object.
Definition: lock_guard_object.h:44
Represents a lockable module.
Definition: module.h:40
void init_locking()
Initializes internal locking.
Definition: module.cpp:35
bool is_inited()
Checks if the module is initialized.
Definition: module.cpp:75
void unlock()
Unlocks this module.
Definition: module.cpp:80
virtual ~Module() override
Destructor.
Definition: module.cpp:33
mysql_rwlock_t m_rwlock
Read-write lock for the module.
Definition: module.h:94
bool deinit() override
Deinitializes the module.
Definition: module.cpp:63
State
Enum defining possible states of the module.
Definition: module.h:43
Lock_guard rdlock()
Acquires a read lock on this object.
Definition: module.cpp:53
PSI_rwlock_key m_rwlock_key
PSI key for the read-write lock.
Definition: module.h:92
virtual bool do_init()
Initialization hook for derived classes.
Definition: module.cpp:82
bool init() override
Initializes the module.
Definition: module.cpp:40
Lock_guard wrlock()
Acquires a write lock on this object.
Definition: module.cpp:58
Module(PSI_mutex_key mutex_key, PSI_rwlock_key rwlock_key)
Constructs the module.
Definition: module.cpp:28
std::atomic< State > m_inited
State of the module.
Definition: module.h:96
virtual bool do_deinit()
Deinitialization hook for derived classes.
Definition: module.cpp:84
unsigned int PSI_mutex_key
Instrumented mutex key.
Definition: psi_mutex_bits.h:52
unsigned int PSI_rwlock_key
Instrumented rwlock key.
Definition: psi_rwlock_bits.h:44
Common header for many mysys elements.
Definition: channel.cpp:28
Instrumentation helpers for mutexes.
Instrumentation helpers for rwlock.
An instrumented rwlock structure.
Definition: mysql_rwlock_bits.h:51