00001
00002
00003
00004
00005 #ifndef TAO_SINGLETON_CPP
00006 #define TAO_SINGLETON_CPP
00007
00008 #include "tao/TAO_Singleton.h"
00009
00010 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00011 # pragma once
00012 #endif
00013
00014 #include "tao/TAO_Singleton_Manager.h"
00015
00016 #include "ace/Guard_T.h"
00017 #include "ace/Object_Manager.h"
00018 #include "ace/Log_Msg.h"
00019 #include "ace/os_include/os_typeinfo.h"
00020
00021 #if !defined (__ACE_INLINE__)
00022 #include "tao/TAO_Singleton.inl"
00023 #endif
00024
00025 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00026
00027 template <class TYPE, class ACE_LOCK> void
00028 TAO_Singleton<TYPE, ACE_LOCK>::dump (void)
00029 {
00030 #if defined (ACE_HAS_DUMP)
00031 ACE_TRACE ("TAO_Singleton<TYPE, ACE_LOCK>::dump");
00032
00033 #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00034 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("instance_ = %x"),
00035 TAO_Singleton<TYPE, ACE_LOCK>::instance_i ()));
00036 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00037 #endif
00038 #endif
00039 }
00040
00041 template <class TYPE, class ACE_LOCK> TAO_Singleton<TYPE, ACE_LOCK> *&
00042 TAO_Singleton<TYPE, ACE_LOCK>::instance_i (void)
00043 {
00044 #if defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00045
00046
00047 static TAO_Singleton<TYPE, ACE_LOCK> *singleton_ = 0;
00048
00049 return singleton_;
00050 #else
00051 return TAO_Singleton<TYPE, ACE_LOCK>::singleton_;
00052 #endif
00053 }
00054
00055 template <class TYPE, class ACE_LOCK> TYPE *
00056 TAO_Singleton<TYPE, ACE_LOCK>::instance (void)
00057 {
00058 ACE_TRACE ("TAO_Singleton<TYPE, ACE_LOCK>::instance");
00059
00060 TAO_Singleton<TYPE, ACE_LOCK> *&singleton =
00061 TAO_Singleton<TYPE, ACE_LOCK>::instance_i ();
00062
00063
00064 if (singleton == 0)
00065 {
00066 if (TAO_Singleton_Manager::starting_up () ||
00067 TAO_Singleton_Manager::shutting_down ())
00068 {
00069
00070
00071
00072
00073
00074
00075
00076 ACE_NEW_RETURN (singleton, (TAO_Singleton<TYPE, ACE_LOCK>), 0);
00077 }
00078 else
00079 {
00080 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00081
00082
00083
00084 static ACE_LOCK *lock = 0;
00085 if (ACE_Object_Manager::get_singleton_lock (lock) != 0)
00086
00087 return 0;
00088
00089 ACE_GUARD_RETURN (ACE_LOCK, ace_mon, *lock, 0);
00090
00091 if (singleton == 0)
00092 {
00093 #endif
00094 ACE_NEW_RETURN (singleton, (TAO_Singleton<TYPE, ACE_LOCK>), 0);
00095
00096
00097 TAO_Singleton_Manager::at_exit (singleton, 0, typeid (TYPE).name());
00098 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00099 }
00100 #endif
00101 }
00102 }
00103
00104 return &singleton->instance_;
00105 }
00106
00107 template <class TYPE, class ACE_LOCK> void
00108 TAO_Singleton<TYPE, ACE_LOCK>::cleanup (void *)
00109 {
00110 delete this;
00111 TAO_Singleton<TYPE, ACE_LOCK>::instance_i () = 0;
00112 }
00113
00114 #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00115
00116 template <class TYPE, class ACE_LOCK> TAO_Singleton<TYPE, ACE_LOCK> *
00117 TAO_Singleton<TYPE, ACE_LOCK>::singleton_ = 0;
00118
00119 template <class TYPE, class ACE_LOCK> TAO_TSS_Singleton<TYPE, ACE_LOCK> *
00120 TAO_TSS_Singleton<TYPE, ACE_LOCK>::singleton_ = 0;
00121 #endif
00122
00123
00124 template <class TYPE, class ACE_LOCK> void
00125 TAO_TSS_Singleton<TYPE, ACE_LOCK>::dump (void)
00126 {
00127 #if defined (ACE_HAS_DUMP)
00128 ACE_TRACE ("TAO_TSS_Singleton<TYPE, ACE_LOCK>::dump");
00129
00130 #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00131 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("instance_ = %x"),
00132 TAO_TSS_Singleton<TYPE, ACE_LOCK>::instance_i ()));
00133 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00134 #endif
00135 #endif
00136 }
00137
00138 template <class TYPE, class ACE_LOCK> TAO_TSS_Singleton<TYPE, ACE_LOCK> *&
00139 TAO_TSS_Singleton<TYPE, ACE_LOCK>::instance_i (void)
00140 {
00141 #if defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00142
00143
00144 static TAO_TSS_Singleton<TYPE, ACE_LOCK> *singleton_ = 0;
00145
00146 return singleton_;
00147 #else
00148 return TAO_TSS_Singleton<TYPE, ACE_LOCK>::singleton_;
00149 #endif
00150 }
00151
00152 template <class TYPE, class ACE_LOCK> TYPE *
00153 TAO_TSS_Singleton<TYPE, ACE_LOCK>::instance (void)
00154 {
00155 ACE_TRACE ("TAO_TSS_Singleton<TYPE, ACE_LOCK>::instance");
00156
00157 TAO_TSS_Singleton<TYPE, ACE_LOCK> *&singleton =
00158 TAO_TSS_Singleton<TYPE, ACE_LOCK>::instance_i ();
00159
00160
00161 if (singleton == 0)
00162 {
00163 if (TAO_Singleton_Manager::starting_up () ||
00164 TAO_Singleton_Manager::shutting_down ())
00165 {
00166
00167
00168
00169
00170
00171
00172
00173 ACE_NEW_RETURN (singleton, (TAO_TSS_Singleton<TYPE, ACE_LOCK>), 0);
00174 }
00175 else
00176 {
00177 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00178
00179
00180
00181 static ACE_LOCK *lock = 0;
00182 if (ACE_Object_Manager::get_singleton_lock (lock) != 0)
00183
00184 return 0;
00185
00186 ACE_GUARD_RETURN (ACE_LOCK, ace_mon, *lock, 0);
00187
00188 if (singleton == 0)
00189 {
00190 #endif
00191 ACE_NEW_RETURN (singleton, (TAO_TSS_Singleton<TYPE, ACE_LOCK>),
00192 0);
00193
00194
00195 TAO_Singleton_Manager::at_exit (singleton, 0, typeid (TYPE).name ());
00196 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00197 }
00198 #endif
00199 }
00200 }
00201
00202 return ACE_TSS_GET (&singleton->instance_, TYPE);
00203 }
00204
00205 template <class TYPE, class ACE_LOCK> void
00206 TAO_TSS_Singleton<TYPE, ACE_LOCK>::cleanup (void *)
00207 {
00208 delete this;
00209 TAO_TSS_Singleton<TYPE, ACE_LOCK>::instance_i () = 0;
00210 }
00211
00212 TAO_END_VERSIONED_NAMESPACE_DECL
00213
00214 #endif