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