ACE_Recursive_Thread_Mutex Class Reference

Implement a C++ wrapper that allows nested acquisition and release of a mutex that occurs in the same thread. More...

#include <Recursive_Thread_Mutex.h>

Collaboration diagram for ACE_Recursive_Thread_Mutex:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 ACE_Recursive_Thread_Mutex (const ACE_TCHAR *name=0, ACE_mutexattr_t *arg=0)
 Initialize a recursive mutex.

 ~ACE_Recursive_Thread_Mutex (void)
 Implicitly release a recursive mutex.

int remove (void)
int acquire (void)
int acquire (ACE_Time_Value &tv)
int acquire (ACE_Time_Value *tv)
int tryacquire (void)
int acquire_read (void)
int acquire_write (void)
int tryacquire_read (void)
int tryacquire_write (void)
int tryacquire_write_upgrade (void)
int release (void)
ACE_thread_t get_thread_id (void)
 Return the id of the thread that currently owns the mutex.

int get_nesting_level (void)
ACE_recursive_thread_mutex_tmutex (void)
 Returns a reference to the recursive mutex;.

ACE_thread_mutex_t & get_nesting_mutex (void)
 Returns a reference to the recursive mutex's internal mutex;.

void dump (void) const
 Dump the state of an object.


Public Attributes

 ACE_ALLOC_HOOK_DECLARE
 Declare the dynamic allocation hooks.


Protected Member Functions

void set_thread_id (ACE_thread_t t)

Protected Attributes

ACE_recursive_thread_mutex_t lock_
 Recursive mutex.

int removed_

Private Member Functions

void operator= (const ACE_Recursive_Thread_Mutex &)
 ACE_Recursive_Thread_Mutex (const ACE_Recursive_Thread_Mutex &)

Detailed Description

Implement a C++ wrapper that allows nested acquisition and release of a mutex that occurs in the same thread.

Definition at line 41 of file Recursive_Thread_Mutex.h.


Constructor & Destructor Documentation

ACE_BEGIN_VERSIONED_NAMESPACE_DECL ACE_Recursive_Thread_Mutex::ACE_Recursive_Thread_Mutex const ACE_TCHAR name = 0,
ACE_mutexattr_t *  arg = 0
 

Initialize a recursive mutex.

Definition at line 27 of file Recursive_Thread_Mutex.cpp.

References ACE_ERROR, ACE_LIB_TEXT, ACE_TCHAR, LM_ERROR, and ACE_OS::recursive_mutex_init().

00029   : removed_ (0)
00030 {
00031   // ACE_TRACE ("ACE_Recursive_Thread_Mutex::ACE_Recursive_Thread_Mutex");
00032    if (ACE_OS::recursive_mutex_init (&this->lock_,
00033                                      name,
00034                                      arg) == -1)
00035      ACE_ERROR ((LM_ERROR,
00036                  ACE_LIB_TEXT ("%p\n"),
00037                  ACE_LIB_TEXT ("recursive_mutex_init")));
00038 }

ACE_Recursive_Thread_Mutex::~ACE_Recursive_Thread_Mutex void   ) 
 

Implicitly release a recursive mutex.

Definition at line 40 of file Recursive_Thread_Mutex.cpp.

References remove().

00041 {
00042   // ACE_TRACE ("ACE_Recursive_Thread_Mutex::~ACE_Recursive_Thread_Mutex");
00043   this->remove ();
00044 }

ACE_Recursive_Thread_Mutex::ACE_Recursive_Thread_Mutex const ACE_Recursive_Thread_Mutex  )  [private]
 

Definition at line 111 of file Recursive_Thread_Mutex.cpp.

00112 {
00113 }


Member Function Documentation

ACE_INLINE int ACE_Recursive_Thread_Mutex::acquire ACE_Time_Value tv  ) 
 

If == 0 the call <acquire()> directly. Otherwise, Block the thread until we acquire the mutex or until times out, in which case -1 is returned with == . Note that <*tv> is assumed to be in "absolute" rather than "relative" time. The value of <*tv> is updated upon return to show the actual (absolute) acquisition time.

