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