Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-07 08:22:00

0001 //////////////////////////////////////////////////////////////////////////////
0002 //
0003 // (C) Copyright Ion Gaztanaga 2009-2012. Distributed under the Boost
0004 // Software License, Version 1.0. (See accompanying file
0005 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // See http://www.boost.org/libs/interprocess for documentation.
0008 //
0009 //////////////////////////////////////////////////////////////////////////////
0010 
0011 #ifndef BOOST_INTERPROCESS_INTERMODULE_SINGLETON_COMMON_HPP
0012 #define BOOST_INTERPROCESS_INTERMODULE_SINGLETON_COMMON_HPP
0013 
0014 #ifndef BOOST_CONFIG_HPP
0015 #  include <boost/config.hpp>
0016 #endif
0017 0018 ">#
0019 #if defined(BOOST_HAS_PRAGMA_ONCE)
0020 #pragma once
0021 #endif
0022 
0023 #include <boost/interprocess/detail/config_begin.hpp>
0024 #include <boost/interprocess/detail/workaround.hpp>
0025 
0026 #include <boost/interprocess/detail/atomic.hpp>
0027 #include <boost/interprocess/detail/os_thread_functions.hpp>
0028 #include <boost/interprocess/exceptions.hpp>
0029 #include <boost/container/detail/type_traits.hpp>  //alignment_of, aligned_storage
0030 #include <boost/interprocess/detail/mpl.hpp>
0031 #include <boost/interprocess/sync/spin/wait.hpp>
0032 #include <boost/assert.hpp>
0033 #include <cstddef>
0034 #include <cstdio>
0035 #include <cstdlib>
0036 #include <cstring>
0037 #include <string>
0038 #include <typeinfo>
0039 #include <sstream>
0040 
0041 namespace boost{
0042 namespace interprocess{
0043 namespace ipcdetail{
0044 
0045 namespace intermodule_singleton_helpers {
0046 
0047 inline void get_pid_creation_time_str(std::string &s)
0048 {
0049    std::stringstream stream;
0050    stream << get_current_process_id() << '_';
0051    const unsigned long long total_microsecs = get_current_process_creation_time();
0052    const unsigned long secs  = static_cast<unsigned long>(total_microsecs/1000000ul);
0053    const unsigned long usecs = static_cast<unsigned long>(total_microsecs%1000000ul);
0054    stream << secs << '.' << usecs;
0055    s = stream.str();
0056 }
0057 
0058 inline const char *get_map_base_name()
0059 {  return "bip.gmem.map.";  }
0060 
0061 inline void get_map_name(std::string &map_name)
0062 {
0063    get_pid_creation_time_str(map_name);
0064    map_name.insert(0, get_map_base_name());
0065 }
0066 
0067 inline std::size_t get_map_size()
0068 {  return 65536;  }
0069 
0070 template<class ThreadSafeGlobalMap>
0071 struct thread_safe_global_map_dependant;
0072 
0073 }  //namespace intermodule_singleton_helpers {
0074 
0075 //This class contains common code for all singleton types, so that we instantiate this
0076 //code just once per module. This class also holds a thread soafe global map
0077 //to be used by all instances protected with a reference count
0078 template<class ThreadSafeGlobalMap>
0079 class intermodule_singleton_common
0080 {
0081    public:
0082    typedef void*(singleton_constructor_t)(ThreadSafeGlobalMap &);
0083    typedef void (singleton_destructor_t)(void *, ThreadSafeGlobalMap &);
0084 
0085    static const ::boost::uint32_t Uninitialized       = 0u;
0086    static const ::boost::uint32_t Initializing        = 1u;
0087    static const ::boost::uint32_t Initialized         = 2u;
0088    static const ::boost::uint32_t Broken              = 3u;
0089    static const ::boost::uint32_t Destroyed           = 4u;
0090 
0091    //Initialize this_module_singleton_ptr, creates the global map if needed and also creates an unique
0092    //opaque type in global map through a singleton_constructor_t function call,
0093    //initializing the passed pointer to that unique instance.
0094    //
0095    //We have two concurrency types here. a)the global map/singleton creation must
0096    //be safe between threads of this process but in different modules/dlls. b)
0097    //the pointer to the singleton is per-module, so we have to protect this
0098    //initization between threads of the same module.
0099    //
0100    //All static variables declared here are shared between inside a module
0101    //so atomic operations will synchronize only threads of the same module.
0102    static void initialize_singleton_logic
0103       (void *&ptr, volatile boost::uint32_t &this_module_singleton_initialized, singleton_constructor_t constructor, bool phoenix)
0104    {
0105       //If current module is not initialized enter to lock free logic
0106       if(atomic_read32(&this_module_singleton_initialized) != Initialized){
0107          //Now a single thread of the module will succeed in this CAS.
0108          //trying to pass from Uninitialized to Initializing
0109          ::boost::uint32_t previous_module_singleton_initialized = atomic_cas32
0110             (&this_module_singleton_initialized, Initializing, Uninitialized);
0111          //If the thread succeeded the CAS (winner) it will compete with other
0112          //winner threads from other modules to create the global map
0113          if(previous_module_singleton_initialized == Destroyed){
0114             //Trying to resurrect a dead Phoenix singleton. Just try to
0115             //mark it as uninitialized and start again
0116             if(phoenix){
0117                atomic_cas32(&this_module_singleton_initialized, Uninitialized, Destroyed);
0118                previous_module_singleton_initialized = atomic_cas32
0119                   (&this_module_singleton_initialized, Initializing, Uninitialized);
0120             }
0121             //Trying to resurrect a non-Phoenix dead singleton is an error
0122             else{
0123                throw interprocess_exception("Boost.Interprocess: Dead reference on non-Phoenix singleton of type");
0124             }
0125          }
0126          if(previous_module_singleton_initialized == Uninitialized){
0127             BOOST_INTERPROCESS_TRY{
0128                //Now initialize the global map, this function must solve concurrency
0129                //issues between threads of several modules
0130                initialize_global_map_handle();
0131                //Now try to create the singleton in global map.
0132                //This function solves concurrency issues
0133                //between threads of several modules
0134                ThreadSafeGlobalMap *const pmap = get_map_ptr();
0135                void *tmp = constructor(*pmap);
0136                //Increment the module reference count that reflects how many
0137                //singletons this module holds, so that we can safely destroy
0138                //module global map object when no singleton is left
0139                atomic_inc32(&this_module_singleton_count);
0140                //Insert a barrier before assigning the pointer to
0141                //make sure this assignment comes after the initialization
0142                atomic_write32(&this_module_singleton_initialized, Initializing);
0143                //Assign the singleton address to the module-local pointer
0144                ptr = tmp;
0145                //Memory barrier inserted, all previous operations should complete
0146                //before this one. Now marked as initialized
0147                atomic_write32(&this_module_singleton_initialized, Initialized);
0148             }
0149             BOOST_INTERPROCESS_CATCH(...){
0150                //Mark singleton failed to initialize
0151                atomic_write32(&this_module_singleton_initialized, Broken);
0152                BOOST_INTERPROCESS_RETHROW
0153             } BOOST_INTERPROCESS_CATCH_END
0154          }
0155          //If previous state was initializing, this means that another winner thread is
0156          //trying to initialize the singleton. Just wait until completes its work.
0157          else if(previous_module_singleton_initialized == Initializing){
0158             spin_wait swait;
0159             while(1){
0160                previous_module_singleton_initialized = atomic_read32(&this_module_singleton_initialized);
0161                if(previous_module_singleton_initialized >= Initialized){
0162                   //Already initialized, or exception thrown by initializer thread
0163                   break;
0164                }
0165                else if(previous_module_singleton_initialized == Initializing){
0166                   swait.yield();
0167                }
0168                else{
0169                   //This can't be happening!
0170                   BOOST_ASSERT(0);
0171                }
0172             }
0173          }
0174          else if(previous_module_singleton_initialized == Initialized){
0175             //Nothing to do here, the singleton is ready
0176          }
0177          //If previous state was greater than initialized, then memory is broken
0178          //trying to initialize the singleton.
0179          else{//(previous_module_singleton_initialized > Initialized)
0180             throw interprocess_exception("boost::interprocess::intermodule_singleton initialization failed");
0181          }
0182       }
0183       BOOST_ASSERT(ptr != 0);
0184    }
0185 
0186    static void finalize_singleton_logic(void *&ptr, volatile boost::uint32_t &this_module_singleton_initialized, singleton_destructor_t destructor)
0187    {
0188       //Protect destruction against lazy singletons not initialized in this execution
0189       if(ptr){
0190          //Note: this destructor might provoke a Phoenix singleton
0191          //resurrection. This means that this_module_singleton_count
0192          //might change after this call.
0193          ThreadSafeGlobalMap * const pmap = get_map_ptr();
0194          destructor(ptr, *pmap);
0195          ptr = 0;
0196 
0197          //Memory barrier to make sure pointer is nulled.
0198          //Mark this singleton as destroyed.
0199          atomic_write32(&this_module_singleton_initialized, Destroyed);
0200 
0201          //If this is the last singleton of this module
0202          //apply map destruction.
0203          //Note: singletons are destroyed when the module is unloaded
0204          //so no threads should be executing or holding references
0205          //to this module
0206          if(1 == atomic_dec32(&this_module_singleton_count)){
0207             destroy_global_map_handle();
0208          }
0209       }
0210    }
0211 
0212    private:
0213    static ThreadSafeGlobalMap *get_map_ptr()
0214    {
0215       return static_cast<ThreadSafeGlobalMap *>(static_cast<void*>(mem_holder.map_mem));
0216    }
0217 
0218    static void initialize_global_map_handle()
0219    {
0220       //Obtain unique map name and size
0221       spin_wait swait;
0222       while(1){
0223          //Try to pass map state to initializing
0224          ::boost::uint32_t tmp = atomic_cas32(&this_module_map_initialized, Initializing, Uninitialized);
0225          if(tmp == Initialized || tmp == Broken){
0226             break;
0227          }
0228          else if(tmp == Destroyed){
0229             tmp = atomic_cas32(&this_module_map_initialized, Uninitialized, Destroyed);
0230             continue;
0231          }
0232          //If some other thread is doing the work wait
0233          else if(tmp == Initializing){
0234             swait.yield();
0235          }
0236          else{ //(tmp == Uninitialized)
0237             //If not initialized try it again?
0238             BOOST_INTERPROCESS_TRY{
0239                //Remove old global map from the system
0240                intermodule_singleton_helpers::thread_safe_global_map_dependant<ThreadSafeGlobalMap>::remove_old_gmem();
0241                //in-place construction of the global map class
0242                ThreadSafeGlobalMap * const pmap = get_map_ptr();
0243                intermodule_singleton_helpers::thread_safe_global_map_dependant
0244                   <ThreadSafeGlobalMap>::construct_map(static_cast<void*>(pmap));
0245                //Use global map's internal lock to initialize the lock file
0246                //that will mark this gmem as "in use".
0247                typename intermodule_singleton_helpers::thread_safe_global_map_dependant<ThreadSafeGlobalMap>::
0248                   lock_file_logic f(*pmap);
0249                //If function failed (maybe a competing process has erased the shared
0250                //memory between creation and file locking), retry with a new instance.
0251                if(f.retry()){
0252                   pmap->~ThreadSafeGlobalMap();
0253                   atomic_write32(&this_module_map_initialized, Destroyed);
0254                }
0255                else{
0256                   //Locking succeeded, so this global map module-instance is ready
0257                   atomic_write32(&this_module_map_initialized, Initialized);
0258                   break;
0259                }
0260             }
0261             BOOST_INTERPROCESS_CATCH(...){
0262                //
0263                BOOST_INTERPROCESS_RETHROW
0264             } BOOST_INTERPROCESS_CATCH_END
0265          }
0266       }
0267    }
0268 
0269    static void destroy_global_map_handle()
0270    {
0271       if(!atomic_read32(&this_module_singleton_count)){
0272          //This module is being unloaded, so destroy
0273          //the global map object of this module
0274          //and unlink the global map if it's the last
0275          ThreadSafeGlobalMap * const pmap = get_map_ptr();
0276          typename intermodule_singleton_helpers::thread_safe_global_map_dependant<ThreadSafeGlobalMap>::
0277             unlink_map_logic f(*pmap);
0278          pmap->~ThreadSafeGlobalMap();
0279          atomic_write32(&this_module_map_initialized, Destroyed);
0280          //Do some cleanup for other processes old gmem instances
0281          intermodule_singleton_helpers::thread_safe_global_map_dependant<ThreadSafeGlobalMap>::remove_old_gmem();
0282       }
0283    }
0284 
0285    //Static data, zero-initalized without any dependencies
0286    //this_module_singleton_count is the number of singletons used by this module
0287    static volatile boost::uint32_t this_module_singleton_count;
0288 
0289    //this_module_map_initialized is the state of this module's map class object.
0290    //Values: Uninitialized, Initializing, Initialized, Broken
0291    static volatile boost::uint32_t this_module_map_initialized;
0292 
0293    //Raw memory to construct the global map manager
0294    static union mem_holder_t
0295    {
0296       unsigned char map_mem [sizeof(ThreadSafeGlobalMap)];
0297       ::boost::container::dtl::max_align_t aligner;
0298    } mem_holder;
0299 };
0300 
0301 template<class ThreadSafeGlobalMap>
0302 volatile boost::uint32_t intermodule_singleton_common<ThreadSafeGlobalMap>::this_module_singleton_count;
0303 
0304 template<class ThreadSafeGlobalMap>
0305 volatile boost::uint32_t intermodule_singleton_common<ThreadSafeGlobalMap>::this_module_map_initialized;
0306 
0307 template<class ThreadSafeGlobalMap>
0308 typename intermodule_singleton_common<ThreadSafeGlobalMap>::mem_holder_t
0309    intermodule_singleton_common<ThreadSafeGlobalMap>::mem_holder;
0310 
0311 //A reference count to be stored in global map holding the number
0312 //of singletons (one per module) attached to the instance pointed by
0313 //the internal ptr.
0314 struct ref_count_ptr
0315 {
0316    ref_count_ptr(void *p, boost::uint32_t count)
0317       : ptr(p), singleton_ref_count(count)
0318    {}
0319    void *ptr;
0320    //This reference count serves to count the number of attached
0321    //modules to this singleton
0322    volatile boost::uint32_t singleton_ref_count;
0323 };
0324 
0325 
0326 //Now this class is a singleton, initializing the singleton in
0327 //the first get() function call if LazyInit is true. If false
0328 //then the singleton will be initialized when loading the module.
0329 template<typename C, bool LazyInit, bool Phoenix, class ThreadSafeGlobalMap>
0330 class intermodule_singleton_impl
0331 {
0332    public:
0333 
0334    static C& get()   //Let's make inlining easy
0335    {
0336       if(!this_module_singleton_ptr){
0337          if(lifetime.dummy_function()){  //This forces lifetime instantiation, for reference counted destruction
0338             atentry_work();
0339          }
0340       }
0341       return *static_cast<C*>(this_module_singleton_ptr);
0342    }
0343 
0344    private:
0345 
0346    static void atentry_work()
0347    {
0348       intermodule_singleton_common<ThreadSafeGlobalMap>::initialize_singleton_logic
0349          (this_module_singleton_ptr, this_module_singleton_initialized, singleton_constructor, Phoenix);
0350    }
0351 
0352    static void atexit_work()
0353    {
0354       intermodule_singleton_common<ThreadSafeGlobalMap>::finalize_singleton_logic
0355          (this_module_singleton_ptr, this_module_singleton_initialized, singleton_destructor);
0356    }
0357 
0358    //These statics will be zero-initialized without any constructor call dependency
0359    //this_module_singleton_ptr will be a module-local pointer to the singleton
0360    static void*                      this_module_singleton_ptr;
0361 
0362    //this_module_singleton_count will be used to synchronize threads of the same module
0363    //for access to a singleton instance, and to flag the state of the
0364    //singleton.
0365    static volatile boost::uint32_t   this_module_singleton_initialized;
0366 
0367    //This class destructor will trigger singleton destruction
0368    struct lifetime_type_lazy
0369    {
0370       bool dummy_function()
0371       {  return m_dummy == 0; }
0372 
0373       ~lifetime_type_lazy()
0374       {
0375          //if(!Phoenix){
0376             //atexit_work();
0377          //}
0378       }
0379 
0380       //Dummy volatile so that the compiler can't resolve its value at compile-time
0381       //and can't avoid lifetime_type instantiation if dummy_function() is called.
0382       static volatile int m_dummy;
0383    };
0384 
0385    struct lifetime_type_static
0386       : public lifetime_type_lazy
0387    {
0388       lifetime_type_static()
0389       {  atentry_work();  }
0390    };
0391 
0392    typedef typename if_c
0393       <LazyInit, lifetime_type_lazy, lifetime_type_static>::type lifetime_type;
0394 
0395    static lifetime_type lifetime;
0396 
0397    //A functor to be executed inside global map lock that just
0398    //searches for the singleton in map and if not present creates a new one.
0399    //If singleton constructor throws, the exception is propagated
0400    struct init_atomic_func
0401    {
0402       init_atomic_func(ThreadSafeGlobalMap &m)
0403          : m_map(m), ret_ptr()
0404       {}
0405 
0406       void operator()()
0407       {
0408          ref_count_ptr *rcount = intermodule_singleton_helpers::thread_safe_global_map_dependant
0409             <ThreadSafeGlobalMap>::find(m_map, typeid(C).name());
0410          if(!rcount){
0411             C *p = new C;
0412             BOOST_INTERPROCESS_TRY{
0413                ref_count_ptr val(p, 0u);
0414                rcount = intermodule_singleton_helpers::thread_safe_global_map_dependant
0415                            <ThreadSafeGlobalMap>::insert(m_map, typeid(C).name(), val);
0416             }
0417             BOOST_INTERPROCESS_CATCH(...){
0418                intermodule_singleton_helpers::thread_safe_global_map_dependant
0419                            <ThreadSafeGlobalMap>::erase(m_map, typeid(C).name());
0420                delete p;
0421                BOOST_INTERPROCESS_RETHROW
0422             } BOOST_INTERPROCESS_CATCH_END
0423          }
0424          //if(Phoenix){
0425             std::atexit(&atexit_work);
0426          //}
0427          atomic_inc32(&rcount->singleton_ref_count);
0428          ret_ptr = rcount->ptr;
0429       }
0430       void *data() const
0431          { return ret_ptr;  }
0432 
0433       private:
0434       ThreadSafeGlobalMap &m_map;
0435       void *ret_ptr;
0436    };
0437 
0438    //A functor to be executed inside global map lock that just
0439    //deletes the singleton in map if the attached count reaches to zero
0440    struct fini_atomic_func
0441    {
0442       fini_atomic_func(ThreadSafeGlobalMap &m)
0443          : m_map(m)
0444       {}
0445 
0446       void operator()()
0447       {
0448          ref_count_ptr *rcount = intermodule_singleton_helpers::thread_safe_global_map_dependant
0449             <ThreadSafeGlobalMap>::find(m_map, typeid(C).name());
0450             //The object must exist
0451          BOOST_ASSERT(rcount);
0452          BOOST_ASSERT(rcount->singleton_ref_count > 0);
0453          //Check if last reference
0454          if(atomic_dec32(&rcount->singleton_ref_count) == 1){
0455             //If last, destroy the object
0456             BOOST_ASSERT(rcount->ptr != 0);
0457             C *pc = static_cast<C*>(rcount->ptr);
0458             //Now destroy map entry
0459             bool destroyed = intermodule_singleton_helpers::thread_safe_global_map_dependant
0460                         <ThreadSafeGlobalMap>::erase(m_map, typeid(C).name());
0461             (void)destroyed;  BOOST_ASSERT(destroyed == true);
0462             delete pc;
0463          }
0464       }
0465 
0466       private:
0467       ThreadSafeGlobalMap &m_map;
0468    };
0469 
0470    //A wrapper to execute init_atomic_func
0471    static void *singleton_constructor(ThreadSafeGlobalMap &map)
0472    {
0473       init_atomic_func f(map);
0474       intermodule_singleton_helpers::thread_safe_global_map_dependant
0475                   <ThreadSafeGlobalMap>::atomic_func(map, f);
0476       return f.data();
0477    }
0478 
0479    //A wrapper to execute fini_atomic_func
0480    static void singleton_destructor(void *p, ThreadSafeGlobalMap &map)
0481    {  (void)p;
0482       fini_atomic_func f(map);
0483       intermodule_singleton_helpers::thread_safe_global_map_dependant
0484                   <ThreadSafeGlobalMap>::atomic_func(map, f);
0485    }
0486 };
0487 
0488 template <typename C, bool L, bool P, class ThreadSafeGlobalMap>
0489 volatile int intermodule_singleton_impl<C, L, P, ThreadSafeGlobalMap>::lifetime_type_lazy::m_dummy = 0;
0490 
0491 //These will be zero-initialized by the loader
0492 template <typename C, bool L, bool P, class ThreadSafeGlobalMap>
0493 void *intermodule_singleton_impl<C, L, P, ThreadSafeGlobalMap>::this_module_singleton_ptr = 0;
0494 
0495 template <typename C, bool L, bool P, class ThreadSafeGlobalMap>
0496 volatile boost::uint32_t intermodule_singleton_impl<C, L, P, ThreadSafeGlobalMap>::this_module_singleton_initialized = 0;
0497 
0498 template <typename C, bool L, bool P, class ThreadSafeGlobalMap>
0499 typename intermodule_singleton_impl<C, L, P, ThreadSafeGlobalMap>::lifetime_type
0500    intermodule_singleton_impl<C, L, P, ThreadSafeGlobalMap>::lifetime;
0501 
0502 }  //namespace ipcdetail{
0503 }  //namespace interprocess{
0504 }  //namespace boost{
0505 
0506 #include <boost/interprocess/detail/config_end.hpp>
0507 
0508 #endif   //#ifndef BOOST_INTERPROCESS_INTERMODULE_SINGLETON_COMMON_HPP