Definition at line 59 of file Recursive_Thread_Mutex.inl.

References ACE_OS::recursive_mutex_lock().

00060 {
00061   return ACE_OS::recursive_mutex_lock (&this->lock_, tv);
00062 }

ACE_INLINE int ACE_Recursive_Thread_Mutex::acquire ACE_Time_Value tv  ) 
 

Block the thread until we acquire the mutex or until tv times out, in which case -1 is returned with == . Note that tv is assumed to be in "absolute" rather than "relative" time. The value of tv is updated upon return to show the actual (absolute) acquisition time.

Definition at line 53 of file Recursive_Thread_Mutex.inl.

References ACE_OS::recursive_mutex_lock().

00054 {
00055   return ACE_OS::recursive_mutex_lock (&this->lock_, tv);
00056 }

ACE_INLINE int ACE_Recursive_Thread_Mutex::acquire void   ) 
 

Acquire a recursive mutex (will increment the nesting level and not deadmutex if the owner of the mutex calls this method more than once).

Definition at line 35 of file Recursive_Thread_Mutex.inl.

References ACE_OS::recursive_mutex_lock().

Referenced by ACE_Log_Msg::acquire(), acquire_read(), and acquire_write().

00036 {
00037   return ACE_OS::recursive_mutex_lock (&this->lock_);
00038 }

ACE_INLINE int ACE_Recursive_Thread_Mutex::acquire_read void   ) 
 

Acquire mutex ownership. This calls and is only here to make the interface consistent with the other synchronization APIs.

Definition at line 65 of file Recursive_Thread_Mutex.inl.

References acquire().

00066 {
00067   return this->acquire ();
00068 }

ACE_INLINE int ACE_Recursive_Thread_Mutex::acquire_write void   ) 
 

Acquire mutex ownership. This calls and is only here to make the interface consistent with the other synchronization APIs.

Definition at line 71 of file Recursive_Thread_Mutex.inl.

References acquire().

00072 {
00073   return this->acquire ();
00074 }

void ACE_Recursive_Thread_Mutex::dump void   )  const
 

Dump the state of an object.

Definition at line 116 of file Recursive_Thread_Mutex.cpp.

References ACE_BEGIN_DUMP, ACE_DEBUG, ACE_END_DUMP, and LM_DEBUG.

Referenced by ACE_Future_Rep< T >::dump().

00117 {
00118 #if defined (ACE_HAS_DUMP)
00119 // ACE_TRACE ("ACE_Recursive_Thread_Mutex::dump");
00120 
00121   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00122   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00123 #endif /* ACE_HAS_DUMP */
00124 }

int ACE_Recursive_Thread_Mutex::get_nesting_level void   ) 
 

Return the nesting level of the recursion. When a thread has acquired the mutex for the first time, the nesting level == 1. The nesting level is incremented every time the thread acquires the mutex recursively. Note that if the ACE_HAS_RECURSIVE_MUTEXES macro is enabled then this method may return -1 on platforms that do not expose the internal count.

Definition at line 82 of file Recursive_Thread_Mutex.cpp.

References ACE_NOTSUP_RETURN, ACE_OS::mutex_lock(), ACE_OS::mutex_unlock(), and ACE_recursive_thread_mutex_t::nesting_level_.

00083 {
00084   // ACE_TRACE ("ACE_Recursive_Thread_Mutex::get_nesting_level");
00085 #if defined (ACE_HAS_WINCE) || defined (ACE_VXWORKS) 
00086   ACE_NOTSUP_RETURN (-1);
00087 #elif defined (ACE_HAS_RECURSIVE_MUTEXES)
00088   // Nothing inside of a CRITICAL_SECTION object should ever be
00089   // accessed directly.  It is documented to change at any time.
00090 # if defined (ACE_WIN64) && !defined(_M_AMD64)
00091   // Things are different on Windows XP 64-bit. However, as of Feb 2006
00092   // Windows XP 64-bit edition on Intel EM64T w/ VC8, LockCount is
00093   // decremented at first acquire and then doesn't change. RecursionCount,
00094   // however, works the same as Win32, below.
00095   return this->lock_.LockCount + 1;
00096 # elif defined (ACE_WIN32)
00097   // This is really a Win32-ism...
00098   return this->lock_.RecursionCount;
00099 # else
00100   ACE_NOTSUP_RETURN (-1);
00101 # endif /* ACE_HAS_RECURSIVE_MUTEXES */
00102 #else
00103   int nesting_level = 0;
00104   ACE_OS::mutex_lock (&this->lock_.nesting_mutex_);
00105   nesting_level = this->lock_.nesting_level_;
00106   ACE_OS::mutex_unlock (&this->lock_.nesting_mutex_);
00107   return nesting_level;
00108 #endif /* !ACE_HAS_WINCE */
00109 }

