ACE_Thread_Descriptor Class Reference

Information for controlling threads that run under the control of the <Thread_Manager>. More...

#include <Thread_Manager.h>

Inheritance diagram for ACE_Thread_Descriptor:

Inheritance graph
[legend]
Collaboration diagram for ACE_Thread_Descriptor:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 ACE_Thread_Descriptor (void)
ACE_thread_t self (void) const
 Unique thread id.
void self (ACE_hthread_t &)
 Unique handle to thread (used by Win32 and AIX).
void dump (void) const
 Dump the state of an object.
void log_msg_cleanup (ACE_Log_Msg *log_msg)
int at_exit (ACE_At_Thread_Exit *cleanup)
int at_exit (ACE_At_Thread_Exit &cleanup)
int at_exit (void *object, ACE_CLEANUP_FUNC cleanup_hook, void *param)
 ~ACE_Thread_Descriptor (void)
 Do nothing destructor to keep some compilers happy.
void acquire_release (void)
void acquire (void)
void release (void)
void set_next (ACE_Thread_Descriptor *td)
ACE_Thread_Descriptorget_next (void) const

Private Member Functions

void reset (ACE_Thread_Manager *tm)
 Reset this thread descriptor.
void at_pop (int apply=1)
void at_push (ACE_At_Thread_Exit *cleanup, bool is_owner=false)
void do_at_exit (void)
 Run the AT_Thread_Exit hooks.
void terminate (void)
 Terminate realize the cleanup process to thread termination.

Private Attributes

ACE_Log_Msglog_msg_
ACE_At_Thread_Exitat_exit_list_
 The AT_Thread_Exit list.
ACE_Cleanup_Info cleanup_info_
ACE_Thread_Managertm_
ACE_DEFAULT_THREAD_MANAGER_LOCK * sync_
 Registration lock to prevent premature removal of thread descriptor.
bool terminated_
 Keep track of termination status.

Friends

class ACE_At_Thread_Exit
class ACE_Thread_Manager
class ACE_Double_Linked_List< ACE_Thread_Descriptor >
class ACE_Double_Linked_List_Iterator< ACE_Thread_Descriptor >

Detailed Description

Information for controlling threads that run under the control of the <Thread_Manager>.

Definition at line 229 of file Thread_Manager.h.


Constructor & Destructor Documentation

ACE_Thread_Descriptor::ACE_Thread_Descriptor ( void   ) 

Definition at line 262 of file Thread_Manager.cpp.

References ACE_DEFAULT_THREAD_MANAGER_LOCK, ACE_NEW, and ACE_TRACE.

00263   : log_msg_ (0),
00264     at_exit_list_ (0),
00265     terminated_ (false)
00266 {
00267   ACE_TRACE ("ACE_Thread_Descriptor::ACE_Thread_Descriptor");
00268   ACE_NEW (this->sync_,
00269            ACE_DEFAULT_THREAD_MANAGER_LOCK);
00270 }

ACE_Thread_Descriptor::~ACE_Thread_Descriptor ( void   ) 

Do nothing destructor to keep some compilers happy.

Definition at line 89 of file Thread_Manager.cpp.

References sync_.

00090 {
00091   delete this->sync_;
00092 }


Member Function Documentation

void ACE_Thread_Descriptor::acquire ( void   ) 

Definition at line 294 of file Thread_Manager.cpp.

References ACE_BIT_DISABLED, ACE_Thread_Manager::ACE_THR_SPAWNED, and sync_.

00295 {
00296   // Just try to acquire the lock then release it.
00297 #if defined (ACE_THREAD_MANAGER_USES_SAFE_SPAWN)
00298   if (ACE_BIT_DISABLED (this->thr_state_, ACE_Thread_Manager::ACE_THR_SPAWNED))
00299 #endif /* ACE_THREAD_MANAGER_USES_SAFE_SPAWN */
00300     {
00301       this->sync_->acquire ();
00302     }
00303 }

void ACE_Thread_Descriptor::acquire_release ( void   ) 

Do nothing but to acquire the thread descriptor's lock and release. This will first check if the thread is registered or not. If it is already registered, there's no need to reacquire the lock again. This is used mainly to get newly spawned thread in synch with thread manager and prevent it from accessing its thread descriptor before it gets fully built. This function is only called from ACE_Log_Msg::thr_desc.

