TAO_Singleton.cpp

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // TAO_Singleton.cpp,v 1.7 2005/11/02 11:03:27 ossama Exp
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 /* ACE_LACKS_PRAGMA_ONCE */
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 
00020 #if !defined (__ACE_INLINE__)
00021 #include "tao/TAO_Singleton.inl"
00022 #endif /* __ACE_INLINE__ */
00023 
00024 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00025 
00026 template <class TYPE, class ACE_LOCK> void
00027 TAO_Singleton<TYPE, ACE_LOCK>::dump (void)
00028 {
00029   ACE_TRACE ("TAO_Singleton<TYPE, ACE_LOCK>::dump");
00030 
00031 #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00032   ACE_DEBUG ((LM_DEBUG,  ACE_TEXT ("instance_ = %x"),
00033               TAO_Singleton<TYPE, ACE_LOCK>::instance_i ()));
00034   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00035 #endif /* ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES */
00036 }
00037 
00038 template <class TYPE, class ACE_LOCK> TAO_Singleton<TYPE, ACE_LOCK> *&
00039 TAO_Singleton<TYPE, ACE_LOCK>::instance_i (void)
00040 {
00041 #if defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00042   // Pointer to the Singleton instance.  This works around a bug with
00043   // G++ and it's (mis-)handling of templates and statics...
00044   static TAO_Singleton<TYPE, ACE_LOCK> *singleton_ = 0;
00045 
00046   return singleton_;
00047 #else
00048   return TAO_Singleton<TYPE, ACE_LOCK>::singleton_;
00049 #endif /* ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES */
00050 }
00051 
00052 template <class TYPE, class ACE_LOCK> TYPE *
00053 TAO_Singleton<TYPE, ACE_LOCK>::instance (void)
00054 {
00055   ACE_TRACE ("TAO_Singleton<TYPE, ACE_LOCK>::instance");
00056 
00057   TAO_Singleton<TYPE, ACE_LOCK> *&singleton =
00058     TAO_Singleton<TYPE, ACE_LOCK>::instance_i ();
00059 
00060   // Perform the Double-Check pattern...
00061   if (singleton == 0)
00062     {
00063       if (TAO_Singleton_Manager::starting_up () ||
00064           TAO_Singleton_Manager::shutting_down ())
00065         {
00066           // The program is still starting up, and therefore assumed
00067           // to be single threaded.  There's no need to double-check.
00068           // Or, the TAO_Singleton_Manager instance has been destroyed,
00069           // so the preallocated lock is not available.  Either way,
00070           // don't register for destruction with the
00071           // TAO_Singleton_Manager:  we'll have to leak this instance.
00072 
00073           ACE_NEW_RETURN (singleton, (TAO_Singleton<TYPE, ACE_LOCK>), 0);
00074         }
00075       else
00076         {
00077 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00078           // Obtain a lock from the ACE_Object_Manager.  The pointer
00079           // is static, so we only obtain one per TAO_Singleton
00080           // instantiation.
00081           static ACE_LOCK *lock = 0;
00082           if (ACE_Object_Manager::get_singleton_lock (lock) != 0)
00083             // Failed to acquire the lock!
00084             return 0;
00085 
00086           ACE_GUARD_RETURN (ACE_LOCK, ace_mon, *lock, 0);
00087 
00088           if (singleton == 0)
00089             {
00090 #endif /* ACE_MT_SAFE */
00091               ACE_NEW_RETURN (singleton, (TAO_Singleton<TYPE, ACE_LOCK>), 0);
00092 
00093               // Register for destruction with TAO_Singleton_Manager.
00094               TAO_Singleton_Manager::at_exit (singleton);
00095 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00096             }
00097 #endif /* ACE_MT_SAFE */
00098         }
00099     }
00100 
00101   return &singleton->instance_;
00102 }
00103 
00104 template <class TYPE, class ACE_LOCK> void
00105 TAO_Singleton<TYPE, ACE_LOCK>::cleanup (void *)
00106 {
00107   delete this;
00108   TAO_Singleton<TYPE, ACE_LOCK>::instance_i () = 0;
00109 }
00110 
00111 #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00112 // Pointer to the Singleton instance.
00113 template <class TYPE, class ACE_LOCK> TAO_Singleton<TYPE, ACE_LOCK> *
00114 TAO_Singleton<TYPE, ACE_LOCK>::singleton_ = 0;
00115 
00116 template <class TYPE, class ACE_LOCK> TAO_TSS_Singleton<TYPE, ACE_LOCK> *
00117 TAO_TSS_Singleton<TYPE, ACE_LOCK>::singleton_ = 0;
00118 #endif /* !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES) */
00119 
00120 
00121 template <class TYPE, class ACE_LOCK> void
00122 TAO_TSS_Singleton<TYPE, ACE_LOCK>::dump (void)
00123 {
00124   ACE_TRACE ("TAO_TSS_Singleton<TYPE, ACE_LOCK>::dump");
00125 
00126 #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00127   ACE_DEBUG ((LM_DEBUG,  ACE_TEXT ("instance_ = %x"),
00128               TAO_TSS_Singleton<TYPE, ACE_LOCK>::instance_i ()));
00129   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00130 #endif /* ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES */
00131 }
00132 
00133 template <class TYPE, class ACE_LOCK> TAO_TSS_Singleton<TYPE, ACE_LOCK> *&
00134 TAO_TSS_Singleton<TYPE, ACE_LOCK>::instance_i (void)
00135 {
00136 #if defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00137   // Pointer to the Singleton instance.  This works around a bug with
00138   // G++ and it's (mis-)handling of templates and statics...
00139   static TAO_TSS_Singleton<TYPE, ACE_LOCK> *singleton_ = 0;
00140 
00141   return singleton_;
00142 #else
00143   return TAO_TSS_Singleton<TYPE, ACE_LOCK>::singleton_;
00144 #endif /* ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES */
00145 }
00146 
00147 template <class TYPE, class ACE_LOCK> TYPE *
00148 TAO_TSS_Singleton<TYPE, ACE_LOCK>::instance (void)
00149 {
00150   ACE_TRACE ("TAO_TSS_Singleton<TYPE, ACE_LOCK>::instance");
00151 
00152   TAO_TSS_Singleton<TYPE, ACE_LOCK> *&singleton =
00153     TAO_TSS_Singleton<TYPE, ACE_LOCK>::instance_i ();
00154 
00155   // Perform the Double-Check pattern...
00156   if (singleton == 0)
00157     {
00158       if (TAO_Singleton_Manager::starting_up () ||
00159           TAO_Singleton_Manager::shutting_down ())
00160         {
00161           // The program is still starting up, and therefore assumed
00162           // to be single threaded.  There's no need to double-check.
00163           // Or, the TAO_Singleton_Manager instance has been destroyed,
00164           // so the preallocated lock is not available.  Either way,
00165           // don't register for destruction with the
00166           // TAO_Singleton_Manager:  we'll have to leak this instance.
00167 
00168           ACE_NEW_RETURN (singleton, (TAO_TSS_Singleton<TYPE, ACE_LOCK>), 0);
00169         }
00170       else
00171         {
00172 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00173           // Obtain a lock from the ACE_Object_Manager.  The pointer
00174           // is static, so we only obtain one per TAO_Singleton
00175           // instantiation.
00176           static ACE_LOCK *lock = 0;
00177           if (ACE_Object_Manager::get_singleton_lock (lock) != 0)
00178             // Failed to acquire the lock!
00179             return 0;
00180 
00181           ACE_GUARD_RETURN (ACE_LOCK, ace_mon, *lock, 0);
00182 
00183           if (singleton == 0)
00184             {
00185 #endif /* ACE_MT_SAFE */
00186               ACE_NEW_RETURN (singleton, (TAO_TSS_Singleton<TYPE, ACE_LOCK>),
00187                               0);
00188 
00189               // Register for destruction with TAO_Singleton_Manager.
00190               TAO_Singleton_Manager::at_exit (singleton);
00191 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00192             }
00193 #endif /* ACE_MT_SAFE */
00194         }
00195     }
00196 
00197   return ACE_TSS_GET (&singleton->instance_, TYPE);
00198 }
00199 
00200 template <class TYPE, class ACE_LOCK> void
00201 TAO_TSS_Singleton<TYPE, ACE_LOCK>::cleanup (void *)
00202 {
00203   delete this;
00204   TAO_TSS_Singleton<TYPE, ACE_LOCK>::instance_i () = 0;
00205 }
00206 
00207 TAO_END_VERSIONED_NAMESPACE_DECL
00208 
00209 #endif  /* TAO_SINGLETON_CPP */

Generated on Thu Nov 9 11:54:24 2006 for TAO by doxygen 1.3.6