ACE_INLINE ACE_thread_mutex_t & ACE_Recursive_Thread_Mutex::get_nesting_mutex void   ) 
 

Returns a reference to the recursive mutex's internal mutex;.

Definition at line 14 of file Recursive_Thread_Mutex.inl.

References ACE_recursive_thread_mutex_t::nesting_mutex_.

Referenced by ACE_Condition< ACE_Recursive_Thread_Mutex >::wait().

00015 {
00016 #if defined (ACE_HAS_RECURSIVE_MUTEXES)
00017   return static_cast<ACE_thread_mutex_t &> (lock_);
00018 #else
00019   return lock_.nesting_mutex_;
00020 #endif /* ACE_HAS_RECURSIVE_MUTEXES */
00021 }

ACE_thread_t ACE_Recursive_Thread_Mutex::get_thread_id void   ) 
 

Return the id of the thread that currently owns the mutex.

Definition at line 62 of file Recursive_Thread_Mutex.cpp.

References ACE_thread_t, ENOTSUP, ACE_OS::mutex_lock(), ACE_OS::mutex_unlock(), and ACE_recursive_thread_mutex_t::owner_id_.

00063 {
00064   // ACE_TRACE ("ACE_Recursive_Thread_Mutex::get_thread_id");
00065 #if defined (ACE_HAS_RECURSIVE_MUTEXES)
00066   // @@ The structure CriticalSection in Win32 doesn't hold the thread
00067   // handle of the thread that owns the lock.  However it is still not
00068   // clear at this point how to translate a thread handle to its
00069   // corresponding thread id.
00070   errno = ENOTSUP;
00071   return ACE_OS::NULL_thread;
00072 #else
00073   ACE_thread_t owner_id;
00074   ACE_OS::mutex_lock (&this->lock_.nesting_mutex_);
00075   owner_id = this->lock_.owner_id_;
00076   ACE_OS::mutex_unlock (&this->lock_.nesting_mutex_);
00077   return owner_id;
00078 #endif /* ACE_WIN32 */
00079 }

ACE_BEGIN_VERSIONED_NAMESPACE_DECL ACE_INLINE ACE_recursive_thread_mutex_t & ACE_Recursive_Thread_Mutex::mutex void   ) 
 

Returns a reference to the recursive mutex;.

Definition at line 8 of file Recursive_Thread_Mutex.inl.

Referenced by ACE_Condition< ACE_Recursive_Thread_Mutex >::wait().

00009 {
00010   return lock_;
00011 }

void ACE_Recursive_Thread_Mutex::operator= const ACE_Recursive_Thread_Mutex  )  [private]
 

ACE_INLINE int ACE_Recursive_Thread_Mutex::release void   ) 
 

Releases a recursive mutex (will not release mutex until all the nesting level drops to 0, which means the mutex is no longer held).

Definition at line 41 of file Recursive_Thread_Mutex.inl.

References ACE_OS::recursive_mutex_unlock().

Referenced by ACE_Log_Msg::release().

00042 {
00043   return ACE_OS::recursive_mutex_unlock (&this->lock_);
00044 }

int ACE_Recursive_Thread_Mutex::remove void   ) 
 

Implicitly release a recursive mutex. Note that only one thread should call this method since it doesn't protect against race conditions.

