Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

ACE_TSS_Singleton Class Template Reference

This class uses the Adapter pattern to turn ordinary classes into Thread-specific Singletons optimized with the Double-Checked Locking optimization pattern. More...

#include <Singleton.h>

Inheritance diagram for ACE_TSS_Singleton:

Inheritance graph
[legend]
Collaboration diagram for ACE_TSS_Singleton:

Collaboration graph
[legend]
List of all members.

Public Methods

virtual void cleanup (void *param=0)
 Cleanup method, used by <ace_cleanup_destroyer> to destroy the singleton. More...


Static Public Methods

TYPE * instance (void)
 Global access point to the singleton. More...

void dump (void)
 Dump the state of the object. More...


Protected Methods

 ACE_TSS_Singleton (void)
 Default constructor. More...

 ACE_TSS_TYPE (TYPE) instance_
 Contained instance. More...

void operator= (const ACE_TSS_Singleton< TYPE, ACE_LOCK > &)
 ACE_TSS_Singleton (const ACE_TSS_Singleton< TYPE, ACE_LOCK > &)

Static Protected Methods

ACE_TSS_Singleton< TYPE, ACE_LOCK > *& instance_i (void)
 Get pointer to the TSS Singleton instance. More...


Static Protected Attributes

ACE_TSS_Singleton< TYPE, ACE_LOCK > * singleton_ = 0
 Pointer to the Singleton (ACE_Cleanup) instance. More...


Detailed Description

template<class TYPE, class ACE_LOCK>
class ACE_TSS_Singleton< TYPE, ACE_LOCK >

This class uses the Adapter pattern to turn ordinary classes into Thread-specific Singletons optimized with the Double-Checked Locking optimization pattern.

This implementation is another variation on the GoF Singleton pattern. In this case, a single <ACE_TSS_Singleton<TYPE, LOCK> > instance is allocated here, not a <TYPE> instance. Each call to the <instance> static method returns a Singleton whose pointer resides in thread-specific storage. As with ACE_Singleton, we use the ACE_Object_Manager so that the Singleton can be cleaned up when the process exits. For this scheme to work, a (static) cleanup() function must be provided. ACE_Singleton provides one so that TYPE doesn't need to.

Definition at line 167 of file Singleton.h.


Constructor & Destructor Documentation

template<class TYPE, class ACE_LOCK>
ACE_INLINE ACE_TSS_Singleton< TYPE, ACE_LOCK >::ACE_TSS_Singleton void    [protected]
 

Default constructor.

Definition at line 20 of file Singleton.i.

00021 {
00022 }

template<class TYPE, class ACE_LOCK>
ACE_TSS_Singleton< TYPE, ACE_LOCK >::ACE_TSS_Singleton const ACE_TSS_Singleton< TYPE, ACE_LOCK > &    [protected]
 


Member Function Documentation

template<class TYPE, class ACE_LOCK>
ACE_TSS_Singleton< TYPE, ACE_LOCK >::ACE_TSS_TYPE TYPE    [protected]
 

Contained instance.

template<class TYPE, class ACE_LOCK>
void ACE_TSS_Singleton< TYPE, ACE_LOCK >::cleanup void *    param = 0 [virtual]
 

Cleanup method, used by <ace_cleanup_destroyer> to destroy the singleton.

Reimplemented from ACE_Cleanup.

Definition at line 287 of file Singleton.cpp.

References instance_i.

Referenced by ACE_Unmanaged_TSS_Singleton::close.

00288 {
00289   delete this;
00290   ACE_TSS_Singleton<TYPE, ACE_LOCK>::instance_i () = 0;
00291 }

template<class TYPE, class ACE_LOCK>
void ACE_TSS_Singleton< TYPE, ACE_LOCK >::dump void    [static]
 

Dump the state of the object.

Reimplemented in ACE_Unmanaged_TSS_Singleton.

Definition at line 208 of file Singleton.cpp.

References ACE_DEBUG, ACE_END_DUMP, ACE_LIB_TEXT, ACE_TRACE, instance_i, and LM_DEBUG.

00209 {
00210   ACE_TRACE ("ACE_TSS_Singleton<TYPE, ACE_LOCK>::dump");
00211 
00212 #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00213   ACE_DEBUG ((LM_DEBUG,  ACE_LIB_TEXT ("instance_ = %x"),
00214               ACE_TSS_Singleton<TYPE, ACE_LOCK>::instance_i ()));
00215   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00216 #endif /* ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES */
00217 }

