intermodule_singleton_common.hpp 20 KB

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