]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/interprocess/detail/intermodule_singleton_common.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / interprocess / detail / intermodule_singleton_common.hpp
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2009-2012. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // See http://www.boost.org/libs/interprocess for documentation.
8 //
9 //////////////////////////////////////////////////////////////////////////////
10
11 #ifndef BOOST_INTERPROCESS_INTERMODULE_SINGLETON_COMMON_HPP
12 #define BOOST_INTERPROCESS_INTERMODULE_SINGLETON_COMMON_HPP
13
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
16 #endif
17 #
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
19 #pragma once
20 #endif
21
22 #include <boost/interprocess/detail/config_begin.hpp>
23 #include <boost/interprocess/detail/workaround.hpp>
24
25 #include <boost/interprocess/detail/atomic.hpp>
26 #include <boost/interprocess/detail/os_thread_functions.hpp>
27 #include <boost/interprocess/exceptions.hpp>
28 #include <boost/container/detail/type_traits.hpp> //alignment_of, aligned_storage
29 #include <boost/interprocess/detail/mpl.hpp>
30 #include <boost/interprocess/sync/spin/wait.hpp>
31 #include <boost/assert.hpp>
32 #include <cstddef>
33 #include <cstdio>
34 #include <cstdlib>
35 #include <cstring>
36 #include <string>
37 #include <typeinfo>
38 #include <sstream>
39
40 namespace boost{
41 namespace interprocess{
42 namespace ipcdetail{
43
44 namespace intermodule_singleton_helpers {
45
46 inline void get_pid_creation_time_str(std::string &s)
47 {
48 std::stringstream stream;
49 stream << get_current_process_id() << '_';
50 stream.precision(6);
51 stream << std::fixed << get_current_process_creation_time();
52 s = stream.str();
53 }
54
55 inline const char *get_map_base_name()
56 { return "bip.gmem.map."; }
57
58 inline void get_map_name(std::string &map_name)
59 {
60 get_pid_creation_time_str(map_name);
61 map_name.insert(0, get_map_base_name());
62 }
63
64 inline std::size_t get_map_size()
65 { return 65536; }
66
67 template<class ThreadSafeGlobalMap>
68 struct thread_safe_global_map_dependant;
69
70 } //namespace intermodule_singleton_helpers {
71
72 //This class contains common code for all singleton types, so that we instantiate this
73 //code just once per module. This class also holds a thread soafe global map
74 //to be used by all instances protected with a reference count
75 template<class ThreadSafeGlobalMap>
76 class intermodule_singleton_common
77 {
78 public:
79 typedef void*(singleton_constructor_t)(ThreadSafeGlobalMap &);
80 typedef void (singleton_destructor_t)(void *, ThreadSafeGlobalMap &);
81
82 static const ::boost::uint32_t Uninitialized = 0u;
83 static const ::boost::uint32_t Initializing = 1u;
84 static const ::boost::uint32_t Initialized = 2u;
85 static const ::boost::uint32_t Broken = 3u;
86 static const ::boost::uint32_t Destroyed = 4u;
87
88 //Initialize this_module_singleton_ptr, creates the global map if needed and also creates an unique
89 //opaque type in global map through a singleton_constructor_t function call,
90 //initializing the passed pointer to that unique instance.
91 //
92 //We have two concurrency types here. a)the global map/singleton creation must
93 //be safe between threads of this process but in different modules/dlls. b)
94 //the pointer to the singleton is per-module, so we have to protect this
95 //initization between threads of the same module.
96 //
97 //All static variables declared here are shared between inside a module
98 //so atomic operations will synchronize only threads of the same module.
99 static void initialize_singleton_logic
100 (void *&ptr, volatile boost::uint32_t &this_module_singleton_initialized, singleton_constructor_t constructor, bool phoenix)
101 {
102 //If current module is not initialized enter to lock free logic
103 if(atomic_read32(&this_module_singleton_initialized) != Initialized){
104 //Now a single thread of the module will succeed in this CAS.
105 //trying to pass from Uninitialized to Initializing
106 ::boost::uint32_t previous_module_singleton_initialized = atomic_cas32
107 (&this_module_singleton_initialized, Initializing, Uninitialized);
108 //If the thread succeeded the CAS (winner) it will compete with other
109 //winner threads from other modules to create the global map
110 if(previous_module_singleton_initialized == Destroyed){
111 //Trying to resurrect a dead Phoenix singleton. Just try to
112 //mark it as uninitialized and start again
113 if(phoenix){
114 atomic_cas32(&this_module_singleton_initialized, Uninitialized, Destroyed);
115 previous_module_singleton_initialized = atomic_cas32
116 (&this_module_singleton_initialized, Initializing, Uninitialized);
117 }
118 //Trying to resurrect a non-Phoenix dead singleton is an error
119 else{
120 throw interprocess_exception("Boost.Interprocess: Dead reference on non-Phoenix singleton of type");
121 }
122 }
123 if(previous_module_singleton_initialized == Uninitialized){
124 BOOST_TRY{
125 //Now initialize the global map, this function must solve concurrency
126 //issues between threads of several modules
127 initialize_global_map_handle();
128 //Now try to create the singleton in global map.
129 //This function solves concurrency issues
130 //between threads of several modules
131 ThreadSafeGlobalMap *const pmap = get_map_ptr();
132 void *tmp = constructor(*pmap);
133 //Increment the module reference count that reflects how many
134 //singletons this module holds, so that we can safely destroy
135 //module global map object when no singleton is left
136 atomic_inc32(&this_module_singleton_count);
137 //Insert a barrier before assigning the pointer to
138 //make sure this assignment comes after the initialization
139 atomic_write32(&this_module_singleton_initialized, Initializing);
140 //Assign the singleton address to the module-local pointer
141 ptr = tmp;
142 //Memory barrier inserted, all previous operations should complete
143 //before this one. Now marked as initialized
144 atomic_write32(&this_module_singleton_initialized, Initialized);
145 }
146 BOOST_CATCH(...){
147 //Mark singleton failed to initialize
148 atomic_write32(&this_module_singleton_initialized, Broken);
149 BOOST_RETHROW
150 } BOOST_CATCH_END
151 }
152 //If previous state was initializing, this means that another winner thread is
153 //trying to initialize the singleton. Just wait until completes its work.
154 else if(previous_module_singleton_initialized == Initializing){
155 spin_wait swait;
156 while(1){
157 previous_module_singleton_initialized = atomic_read32(&this_module_singleton_initialized);
158 if(previous_module_singleton_initialized >= Initialized){
159 //Already initialized, or exception thrown by initializer thread
160 break;
161 }
162 else if(previous_module_singleton_initialized == Initializing){
163 swait.yield();
164 }
165 else{
166 //This can't be happening!
167 BOOST_ASSERT(0);
168 }
169 }
170 }
171 else if(previous_module_singleton_initialized == Initialized){
172 //Nothing to do here, the singleton is ready
173 }
174 //If previous state was greater than initialized, then memory is broken
175 //trying to initialize the singleton.
176 else{//(previous_module_singleton_initialized > Initialized)
177 throw interprocess_exception("boost::interprocess::intermodule_singleton initialization failed");
178 }
179 }
180 BOOST_ASSERT(ptr != 0);
181 }
182
183 static void finalize_singleton_logic(void *&ptr, volatile boost::uint32_t &this_module_singleton_initialized, singleton_destructor_t destructor)
184 {
185 //Protect destruction against lazy singletons not initialized in this execution
186 if(ptr){
187 //Note: this destructor might provoke a Phoenix singleton
188 //resurrection. This means that this_module_singleton_count
189 //might change after this call.
190 ThreadSafeGlobalMap * const pmap = get_map_ptr();
191 destructor(ptr, *pmap);
192 ptr = 0;
193
194 //Memory barrier to make sure pointer is nulled.
195 //Mark this singleton as destroyed.
196 atomic_write32(&this_module_singleton_initialized, Destroyed);
197
198 //If this is the last singleton of this module
199 //apply map destruction.
200 //Note: singletons are destroyed when the module is unloaded
201 //so no threads should be executing or holding references
202 //to this module
203 if(1 == atomic_dec32(&this_module_singleton_count)){
204 destroy_global_map_handle();
205 }
206 }
207 }
208
209 private:
210 static ThreadSafeGlobalMap *get_map_ptr()
211 {
212 return static_cast<ThreadSafeGlobalMap *>(static_cast<void*>(mem_holder.map_mem));
213 }
214
215 static void initialize_global_map_handle()
216 {
217 //Obtain unique map name and size
218 spin_wait swait;
219 while(1){
220 //Try to pass map state to initializing
221 ::boost::uint32_t tmp = atomic_cas32(&this_module_map_initialized, Initializing, Uninitialized);
222 if(tmp == Initialized || tmp == Broken){
223 break;
224 }
225 else if(tmp == Destroyed){
226 tmp = atomic_cas32(&this_module_map_initialized, Uninitialized, Destroyed);
227 continue;
228 }
229 //If some other thread is doing the work wait
230 else if(tmp == Initializing){
231 swait.yield();
232 }
233 else{ //(tmp == Uninitialized)
234 //If not initialized try it again?
235 BOOST_TRY{
236 //Remove old global map from the system
237 intermodule_singleton_helpers::thread_safe_global_map_dependant<ThreadSafeGlobalMap>::remove_old_gmem();
238 //in-place construction of the global map class
239 ThreadSafeGlobalMap * const pmap = get_map_ptr();
240 intermodule_singleton_helpers::thread_safe_global_map_dependant
241 <ThreadSafeGlobalMap>::construct_map(static_cast<void*>(pmap));
242 //Use global map's internal lock to initialize the lock file
243 //that will mark this gmem as "in use".
244 typename intermodule_singleton_helpers::thread_safe_global_map_dependant<ThreadSafeGlobalMap>::
245 lock_file_logic f(*pmap);
246 //If function failed (maybe a competing process has erased the shared
247 //memory between creation and file locking), retry with a new instance.
248 if(f.retry()){
249 pmap->~ThreadSafeGlobalMap();
250 atomic_write32(&this_module_map_initialized, Destroyed);
251 }
252 else{
253 //Locking succeeded, so this global map module-instance is ready
254 atomic_write32(&this_module_map_initialized, Initialized);
255 break;
256 }
257 }
258 BOOST_CATCH(...){
259 //
260 BOOST_RETHROW
261 } BOOST_CATCH_END
262 }
263 }
264 }
265
266 static void destroy_global_map_handle()
267 {
268 if(!atomic_read32(&this_module_singleton_count)){
269 //This module is being unloaded, so destroy
270 //the global map object of this module
271 //and unlink the global map if it's the last
272 ThreadSafeGlobalMap * const pmap = get_map_ptr();
273 typename intermodule_singleton_helpers::thread_safe_global_map_dependant<ThreadSafeGlobalMap>::
274 unlink_map_logic f(*pmap);
275 pmap->~ThreadSafeGlobalMap();
276 atomic_write32(&this_module_map_initialized, Destroyed);
277 //Do some cleanup for other processes old gmem instances
278 intermodule_singleton_helpers::thread_safe_global_map_dependant<ThreadSafeGlobalMap>::remove_old_gmem();
279 }
280 }
281
282 //Static data, zero-initalized without any dependencies
283 //this_module_singleton_count is the number of singletons used by this module
284 static volatile boost::uint32_t this_module_singleton_count;
285
286 //this_module_map_initialized is the state of this module's map class object.
287 //Values: Uninitialized, Initializing, Initialized, Broken
288 static volatile boost::uint32_t this_module_map_initialized;
289
290 //Raw memory to construct the global map manager
291 static union mem_holder_t
292 {
293 unsigned char map_mem [sizeof(ThreadSafeGlobalMap)];
294 ::boost::container::dtl::max_align_t aligner;
295 } mem_holder;
296 };
297
298 template<class ThreadSafeGlobalMap>
299 volatile boost::uint32_t intermodule_singleton_common<ThreadSafeGlobalMap>::this_module_singleton_count;
300
301 template<class ThreadSafeGlobalMap>
302 volatile boost::uint32_t intermodule_singleton_common<ThreadSafeGlobalMap>::this_module_map_initialized;
303
304 template<class ThreadSafeGlobalMap>
305 typename intermodule_singleton_common<ThreadSafeGlobalMap>::mem_holder_t
306 intermodule_singleton_common<ThreadSafeGlobalMap>::mem_holder;
307
308 //A reference count to be stored in global map holding the number
309 //of singletons (one per module) attached to the instance pointed by
310 //the internal ptr.
311 struct ref_count_ptr
312 {
313 ref_count_ptr(void *p, boost::uint32_t count)
314 : ptr(p), singleton_ref_count(count)
315 {}
316 void *ptr;
317 //This reference count serves to count the number of attached
318 //modules to this singleton
319 volatile boost::uint32_t singleton_ref_count;
320 };
321
322
323 //Now this class is a singleton, initializing the singleton in
324 //the first get() function call if LazyInit is true. If false
325 //then the singleton will be initialized when loading the module.
326 template<typename C, bool LazyInit, bool Phoenix, class ThreadSafeGlobalMap>
327 class intermodule_singleton_impl
328 {
329 public:
330
331 static C& get() //Let's make inlining easy
332 {
333 if(!this_module_singleton_ptr){
334 if(lifetime.dummy_function()){ //This forces lifetime instantiation, for reference counted destruction
335 atentry_work();
336 }
337 }
338 return *static_cast<C*>(this_module_singleton_ptr);
339 }
340
341 private:
342
343 static void atentry_work()
344 {
345 intermodule_singleton_common<ThreadSafeGlobalMap>::initialize_singleton_logic
346 (this_module_singleton_ptr, this_module_singleton_initialized, singleton_constructor, Phoenix);
347 }
348
349 static void atexit_work()
350 {
351 intermodule_singleton_common<ThreadSafeGlobalMap>::finalize_singleton_logic
352 (this_module_singleton_ptr, this_module_singleton_initialized, singleton_destructor);
353 }
354
355 //These statics will be zero-initialized without any constructor call dependency
356 //this_module_singleton_ptr will be a module-local pointer to the singleton
357 static void* this_module_singleton_ptr;
358
359 //this_module_singleton_count will be used to synchronize threads of the same module
360 //for access to a singleton instance, and to flag the state of the
361 //singleton.
362 static volatile boost::uint32_t this_module_singleton_initialized;
363
364 //This class destructor will trigger singleton destruction
365 struct lifetime_type_lazy
366 {
367 bool dummy_function()
368 { return m_dummy == 0; }
369
370 ~lifetime_type_lazy()
371 {
372 //if(!Phoenix){
373 //atexit_work();
374 //}
375 }
376
377 //Dummy volatile so that the compiler can't resolve its value at compile-time
378 //and can't avoid lifetime_type instantiation if dummy_function() is called.
379 static volatile int m_dummy;
380 };
381
382 struct lifetime_type_static
383 : public lifetime_type_lazy
384 {
385 lifetime_type_static()
386 { atentry_work(); }
387 };
388
389 typedef typename if_c
390 <LazyInit, lifetime_type_lazy, lifetime_type_static>::type lifetime_type;
391
392 static lifetime_type lifetime;
393
394 //A functor to be executed inside global map lock that just
395 //searches for the singleton in map and if not present creates a new one.
396 //If singleton constructor throws, the exception is propagated
397 struct init_atomic_func
398 {
399 init_atomic_func(ThreadSafeGlobalMap &m)
400 : m_map(m), ret_ptr()
401 {}
402
403 void operator()()
404 {
405 ref_count_ptr *rcount = intermodule_singleton_helpers::thread_safe_global_map_dependant
406 <ThreadSafeGlobalMap>::find(m_map, typeid(C).name());
407 if(!rcount){
408 C *p = new C;
409 BOOST_TRY{
410 ref_count_ptr val(p, 0u);
411 rcount = intermodule_singleton_helpers::thread_safe_global_map_dependant
412 <ThreadSafeGlobalMap>::insert(m_map, typeid(C).name(), val);
413 }
414 BOOST_CATCH(...){
415 intermodule_singleton_helpers::thread_safe_global_map_dependant
416 <ThreadSafeGlobalMap>::erase(m_map, typeid(C).name());
417 delete p;
418 BOOST_RETHROW
419 } BOOST_CATCH_END
420 }
421 //if(Phoenix){
422 std::atexit(&atexit_work);
423 //}
424 atomic_inc32(&rcount->singleton_ref_count);
425 ret_ptr = rcount->ptr;
426 }
427 void *data() const
428 { return ret_ptr; }
429
430 private:
431 ThreadSafeGlobalMap &m_map;
432 void *ret_ptr;
433 };
434
435 //A functor to be executed inside global map lock that just
436 //deletes the singleton in map if the attached count reaches to zero
437 struct fini_atomic_func
438 {
439 fini_atomic_func(ThreadSafeGlobalMap &m)
440 : m_map(m)
441 {}
442
443 void operator()()
444 {
445 ref_count_ptr *rcount = intermodule_singleton_helpers::thread_safe_global_map_dependant
446 <ThreadSafeGlobalMap>::find(m_map, typeid(C).name());
447 //The object must exist
448 BOOST_ASSERT(rcount);
449 BOOST_ASSERT(rcount->singleton_ref_count > 0);
450 //Check if last reference
451 if(atomic_dec32(&rcount->singleton_ref_count) == 1){
452 //If last, destroy the object
453 BOOST_ASSERT(rcount->ptr != 0);
454 C *pc = static_cast<C*>(rcount->ptr);
455 //Now destroy map entry
456 bool destroyed = intermodule_singleton_helpers::thread_safe_global_map_dependant
457 <ThreadSafeGlobalMap>::erase(m_map, typeid(C).name());
458 (void)destroyed; BOOST_ASSERT(destroyed == true);
459 delete pc;
460 }
461 }
462
463 private:
464 ThreadSafeGlobalMap &m_map;
465 };
466
467 //A wrapper to execute init_atomic_func
468 static void *singleton_constructor(ThreadSafeGlobalMap &map)
469 {
470 init_atomic_func f(map);
471 intermodule_singleton_helpers::thread_safe_global_map_dependant
472 <ThreadSafeGlobalMap>::atomic_func(map, f);
473 return f.data();
474 }
475
476 //A wrapper to execute fini_atomic_func
477 static void singleton_destructor(void *p, ThreadSafeGlobalMap &map)
478 { (void)p;
479 fini_atomic_func f(map);
480 intermodule_singleton_helpers::thread_safe_global_map_dependant
481 <ThreadSafeGlobalMap>::atomic_func(map, f);
482 }
483 };
484
485 template <typename C, bool L, bool P, class ThreadSafeGlobalMap>
486 volatile int intermodule_singleton_impl<C, L, P, ThreadSafeGlobalMap>::lifetime_type_lazy::m_dummy = 0;
487
488 //These will be zero-initialized by the loader
489 template <typename C, bool L, bool P, class ThreadSafeGlobalMap>
490 void *intermodule_singleton_impl<C, L, P, ThreadSafeGlobalMap>::this_module_singleton_ptr = 0;
491
492 template <typename C, bool L, bool P, class ThreadSafeGlobalMap>
493 volatile boost::uint32_t intermodule_singleton_impl<C, L, P, ThreadSafeGlobalMap>::this_module_singleton_initialized = 0;
494
495 template <typename C, bool L, bool P, class ThreadSafeGlobalMap>
496 typename intermodule_singleton_impl<C, L, P, ThreadSafeGlobalMap>::lifetime_type
497 intermodule_singleton_impl<C, L, P, ThreadSafeGlobalMap>::lifetime;
498
499 } //namespace ipcdetail{
500 } //namespace interprocess{
501 } //namespace boost{
502
503 #include <boost/interprocess/detail/config_end.hpp>
504
505 #endif //#ifndef BOOST_INTERPROCESS_INTERMODULE_SINGLETON_COMMON_HPP