Definition at line 273 of file Thread_Manager.cpp.

References ACE_ASSERT, ACE_BIT_DISABLED, ACE_BIT_ENABLED, ACE_Thread_Manager::ACE_THR_SPAWNED, and sync_.

Referenced by ACE_Log_Msg::thr_desc().

00274 {
00275   // Just try to acquire the lock then release it.
00276 #if defined (ACE_THREAD_MANAGER_USES_SAFE_SPAWN)
00277   if (ACE_BIT_DISABLED (this->thr_state_, ACE_Thread_Manager::ACE_THR_SPAWNED))
00278 #endif /* ACE_THREAD_MANAGER_USES_SAFE_SPAWN */
00279     {
00280       this->sync_->acquire ();
00281       // Acquire the lock before removing <td> from the thread table.  If
00282       // this thread is in the table already, it should simply acquire the
00283       // lock easily.
00284 
00285       // Once we get the lock, we must have registered.
00286       ACE_ASSERT (ACE_BIT_ENABLED (this->thr_state_, ACE_Thread_Manager::ACE_THR_SPAWNED));
00287 
00288       this->sync_->release ();
00289       // Release the lock before putting it back to freelist.
00290     }
00291 }

int ACE_Thread_Descriptor::at_exit ( void *  object,
ACE_CLEANUP_FUNC  cleanup_hook,
void *  param 
)

Register an object (or array) for cleanup at thread termination. "cleanup_hook" points to a (global, or static member) function that is called for the object or array when it to be destroyed. It may perform any necessary cleanup specific for that object or its class. "param" is passed as the second parameter to the "cleanup_hook" function; the first parameter is the object (or array) to be destroyed. Returns 0 on success, non-zero on failure: -1 if virtual memory is exhausted or 1 if the object (or arrayt) had already been registered.

Definition at line 219 of file Thread_Manager.cpp.

References ACE_NEW_RETURN, ACE_TRACE, at_pop(), and at_push().

00222 {
00223   ACE_TRACE ("ACE_Thread_Descriptor::at_exit");
00224   // To keep compatibility, when cleanup_hook is null really is a at_pop
00225   // without apply.
00226   if (cleanup_hook == 0)
00227    {
00228      if (this->at_exit_list_!= 0)
00229       this->at_pop(0);
00230    }
00231   else
00232    {
00233      ACE_At_Thread_Exit* cleanup = 0;
00234      ACE_NEW_RETURN (cleanup,
00235                      ACE_At_Thread_Exit_Func (object,
00236                                               cleanup_hook,
00237                                               param),
00238                      -1);
00239      this->at_push (cleanup);
00240    }
00241   return 0;
00242 }

int ACE_Thread_Descriptor::at_exit ( ACE_At_Thread_Exit cleanup  ) 

Register an At_Thread_Exit hook and the ownership is retained for the caller. Normally used when the at_exit hook is created in stack.

Definition at line 127 of file Thread_Manager.cpp.

References ACE_TRACE, and at_push().

00128 {
00129   ACE_TRACE ("ACE_Thread_Descriptor::at_exit");
00130   at_push (&cleanup, 1);
00131   return 0;
00132 }

int ACE_Thread_Descriptor::at_exit ( ACE_At_Thread_Exit cleanup  ) 

Register an At_Thread_Exit hook and the ownership is acquire by Thread_Descriptor, this is the usual case when the AT is dynamically allocated.

Definition at line 135 of file Thread_Manager.cpp.

References ACE_TRACE, and at_push().

Referenced by ACE_Thread_Manager::at_exit().

00136 {
00137   ACE_TRACE ("ACE_Thread_Descriptor::at_exit");
00138   if (cleanup==0)
00139    return -1;
00140   else
00141    {
00142      this->at_push (cleanup);
00143      return 0;
00144    }
00145 }

void ACE_Thread_Descriptor::at_pop ( int  apply = 1  )  [private]

Pop an At_Thread_Exit from at thread termination list, apply the at if apply is true.

Definition at line 95 of file Thread_Manager.cpp.

References ACE_TRACE, ACE_At_Thread_Exit::apply(), at_exit_list_, ACE_At_Thread_Exit::is_owner(), ACE_At_Thread_Exit::next_, and ACE_At_Thread_Exit::was_applied().

