MySQL 8.4.13
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
74 return logging_registry_.get();
75 }
76
77 // RandomGenerator
80 random_generator_.set_static(inst);
81 }
82
85 const std::function<void(mysql_harness::RandomGeneratorInterface *)>
86 &deleter) {
87 random_generator_.set(inst, deleter);
88 }
89
91 return random_generator_.get();
92 }
93
94 // LoaderConfig
95
98 const std::function<void(mysql_harness::LoaderConfig *)> &deleter) {
99 loader_config_.set(instance, deleter);
100 }
101
102 bool has_Config() const { return static_cast<bool>(loader_config_); }
103
105 return loader_config_.get();
106 }
107
108 // DynamicState
109
112 const std::function<void(mysql_harness::DynamicState *)> &deleter) {
113 dynamic_state_.set(instance, deleter);
114 }
115
116 bool is_DynamicState() const { return static_cast<bool>(dynamic_state_); }
117
119 return dynamic_state_.get();
120 }
121
123 [[maybe_unused]]) {
124#if !defined(NDEBUG)
125 std::unique_lock lk(sync_point_registry_mtx_);
126 sync_point_registry_ = inst;
127#endif
128 }
129
130#if !defined(NDEBUG)
132 std::shared_lock lk(sync_point_registry_mtx_);
133 return sync_point_registry_ != nullptr;
134 }
135
137 std::shared_lock lk(sync_point_registry_mtx_);
138 return *sync_point_registry_;
139 }
140#endif
141
142 private:
143 template <class T>
145 public:
146 using value_type = T;
147
148 void set_static(value_type *inst) {
149 std::unique_lock lk(mtx_);
150
151 inst_ = {inst, [](value_type *) {}};
152 }
153
154 void set(value_type *inst, std::function<void(value_type *)> deleter) {
155 std::unique_lock lk(mtx_);
156
157 inst_ = {inst, deleter};
158 }
159
160 value_type &get() const {
161 std::shared_lock lk(mtx_);
162
163 return *inst_;
164 }
165
166 void reset() {
167 std::unique_lock lk(mtx_);
168
169 inst_.reset();
170 }
171
172 explicit operator bool() const {
173 std::shared_lock lk(mtx_);
174
175 return static_cast<bool>(inst_);
176 }
177
178 private:
180
181 mutable std::shared_mutex mtx_;
182 };
183
185
187
189
191#if !defined(NDEBUG)
192 mysql_harness::SyncPointRegistry *sync_point_registry_{nullptr};
193 mutable std::shared_mutex sync_point_registry_mtx_;
194#endif
195
196}; // class DIM
197
198} // namespace mysql_harness
199#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:160
UniquePtr< value_type > inst_
Definition: dim.h:179
void set(value_type *inst, std::function< void(value_type *)> deleter)
Definition: dim.h:154
void reset()
Definition: dim.h:166
std::shared_mutex mtx_
Definition: dim.h:181
void set_static(value_type *inst)
Definition: dim.h:148
T value_type
Definition: dim.h:146
Definition: dim.h:49
bool has_SyncPointRegistry() const
Definition: dim.h:131
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:90
void set_DynamicState(mysql_harness::DynamicState *instance, const std::function< void(mysql_harness::DynamicState *)> &deleter)
Definition: dim.h:110
RWLockedUniquePtr< mysql_harness::logging::Registry > logging_registry_
Definition: dim.h:184
mysql_harness::DynamicState & get_DynamicState() const
Definition: dim.h:118
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:186
bool has_Config() const
Definition: dim.h:102
void set_Config(mysql_harness::LoaderConfig *instance, const std::function< void(mysql_harness::LoaderConfig *)> &deleter)
Definition: dim.h:96
mysql_harness::logging::Registry & get_LoggingRegistry() const
Definition: dim.h:73
mysql_harness::SyncPointRegistry & get_SyncPointRegistry() const
Definition: dim.h:136
void set_static_RandomGenerator(mysql_harness::RandomGeneratorInterface *inst)
Definition: dim.h:78
RWLockedUniquePtr< mysql_harness::LoaderConfig > loader_config_
Definition: dim.h:188
RWLockedUniquePtr< mysql_harness::DynamicState > dynamic_state_
Definition: dim.h:190
mysql_harness::LoaderConfig & get_Config() const
Definition: dim.h:104
bool is_DynamicState() const
Definition: dim.h:116
DIM & operator=(const DIM &)=delete
std::shared_mutex sync_point_registry_mtx_
Definition: dim.h:193
void set_static_SyncPointRegistry(mysql_harness::SyncPointRegistry *inst)
Definition: dim.h:122
void set_RandomGenerator(mysql_harness::RandomGeneratorInterface *inst, const std::function< void(mysql_harness::RandomGeneratorInterface *)> &deleter)
Definition: dim.h:83
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
Definition: unique_ptr.h:75
Definition: registry.h:47
Definition: common.h:42