1 //////////////////////////////////////////////////////////////////////////////
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)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_INTERPROCESS_INTERMODULE_SINGLETON_COMMON_HPP
12 #define BOOST_INTERPROCESS_INTERMODULE_SINGLETON_COMMON_HPP
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
22 #include <boost/interprocess/detail/config_begin.hpp>
23 #include <boost/interprocess/detail/workaround.hpp>
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>
41 namespace interprocess{
44 namespace intermodule_singleton_helpers {
46 inline void get_pid_creation_time_str(std::string &s)
48 std::stringstream stream;
49 stream << get_current_process_id() << '_';
51 stream << std::fixed << get_current_process_creation_time();
55 inline const char *get_map_base_name()
56 { return "bip.gmem.map."; }
58 inline void get_map_name(std::string &map_name)
60 get_pid_creation_time_str(map_name);
61 map_name.insert(0, get_map_base_name());
64 inline std::size_t get_map_size()
67 template<class ThreadSafeGlobalMap>
68 struct thread_safe_global_map_dependant;
70 } //namespace intermodule_singleton_helpers {
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
79 typedef void*(singleton_constructor_t)(ThreadSafeGlobalMap &);
80 typedef void (singleton_destructor_t)(void *, ThreadSafeGlobalMap &);
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;
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.
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.
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)
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
114 atomic_cas32(&this_module_singleton_initialized, Uninitialized, Destroyed);
115 previous_module_singleton_initialized = atomic_cas32
116 (&this_module_singleton_initialized, Initializing, Uninitialized);
118 //Trying to resurrect a non-Phoenix dead singleton is an error
120 throw interprocess_exception("Boost.Interprocess: Dead reference on non-Phoenix singleton of type");
123 if(previous_module_singleton_initialized == Uninitialized){
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
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);
147 //Mark singleton failed to initialize
148 atomic_write32(&this_module_singleton_initialized, Broken);
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){
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
162 else if(previous_module_singleton_initialized == Initializing){
166 //This can't be happening!
171 else if(previous_module_singleton_initialized == Initialized){
172 //Nothing to do here, the singleton is ready
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");
180 BOOST_ASSERT(ptr != 0);
183 static void finalize_singleton_logic(void *&ptr, volatile boost::uint32_t &this_module_singleton_initialized, singleton_destructor_t destructor)
185 //Protect destruction against lazy singletons not initialized in this execution
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);
194 //Memory barrier to make sure pointer is nulled.
195 //Mark this singleton as destroyed.
196 atomic_write32(&this_module_singleton_initialized, Destroyed);
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
203 if(1 == atomic_dec32(&this_module_singleton_count)){
204 destroy_global_map_handle();
210 static ThreadSafeGlobalMap *get_map_ptr()
212 return static_cast<ThreadSafeGlobalMap *>(static_cast<void*>(mem_holder.map_mem));
215 static void initialize_global_map_handle()
217 //Obtain unique map name and size
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){
225 else if(tmp == Destroyed){
226 tmp = atomic_cas32(&this_module_map_initialized, Uninitialized, Destroyed);
229 //If some other thread is doing the work wait
230 else if(tmp == Initializing){
233 else{ //(tmp == Uninitialized)
234 //If not initialized try it again?
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.
249 pmap->~ThreadSafeGlobalMap();
250 atomic_write32(&this_module_map_initialized, Destroyed);
253 //Locking succeeded, so this global map module-instance is ready
254 atomic_write32(&this_module_map_initialized, Initialized);
266 static void destroy_global_map_handle()
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();
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;
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;
290 //Raw memory to construct the global map manager
291 static union mem_holder_t
293 unsigned char map_mem [sizeof(ThreadSafeGlobalMap)];
294 ::boost::container::dtl::max_align_t aligner;
298 template<class ThreadSafeGlobalMap>
299 volatile boost::uint32_t intermodule_singleton_common<ThreadSafeGlobalMap>::this_module_singleton_count;
301 template<class ThreadSafeGlobalMap>
302 volatile boost::uint32_t intermodule_singleton_common<ThreadSafeGlobalMap>::this_module_map_initialized;
304 template<class ThreadSafeGlobalMap>
305 typename intermodule_singleton_common<ThreadSafeGlobalMap>::mem_holder_t
306 intermodule_singleton_common<ThreadSafeGlobalMap>::mem_holder;
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
313 ref_count_ptr(void *p, boost::uint32_t count)
314 : ptr(p), singleton_ref_count(count)
317 //This reference count serves to count the number of attached
318 //modules to this singleton
319 volatile boost::uint32_t singleton_ref_count;
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
331 static C& get() //Let's make inlining easy
333 if(!this_module_singleton_ptr){
334 if(lifetime.dummy_function()){ //This forces lifetime instantiation, for reference counted destruction
338 return *static_cast<C*>(this_module_singleton_ptr);
343 static void atentry_work()
345 intermodule_singleton_common<ThreadSafeGlobalMap>::initialize_singleton_logic
346 (this_module_singleton_ptr, this_module_singleton_initialized, singleton_constructor, Phoenix);
349 static void atexit_work()
351 intermodule_singleton_common<ThreadSafeGlobalMap>::finalize_singleton_logic
352 (this_module_singleton_ptr, this_module_singleton_initialized, singleton_destructor);
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;
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
362 static volatile boost::uint32_t this_module_singleton_initialized;
364 //This class destructor will trigger singleton destruction
365 struct lifetime_type_lazy
367 bool dummy_function()
368 { return m_dummy == 0; }
370 ~lifetime_type_lazy()
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;
382 struct lifetime_type_static
383 : public lifetime_type_lazy
385 lifetime_type_static()
389 typedef typename if_c
390 <LazyInit, lifetime_type_lazy, lifetime_type_static>::type lifetime_type;
392 static lifetime_type lifetime;
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
399 init_atomic_func(ThreadSafeGlobalMap &m)
400 : m_map(m), ret_ptr()
405 ref_count_ptr *rcount = intermodule_singleton_helpers::thread_safe_global_map_dependant
406 <ThreadSafeGlobalMap>::find(m_map, typeid(C).name());
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);
415 intermodule_singleton_helpers::thread_safe_global_map_dependant
416 <ThreadSafeGlobalMap>::erase(m_map, typeid(C).name());
422 std::atexit(&atexit_work);
424 atomic_inc32(&rcount->singleton_ref_count);
425 ret_ptr = rcount->ptr;
431 ThreadSafeGlobalMap &m_map;
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
439 fini_atomic_func(ThreadSafeGlobalMap &m)
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);
464 ThreadSafeGlobalMap &m_map;
467 //A wrapper to execute init_atomic_func
468 static void *singleton_constructor(ThreadSafeGlobalMap &map)
470 init_atomic_func f(map);
471 intermodule_singleton_helpers::thread_safe_global_map_dependant
472 <ThreadSafeGlobalMap>::atomic_func(map, f);
476 //A wrapper to execute fini_atomic_func
477 static void singleton_destructor(void *p, ThreadSafeGlobalMap &map)
479 fini_atomic_func f(map);
480 intermodule_singleton_helpers::thread_safe_global_map_dependant
481 <ThreadSafeGlobalMap>::atomic_func(map, f);
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;
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;
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;
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;
499 } //namespace ipcdetail{
500 } //namespace interprocess{
503 #include <boost/interprocess/detail/config_end.hpp>
505 #endif //#ifndef BOOST_INTERPROCESS_INTERMODULE_SINGLETON_COMMON_HPP