BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslmt_lockguard.h
Go to the documentation of this file.
1/// @file bslmt_lockguard.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmt_lockguard.h -*-C++-*-
8#ifndef INCLUDED_BSLMT_LOCKGUARD
9#define INCLUDED_BSLMT_LOCKGUARD
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmt_lockguard bslmt_lockguard
15/// @brief Provide generic scoped guards for synchronization objects.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmt
19/// @{
20/// @addtogroup bslmt_lockguard
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmt_lockguard-purpose"> Purpose</a>
25/// * <a href="#bslmt_lockguard-classes"> Classes </a>
26/// * <a href="#bslmt_lockguard-description"> Description </a>
27/// * <a href="#bslmt_lockguard-behavior-of-the-release-method"> Behavior of the release Method </a>
28/// * <a href="#bslmt_lockguard-usage"> Usage </a>
29/// * <a href="#bslmt_lockguard-example-1-basic-usage"> Example 1: Basic Usage </a>
30///
31/// # Purpose {#bslmt_lockguard-purpose}
32/// Provide generic scoped guards for synchronization objects.
33///
34/// # Classes {#bslmt_lockguard-classes}
35///
36/// - bslmt::LockGuard: automatic mutex locking-unlocking
37/// - bslmt::LockGuardUnlock: automatic mutex unlocking-locking
38/// - bslmt::LockGuardTryLock: automatic non-blocking mutex locking-unlocking
39/// - bslmt::UnLockGuard: DEPRECATED
40/// - bslmt::TryLockGuard: DEPRECATED
41///
42/// @see bslmt_readlockguard, bslmt_writelockguard
43///
44/// # Description {#bslmt_lockguard-description}
45/// This component provides generic guards, `bslmt::LockGuard`,
46/// `bslmt::LockGuardUnlock`, and `bslmt::LockGuardTryLock`, to automatically
47/// lock and unlock an external synchronization object. The synchronization
48/// object can be any type (e.g., `bslmt::Mutex` or `bslmt::RecursiveMutex`)
49/// that provides the following methods:
50/// @code
51/// void lock();
52/// void unlock();
53/// @endcode
54/// Both `bslmt::LockGuard` and `bslmt::LockGuardUnlock` implement the
55/// "construction is acquisition, destruction is release" idiom. During
56/// construction, `bslmt::LockGuard` automatically calls `lock` on the
57/// user-supplied object, and `unlock` when it is destroyed (unless released).
58/// `bslmt::LockGuardUnlock` does the opposite -- it invokes the `unlock` method
59/// when constructed and the `lock` method when it is destroyed.
60///
61/// A third type of guard, `bslmt::LockGuardTryLock`, attempts to acquire a
62/// lock, and if acquisition succeeds, releases it upon destruction. Since the
63/// acquisition is done at construction time, it is not possible to return a
64/// value to indicate success. Rather, the `bslmt::LockGuardTryLock` contains a
65/// pointer to the synchronization object if `tryLock` succeeds, and is null
66/// otherwise. The synchronization object can be any type (e.g., `bslmt::Mutex`
67/// or `bslmt::RecursiveMutex`) that provides the following methods:
68/// @code
69/// int tryLock();
70/// void unlock();
71/// @endcode
72/// Note that none of these guard types assumes ownership of the external
73/// synchronization object. Also note that objects of all of the guard types
74/// may be constructed with a null `lock` whereby the constructed guard objects
75/// manage no lock. The destructor of each of the guard types has no effect if
76/// no lock is under management.
77///
78/// ## Behavior of the release Method {#bslmt_lockguard-behavior-of-the-release-method}
79///
80///
81/// Like all BDE guard classes, each of the three `bslmt::LockGuard*` classes
82/// provides a `release` method that terminates the guard's management of any
83/// lock object that the guard holds. The `release` method has *no* *effect*
84/// on the state of the lock object.
85///
86/// In particular, `bslmt::ReadLockGuard::release` does not unlock the lock
87/// object under management. If a user wants to release the lock object *and*
88/// unlock the lock object (because the lock is no longer required before the
89/// guard goes out of scope), the following idiom can be used:
90/// @code
91/// // 'guard' is an existing guard of type 'bslmt::LockGuard<my_Lock>',
92/// // created in a scope that we do not control.
93///
94/// {
95/// // ... Do work that requires the lock.
96///
97/// // We know that the lock is no longer needed.
98///
99/// my_Lock *lock = guard.release();
100///
101/// // 'lock' is no longer managed, but is *still* *locked*.
102///
103/// lock->unlock();
104///
105/// // ... Do work that does not require the lock.
106/// }
107/// @endcode
108///
109/// ## Usage {#bslmt_lockguard-usage}
110///
111///
112/// This section illustrates intended use of this component.
113///
114/// ### Example 1: Basic Usage {#bslmt_lockguard-example-1-basic-usage}
115///
116///
117/// Use this component to ensure that in the event of an exception or exit from
118/// any point in a given scope, the synchronization object will be properly
119/// unlocked. The following function, `errorProneFunc`, is overly complex, not
120/// exception safe, and contains a bug.
121/// @code
122/// static void errorProneFunc(my_Object *obj, my_Mutex *mutex)
123/// {
124/// mutex->lock();
125/// if (someCondition) {
126/// obj->someMethod();
127/// mutex->unlock();
128/// return; // RETURN
129/// } else if (someOtherCondition) {
130/// obj->someOtherMethod();
131/// // MISTAKE! forgot to unlock mutex
132/// return; // RETURN
133/// }
134/// obj->defaultMethod();
135/// mutex->unlock();
136/// return;
137/// }
138/// @endcode
139/// The function can be rewritten with a cleaner and safer implementation using
140/// a guard object. The `safeFunc` function is simpler than `errorProneFunc`,
141/// is exception safe, and avoids the multiple calls to unlock that can be a
142/// source of errors.
143/// @code
144/// static void safeFunc(my_Object *obj, my_Mutex *mutex)
145/// {
146/// bslmt::LockGuard<my_Mutex> guard(mutex);
147/// if (someCondition) {
148/// obj->someMethod();
149/// return; // RETURN
150/// } else if (someOtherCondition) {
151/// obj->someOtherMethod();
152/// // OK, mutex is automatically unlocked
153/// return; // RETURN
154/// }
155/// obj->defaultMethod();
156/// return;
157/// }
158/// @endcode
159/// When blocking while acquiring the lock is not desirable, one may instead use
160/// a `bslmt::LockGuardTryLock` in the typical following fashion:
161/// @code
162/// /// Perform task and return positive value if locking succeeds. Return
163/// /// 0 if locking fails.
164/// static int safeButNonBlockingFunc(my_Object *obj, my_Mutex *mutex)
165/// {
166/// const int RETRIES = 1; // use higher values for higher success rate
167/// bslmt::LockGuardTryLock<my_Mutex> guard(mutex, RETRIES);
168/// if (guard.ptr()) { // mutex is locked
169/// if (someCondition) {
170/// obj->someMethod();
171/// return 2; // RETURN
172/// } else if (someOtherCondition) {
173/// obj->someOtherMethod();
174/// return 3; // RETURN
175/// }
176/// obj->defaultMethod();
177/// return 1; // RETURN
178/// }
179/// return 0;
180/// }
181/// @endcode
182/// Instantiations of `bslmt::LockGuardUnlock` can be interleaved with
183/// instantiations of `bslmt::LockGuard` to create both critical sections and
184/// regions where the lock is released.
185/// @code
186/// void f(my_Mutex *mutex)
187/// {
188/// bslmt::LockGuard<my_Mutex> guard(mutex);
189///
190/// // critical section here
191///
192/// {
193/// bslmt::LockGuardUnlock<my_Mutex> guard(mutex);
194///
195/// // mutex is unlocked here
196///
197/// } // mutex is locked again here
198///
199/// // critical section here
200///
201/// } // mutex is unlocked here
202/// @endcode
203/// Care must be taken so as not to interleave guard objects in such a way as to
204/// cause an illegal sequence of calls on a lock (two sequential lock calls or
205/// two sequential unlock calls on a non-recursive mutex).
206/// @}
207/** @} */
208/** @} */
209
210/** @addtogroup bsl
211 * @{
212 */
213/** @addtogroup bslmt
214 * @{
215 */
216/** @addtogroup bslmt_lockguard
217 * @{
218 */
219
220#include <bslscm_version.h>
221
222
223namespace bslmt {
224
225 // ===============
226 // class LockGuard
227 // ===============
228
229/// This class template implements a guard for acquisition and release of
230/// synchronization resources (i.e., locks).
231///
232/// See @ref bslmt_lockguard
233template <class T>
235
236 // DATA
237 T *d_lock_p; // lock guarded by this object (held, not owned)
238
239 private:
240 // NOT IMPLEMENTED
241 LockGuard(const LockGuard<T>&);
242 LockGuard<T>& operator=(const LockGuard<T>&);
243
244 public:
245 // CREATORS
246
247 /// Create a scoped guard that conditionally manages the specified
248 /// `lock` (if non-null) and invokes `lock->lock()`. Supplying a null
249 /// `lock` has no effect. The behavior is undefined unless `lock` (if
250 /// non-null) is not already locked by this thread. Note that `lock`
251 /// must remain valid throughout the lifetime of this guard, or until
252 /// `release` is called.
253 explicit LockGuard(T *lock);
254
255 /// Create a scoped guard that conditionally manages the specified
256 /// `lock` (if non-null) and invokes `lock->lock()` if the specified
257 /// `alreadyLockedFlag` is `false`. Supplying a null `lock` has no
258 /// effect. The behavior is undefined unless the state of `lock` (if
259 /// non-null) is consistent with `alreadyLockedFlag`. Note that
260 /// `alreadyLockedFlag` is used to indicate whether `lock` is in an
261 /// already-locked state when passed, so if `alreadyLockedFlag` is
262 /// `true` the `lock` method will *not* be called on the supplied
263 /// `lock`. Also note that `lock` must remain valid throughout the
264 /// lifetime of this guard, or until `release` is called.
265 LockGuard(T *lock, bool alreadyLockedFlag);
266
267 /// Destroy this scoped guard and invoke the `unlock` method on the
268 /// lock object under management by this guard, if any. If no lock is
269 /// currently being managed, this method has no effect.
271
272 // MANIPULATORS
273
274 /// Return the address of the modifiable lock object under management by
275 /// this guard, and release the lock from further management by this
276 /// guard. If no lock is currently being managed, return 0 with no
277 /// other effect. Note that this operation does *not* unlock the lock
278 /// object (if any) that was under management.
280
281 // ACCESSORS
282
283 /// Return the address of the modifiable lock object under management by
284 /// this guard, or 0 if no lock is currently being managed.
285 T *ptr() const;
286};
287
288 // =====================
289 // class LockGuardUnlock
290 // =====================
291
292/// This class template implements a guard for release and reacquisition
293/// of synchronization resources (i.e., locks).
294///
295/// See @ref bslmt_lockguard
296template <class T>
298
299 // DATA
300 T *d_lock_p; // lock guarded by this object (held, not owned)
301
302 private:
303 // NOT IMPLEMENTED
305 LockGuardUnlock<T>& operator=(const LockGuardUnlock<T>&);
306
307 public:
308 // CREATORS
309
310 /// Create a scoped guard that conditionally manages the specified
311 /// `lock` (if non-null) and invokes `lock->unlock()`. Supplying a null
312 /// `lock` has no effect. The behavior is undefined unless `lock` (if
313 /// non-null) is locked by this thread. Note that `lock` must remain
314 /// valid throughout the lifetime of this guard, or until `release` is
315 /// called.
316 explicit LockGuardUnlock(T *lock);
317
318 /// Create a scoped guard that conditionally manages the specified
319 /// `lock` (if non-null) and invokes `lock->unlock()` if the specified
320 /// `alreadyUnlockedFlag` is `false`. Supplying a null `lock` has no
321 /// effect. The behavior is undefined unless the state of `lock` (if
322 /// non-null) is consistent with `alreadyUnlockedFlag`. Note that
323 /// `alreadyUnlockedFlag` is used to indicate whether `lock` is in an
324 /// already-unlocked state when passed, so if `alreadyUnlockedFlag` is
325 /// `true` the `unlock` method will *not* be called on the supplied
326 /// `lock`. Also note that `lock` must remain valid throughout the
327 /// lifetime of this guard, or until `release` is called.
328 LockGuardUnlock(T *lock, bool alreadyUnlockedFlag);
329
330 /// Destroy this scoped guard and invoke the `lock` method on the lock
331 /// object under management by this guard, if any. If no lock is
332 /// currently being managed, this method has no effect.
334
335 // MANIPULATORS
336
337 /// Return the address of the modifiable lock object under management by
338 /// this guard, and release the lock from further management by this
339 /// guard. If no lock is currently being managed, return 0 with no
340 /// other effect. Note that this operation does *not* lock the lock
341 /// object (if any) that was under management.
342 T *release();
343
344 // ACCESSORS
345
346 /// Return the address of the modifiable lock object under management by
347 /// this guard, or 0 if no lock is currently being managed.
348 T *ptr() const;
349};
350
351 // =================
352 // class UnLockGuard
353 // =================
354
355/// This class is DEPRECATED. Use `LockGuardUnlock` instead.
356///
357/// See @ref bslmt_lockguard
358template <class T>
359class UnLockGuard : public LockGuardUnlock<T> {
360
361 private:
362 // NOT IMPLEMENTED
364 UnLockGuard<T>& operator=(const UnLockGuard<T>&);
365
366 public:
367 // CREATORS
368
369 /// DEPRECATED: Use `LockGuardUnlock` instead.
370 explicit UnLockGuard(T *lock);
371
372 /// DEPRECATED: Use `LockGuardUnlock` instead.
373 UnLockGuard(T *lock, bool alreadyUnlockedFlag);
374};
375
376 // ======================
377 // class LockGuardTryLock
378 // ======================
379
380/// This class template implements a guard for tentative acquisition and
381/// release of synchronization resources (i.e., locks).
382///
383/// See @ref bslmt_lockguard
384template <class T>
386
387 // DATA
388 T *d_lock_p; // lock guarded by this object (held, not owned)
389
390 private:
391 // NOT IMPLEMENTED
393 LockGuardTryLock<T>& operator=(const LockGuardTryLock<T>&);
394
395 public:
396 // CREATORS
397
398 /// Create a scoped guard that conditionally manages the specified
399 /// `lock` (if non-null) and invokes `lock->tryLock()` until the lock is
400 /// acquired, or until the optionally specified `attempts` have been
401 /// made to acquire the lock. If `attempts` is not specified only one
402 /// attempt is made to acquire the lock. Supplying a null `lock` has no
403 /// effect. The behavior is undefined unless `lock` (if non-null) is
404 /// not already locked by this thread and `0 < attempts`. Note that
405 /// `lock` must remain valid throughout the lifetime of this guard, or
406 /// until `release` is called.
407 explicit LockGuardTryLock(T *lock, int attempts = 1);
408
409 /// Destroy this scoped guard and invoke the `unlock` method on the
410 /// lock object under management by this guard, if any. If no lock is
411 /// currently being managed, this method has no effect.
413
414 // MANIPULATORS
415
416 /// Return the address of the modifiable lock object under management by
417 /// this guard, and release the lock from further management by this
418 /// guard. If no lock is currently being managed, return 0 with no
419 /// other effect. Note that this operation does *not* unlock the lock
420 /// object (if any) that was under management.
421 T *release();
422
423 // ACCESSORS
424
425 /// Return the address of the modifiable lock object under management by
426 /// this guard, or 0 if no lock is currently being managed.
427 T *ptr() const;
428};
429
430 // ==================
431 // class TryLockGuard
432 // ==================
433
434/// @deprecated Use @ref LockGuardTryLock instead.
435///
436/// See @ref bslmt_lockguard
437template <class T>
439
440 private:
441 // NOT IMPLEMENTED
443 TryLockGuard<T>& operator=(const TryLockGuard<T>&);
444
445 public:
446 // CREATORS
447
448 /// @deprecated Use @ref LockGuardTryLock instead.
449 explicit TryLockGuard(T *lock, int attempts = 1);
450};
451
452// ============================================================================
453// INLINE DEFINITIONS
454// ============================================================================
455
456 // ---------------
457 // class LockGuard
458 // ---------------
459
460// CREATORS
461template <class T>
462inline
464: d_lock_p(lock)
465{
466 if (d_lock_p) {
467 d_lock_p->lock();
468 }
469}
470
471template <class T>
472inline
473LockGuard<T>::LockGuard(T *lock, bool alreadyLockedFlag)
474: d_lock_p(lock)
475{
476 if (d_lock_p && !alreadyLockedFlag) {
477 d_lock_p->lock();
478 }
479}
480
481template <class T>
482inline
484{
485 if (d_lock_p) {
486 d_lock_p->unlock();
487 }
488}
489
490// MANIPULATORS
491template <class T>
492inline
494{
495 T *lock = d_lock_p;
496 d_lock_p = 0;
497 return lock;
498}
499
500// ACCESSORS
501template <class T>
502inline
504{
505 return d_lock_p;
506}
507
508 // -----------------
509 // class UnLockGuard
510 // -----------------
511
512// CREATORS
513template <class T>
514inline
516: LockGuardUnlock<T>(lock)
517{
518}
519
520template <class T>
521inline
522UnLockGuard<T>::UnLockGuard(T *lock, bool alreadyUnlockedFlag)
523: LockGuardUnlock<T>(lock, alreadyUnlockedFlag)
524{
525}
526
527 // ------------------
528 // class TryLockGuard
529 // ------------------
530
531template <class T>
532inline
533TryLockGuard<T>::TryLockGuard(T *lock, int attempts)
534: LockGuardTryLock<T>(lock, attempts)
535{
536}
537
538 // ---------------------
539 // class LockGuardUnlock
540 // ---------------------
541
542// CREATORS
543template <class T>
544inline
546: d_lock_p(lock)
547{
548 if (d_lock_p) {
549 d_lock_p->unlock();
550 }
551}
552
553template <class T>
554inline
555LockGuardUnlock<T>::LockGuardUnlock(T *lock, bool alreadyUnlockedFlag)
556: d_lock_p(lock)
557{
558 if (d_lock_p && !alreadyUnlockedFlag) {
559 d_lock_p->unlock();
560 }
561}
562
563template <class T>
564inline
566{
567 if (d_lock_p) {
568 d_lock_p->lock();
569 }
570}
571
572// MANIPULATORS
573template <class T>
574inline
576{
577 T *lock = d_lock_p;
578 d_lock_p = 0;
579 return lock;
580}
581
582// ACCESSORS
583template <class T>
584inline
586{
587 return d_lock_p;
588}
589
590 // ----------------------
591 // class LockGuardTryLock
592 // ----------------------
593
594// CREATORS
595template <class T>
597: d_lock_p(0)
598{
599 if (lock) {
600 while (attempts--) {
601 if (!lock->tryLock()) {
602 d_lock_p = lock;
603 break;
604 }
605 }
606 }
607}
608
609template <class T>
610inline
612{
613 if (d_lock_p) {
614 d_lock_p->unlock();
615 }
616}
617
618// MANIPULATORS
619template <class T>
620inline
622{
623 T *lock = d_lock_p;
624 d_lock_p = 0;
625 return lock;
626}
627
628// ACCESSORS
629template <class T>
630inline
632{
633 return d_lock_p;
634}
635
636} // close package namespace
637
638
639#endif
640
641// ----------------------------------------------------------------------------
642// Copyright 2015 Bloomberg Finance L.P.
643//
644// Licensed under the Apache License, Version 2.0 (the "License");
645// you may not use this file except in compliance with the License.
646// You may obtain a copy of the License at
647//
648// http://www.apache.org/licenses/LICENSE-2.0
649//
650// Unless required by applicable law or agreed to in writing, software
651// distributed under the License is distributed on an "AS IS" BASIS,
652// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
653// See the License for the specific language governing permissions and
654// limitations under the License.
655// ----------------------------- END-OF-FILE ----------------------------------
656
657/** @} */
658/** @} */
659/** @} */
Definition bslmt_lockguard.h:385
T * release()
Definition bslmt_lockguard.h:621
T * ptr() const
Definition bslmt_lockguard.h:631
~LockGuardTryLock()
Definition bslmt_lockguard.h:611
Definition bslmt_lockguard.h:297
T * release()
Definition bslmt_lockguard.h:575
T * ptr() const
Definition bslmt_lockguard.h:585
~LockGuardUnlock()
Definition bslmt_lockguard.h:565
Definition bslmt_lockguard.h:234
LockGuard(T *lock, bool alreadyLockedFlag)
Definition bslmt_lockguard.h:473
LockGuard(T *lock)
Definition bslmt_lockguard.h:463
~LockGuard()
Definition bslmt_lockguard.h:483
T * release()
Definition bslmt_lockguard.h:493
T * ptr() const
Definition bslmt_lockguard.h:503
Definition bslmt_lockguard.h:438
Definition bslmt_lockguard.h:359
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bslmt_barrier.h:344