3 //==========================================================================
7 * @author Douglas C. Schmidt <d.schmidt@vanderbilt.edu>
9 //==========================================================================
13 #include /**/ "ace/pre.h"
17 #if !defined (ACE_LACKS_PRAGMA_ONCE)
19 #endif /* ACE_LACKS_PRAGMA_ONCE */
21 #include "ace/Global_Macros.h"
22 #include "ace/OS_NS_Thread.h"
24 // FUZZ: disable check_for_ACE_Guard
26 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
31 * @brief This data structure is meant to be used within a method or
32 * function... It performs automatic acquisition and release of
33 * a parameterized synchronization object ACE_LOCK.
35 * The ACE_LOCK class given as an actual parameter must provide, at
36 * the very least the acquire(), tryacquire(), release(), and
39 * @warning A successfully constructed ACE_Guard does NOT mean that the
40 * lock was acquired! It is the caller's responsibility, after
41 * constructing an ACE_Guard, to check whether the lock was successfully
42 * acquired. Code like this is dangerous:
44 * ACE_Guard<ACE_Lock> g(lock);
45 * ... perform critical operation requiring lock to be held ...
47 * Instead, one must do something like this:
49 * ACE_Guard<ACE_Lock> g(lock);
52 * ... handle error ...
56 * ... perform critical operation requiring lock to be held ...
59 * The ACE_GUARD_RETURN() and ACE_GUARD_REACTION() macros are designed to
62 template <class ACE_LOCK
>
66 ACE_Guard (ACE_LOCK
&l
);
68 /// Implicitly and automatically acquire (or try to acquire) the
69 /// lock. If @a block is non-0 then acquire() the ACE_LOCK, else
71 ACE_Guard (ACE_LOCK
&l
, bool block
);
73 /// Initialize the guard without implicitly acquiring the lock. The
74 /// @a become_owner parameter indicates whether the guard should release
75 /// the lock implicitly on destruction. The @a block parameter is
76 /// ignored and is used here to disambiguate with the preceding
78 ACE_Guard (ACE_LOCK
&l
, bool block
, int become_owner
);
80 /// Implicitly release the lock.
85 /// Explicitly acquire the lock.
88 /// Conditionally acquire the lock (i.e., won't block).
91 /// Explicitly release the lock, but only if it is held!
94 /// Relinquish ownership of the lock so that it is not released
95 /// implicitly in the destructor.
99 /// true if locked, false if couldn't acquire the lock
100 /// (errno will contain the reason for this).
101 bool locked () const;
103 /// Explicitly remove the lock.
106 /// Dump the state of an object.
109 // ACE_ALLOC_HOOK_DECLARE;
110 // Declare the dynamic allocation hooks.
113 /// Helper, meant for subclass only.
114 ACE_Guard (ACE_LOCK
*lock
): lock_ (lock
), owner_ (0) {}
116 /// Pointer to the ACE_LOCK we're guarding.
119 /// Keeps track of whether we acquired the lock or failed.
123 void operator= (const ACE_Guard
<ACE_LOCK
> &) = delete;
124 ACE_Guard (const ACE_Guard
<ACE_LOCK
> &) = delete;
128 * @class ACE_Write_Guard
130 * @brief This class is similar to class ACE_Guard, though it
131 * acquires/releases a write lock automatically (naturally, the
132 * <ACE_LOCK> it is instantiated with must support the appropriate
135 * @warning See important "WARNING" in comments at top of ACE_Guard.
137 template <class ACE_LOCK
>
138 class ACE_Write_Guard
: public ACE_Guard
<ACE_LOCK
>
141 /// Implicitly and automatically acquire a write lock.
142 ACE_Write_Guard (ACE_LOCK
&m
);
144 /// Implicitly and automatically acquire (or try to acquire) a write
146 ACE_Write_Guard (ACE_LOCK
&m
, bool block
);
150 /// Explicitly acquire the write lock.
151 int acquire_write ();
153 /// Explicitly acquire the write lock.
156 /// Conditionally acquire the write lock (i.e., won't block).
157 int tryacquire_write ();
159 /// Conditionally acquire the write lock (i.e., won't block).
162 // = Utility methods.
164 /// Dump the state of an object.
167 // ACE_ALLOC_HOOK_DECLARE;
168 // Declare the dynamic allocation hooks.
172 * @class ACE_Read_Guard
174 * @brief This class is similar to class ACE_Guard, though it
175 * acquires/releases a read lock automatically (naturally, the
176 * <ACE_LOCK> it is instantiated with must support the appropriate
179 * @warning See important "WARNING" in comments at top of ACE_Guard.
181 template <class ACE_LOCK
>
182 class ACE_Read_Guard
: public ACE_Guard
<ACE_LOCK
>
185 /// Implicitly and automatically acquire a read lock.
186 ACE_Read_Guard (ACE_LOCK
& m
);
188 /// Implicitly and automatically acquire (or try to acquire) a read
190 ACE_Read_Guard (ACE_LOCK
&m
, bool block
);
194 /// Explicitly acquire the read lock.
197 /// Explicitly acquire the read lock.
200 /// Conditionally acquire the read lock (i.e., won't block).
201 int tryacquire_read ();
203 /// Conditionally acquire the read lock (i.e., won't block).
206 // = Utility methods.
208 /// Dump the state of an object.
211 // ACE_ALLOC_HOOK_DECLARE;
212 // Declare the dynamic allocation hooks.
215 #if !(defined (ACE_HAS_THREADS) && (defined (ACE_HAS_THREAD_SPECIFIC_STORAGE) || defined (ACE_HAS_TSS_EMULATION)))
217 #define ACE_TSS_Guard ACE_Guard
218 #define ACE_TSS_Write_GUARD ACE_Write_Guard
219 #define ACE_TSS_Read_GUARD ACE_Read_Guard
222 /* ACE platform supports some form of threading and
223 thread-specific storage. */
226 * @class ACE_TSS_Guard
228 * @brief This data structure is meant to be used within a method or
229 * function... It performs automatic aquisition and release of
230 * a synchronization object. Moreover, it ensures that the lock
231 * is released even if a thread exits via <thr_exit>!
233 template <class ACE_LOCK
>
237 /// Implicitly and automatically acquire the thread-specific lock.
238 ACE_TSS_Guard (ACE_LOCK
&lock
, bool block
= true);
240 /// Implicitly release the thread-specific lock.
245 /// Explicitly acquire the thread-specific lock.
248 /// Conditionally acquire the thread-specific lock (i.e., won't
252 /// Explicitly release the thread-specific lock.
255 // = Utility methods.
256 /// Explicitly release the thread-specific lock.
259 /// Dump the state of an object.
262 /// Declare the dynamic allocation hooks.
263 ACE_ALLOC_HOOK_DECLARE
;
266 /// Helper, meant for subclass only.
269 /// Initialize the key.
272 /// Called when thread exits to clean up the lock.
273 static void cleanup (void *ptr
);
275 /// Thread-specific key...
276 ACE_thread_key_t key_
;
279 // FUZZ: disable check_for_ACE_Guard
280 typedef ACE_Guard
<ACE_LOCK
> Guard_Type
;
281 // FUZZ: enable check_for_ACE_Guard
283 void operator= (const ACE_TSS_Guard
<ACE_LOCK
> &) = delete;
284 ACE_TSS_Guard (const ACE_TSS_Guard
<ACE_LOCK
> &) = delete;
288 * @class ACE_TSS_Write_Guard
290 * @brief This class is similar to class ACE_TSS_Guard, though it
291 * acquires/releases a write-lock automatically (naturally, the
292 * ACE_LOCK it is instantiated with must support the appropriate
295 template <class ACE_LOCK
>
296 class ACE_TSS_Write_Guard
: public ACE_TSS_Guard
<ACE_LOCK
>
299 /// Implicitly and automatically acquire the thread-specific write lock.
300 ACE_TSS_Write_Guard (ACE_LOCK
&lock
, bool block
= true);
304 /// Explicitly acquire the thread-specific write lock.
305 int acquire_write ();
307 /// Explicitly acquire the thread-specific write lock.
310 /// Conditionally acquire the thread-specific write lock (i.e., won't block).
311 int tryacquire_write ();
313 /// Conditionally acquire the thread-specific write lock (i.e., won't block).
316 // = Utility methods.
318 /// Dump the state of an object.
321 // ACE_ALLOC_HOOK_DECLARE;
322 // Declare the dynamic allocation hooks.
324 // FUZZ: disable check_for_ACE_Guard
325 typedef ACE_Guard
<ACE_LOCK
> Guard_Type
;
326 typedef ACE_Write_Guard
<ACE_LOCK
> Write_Guard_Type
;
327 // FUZZ: enable check_for_ACE_Guard
331 * @class ACE_TSS_Read_Guard
333 * @brief This class is similar to class <ACE_TSS_Guard>, though it
334 * acquires/releases a read lock automatically (naturally, the
335 * <ACE_LOCK> it is instantiated with must support the
338 template <class ACE_LOCK
>
339 class ACE_TSS_Read_Guard
: public ACE_TSS_Guard
<ACE_LOCK
>
342 /// Implicitly and automatically acquire the thread-specific read lock.
343 ACE_TSS_Read_Guard (ACE_LOCK
&lock
, bool block
= true);
346 /// Explicitly acquire the thread-specific read lock.
349 /// Explicitly acquire the thread-specific read lock.
352 /// Conditionally acquire the thread-specific read lock (i.e., won't
354 int tryacquire_read ();
356 /// Conditionally acquire the thread-specific read lock (i.e., won't
360 // = Utility methods.
361 /// Dump the state of an object.
364 // ACE_ALLOC_HOOK_DECLARE;
365 // Declare the dynamic allocation hooks.
367 // FUZZ: disable check_for_ACE_Guard
368 typedef ACE_Guard
<ACE_LOCK
> Guard_Type
;
369 typedef ACE_Read_Guard
<ACE_LOCK
> Read_Guard_Type
;
370 // FUZZ: enable check_for_ACE_Guard
373 #endif /* !(defined (ACE_HAS_THREADS) && (defined (ACE_HAS_THREAD_SPECIFIC_STORAGE) || defined (ACE_HAS_TSS_EMULATION))) */
375 ACE_END_VERSIONED_NAMESPACE_DECL
377 #if defined (__ACE_INLINE__)
378 #include "ace/Guard_T.inl"
379 #endif /* __ACE_INLINE__ */
381 #include "ace/Guard_T.cpp"
383 #include /**/ "ace/post.h"
384 #endif /* ACE_GUARD_T_H */