Referenced by at_exit(), ACE_At_Thread_Exit::do_apply(), and do_at_exit().

00096 {
00097   ACE_TRACE ("ACE_Thread_Descriptor::at_pop");
00098   // Get first at from at_exit_list
00099   ACE_At_Thread_Exit* at = this->at_exit_list_;
00100   // Remove at from at_exit list
00101   this->at_exit_list_ = at->next_;
00102   // Apply if required
00103   if (apply)
00104    {
00105      at->apply ();
00106      // Do the apply method
00107      at->was_applied (1);
00108      // Mark at has been applied to avoid double apply from
00109      // at destructor
00110    }
00111   // If at is not owner delete at.
00112   if (!at->is_owner ())
00113    delete at;
00114 }

void ACE_Thread_Descriptor::at_push ( ACE_At_Thread_Exit cleanup,
bool  is_owner = false 
) [private]

Push an At_Thread_Exit to at thread termination list and set the ownership of at.

Definition at line 117 of file Thread_Manager.cpp.

References ACE_TRACE, at_exit_list_, ACE_At_Thread_Exit::is_owner(), ACE_At_Thread_Exit::next_, and ACE_At_Thread_Exit::td_.

Referenced by at_exit().

00118 {
00119   ACE_TRACE ("ACE_Thread_Descriptor::at_push");
00120   cleanup->is_owner (is_owner);
00121   cleanup->td_ = this;
00122   cleanup->next_ = at_exit_list_;
00123   at_exit_list_ = cleanup;
00124 }

void ACE_Thread_Descriptor::do_at_exit ( void   )  [private]

Run the AT_Thread_Exit hooks.

Definition at line 148 of file Thread_Manager.cpp.

References ACE_TRACE, at_exit_list_, and at_pop().

Referenced by terminate().

00149 {
00150   ACE_TRACE ("ACE_Thread_Descriptor::do_at_exit");
00151   while (at_exit_list_!=0)
00152     this->at_pop ();
00153 }

void ACE_Thread_Descriptor::dump ( void   )  const

Dump the state of an object.

Definition at line 245 of file Thread_Manager.cpp.

References ACE_BEGIN_DUMP, ACE_DEBUG, ACE_END_DUMP, ACE_TEXT, ACE_TRACE, and LM_DEBUG.

00246 {
00247 #if defined (ACE_HAS_DUMP)
00248   ACE_TRACE ("ACE_Thread_Descriptor::dump");
00249   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00250 
00251   ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nthr_id_ = %d"), this->thr_id_));
00252   ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nthr_handle_ = %d"), this->thr_handle_));
00253   ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\ngrp_id_ = %d"), this->grp_id_));
00254   ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nthr_state_ = %d"), this->thr_state_));
00255   ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\ncleanup_info_.cleanup_hook_ = %x"), this->cleanup_info_.cleanup_hook_));
00256   ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nflags_ = %x\n"), this->flags_));
00257 
00258   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00259 #endif /* ACE_HAS_DUMP */
00260 }

ACE_INLINE ACE_Thread_Descriptor * ACE_Thread_Descriptor::get_next ( void   )  const

Definition at line 166 of file Thread_Manager.inl.

References ACE_TRACE, and ACE_Thread_Descriptor_Base::next_.

00167 {
00168   ACE_TRACE ("ACE_Thread_Descriptor::get_next");
00169   return static_cast<ACE_Thread_Descriptor * ACE_CAST_CONST> (this->next_);
00170 }

ACE_INLINE void ACE_Thread_Descriptor::log_msg_cleanup ( ACE_Log_Msg log_msg  ) 

This cleanup function must be called only for ACE_TSS_cleanup. The ACE_TSS_cleanup delegate Log_Msg instance destruction when Log_Msg cleanup is called before terminate.

Definition at line 150 of file Thread_Manager.inl.

References log_msg_.

00152 {
00153   log_msg_ = log_msg;
00154 }

void ACE_Thread_Descriptor::release ( void   ) 

Definition at line 306 of file Thread_Manager.cpp.

References ACE_BIT_DISABLED, ACE_Thread_Manager::ACE_THR_SPAWNED, and sync_.