Definition at line 47 of file Recursive_Thread_Mutex.cpp.

References ACE_OS::recursive_mutex_destroy().

Referenced by ~ACE_Recursive_Thread_Mutex().

00048 {
00049 // ACE_TRACE ("ACE_Recursive_Thread_Mutex::remove");
00050   int result = 0;
00051   if (this->removed_ == 0)
00052     {
00053       this->removed_ = 1;
00054       result = ACE_OS::recursive_mutex_destroy (&this->lock_);
00055     }
00056   return result;
00057 }

ACE_INLINE void ACE_Recursive_Thread_Mutex::set_thread_id ACE_thread_t  t  )  [protected]
 

Definition at line 24 of file Recursive_Thread_Mutex.inl.

References ACE_recursive_thread_mutex_t::owner_id_.

00025 {
00026 // ACE_TRACE ("ACE_Recursive_Thread_Mutex::set_thread_id");
00027 #if defined (ACE_HAS_RECURSIVE_MUTEXES)
00028   ACE_UNUSED_ARG (t);
00029 #else  /* ! ACE_HAS_RECURSIVE_MUTEXES */
00030   this->lock_.owner_id_ = t;
00031 #endif /* ! ACE_HAS_RECURSIVE_MUTEXES */
00032 }

ACE_INLINE int ACE_Recursive_Thread_Mutex::tryacquire void   ) 
 

Conditionally acquire a recursive mutex (i.e., won't block). Returns -1 on failure. If we "failed" because someone else already had the lock, is set to .

Definition at line 47 of file Recursive_Thread_Mutex.inl.

References ACE_OS::recursive_mutex_trylock().

Referenced by tryacquire_read(), and tryacquire_write().

00048 {
00049   return ACE_OS::recursive_mutex_trylock (&this->lock_);
00050 }

ACE_INLINE int ACE_Recursive_Thread_Mutex::tryacquire_read void   ) 
 

Conditionally acquire mutex (i.e., won't block). This calls and is only here to make the interface consistent with the other synchronization APIs. Returns -1 on failure. If we "failed" because someone else already had the lock, is set to .

Definition at line 77 of file Recursive_Thread_Mutex.inl.

References tryacquire().

00078 {
00079   return this->tryacquire ();
00080 }

ACE_INLINE int ACE_Recursive_Thread_Mutex::tryacquire_write void   ) 
 

Conditionally acquire mutex (i.e., won't block). This calls and is only here to make the interface consistent with the other synchronization APIs. Returns -1 on failure. If we "failed" because someone else already had the lock, is set to .

Definition at line 83 of file Recursive_Thread_Mutex.inl.

References tryacquire().

00084 {
00085   return this->tryacquire ();
00086 }

ACE_INLINE int ACE_Recursive_Thread_Mutex::tryacquire_write_upgrade void   ) 
 

This is only here to make the ACE_Recursive_Thread_Mutex interface consistent with the other synchronization APIs. Assumes the caller has already acquired the mutex using one of the above calls, and returns 0 (success) always.

Definition at line 89 of file Recursive_Thread_Mutex.inl.

00090 {
00091   return 0;
00092 }


Member Data Documentation

ACE_Recursive_Thread_Mutex::ACE_ALLOC_HOOK_DECLARE
 

Declare the dynamic allocation hooks.

Definition at line 163 of file Recursive_Thread_Mutex.h.

ACE_recursive_thread_mutex_t ACE_Recursive_Thread_Mutex::lock_ [protected]
 

Recursive mutex.

Definition at line 170 of file Recursive_Thread_Mutex.h.

int ACE_Recursive_Thread_Mutex::removed_ [protected]
 

Keeps track of whether has been called yet to avoid multiple calls, e.g., explicitly and implicitly in the destructor. This flag isn't protected by a lock, so make sure that you don't have multiple threads simultaneously calling on the same object, which is a bad idea anyway...

Definition at line 177 of file Recursive_Thread_Mutex.h.


The documentation for this class was generated from the following files:
Generated on Thu Nov 9 11:27:50 2006 for ACE by doxygen 1.3.6