intermodule_singleton_common.hpp 20 KB

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