Revert "Minor modernization of DynamicAny code"
[ACE_TAO.git] / TAO / tao / TAO_Singleton.cpp
blobf5c235ae8fa4e5328b9eed30db0e2f4db558d03b
1 // -*- C++ -*-
2 #ifndef TAO_SINGLETON_CPP
3 #define TAO_SINGLETON_CPP
5 #include "tao/TAO_Singleton.h"
7 #if !defined (ACE_LACKS_PRAGMA_ONCE)
8 # pragma once
9 #endif /* ACE_LACKS_PRAGMA_ONCE */
11 #include "tao/TAO_Singleton_Manager.h"
13 #include "ace/Guard_T.h"
14 #include "ace/Object_Manager.h"
15 #include "tao/debug.h"
16 #include "ace/os_include/os_typeinfo.h"
18 #if !defined (__ACE_INLINE__)
19 #include "tao/TAO_Singleton.inl"
20 #endif /* __ACE_INLINE__ */
22 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
24 template <class TYPE, class ACE_LOCK> void
25 TAO_Singleton<TYPE, ACE_LOCK>::dump ()
27 #if defined (ACE_HAS_DUMP)
28 ACE_TRACE ("TAO_Singleton<TYPE, ACE_LOCK>::dump");
30 TAOLIB_DEBUG ((LM_DEBUG, ACE_TEXT ("instance_ = %@"),
31 TAO_Singleton<TYPE, ACE_LOCK>::instance_i ()));
32 TAOLIB_DEBUG ((LM_DEBUG, ACE_END_DUMP));
33 #endif /* ACE_HAS_DUMP */
36 template <class TYPE, class ACE_LOCK> TAO_Singleton<TYPE, ACE_LOCK> *&
37 TAO_Singleton<TYPE, ACE_LOCK>::instance_i ()
39 return TAO_Singleton<TYPE, ACE_LOCK>::singleton_;
42 template <class TYPE, class ACE_LOCK> TYPE *
43 TAO_Singleton<TYPE, ACE_LOCK>::instance ()
45 ACE_TRACE ("TAO_Singleton<TYPE, ACE_LOCK>::instance");
47 TAO_Singleton<TYPE, ACE_LOCK> *&singleton =
48 TAO_Singleton<TYPE, ACE_LOCK>::instance_i ();
50 // Perform the Double-Check pattern...
51 if (singleton == 0)
53 if (TAO_Singleton_Manager::starting_up () ||
54 TAO_Singleton_Manager::shutting_down ())
56 // The program is still starting up, and therefore assumed
57 // to be single threaded. There's no need to double-check.
58 // Or, the TAO_Singleton_Manager instance has been destroyed,
59 // so the preallocated lock is not available. Either way,
60 // don't register for destruction with the
61 // TAO_Singleton_Manager: we'll have to leak this instance.
63 ACE_NEW_RETURN (singleton, (TAO_Singleton<TYPE, ACE_LOCK>), 0);
65 else
67 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
68 // Obtain a lock from the ACE_Object_Manager. The pointer
69 // is static, so we only obtain one per TAO_Singleton
70 // instantiation.
71 static ACE_LOCK *lock = 0;
72 if (ACE_Object_Manager::get_singleton_lock (lock) != 0)
73 // Failed to acquire the lock!
74 return 0;
76 ACE_GUARD_RETURN (ACE_LOCK, ace_mon, *lock, 0);
78 if (singleton == 0)
80 #endif /* ACE_MT_SAFE */
81 ACE_NEW_RETURN (singleton, (TAO_Singleton<TYPE, ACE_LOCK>), 0);
83 // Register for destruction with TAO_Singleton_Manager.
84 #if !defined (ACE_MT_SAFE) || (ACE_MT_SAFE == 0)
85 TAO_Singleton_Manager::at_exit (singleton, 0,
86 typeid (TYPE).name ());
87 #else
88 TAO_Singleton_Manager::at_exit (singleton, &lock,
89 typeid (TYPE).name());
91 #endif /* ACE_MT_SAFE */
95 return &singleton->instance_;
98 template <class TYPE, class ACE_LOCK> void
99 TAO_Singleton<TYPE, ACE_LOCK>::cleanup (void *param)
101 delete this;
102 TAO_Singleton<TYPE, ACE_LOCK>::instance_i () = 0;
104 #if defined ACE_MT_SAFE && ACE_MT_SAFE != 0
105 if (param)
107 ACE_LOCK **lock = static_cast<ACE_LOCK **> (param);
108 *lock = 0;
110 #endif
113 // Pointer to the Singleton instance.
114 template <class TYPE, class ACE_LOCK> TAO_Singleton<TYPE, ACE_LOCK> *
115 TAO_Singleton<TYPE, ACE_LOCK>::singleton_ = 0;
117 template <class TYPE, class ACE_LOCK> TAO_TSS_Singleton<TYPE, ACE_LOCK> *
118 TAO_TSS_Singleton<TYPE, ACE_LOCK>::singleton_ = 0;
120 template <class TYPE, class ACE_LOCK> void
121 TAO_TSS_Singleton<TYPE, ACE_LOCK>::dump ()
123 #if defined (ACE_HAS_DUMP)
124 ACE_TRACE ("TAO_TSS_Singleton<TYPE, ACE_LOCK>::dump");
126 TAOLIB_DEBUG ((LM_DEBUG, ACE_TEXT ("instance_ = %@"),
127 TAO_TSS_Singleton<TYPE, ACE_LOCK>::instance_i ()));
128 TAOLIB_DEBUG ((LM_DEBUG, ACE_END_DUMP));
129 #endif /* ACE_HAS_DUMP */
132 template <class TYPE, class ACE_LOCK> TAO_TSS_Singleton<TYPE, ACE_LOCK> *&
133 TAO_TSS_Singleton<TYPE, ACE_LOCK>::instance_i ()
135 return TAO_TSS_Singleton<TYPE, ACE_LOCK>::singleton_;
138 template <class TYPE, class ACE_LOCK> TYPE *
139 TAO_TSS_Singleton<TYPE, ACE_LOCK>::instance ()
141 ACE_TRACE ("TAO_TSS_Singleton<TYPE, ACE_LOCK>::instance");
143 TAO_TSS_Singleton<TYPE, ACE_LOCK> *&singleton =
144 TAO_TSS_Singleton<TYPE, ACE_LOCK>::instance_i ();
146 // Perform the Double-Check pattern...
147 if (singleton == 0)
149 if (TAO_Singleton_Manager::starting_up () ||
150 TAO_Singleton_Manager::shutting_down ())
152 // The program is still starting up, and therefore assumed
153 // to be single threaded. There's no need to double-check.
154 // Or, the TAO_Singleton_Manager instance has been destroyed,
155 // so the preallocated lock is not available. Either way,
156 // don't register for destruction with the
157 // TAO_Singleton_Manager: we'll have to leak this instance.
159 ACE_NEW_RETURN (singleton, (TAO_TSS_Singleton<TYPE, ACE_LOCK>), 0);
161 else
163 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
164 // Obtain a lock from the ACE_Object_Manager. The pointer
165 // is static, so we only obtain one per TAO_Singleton
166 // instantiation.
167 static ACE_LOCK *lock = 0;
168 if (ACE_Object_Manager::get_singleton_lock (lock) != 0)
169 // Failed to acquire the lock!
170 return 0;
172 ACE_GUARD_RETURN (ACE_LOCK, ace_mon, *lock, 0);
174 if (singleton == 0)
176 #endif /* ACE_MT_SAFE */
177 ACE_NEW_RETURN (singleton, (TAO_TSS_Singleton<TYPE, ACE_LOCK>),
180 // Register for destruction with TAO_Singleton_Manager.
181 TAO_Singleton_Manager::at_exit (singleton, 0, typeid (TYPE).name ());
182 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
184 #endif /* ACE_MT_SAFE */
188 return ACE_TSS_GET (&singleton->instance_, TYPE);
191 template <class TYPE, class ACE_LOCK> void
192 TAO_TSS_Singleton<TYPE, ACE_LOCK>::cleanup (void *)
194 delete this;
195 TAO_TSS_Singleton<TYPE, ACE_LOCK>::instance_i () = 0;
198 TAO_END_VERSIONED_NAMESPACE_DECL
200 #endif /* TAO_SINGLETON_CPP */