MySQL 26.10.0
Source Code Documentation
dim.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2016, 2026, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef MYSQL_HARNESS_DIMANAGER_INCLUDED
27#define MYSQL_HARNESS_DIMANAGER_INCLUDED
28
29#include "harness_export.h"
30
31#include "unique_ptr.h"
32
33#include <functional>
34#include <mutex>
35#include <shared_mutex>
36#include <string>
37
38namespace mysql_harness {
39
40// forward declarations
41class RandomGeneratorInterface;
42namespace logging {
43class Registry;
44} // namespace logging
45class LoaderConfig;
46class DynamicState;
48
49class HARNESS_EXPORT DIM { // DIM = Dependency Injection Manager
50
51 // this class is a singleton
52 protected:
53 DIM();
55
56 public:
57 DIM(const DIM &) = delete;
58 DIM &operator=(const DIM &) = delete;
59 static DIM &instance();
60
61 // Logging Registry
62 //
64 logging_registry_.set_static(instance);
65 }
66
69 const std::function<void(mysql_harness::logging::Registry *)> &deleter) {
70 logging_registry_.set(instance, deleter);
71 }
72
73 bool has_LoggingRegistry() const {
74 return static_cast<bool>(logging_registry_);
75 }
76
78 return logging_registry_.get();
79 }
80
81 // RandomGenerator
84 random_generator_.set_static(inst);
85 }
86
89 const std::function<void(mysql_harness::RandomGeneratorInterface *)>
90 &deleter) {
91 random_generator_.set(inst, deleter);
92 }
93
95 return random_generator_.get();
96 }
97
98 // LoaderConfig
99
102 const std::function<void(mysql_harness::LoaderConfig *)> &deleter) {
103 loader_config_.set(instance, deleter);
104 }
105
106 bool has_Config() const { return static_cast<bool>(loader_config_); }
107
109 return loader_config_.get();
110 }
111
112 // DynamicState
113
116 const std::function<void(mysql_harness::DynamicState *)> &deleter) {
117 dynamic_state_.set(instance, deleter);
118 }
119
120 bool is_DynamicState() const { return static_cast<bool>(dynamic_state_); }
121
123 return dynamic_state_.get();
124 }
125
126 void set_static_SyncPointRegistry(mysql_harness::SyncPointRegistry *inst);
127
128#if !defined(NDEBUG)
130 return static_cast<bool>(sync_point_registry_);
131 }
132
134 return sync_point_registry_.get();
135 }
136#endif
137
138 private:
139 template <class T>
141 public:
142 using value_type = T;
143
144 void set_static(value_type *inst) {
145 std::unique_lock lk(mtx_);
146
147 inst_ = {inst, [](value_type *) {}};
148 }
149
150 void set(value_type *inst, std::function<void(value_type *)> deleter) {
151 std::unique_lock lk(mtx_);
152
153 inst_ = {inst, deleter};
154 }
155
156 value_type &get() const {
157 std::shared_lock lk(mtx_);
158
159 return *inst_;
160 }
161
162 void reset() {
163 std::unique_lock lk(mtx_);
164
165 inst_.reset();
166 }
167
168 explicit operator bool() const {
169 std::shared_lock lk(mtx_);
170
171 return static_cast<bool>(inst_);
172 }
173
174 private:
176
177 mutable std::shared_mutex mtx_;
178 };
179
181
183
185
187#if !defined(NDEBUG)
189#endif
190
191}; // class DIM
192
193} // namespace mysql_harness
194#endif // #ifndef MYSQL_HARNESS_DIMANAGER_INCLUDED
ConstSectionList get(const std::string &section) const
Get a list of sections having a name.
Definition: config_parser.cc:237
value_type & get() const
Definition: dim.h:156
UniquePtr< value_type > inst_
Definition: dim.h:175
void set(value_type *inst, std::function< void(value_type *)> deleter)
Definition: dim.h:150
void reset()
Definition: dim.h:162
std::shared_mutex mtx_
Definition: dim.h:177
void set_static(value_type *inst)
Definition: dim.h:144
T value_type
Definition: dim.h:142
Definition: dim.h:49
bool has_SyncPointRegistry() const
Definition: dim.h:129
void set_LoggingRegistry(mysql_harness::logging::Registry *instance, const std::function< void(mysql_harness::logging::Registry *)> &deleter)
Definition: dim.h:67
mysql_harness::RandomGeneratorInterface & get_RandomGenerator() const
Definition: dim.h:94
void set_DynamicState(mysql_harness::DynamicState *instance, const std::function< void(mysql_harness::DynamicState *)> &deleter)
Definition: dim.h:114
RWLockedUniquePtr< mysql_harness::logging::Registry > logging_registry_
Definition: dim.h:180
mysql_harness::DynamicState & get_DynamicState() const
Definition: dim.h:122
void set_static_LoggingRegistry(mysql_harness::logging::Registry *instance)
Definition: dim.h:63
DIM(const DIM &)=delete
RWLockedUniquePtr< mysql_harness::RandomGeneratorInterface > random_generator_
Definition: dim.h:182
bool has_Config() const
Definition: dim.h:106
void set_Config(mysql_harness::LoaderConfig *instance, const std::function< void(mysql_harness::LoaderConfig *)> &deleter)
Definition: dim.h:100
mysql_harness::logging::Registry & get_LoggingRegistry() const
Definition: dim.h:77
mysql_harness::SyncPointRegistry & get_SyncPointRegistry() const
Definition: dim.h:133
void set_static_RandomGenerator(mysql_harness::RandomGeneratorInterface *inst)
Definition: dim.h:82
RWLockedUniquePtr< mysql_harness::LoaderConfig > loader_config_
Definition: dim.h:184
RWLockedUniquePtr< mysql_harness::DynamicState > dynamic_state_
Definition: dim.h:186
mysql_harness::LoaderConfig & get_Config() const
Definition: dim.h:108
bool is_DynamicState() const
Definition: dim.h:120
DIM & operator=(const DIM &)=delete
RWLockedUniquePtr< mysql_harness::SyncPointRegistry > sync_point_registry_
Definition: dim.h:188
void set_RandomGenerator(mysql_harness::RandomGeneratorInterface *inst, const std::function< void(mysql_harness::RandomGeneratorInterface *)> &deleter)
Definition: dim.h:87
bool has_LoggingRegistry() const
Definition: dim.h:73
DynamicState represents a MySQLRouter dynamic state object.
Definition: dynamic_state.h:64
Configuration file handler for the loader.
Definition: loader_config.h:46
Definition: random_generator.h:36
Definition: sync_point.h:42
virtual std::shared_ptr< SyncPoint > get(const std::string &name)=0
Definition: unique_ptr.h:75
Definition: registry.h:48
#define T
Definition: jit_executor_value.cc:373
Singleton class that stores the application type.
Definition: component_application_type_name.h:31