Public Member Functions | Static Public Member Functions | Private Attributes | Static Private Attributes

ACE_Thread_Exit Class Reference

Keep exit information for a Thread in thread specific storage. so that the thread-specific exit hooks will get called no matter how the thread exits (e.g., via <ACE_Thread::exit>, C++ or Win32 exception, "falling off the end" of the thread entry point function, etc.). More...

#include <Thread_Exit.h>

Collaboration diagram for ACE_Thread_Exit:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 ACE_Thread_Exit (void)
 Capture the Thread that will be cleaned up automatically.
void thr_mgr (ACE_Thread_Manager *tm)
 Set the ACE_Thread_Manager.
 ~ACE_Thread_Exit (void)

Static Public Member Functions

static ACE_Thread_Exitinstance (void)
 Singleton access point.
static void cleanup (void *instance)

Private Attributes

ACE_Thread_Control thread_control_

Static Private Attributes

static bool is_constructed_ = false

Detailed Description

Keep exit information for a Thread in thread specific storage. so that the thread-specific exit hooks will get called no matter how the thread exits (e.g., via <ACE_Thread::exit>, C++ or Win32 exception, "falling off the end" of the thread entry point function, etc.).

This clever little helper class is stored in thread-specific storage using the <ACE_TSS> wrapper. When a thread exits the <ACE_TSS::cleanup> function deletes this object, thereby closing it down gracefully.

Definition at line 42 of file Thread_Exit.h.


Constructor & Destructor Documentation

ACE_Thread_Exit::ACE_Thread_Exit ( void   ) 

Capture the Thread that will be cleaned up automatically.

Definition at line 73 of file Thread_Exit.cpp.

{
  ACE_OS_TRACE ("ACE_Thread_Exit::ACE_Thread_Exit");
}

ACE_Thread_Exit::~ACE_Thread_Exit ( void   ) 

Destructor calls the thread-specific exit hooks when a thread exits.

Definition at line 92 of file Thread_Exit.cpp.

{
  ACE_OS_TRACE ("ACE_Thread_Exit::~ACE_Thread_Exit");
}


Member Function Documentation

void ACE_Thread_Exit::cleanup ( void *  instance  )  [static]

Cleanup method, used by the ACE_Object_Manager to destroy the singleton.

Definition at line 15 of file Thread_Exit.cpp.

{
  ACE_OS_TRACE ("ACE_Thread_Exit::cleanup");

  delete (ACE_TSS_TYPE (ACE_Thread_Exit) *) instance;

  // Set the thr_exit_ static to null to keep things from crashing if
  // ACE::fini() is enabled here.
  ACE_Thread_Manager::thr_exit_ = 0;

  ACE_Thread_Exit::is_constructed_ = false;
  // All TSS objects have been destroyed.  Reset this flag so
  // ACE_Thread_Exit singleton can be created again.
}

ACE_Thread_Exit * ACE_Thread_Exit::instance ( void   )  [static]

Singleton access point.

Definition at line 35 of file Thread_Exit.cpp.

{
#if defined (ACE_HAS_THREAD_SPECIFIC_STORAGE) || defined (ACE_HAS_TSS_EMULATION)
  ACE_OS_TRACE ("ACE_Thread_Exit::instance");

  // Determines if we were dynamically allocated.
  static ACE_TSS_TYPE (ACE_Thread_Exit) * volatile instance_;

  // Implement the Double Check pattern.

  if (!ACE_Thread_Exit::is_constructed_)
    {
      ACE_MT (ACE_Thread_Mutex *lock =
              ACE_Managed_Object<ACE_Thread_Mutex>::get_preallocated_object
                (ACE_Object_Manager::ACE_THREAD_EXIT_LOCK);
              ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, *lock, 0));

      if (!ACE_Thread_Exit::is_constructed_)
        {
          ACE_NEW_RETURN (instance_,
                          ACE_TSS_TYPE (ACE_Thread_Exit),
                          0);

          ACE_Thread_Exit::is_constructed_ = true;

          ACE_Thread_Manager::set_thr_exit (instance_);
        }
    }

  return ACE_TSS_GET (instance_, ACE_Thread_Exit);
#else
  return 0;
#endif /* ACE_HAS_THREAD_SPECIFIC_STORAGE || ACE_HAS_TSS_EMULATION */
}

void ACE_Thread_Exit::thr_mgr ( ACE_Thread_Manager tm  ) 

Set the ACE_Thread_Manager.

Definition at line 81 of file Thread_Exit.cpp.

{
  ACE_OS_TRACE ("ACE_Thread_Exit::thr_mgr");

  if (tm != 0)
    this->thread_control_.insert (tm, 0);
}


Member Data Documentation

bool ACE_Thread_Exit::is_constructed_ = false [static, private]

Used to detect whether we should create a new instance (or not) within the instance method -- we don't trust the instance_ ptr because the destructor may have run (if ACE::fini() was called). See bug #526. We don't follow the singleton pattern due to dependency issues.

Definition at line 74 of file Thread_Exit.h.

Automatically add/remove the thread from the ACE_Thread_Manager.

Definition at line 65 of file Thread_Exit.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines