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