MySQL 8.0.39
Source Code Documentation
registry_imp.h
Go to the documentation of this file.
1/* Copyright (c) 2016, 2024, Oracle and/or its affiliates.
2
3This program is free software; you can redistribute it and/or modify
4it under the terms of the GNU General Public License, version 2.0,
5as published by the Free Software Foundation.
6
7This program is designed to work with certain software (including
8but not limited to OpenSSL) that is licensed under separate terms,
9as designated in a particular file or component or in included license
10documentation. The authors of MySQL hereby grant you an additional
11permission to link the program and your derivative works with the
12separately licensed software that they have either included with
13the program or referenced in the documentation.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License, version 2.0, for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef MYSQL_SERVER_REGISTRY_H
25#define MYSQL_SERVER_REGISTRY_H
26
30#include <map>
31#include <memory>
32
33#include "c_string_less.h"
35#include "rwlock_scoped_lock.h"
36
37typedef std::map<const char *, mysql_service_implementation *, c_string_less>
39
41 typedef std::map<my_h_service, mysql_service_implementation *>
43
44 /* contain the actual fields definitions */
48
49 public:
50 /**
51 Initializes registry for usage. Initializes RW lock, all other structures
52 should be empty. Shouldn't be called multiple times.
53 */
54 static void init();
55 /**
56 De-initializes registry. De-initializes RW lock, all other structures
57 are cleaned up.
58 */
59 static void deinit();
60
61 /** De-initializes RW lock */
62 static void rw_lock_deinit();
63
64 /**
65 Locks whole registry for write. For internal use only.
66
67 @return A lock acquired wrapped into RAII object.
68 */
70
71 /**
72 Gets current reference count for a Service Implementation related to the
73 specified pointer to the interface structure. Assumes caller has at least
74 a read lock on the Registry.
75
76 @param interface A pointer to the interface structure of the Service
77 Implementation to get reference count of.
78 @return A current reference count for specified Service Implementation.
79 Returns 0 in case there is no such interface or it is not referenced.
80 */
82 my_h_service interface);
83
84 /**
85 Finds and acquires a Service by name. A name of the Service or the Service
86 Implementation can be specified. In case of the Service name, the default
87 Service Implementation for Service specified will be returned. Assumes
88 caller has at least a read lock on the Registry.
89
90 @param service_name Name of Service or Service Implementation to acquire.
91 @param [out] out_service Pointer to Service handle to set acquired Service.
92 @return Status of performed operation
93 @retval false success
94 @retval true failure
95 */
96 static bool acquire_nolock(const char *service_name,
97 my_h_service *out_service);
98
99 /**
100 Releases the Service Implementation previously acquired. After the call to
101 this method the usage of the Service Implementation handle will lead to
102 unpredicted results. Assumes caller has at least a read lock on the
103 Registry.
104
105 @param service Service Implementation handle of already acquired Service.
106 @return Status of performed operation
107 @retval false success
108 @retval true failure
109 */
110 static bool release_nolock(my_h_service service);
111
112 /**
113 Registers a new Service Implementation. If it is the first Service
114 Implementation for the specified Service then it is made a default one.
115 Assumes caller has a write lock on the Registry.
116
117 @param service_implementation_name Name of the Service Implementation to
118 register.
119 @param ptr Pointer to the Service Implementation structure.
120 @return Status of performed operation
121 @retval false success
122 @retval true failure
123 */
124 static bool register_service_nolock(const char *service_implementation_name,
125 my_h_service ptr);
126
127 /**
128 Removes previously registered Service Implementation from registry. If it is
129 the default one for specified Service then any one still registered is made
130 default. If there is no other, the default entry is removed from the
131 Registry too. Assumes caller has a write lock on the Registry.
132
133 @param service_implementation_name Name of the Service Implementation to
134 unregister.
135 @return Status of performed operation
136 @retval false success
137 @retval true Failure. May happen when Service is still being referenced.
138 */
139 static bool unregister_nolock(const char *service_implementation_name);
140
141 public: /* Service Implementations */
142 /**
143 Finds and acquires a Service by name. A name of the Service or the Service
144 Implementation can be specified. In case of the Service name, the default
145 Service Implementation for Service specified will be returned.
146
147 @param service_name Name of Service or Service Implementation to acquire.
148 @param [out] out_service Pointer to Service handle to set acquired Service.
149 @return Status of performed operation
150 @retval false success
151 @retval true failure
152 */
153 static DEFINE_BOOL_METHOD(acquire, (const char *service_name,
154 my_h_service *out_service));
155
156 /**
157 Finds a Service by name. If there is a Service Implementation with the same
158 Component part of name as the input Service then the found Service is
159 returned.
160
161 @param service_name Name of Service or Service Implementation to acquire.
162 @param service Service handle already acquired Service Implementation.
163 @param [out] out_service Pointer to Service Implementation handle to set
164 acquired Service Implementation.
165 @return Status of performed operation
166 @retval false success
167 @retval true failure
168 */
170 (const char *service_name, my_h_service service,
171 my_h_service *out_service));
172
173 /**
174 Releases the Service Implementation previously acquired. After the call to
175 this method the usage of the Service Implementation handle will lead to
176 unpredicted results.
177
178 @param service Service Implementation handle of already acquired Service.
179 @return Status of performed operation
180 @retval false success
181 @retval true failure
182 */
183 static DEFINE_BOOL_METHOD(release, (my_h_service service));
184
185 /**
186 Registers a new Service Implementation. If it is the first Service
187 Implementation for the specified Service then it is made a default one.
188
189 @param service_implementation_name Name of the Service Implementation to
190 register.
191 @param ptr Pointer to the Service Implementation structure.
192 @return Status of performed operation
193 @retval false success
194 @retval true failure
195 */
197 (const char *service_implementation_name,
198 my_h_service ptr));
199
200 /**
201 Removes previously registered Service Implementation from registry. If it is
202 the default one for specified Service then any one still registered is made
203 default. If there is no other, the default entry is removed from the
204 Registry too.
205
206 @param service_implementation_name Name of the Service Implementation to
207 unregister.
208 @return Status of performed operation
209 @retval false success
210 @retval true Failure. May happen when Service is still being referenced.
211 */
213 (const char *service_implementation_name));
214
215 /**
216 Sets new default Service Implementation for corresponding Service name.
217
218 @param service_implementation_name Name of the Service Implementation to
219 set as default one.
220 @return Status of performed operation
221 @retval false success
222 @retval true failure
223 */
225 (const char *service_implementation_name));
226
227 /**
228 Creates iterator that iterates through all registered Service
229 Implementations. If successful it leaves read lock on the Registry until
230 iterator is released. The starting point of iteration may be specified
231 to be on one particular Service Implementation. The iterator will move
232 through all Service Implementations and additionally through all default
233 Service Implementation additionally, i.e. the default Service Implementation
234 will be returned twice. If no name is specified for search, iterator will be
235 positioned on the first Service Implementation.
236
237 @param service_name_pattern Name of Service or Service Implementation to
238 start iteration from. May be empty string or NULL pointer, in which case
239 iteration starts from the first Service Implementation.
240 @param [out] out_iterator Pointer to the Service Implementation iterator
241 handle.
242 @return Status of performed operation
243 @retval false success
244 @retval true failure
245 */
247 (const char *service_name_pattern,
248 my_h_service_iterator *out_iterator));
249
250 /**
251 Releases Service implementations iterator. Releases read lock on registry.
252
253 @param iterator Service Implementation iterator handle.
254 */
255 static DEFINE_METHOD(void, iterator_release,
256 (my_h_service_iterator iterator));
257
258 /**
259 Gets name of Service pointed to by iterator. The pointer returned will last
260 at least up to the moment of call to the release() method on the iterator.
261
262 @param iterator Service Implementation iterator handle.
263 @param [out] out_name Pointer to string with name to set result pointer to.
264 @return Status of performed operation
265 @retval false success
266 @retval true Failure, may be caused when called on iterator that went
267 through all values already.
268 */
270 const char **out_name));
271
272 /**
273 Advances specified iterator to next element. Will succeed but return true if
274 it reaches one-past-last element.
275
276 @param iterator Service Implementation iterator handle.
277 @return Status of performed operation and validity of iterator after
278 operation.
279 @retval false success
280 @retval true Failure or called on iterator that was on last element.
281 */
283
284 /**
285 Checks if specified iterator is valid, i.e. have not reached one-past-last
286 element.
287
288 @param iterator Service Implementation iterator handle.
289 @return Validity of iterator
290 @retval false Valid
291 @retval true Invalid or reached one-past-last element.
292 */
294 (my_h_service_iterator iterator));
295
296 /* This includes metadata-related method implementations that are shared
297 by registry and dynamic_loader, so we don't duplicate the code. Following
298 defines set up all required symbols. Unfortunately they are not only the
299 types, but also static members with different name, so usage of templates
300 is not enough to reuse that part of code. */
301#define OBJECT_ITERATOR my_h_service_iterator
302#define METADATA_ITERATOR my_h_service_metadata_iterator
303
305
306 private:
307 /**
308 Finds a Service Implementation data structure based on the pointer to
309 interface struct supplied. Assumes caller has at least a read lock on the
310 Registry.
311
312 @param interface A pointer to the interface structure of the Service
313 Implementation to look for.
314 @return A pointer to respective Service Implementation data structure, or
315 NULL if no such interface pointer is registered within the Registry.
316 */
318 my_h_service interface);
319};
320
321#endif /* MYSQL_SERVER_REGISTRY_H */
Locks RW-lock and releases lock on scope exit.
Definition: rwlock_scoped_lock.h:33
Definition: registry_imp.h:40
static void init()
Initializes registry for usage.
Definition: registry.cc:84
static mysql_service_status_t register_service(const char *service_implementation_name, my_h_service ptr) noexcept
Registers a new Service Implementation.
Definition: registry.cc:448
static mysql_rwlock_t LOCK_registry
Definition: registry_imp.h:47
static mysql_service_status_t iterator_get(my_h_service_iterator iterator, const char **out_name) noexcept
Gets name of Service pointed to by iterator.
Definition: registry.cc:589
static mysql_service_status_t iterator_is_valid(my_h_service_iterator iterator) noexcept
Checks if specified iterator is valid, i.e.
Definition: registry.cc:645
static my_interface_mapping interface_mapping
Definition: registry_imp.h:46
std::map< my_h_service, mysql_service_implementation * > my_interface_mapping
Definition: registry_imp.h:42
static mysql_service_status_t acquire(const char *service_name, my_h_service *out_service) noexcept
Finds and acquires a Service by name.
Definition: registry.cc:362
static bool release_nolock(my_h_service service)
Releases the Service Implementation previously acquired.
Definition: registry.cc:205
static mysql_service_status_t set_default(const char *service_implementation_name) noexcept
Sets new default Service Implementation for corresponding Service name.
Definition: registry.cc:486
static uint64_t get_service_implementation_reference_count(my_h_service interface)
Gets current reference count for a Service Implementation related to the specified pointer to the int...
Definition: registry.cc:150
static minimal_chassis::rwlock_scoped_lock lock_registry_for_write()
Locks whole registry for write.
Definition: registry.cc:113
static mysql_service_implementation * get_service_implementation_by_interface(my_h_service interface)
Finds a Service Implementation data structure based on the pointer to interface struct supplied.
Definition: registry.cc:129
static my_service_registry service_registry
Definition: registry_imp.h:45
static bool register_service_nolock(const char *service_implementation_name, my_h_service ptr)
Registers a new Service Implementation.
Definition: registry.cc:234
static void deinit()
De-initializes registry.
Definition: registry.cc:93
static mysql_service_status_t acquire_related(const char *service_name, my_h_service service, my_h_service *out_service) noexcept
Finds a Service by name.
Definition: registry.cc:384
static mysql_service_status_t unregister(const char *service_implementation_name) noexcept
Removes previously registered Service Implementation from registry.
Definition: registry.cc:469
static mysql_service_status_t iterator_next(my_h_service_iterator iterator) noexcept
Advances specified iterator to next element.
Definition: registry.cc:619
static void iterator_release(my_h_service_iterator iterator) noexcept
Releases Service implementations iterator.
Definition: registry.cc:565
static mysql_service_status_t release(my_h_service service) noexcept
Releases the Service Implementation previously acquired.
Definition: registry.cc:428
static void rw_lock_deinit()
De-initializes RW lock.
Definition: registry.cc:103
static mysql_service_status_t iterator_create(const char *service_name_pattern, my_h_service_iterator *out_iterator) noexcept
Creates iterator that iterates through all registered Service Implementations.
Definition: registry.cc:535
static bool acquire_nolock(const char *service_name, my_h_service *out_service)
Finds and acquires a Service by name.
Definition: registry.cc:173
static bool unregister_nolock(const char *service_implementation_name)
Removes previously registered Service Implementation from registry.
Definition: registry.cc:292
a Service implementation registry data
Definition: mysql_service_implementation.h:32
std::map< const char *, mysql_service_implementation *, c_string_less > my_service_registry
Definition: registry.cc:56
struct my_h_service_imp * my_h_service
A handle type for acquired Service.
Definition: registry.h:33
std::map< const char *, mysql_service_implementation *, c_string_less > my_service_registry
Definition: registry_imp.h:38
Specifies macros to define Service Implementations.
#define DEFINE_BOOL_METHOD(name, args)
A short macro to define method that returns bool, which is the most common case.
Definition: service_implementation.h:88
#define DEFINE_METHOD(retval, name, args)
A macro to ensure method implementation has required properties, that is it does not throw exceptions...
Definition: service_implementation.h:79
Definition: registry.cc:58
An instrumented rwlock structure.
Definition: mysql_rwlock_bits.h:51