template<class TYPE, class ACE_LOCK>
TYPE * ACE_TSS_Singleton< TYPE, ACE_LOCK >::instance void    [static]
 

Global access point to the singleton.

Reimplemented in ACE_Unmanaged_TSS_Singleton.

Definition at line 234 of file Singleton.cpp.

References ACE_GUARD_RETURN, ACE_NEW_RETURN, ACE_TRACE, ACE_TSS_GET, ACE_Object_Manager::at_exit, instance_i, ACE_Object_Manager::shutting_down, and ACE_Object_Manager::starting_up.

Referenced by ACE_Dynamic::instance.

00235 {
00236   ACE_TRACE ("ACE_TSS_Singleton<TYPE, ACE_LOCK>::instance");
00237 
00238   ACE_TSS_Singleton<TYPE, ACE_LOCK> *&singleton =
00239     ACE_TSS_Singleton<TYPE, ACE_LOCK>::instance_i ();
00240 
00241   // Perform the Double-Check pattern...
00242   if (singleton == 0)
00243     {
00244       if (ACE_Object_Manager::starting_up () ||
00245           ACE_Object_Manager::shutting_down ())
00246         {
00247           // The program is still starting up, and therefore assumed
00248           // to be single threaded.  There's no need to double-check.
00249           // Or, the ACE_Object_Manager instance has been destroyed,
00250           // so the preallocated lock is not available.  Either way,
00251           // don't register for destruction with the
00252           // ACE_Object_Manager:  we'll have to leak this instance.
00253 
00254           ACE_NEW_RETURN (singleton, (ACE_TSS_Singleton<TYPE, ACE_LOCK>), 0);
00255         }
00256       else
00257         {
00258 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00259 
00260           // Obtain a lock from the ACE_Object_Manager.  The pointer
00261           // is static, so we only obtain one per ACE_Singleton instantiation.
00262           static ACE_LOCK *lock = 0;
00263           if (ACE_Object_Manager::get_singleton_lock (lock) != 0)
00264             // Failed to acquire the lock!
00265             return 0;
00266 
00267           ACE_GUARD_RETURN (ACE_LOCK, ace_mon, *lock, 0);
00268 
00269           if (singleton == 0)
00270             {
00271 #endif /* ACE_MT_SAFE */
00272               ACE_NEW_RETURN (singleton, (ACE_TSS_Singleton<TYPE, ACE_LOCK>),
00273                               0);
00274 
00275               // Register for destruction with ACE_Object_Manager.
00276               ACE_Object_Manager::at_exit (singleton);
00277 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00278             }
00279 #endif /* ACE_MT_SAFE */
00280         }
00281     }
00282 
00283   return ACE_TSS_GET (&singleton->instance_, TYPE);
00284 }

template<class TYPE, class ACE_LOCK>
ACE_TSS_Singleton< TYPE, ACE_LOCK > *& ACE_TSS_Singleton< TYPE, ACE_LOCK >::instance_i void    [static, protected]
 

Get pointer to the TSS Singleton instance.

Reimplemented in ACE_Unmanaged_TSS_Singleton.

Definition at line 220 of file Singleton.cpp.

References singleton_.

Referenced by cleanup, dump, and instance.

00221 {
00222 #if defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00223   // Pointer to the Singleton instance.  This works around a bug with
00224   // G++ and it's (mis-)handling of templates and statics...
00225   static ACE_TSS_Singleton<TYPE, ACE_LOCK> *singleton_ = 0;
00226 
00227   return singleton_;
00228 #else
00229   return ACE_TSS_Singleton<TYPE, ACE_LOCK>::singleton_;
00230 #endif /* ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES */
00231 }

template<class TYPE, class ACE_LOCK>
void ACE_TSS_Singleton< TYPE, ACE_LOCK >::operator= const ACE_TSS_Singleton< TYPE, ACE_LOCK > &    [protected]
 


Member Data Documentation

template<class TYPE, class ACE_LOCK>
ACE_TSS_Singleton< TYPE, ACE_LOCK > * ACE_TSS_Singleton< TYPE, ACE_LOCK >::singleton_ = 0 [static, protected]
 

Pointer to the Singleton (ACE_Cleanup) instance.

Reimplemented in ACE_Unmanaged_TSS_Singleton.

Definition at line 382 of file Singleton.cpp.

Referenced by instance_i.


The documentation for this class was generated from the following files:
Generated on Mon Jun 16 12:58:44 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002