00307 {
00308   // Just try to acquire the lock then release it.
00309 #if defined (ACE_THREAD_MANAGER_USES_SAFE_SPAWN)
00310   if (ACE_BIT_DISABLED (this->thr_state_, ACE_Thread_Manager::ACE_THR_SPAWNED))
00311 #endif /* ACE_THREAD_MANAGER_USES_SAFE_SPAWN */
00312     {
00313       this->sync_->release ();
00314       // Release the lock before putting it back to freelist.
00315     }
00316 }

ACE_INLINE void ACE_Thread_Descriptor::reset ( ACE_Thread_Manager tm  )  [private]

Reset this thread descriptor.

Definition at line 174 of file Thread_Manager.inl.

References ACE_TRACE, at_exit_list_, log_msg_, ACE_Thread_Descriptor_Base::reset(), terminated_, and tm_.

00175 {
00176   ACE_TRACE ("ACE_Thread_Descriptor::reset");
00177   this->ACE_Thread_Descriptor_Base::reset ();
00178   this->at_exit_list_ = 0;
00179     // Start the at_exit hook list.
00180   this->tm_ = tm;
00181     // Setup the Thread_Manager.
00182   this->log_msg_ = 0;
00183   this->terminated_ = false;
00184 }

ACE_INLINE void ACE_Thread_Descriptor::self ( ACE_hthread_t  ) 

Unique handle to thread (used by Win32 and AIX).

Definition at line 143 of file Thread_Manager.inl.

References ACE_TRACE, and ACE_Thread_Descriptor_Base::thr_handle_.

00144 {
00145   ACE_TRACE ("ACE_Thread_Descriptor::self");
00146   handle = this->thr_handle_;
00147 }

ACE_INLINE ACE_thread_t ACE_Thread_Descriptor::self ( void   )  const

Unique thread id.

Definition at line 134 of file Thread_Manager.inl.

References ACE_TRACE, and ACE_Thread_Descriptor_Base::thr_id_.

Referenced by ACE_Thread_Manager::thr_self().

00135 {
00136   ACE_TRACE ("ACE_Thread_Descriptor::self");
00137   return this->thr_id_;
00138 }

ACE_INLINE void ACE_Thread_Descriptor::set_next ( ACE_Thread_Descriptor td  ) 

Set/get the next_ pointer. These are required by the ACE_Free_List.

Definition at line 158 of file Thread_Manager.inl.

References ACE_TRACE, and ACE_Thread_Descriptor_Base::next_.

00159 {
00160   ACE_TRACE ("ACE_Thread_Descriptor::set_next");
00161   this->next_ = td;
00162 }

void ACE_Thread_Descriptor::terminate ( void   )  [private]

Terminate realize the cleanup process to thread termination.

Definition at line 156 of file Thread_Manager.cpp.

References ACE_BIT_DISABLED, ACE_BIT_ENABLED, ACE_LOG_MSG, ACE_SET_BITS, ACE_Thread_Manager::ACE_THR_JOINING, ACE_Thread_Manager::ACE_THR_TERMINATED, ACE_TRACE, do_at_exit(), log_msg_, ACE_Thread_Manager::register_as_terminated(), ACE_Thread_Manager::remove_thr(), terminated_, THR_DAEMON, THR_DETACHED, THR_JOINABLE, and tm_.

Referenced by ACE_Thread_Manager::exit().

