BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslmt_writelockguard.h
Go to the documentation of this file.
1/// @file bslmt_writelockguard.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmt_writelockguard.h -*-C++-*-
8#ifndef INCLUDED_BSLMT_WRITELOCKGUARD
9#define INCLUDED_BSLMT_WRITELOCKGUARD
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmt_writelockguard bslmt_writelockguard
15/// @brief Provide generic scoped guards for write synchronization objects.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmt
19/// @{
20/// @addtogroup bslmt_writelockguard
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmt_writelockguard-purpose"> Purpose</a>
25/// * <a href="#bslmt_writelockguard-classes"> Classes </a>
26/// * <a href="#bslmt_writelockguard-description"> Description </a>
27/// * <a href="#bslmt_writelockguard-behavior-of-the-release-method"> Behavior of the release Method </a>
28/// * <a href="#bslmt_writelockguard-usage"> Usage </a>
29/// * <a href="#bslmt_writelockguard-example-1-basic-usage"> Example 1: Basic Usage </a>
30///
31/// # Purpose {#bslmt_writelockguard-purpose}
32/// Provide generic scoped guards for write synchronization objects.
33///
34/// # Classes {#bslmt_writelockguard-classes}
35///
36/// - bslmt::WriteLockGuard: automatic locking-unlocking for write access
37/// - bslmt::WriteLockGuardUnlock: automatic unlocking-locking for write access
38/// - bslmt::WriteLockGuardTryLock: automatic non-blocking locking-unlocking
39/// - bslmt::LockWriteGuard: DEPRECATED
40///
41/// @see bslmt_lockguard, bslmt_readlockguard
42///
43/// # Description {#bslmt_writelockguard-description}
44/// This component provides generic guards,
45/// `bslmt::WriteLockGuard`, `bslmt::WriteLockGuardUnlock`, and
46/// `bslmt::WriteLockGuardTryLock`, to automatically lock and unlock an external
47/// synchronization object for writing. The synchronization object can be any
48/// type (e.g., `bslmt::ReaderWriterLock`) that provides the following methods:
49/// @code
50/// void lockWrite();
51/// void unlock();
52/// @endcode
53/// Both `bslmt::WriteLockGuard` and `bslmt::WriteLockGuardUnlock` implement the
54/// "construction is acquisition, destruction is release" idiom. During
55/// construction, `bslmt::WriteLockGuard` automatically calls `lockWrite` on the
56/// user-supplied object, and `unlock` when it is destroyed (unless released).
57/// `bslmt::WriteLockGuardUnlock` does the opposite -- it invokes the `unlock`
58/// method when constructed and the `lockWrite` method when destroyed.
59///
60/// A third type of guard, `bslmt::WriteLockGuardTryLock`, 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::WriteLockGuardTryLock`
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 tryLockWrite();
70/// void unlock();
71/// @endcode
72/// Note that objects of neither guard type assumes ownership of the
73/// synchronization object passed 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_writelockguard-behavior-of-the-release-method}
79///
80///
81/// Like all BDE guard classes, each of the three `bslmt::WriteLockGuard*`
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::WriteLockGuard::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::WriteLockGuard<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_writelockguard-usage}
110///
111///
112/// This section illustrates intended use of this component.
113///
114/// ### Example 1: Basic Usage {#bslmt_writelockguard-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_RWLock *rwlock)
123/// {
124/// rwlock->lockWrite();
125/// if (someUpgradeCondition) {
126/// obj->someUpgradeMethod();
127/// rwlock->unlock();
128/// return; // RETURN
129/// } else if (someOtherUpgradeCondition) {
130/// obj->someOtherUpgradeMethod();
131/// // MISTAKE! forgot to unlock rwlock
132/// return; // RETURN
133/// }
134/// obj->defaultUpgradeMethod();
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(my_Object *obj, my_RWLock *rwlock)
145/// {
146/// bslmt::WriteLockGuard<my_RWLock> guard(rwlock);
147/// if (someUpgradeCondition) {
148/// obj->someUpgradeMethod();
149/// return; // RETURN
150/// } else if (someOtherUpgradeCondition) {
151/// obj->someOtherUpgradeMethod();
152/// // OK, rwlock is automatically unlocked
153/// return; // RETURN
154/// }
155/// obj->defaultUpgradeMethod();
156/// return;
157/// }
158/// @endcode
159/// When blocking while acquiring the lock is not desirable, one may instead use
160/// a `bslmt::WriteLockGuardTryLock` in the typical following fashion:
161/// @code
162/// /// Perform upgrade and return positive value if locking succeeds.
163/// /// Return 0 if locking fails.
164/// static int safeButNonBlockingFunc(my_Object *obj, my_RWLock *rwlock)
165/// {
166/// const int RETRIES = 1; // use higher values for higher success rate
167/// bslmt::WriteLockGuardTryLock<my_RWLock> guard(rwlock, RETRIES);
168/// if (guard.ptr()) { // rwlock is locked
169/// if (someUpgradeCondition) {
170/// obj->someUpgradeMethod();
171/// return 2; // RETURN
172/// } else if (someOtherUpgradeCondition) {
173/// obj->someOtherUpgradeMethod();
174/// return 3; // RETURN
175/// }
176/// obj->defaultUpgradeMethod();
177/// return 1; // RETURN
178/// }
179/// return 0;
180/// }
181/// @endcode
182/// If the underlying lock object provides an upgrade from a lock for read to a
183/// lock for write (as does `bslmt::ReaderWriterLock` with the
184/// `upgradeToWriteLock` function, for example), and the lock is already guarded
185/// by a `bslmt::ReadLockGuard`, then it is not necessary to transfer the guard
186/// to a `bslmt::WriteLockGuard`. In fact, a combination of
187/// `bslmt::ReadLockGuard` and `bslmt::WriteLockGuard` guarding a common lock
188/// object should probably never be needed.
189///
190/// Care must be taken so as not to interleave guard objects in such a way as to
191/// cause an illegal sequence of calls on a lock (two sequential lock calls or
192/// two sequential unlock calls on a non-recursive read/write lock).
193/// @}
194/** @} */
195/** @} */
196
197/** @addtogroup bsl
198 * @{
199 */
200/** @addtogroup bslmt
201 * @{
202 */
203/** @addtogroup bslmt_writelockguard
204 * @{
205 */
206
207#include <bslscm_version.h>
208
209
210namespace bslmt {
211
212 // ====================
213 // class WriteLockGuard
214 // ====================
215
216/// This class template implements a guard for acquisition and release of
217/// write synchronization resources (i.e., writer locks).
218///
219/// See @ref bslmt_writelockguard
220template <class T>
222
223 // DATA
224 T *d_lock_p; // lock guarded by this object (held, not owned)
225
226 private:
227 // NOT IMPLEMENTED
229 WriteLockGuard<T>& operator=(const WriteLockGuard<T>&);
230
231 public:
232 // CREATORS
233
234 /// Create a scoped guard that conditionally manages the specified
235 /// `lock` (if non-null) and invokes `lock->lockWrite()`. Supplying a
236 /// null `lock` has no effect. The behavior is undefined unless `lock`
237 /// (if non-null) is not already locked by this thread. Note that
238 /// `lock` must remain valid throughout the lifetime of this guard, or
239 /// until `release` is called.
240 explicit WriteLockGuard(T *lock);
241
242 /// Create a scoped guard that conditionally manages the specified
243 /// `lock` (if non-null) and invokes `lock->lockWrite()` if the
244 /// specified `alreadyLockedFlag` is `false`. Supplying a null `lock`
245 /// has no effect. The behavior is undefined unless the state of `lock`
246 /// (if non-null) is consistent with `alreadyLockedFlag`. Note that
247 /// `alreadyLockedFlag` is used to indicate whether `lock` is in an
248 /// already-locked state when passed, so if `alreadyLockedFlag` is
249 /// `true` the `lock` method will *not* be called on the supplied
250 /// `lock`. Also note that `lock` must remain valid throughout the
251 /// lifetime of this guard, or until `release` is called.
252 WriteLockGuard(T *lock, bool alreadyLockedFlag);
253
254 /// Destroy this scoped guard and invoke the `unlock` method on the
255 /// lock object under management by this guard, if any. If no lock is
256 /// currently being managed, this method has no effect.
258
259 // MANIPULATORS
260
261 /// Return the address of the modifiable lock object under management by
262 /// this guard, and release the lock from further management by this
263 /// guard. If no lock is currently being managed, return 0 with no
264 /// other effect. Note that this operation does *not* unlock the lock
265 /// object (if any) that was under management.
266 T *release();
267
268 // ACCESSORS
269
270 /// Return the address of the modifiable lock object under management by
271 /// this guard, or 0 if no lock is currently being managed.
272 T *ptr() const;
273};
274
275 // ====================
276 // class LockWriteGuard
277 // ====================
278
279/// @deprecated Use @ref WriteLockGuard instead.
280///
281/// See @ref bslmt_writelockguard
282template <class T>
284
285 private:
286 // NOT IMPLEMENTED
288 LockWriteGuard<T>& operator=(const LockWriteGuard<T>&);
289
290 public:
291 // CREATORS
292
293 /// @deprecated Use @ref WriteLockGuard instead.
294 explicit LockWriteGuard(T *lock);
295
296 /// @deprecated Use @ref WriteLockGuard instead.
297 LockWriteGuard(T *lock, bool alreadyLockedFlag);
298
299};
300
301 // ==========================
302 // class WriteLockGuardUnlock
303 // ==========================
304
305/// This class template implements a guard for release and reacquisition
306/// of write synchronization resources (i.e., writer locks).
307///
308/// See @ref bslmt_writelockguard
309template <class T>
311
312 // DATA
313 T *d_lock_p; // lock guarded by this object (held, not owned)
314
315 private:
316 // NOT IMPLEMENTED
319
320 public:
321 // CREATORS
322
323 /// Create a scoped guard that conditionally manages the specified
324 /// `lock` (if non-null) and invokes `lock->unlock()`. Supplying a null
325 /// `lock` has no effect. The behavior is undefined unless `lock` (if
326 /// non-null) is locked by this thread. Note that `lock` must remain
327 /// valid throughout the lifetime of this guard, or until `release` is
328 /// called.
329 explicit WriteLockGuardUnlock(T *lock);
330
331 /// Create a scoped guard that conditionally manages the specified
332 /// `lock` (if non-null) and invokes `lock->unlock()` if the specified
333 /// `alreadyUnlockedFlag` is `false`. Supplying a null `lock` has no
334 /// effect. The behavior is undefined unless the state of `lock` (if
335 /// non-null) is consistent with `alreadyUnlockedFlag`. Note that
336 /// `alreadyUnlockedFlag` is used to indicate whether `lock` is in an
337 /// already-unlocked state when passed, so if `alreadyUnlockedFlag` is
338 /// `true` the `unlock` method will *not* be called on the supplied
339 /// `lock`. Also note that `lock` must remain valid throughout the
340 /// lifetime of this guard, or until `release` is called.
341 WriteLockGuardUnlock(T *lock, bool alreadyUnlockedFlag);
342
343 /// Destroy this scoped guard and invoke the `lockWrite` method on the
344 /// lock object under management by this guard, if any. If no lock is
345 /// currently being managed, this method has no effect.
347
348 // MANIPULATORS
349
350 /// Return the address of the modifiable lock object under management by
351 /// this guard, and release the lock from further management by this
352 /// guard. If no lock is currently being managed, return 0 with no
353 /// other effect. Note that this operation does *not* lock the lock
354 /// object (if any) that was under management.
355 T *release();
356
357 // ACCESSORS
358
359 /// Return the address of the modifiable lock object under management by
360 /// this guard, or 0 if no lock is currently being managed.
361 T *ptr() const;
362};
363
364 // ===========================
365 // class WriteLockGuardTryLock
366 // ===========================
367
368/// This class template implements a guard for tentative acquisition and
369/// release of write synchronization resources (i.e., writer locks).
370///
371/// See @ref bslmt_writelockguard
372template <class T>
374
375 // DATA
376 T *d_lock_p; // lock guarded by this object (held, not owned)
377
378 private:
379 // NOT IMPLEMENTED
382
383 public:
384 // CREATORS
385
386 /// Create a scoped guard that conditionally manages the specified
387 /// `lock` (if non-null) and invokes `lock->tryLockWrite()` until the
388 /// lock is acquired for writing, or until the optionally specified
389 /// `attempts` have been made to acquire the lock. If `attempts` is not
390 /// specified only one attempt is made to acquire the lock. Supplying a
391 /// null `lock` has no effect. The behavior is undefined unless `lock`
392 /// (if non-null) is not already locked by this thread and
393 /// `0 < attempts`. Note that `lock` must remain valid throughout the
394 /// lifetime of this guard, or until `release` is called.
395 explicit WriteLockGuardTryLock(T *lock, int attempts = 1);
396
397 /// Destroy this scoped guard and invoke the `unlock` method on the
398 /// lock object under management by this guard, if any. If no lock is
399 /// currently being managed, this method has no effect.
401
402 // MANIPULATORS
403
404 /// Return the address of the modifiable lock object under management by
405 /// this guard, and release the lock from further management by this
406 /// guard. If no lock is currently being managed, return 0 with no
407 /// other effect. Note that this operation does *not* unlock the lock
408 /// object (if any) that was under management.
409 T *release();
410
411 // ACCESSORS
412
413 /// Return the address of the modifiable lock object under management by
414 /// this guard, or 0 if no lock is currently being managed.
415 T *ptr() const;
416};
417
418// ============================================================================
419// INLINE DEFINITIONS
420// ============================================================================
421
422 // --------------------
423 // class WriteLockGuard
424 // --------------------
425
426// CREATORS
427template <class T>
428inline
430: d_lock_p(lock)
431{
432 if (d_lock_p) {
433 d_lock_p->lockWrite();
434 }
435}
436
437template <class T>
438inline
439WriteLockGuard<T>::WriteLockGuard(T *lock, bool alreadyLockedFlag)
440: d_lock_p(lock)
441{
442 if (d_lock_p && !alreadyLockedFlag) {
443 d_lock_p->lockWrite();
444 }
445}
446
447template <class T>
448inline
450{
451 if (d_lock_p) {
452 d_lock_p->unlock();
453 }
454}
455
456// MANIPULATORS
457
458template <class T>
459inline
461{
462 T *lock = d_lock_p;
463 d_lock_p = 0;
464 return lock;
465}
466
467// ACCESSORS
468template <class T>
469inline
471{
472 return d_lock_p;
473}
474
475 // --------------------------
476 // class WriteLockGuardUnlock
477 // --------------------------
478
479// CREATORS
480template <class T>
481inline
483: d_lock_p(lock)
484{
485 if (d_lock_p) {
486 d_lock_p->unlock();
487 }
488}
489
490template <class T>
491inline
493 bool alreadyUnlockedFlag)
494: d_lock_p(lock)
495{
496 if (d_lock_p && !alreadyUnlockedFlag) {
497 d_lock_p->unlock();
498 }
499}
500
501template <class T>
502inline
504{
505 if (d_lock_p) {
506 d_lock_p->lockWrite();
507 }
508}
509
510// MANIPULATORS
511template <class T>
512inline
514{
515 T *lock = d_lock_p;
516 d_lock_p = 0;
517 return lock;
518}
519
520// ACCESSORS
521template <class T>
522inline
524{
525 return d_lock_p;
526}
527
528 // ---------------------------
529 // class WriteLockGuardTryLock
530 // ---------------------------
531
532// CREATORS
533template <class T>
535: d_lock_p(0)
536{
537 if (lock) {
538 while (attempts--) {
539 if (!lock->tryLockWrite()) {
540 d_lock_p = lock;
541 break;
542 }
543 }
544 }
545}
546
547template <class T>
548inline
550{
551 if (d_lock_p) {
552 d_lock_p->unlock();
553 }
554}
555
556// MANIPULATORS
557template <class T>
558inline
560{
561 T *lock = d_lock_p;
562 d_lock_p = 0;
563 return lock;
564}
565
566// ACCESSORS
567template <class T>
568inline
570{
571 return d_lock_p;
572}
573
574 // --------------------
575 // class LockWriteGuard
576 // --------------------
577
578// CREATORS
579template <class T>
580inline
582: WriteLockGuard<T>(lock)
583{
584}
585
586template <class T>
587inline
588LockWriteGuard<T>::LockWriteGuard(T *lock, bool alreadyLockedFlag)
589: WriteLockGuard<T>(lock, alreadyLockedFlag)
590{
591}
592
593} // close package namespace
594
595
596#endif
597
598// ----------------------------------------------------------------------------
599// Copyright 2015 Bloomberg Finance L.P.
600//
601// Licensed under the Apache License, Version 2.0 (the "License");
602// you may not use this file except in compliance with the License.
603// You may obtain a copy of the License at
604//
605// http://www.apache.org/licenses/LICENSE-2.0
606//
607// Unless required by applicable law or agreed to in writing, software
608// distributed under the License is distributed on an "AS IS" BASIS,
609// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
610// See the License for the specific language governing permissions and
611// limitations under the License.
612// ----------------------------- END-OF-FILE ----------------------------------
613
614/** @} */
615/** @} */
616/** @} */
Definition bslmt_writelockguard.h:283
Definition bslmt_writelockguard.h:373
~WriteLockGuardTryLock()
Definition bslmt_writelockguard.h:549
T * ptr() const
Definition bslmt_writelockguard.h:569
T * release()
Definition bslmt_writelockguard.h:559
Definition bslmt_writelockguard.h:310
T * release()
Definition bslmt_writelockguard.h:513
~WriteLockGuardUnlock()
Definition bslmt_writelockguard.h:503
T * ptr() const
Definition bslmt_writelockguard.h:523
Definition bslmt_writelockguard.h:221
~WriteLockGuard()
Definition bslmt_writelockguard.h:449
T * release()
Definition bslmt_writelockguard.h:460
T * ptr() const
Definition bslmt_writelockguard.h:470
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bslmt_barrier.h:344