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