00157 {
00158   ACE_TRACE ("ACE_Thread_Descriptor::terminate");
00159 
00160   if (!terminated_)
00161    {
00162      ACE_Log_Msg* log_msg = this->log_msg_;
00163      terminated_ = true;
00164      // Run at_exit hooks
00165      this->do_at_exit ();
00166      // We must remove Thread_Descriptor from Thread_Manager list
00167      if (this->tm_ != 0)
00168       {
00169          int close_handle = 0;
00170 
00171 #if !defined (ACE_HAS_VXTHREADS)
00172          // Threads created with THR_DAEMON shouldn't exist here, but
00173          // just to be safe, let's put it here.
00174 
00175          if (ACE_BIT_DISABLED (this->thr_state_, ACE_Thread_Manager::ACE_THR_JOINING))
00176            {
00177              if (ACE_BIT_DISABLED (this->flags_, THR_DETACHED | THR_DAEMON)
00178                  || ACE_BIT_ENABLED (this->flags_, THR_JOINABLE))
00179                {
00180                  // Mark thread as terminated.
00181                  ACE_SET_BITS (this->thr_state_, ACE_Thread_Manager::ACE_THR_TERMINATED);
00182                  tm_->register_as_terminated (this);
00183                  // Must copy the information here because td will be
00184                  // "freed" below.
00185                }
00186 #if defined (ACE_WIN32)
00187              else
00188                {
00189                  close_handle = 1;
00190                }
00191 #endif /* ACE_WIN32 */
00192            }
00193 #endif /* !ACE_HAS_VXTHREADS */
00194 
00195          // Remove thread descriptor from the table.
00196          if (this->tm_ != 0)
00197            tm_->remove_thr (this, close_handle);
00198       }
00199 
00200      // Check if we need delete ACE_Log_Msg instance
00201      // If ACE_TSS_cleanup was not executed first log_msg == 0
00202      if (log_msg == 0)
00203       {
00204         // Only inform to ACE_TSS_cleanup that it must delete the log instance
00205         // setting ACE_LOG_MSG thr_desc to 0.
00206         ACE_LOG_MSG->thr_desc (0);
00207       }
00208      else
00209       {
00210         // Thread_Descriptor is the owner of the Log_Msg instance!!
00211         // deleted.
00212         this->log_msg_ = 0;
00213         delete log_msg;
00214       }
00215    }
00216 }


Friends And Related Function Documentation

friend class ACE_At_Thread_Exit [friend]

Definition at line 231 of file Thread_Manager.h.

friend class ACE_Double_Linked_List< ACE_Thread_Descriptor > [friend]

Reimplemented from ACE_Thread_Descriptor_Base.

Definition at line 233 of file Thread_Manager.h.

friend class ACE_Double_Linked_List_Iterator< ACE_Thread_Descriptor > [friend]

Reimplemented from ACE_Thread_Descriptor_Base.

Definition at line 234 of file Thread_Manager.h.

friend class ACE_Thread_Manager [friend]

Reimplemented from ACE_Thread_Descriptor_Base.

Definition at line 232 of file Thread_Manager.h.


Member Data Documentation

ACE_At_Thread_Exit* ACE_Thread_Descriptor::at_exit_list_ [private]

The AT_Thread_Exit list.

Definition at line 329 of file Thread_Manager.h.

Referenced by at_pop(), at_push(), do_at_exit(), and reset().

ACE_Cleanup_Info ACE_Thread_Descriptor::cleanup_info_ [private]

Stores the cleanup info for a thread.

Note:
This should be generalized to be a stack of ACE_Cleanup_Info's.

Definition at line 335 of file Thread_Manager.h.

Referenced by ACE_Thread_Manager::run_thread_exit_hooks().

ACE_Log_Msg* ACE_Thread_Descriptor::log_msg_ [private]

Thread_Descriptor is the ownership of ACE_Log_Msg if log_msg_!=0 This can occur because ACE_TSS_cleanup was executed before terminate.

Definition at line 326 of file Thread_Manager.h.

Referenced by log_msg_cleanup(), reset(), and terminate().

ACE_DEFAULT_THREAD_MANAGER_LOCK* ACE_Thread_Descriptor::sync_ [private]

Registration lock to prevent premature removal of thread descriptor.

Definition at line 342 of file Thread_Manager.h.

Referenced by acquire(), acquire_release(), ACE_Thread_Manager::append_thr(), release(), and ~ACE_Thread_Descriptor().

bool ACE_Thread_Descriptor::terminated_ [private]

Keep track of termination status.

Definition at line 345 of file Thread_Manager.h.

Referenced by reset(), and terminate().

ACE_Thread_Manager* ACE_Thread_Descriptor::tm_ [private]

Pointer to an ACE_Thread_Manager or NULL if there's no ACE_Thread_Manager>

Definition at line 339 of file Thread_Manager.h.

Referenced by ACE_Thread_Manager::append_thr(), ACE_Thread_Manager::remove_thr(), reset(), and terminate().


The documentation for this class was generated from the following files:
Generated on Tue Feb 2 17:35:45 2010 for ACE by  doxygen 1.4.7