3 //==========================================================================
5 * @file Recursive_Thread_Mutex.h
7 * $Id: Recursive_Thread_Mutex.h 80826 2008-03-04 14:51:23Z wotte $
11 * @author Douglas C. Schmidt <schmidt@cs.wustl.edu> and
12 * Abdullah Sowayan <abdullah.sowayan@lmco.com>
14 //==========================================================================
16 #ifndef ACE_RECURSIVE_THREAD_MUTEX_H
17 #define ACE_RECURSIVE_THREAD_MUTEX_H
18 #include /**/ "ace/pre.h"
20 #include /**/ "ace/ACE_export.h"
22 #if !defined (ACE_LACKS_PRAGMA_ONCE)
24 #endif /* ACE_LACKS_PRAGMA_ONCE */
26 #if !defined (ACE_HAS_THREADS)
27 # include "ace/Null_Mutex.h"
28 #else /* ACE_HAS_THREADS */
29 // ACE platform supports some form of threading.
31 #include "ace/OS_NS_Thread.h"
33 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
36 * @class ACE_Recursive_Thread_Mutex
38 * @brief Implement a C++ wrapper that allows nested acquisition and
39 * release of a mutex that occurs in the same thread.
41 class ACE_Export ACE_Recursive_Thread_Mutex
44 /// Initialize a recursive mutex.
45 ACE_Recursive_Thread_Mutex (const ACE_TCHAR
*name
= 0,
46 ACE_mutexattr_t
*arg
= 0);
48 /// Implicitly release a recursive mutex.
49 ~ACE_Recursive_Thread_Mutex (void);
52 * Implicitly release a recursive mutex. Note that only one thread
53 * should call this method since it doesn't protect against race
59 * Acquire a recursive mutex (will increment the nesting level and
60 * not deadmutex if the owner of the mutex calls this method more
66 * Block the thread until we acquire the mutex or until @a tv times
67 * out, in which case -1 is returned with @c errno == @c ETIME. Note
68 * that @a tv is assumed to be in "absolute" rather than "relative"
69 * time. The value of @a tv is updated upon return to show the
70 * actual (absolute) acquisition time.
72 int acquire (ACE_Time_Value
&tv
);
75 * If @a tv == 0 the call <acquire()> directly. Otherwise, Block the
76 * thread until we acquire the mutex or until @a tv times out, in
77 * which case -1 is returned with @c errno == @c ETIME. Note that
78 * <*tv> is assumed to be in "absolute" rather than "relative" time.
79 * The value of <*tv> is updated upon return to show the actual
80 * (absolute) acquisition time.
82 int acquire (ACE_Time_Value
*tv
);
85 * Conditionally acquire a recursive mutex (i.e., won't block).
86 * Returns -1 on failure. If we "failed" because someone else
87 * already had the lock, @c errno is set to @c EBUSY.
89 int tryacquire (void);
92 * Acquire mutex ownership. This calls <acquire> and is only
93 * here to make the <ACE_Recusive_Thread_Mutex> interface consistent
94 * with the other synchronization APIs.
96 int acquire_read (void);
99 * Acquire mutex ownership. This calls <acquire> and is only
100 * here to make the <ACE_Recusive_Thread_Mutex> interface consistent
101 * with the other synchronization APIs.
103 int acquire_write (void);
106 * Conditionally acquire mutex (i.e., won't block). This calls
107 * <tryacquire> and is only here to make the
108 * <ACE_Recusive_Thread_Mutex> interface consistent with the other
109 * synchronization APIs. Returns -1 on failure. If we "failed"
110 * because someone else already had the lock, @c errno is set to
113 int tryacquire_read (void);
116 * Conditionally acquire mutex (i.e., won't block). This calls
117 * <tryacquire> and is only here to make the
118 * <ACE_Recusive_Thread_Mutex> interface consistent with the other
119 * synchronization APIs. Returns -1 on failure. If we "failed"
120 * because someone else already had the lock, @c errno is set to
123 int tryacquire_write (void);
126 * This is only here to make the ACE_Recursive_Thread_Mutex
127 * interface consistent with the other synchronization APIs.
128 * Assumes the caller has already acquired the mutex using one of
129 * the above calls, and returns 0 (success) always.
131 int tryacquire_write_upgrade (void);
134 * Releases a recursive mutex (will not release mutex until all the
135 * nesting level drops to 0, which means the mutex is no longer
140 /// Return the id of the thread that currently owns the mutex.
141 ACE_thread_t
get_thread_id (void);
144 * Return the nesting level of the recursion. When a thread has
145 * acquired the mutex for the first time, the nesting level == 1.
146 * The nesting level is incremented every time the thread acquires
147 * the mutex recursively. Note that if the ACE_HAS_RECURSIVE_MUTEXES
148 * macro is enabled then this method may return -1 on platforms that
149 * do not expose the internal count.
151 int get_nesting_level (void);
153 /// Returns a reference to the recursive mutex;
154 ACE_recursive_thread_mutex_t
&mutex (void);
156 /// Returns a reference to the recursive mutex's internal mutex;
157 ACE_thread_mutex_t
&get_nesting_mutex (void);
159 /// Dump the state of an object.
160 void dump (void) const;
162 /// Declare the dynamic allocation hooks.
163 ACE_ALLOC_HOOK_DECLARE
;
166 // = This method should *not* be public (they hold no locks...)
167 void set_thread_id (ACE_thread_t t
);
170 ACE_recursive_thread_mutex_t lock_
;
172 /// Keeps track of whether <remove> has been called yet to avoid
173 /// multiple <remove> calls, e.g., explicitly and implicitly in the
174 /// destructor. This flag isn't protected by a lock, so make sure
175 /// that you don't have multiple threads simultaneously calling
176 /// <remove> on the same object, which is a bad idea anyway...
180 // = Prevent assignment and initialization.
181 void operator= (const ACE_Recursive_Thread_Mutex
&);
182 ACE_Recursive_Thread_Mutex (const ACE_Recursive_Thread_Mutex
&);
185 ACE_END_VERSIONED_NAMESPACE_DECL
187 #if defined (__ACE_INLINE__)
188 #include "ace/Recursive_Thread_Mutex.inl"
189 #endif /* __ACE_INLINE__ */
191 #endif /* !ACE_HAS_THREADS */
193 #include /**/ "ace/post.h"
194 #endif /* ACE_RECURSIVE_THREAD_MUTEX_H */