BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslmt_rwmutex.h
Go to the documentation of this file.
1/// @file bslmt_rwmutex.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmt_rwmutex.h -*-C++-*-
8#ifndef INCLUDED_BSLMT_RWMUTEX
9#define INCLUDED_BSLMT_RWMUTEX
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmt_rwmutex bslmt_rwmutex
15/// @brief <span style="color: var(--deprecated-color-dark)">DEPRECATED:</span> Provide a platform-independent RW mutex class.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmt
19/// @{
20/// @addtogroup bslmt_rwmutex
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmt_rwmutex-purpose"> Purpose</a>
25/// * <a href="#bslmt_rwmutex-classes"> Classes </a>
26/// * <a href="#bslmt_rwmutex-description"> Description </a>
27/// * <a href="#bslmt_rwmutex-usage"> Usage </a>
28/// * <a href="#bslmt_rwmutex-example-1-basic-usage"> Example 1: Basic Usage </a>
29///
30/// # Purpose {#bslmt_rwmutex-purpose}
31/// Provide a platform-independent RW mutex class.
32///
33/// @deprecated Use @ref bslmt_readerwritermutex instead.
34///
35/// # Classes {#bslmt_rwmutex-classes}
36///
37/// - bslmt::RWMutex: platform-independent wrapper of an RW mutex
38///
39/// @see bslmt_readerwritermutex, bslmt_readerwriterlock,
40/// bslmt_readlockguard, bslmt_writelockguard
41///
42/// # Description {#bslmt_rwmutex-description}
43/// This component provides a class, `bslmt::RWMutex`, that defines
44/// a platform-independent RW mutex. An RW mutex provides for a shared "read"
45/// lock that may be held simultaneously by any number of threads, and a "write"
46/// lock that is exclusive (i.e., it may be held by only one thread at a time).
47/// The "write" lock is also exclusive with the "read" lock, so that no threads
48/// may hold a "read" lock while the "write" lock is held, and vice versa.
49///
50/// ## Usage {#bslmt_rwmutex-usage}
51///
52///
53/// This section illustrates intended use of this component.
54///
55/// ### Example 1: Basic Usage {#bslmt_rwmutex-example-1-basic-usage}
56///
57///
58/// TBD
59/// @}
60/** @} */
61/** @} */
62
63/** @addtogroup bsl
64 * @{
65 */
66/** @addtogroup bslmt
67 * @{
68 */
69/** @addtogroup bslmt_rwmutex
70 * @{
71 */
72
73#include <bslscm_version.h>
74
75#include <bslmt_platform.h>
76
77#include <bsls_assert.h>
78#include <bsls_platform.h>
79
80#if defined(BSLMT_PLATFORM_WIN32_THREADS) || defined(BSLS_PLATFORM_OS_AIX)
82#endif
83
84#ifdef BSLMT_PLATFORM_POSIX_THREADS
85#include <pthread.h>
86#endif
87#include <bsl_cstddef.h>
88
89
90namespace bslmt {
91
92template <class THREAD_POLICY>
94
95} // close package namespace
96
97#ifdef BSLMT_PLATFORM_POSIX_THREADS
98
99namespace bslmt {
100
101 // ================================
102 // struct RWMutexImpl<PosixThreads>
103 // ================================
104
105/// This is a platform-specific implementation detail that is not intended
106/// for use outside of this component. Use the `RWMutex` class instead.
107/// This structure is a wrapper around a POSIX RW lock on Sun (on AIX the
108/// POSIX RW lock has poor performance and no writer guarantees).
109template <>
110struct RWMutexImpl<Platform::PosixThreads> {
111
112 private:
113 // DATA
114 pthread_rwlock_t d_lock;
115
116 public:
117 // CREATORS
118 RWMutexImpl();
119 ~RWMutexImpl();
120
121 // MANIPULATORS
122 void lockRead();
123 void lockWrite();
124 int tryLockRead();
125 int tryLockWrite();
126 void unlock();
127};
128
129} // close package namespace
130
131#endif // BSLMT_PLATFORM_POSIX_THREADS
132
133namespace bslmt {
134
135 // =============
136 // class RWMutex
137 // =============
138
139/// This class is a platform-independent interface to a reader-writer lock
140/// ("RW mutex"). Multiple readers can safely hold the lock simultaneously,
141/// whereas only one writer is allowed to hold the lock at a time. This
142/// class uses the most efficient RW mutex implementation available for the current platform.
143///
144/// \note Note that the implementation may allow readers to
145/// starve writers.
146///
147/// See @ref bslmt_rwmutex
148class RWMutex {
149
150 // DATA
151#if defined(BSLS_PLATFORM_OS_AIX) || defined(BSLMT_PLATFORM_WIN32_THREADS)
152 ReaderWriterLock d_impl;
153#else
155#endif
156
157 private:
158 // NOT IMPLEMENTED
159 RWMutex(const RWMutex&);
160 RWMutex& operator=(const RWMutex&);
161
162 public:
163 // CREATORS
164
165 /// Create an RW mutex initialized to an unlocked state.
166 RWMutex();
167
168 /// Destroy this RW mutex.
169 /// \pre The behavior is undefined if the mutex
170 /// is in a locked state.
171 ~RWMutex();
172
173 // MANIPULATORS
174
175 /// Lock this reader-writer mutex for reading. If there are no active
176 /// or pending write locks, lock this mutex for reading and return
177 /// immediately. Otherwise, block until the read lock on this mutex is
178 /// acquired. Use `unlock` to release the lock on this mutex.
179 ///
180 /// \pre The behavior is undefined if this method is called from a thread that
181 /// already has a lock on this mutex.
182 void lockRead();
183
184 /// Lock this reader-writer mutex for writing. If there are no active
185 /// or pending locks on this mutex, lock this mutex for writing and
186 /// return immediately. Otherwise, block until the write lock on this
187 /// mutex is acquired. Use `unlock` to release the lock on this mutex.
188 ///
189 /// \pre The behavior is undefined if this method is called from a thread
190 /// that already has a lock on this mutex.
191 void lockWrite();
192
193 /// Attempt to lock this reader-writer mutex for reading. Immediately
194 /// return 0 on success, and a non-zero value if there are active or
195 /// pending writers. If successful, `unlock` must be used to release the lock on this mutex.
196 ///
197 /// \pre The behavior is undefined if this method is
198 /// called from a thread that already has a lock on this mutex.
199 int tryLockRead();
200
201 /// Attempt to lock this reader-writer mutex for writing. Immediately
202 /// return 0 on success, and a non-zero value if there are active or
203 /// pending locks on this mutex. If successful, `unlock` must be used to release the lock on this mutex.
204 ///
205 /// \pre The behavior is undefined if
206 /// this method is called from a thread that already has a lock on this
207 /// mutex.
208 int tryLockWrite();
209
210 /// Release the lock that the calling thread holds on this reader-writer mutex.
211 ///
212 /// \pre The behavior is undefined unless the calling thread
213 /// currently has a lock on this mutex.
214 void unlock();
215};
216
217
218// ============================================================================
219// INLINE DEFINITIONS
220// ============================================================================
221
222#ifdef BSLMT_PLATFORM_POSIX_THREADS
223
224 // ------------------
225 // struct RWMutexImpl
226 // ------------------
227
228// CREATORS
229inline
231{
232 const int rc = pthread_rwlock_init(&d_lock, NULL);
233
234 // pthread_rwlock_init should not return a failure code.
235
236 if (rc) {
237 BSLS_ASSERT_INVOKE_NORETURN("'pthread_rwlock_init' failed");
238 }
239}
240
241inline
242RWMutexImpl<bslmt::Platform::PosixThreads>::~RWMutexImpl()
243{
244 const int rc = pthread_rwlock_destroy(&d_lock);
245
246 // pthread_rwlock_destroy should not return a failure code.
247
248 BSLS_ASSERT_SAFE(0 == rc);
249 (void) rc; // suppress 'unused variable' warnings
250}
251
252// MANIPULATORS
253inline
254void
255RWMutexImpl<bslmt::Platform::PosixThreads>::lockRead()
256{
257 const int rc = pthread_rwlock_rdlock(&d_lock);
258
259 // pthread_rwlock_rdlock should not return a failure code.
260
261 BSLS_ASSERT_SAFE(0 == rc);
262 (void) rc; // suppress 'unused variable' warnings
263}
264
265inline
266void
267RWMutexImpl<bslmt::Platform::PosixThreads>::lockWrite()
268{
269 const int rc = pthread_rwlock_wrlock(&d_lock);
270
271 // pthread_rwlock_wrlock should not return a failure code.
272
273 BSLS_ASSERT_SAFE(0 == rc);
274 (void) rc; // suppress 'unused variable' warnings
275}
276
277inline
278int
279RWMutexImpl<bslmt::Platform::PosixThreads>::tryLockRead()
280{
281 return pthread_rwlock_tryrdlock(&d_lock) ? 1 : 0;
282}
283
284inline
285int
286RWMutexImpl<bslmt::Platform::PosixThreads>::tryLockWrite()
287{
288 return pthread_rwlock_trywrlock(&d_lock) ? 1 : 0;
289}
290
291inline
292void
293RWMutexImpl<bslmt::Platform::PosixThreads>::unlock()
294{
295 pthread_rwlock_unlock(&d_lock);
296}
297
298#endif // BSLMT_PLATFORM_POSIX_THREADS
299
300 // -------------
301 // class RWMutex
302 // -------------
303
304// CREATORS
305inline
309
310inline
314
315// MANIPULATORS
316inline
318{
319 d_impl.lockRead();
320}
321
322inline
324{
325 d_impl.lockWrite();
326}
327
328inline
330{
331 return d_impl.tryLockRead();
332}
333
334inline
336{
337 return d_impl.tryLockWrite();
338}
339
340inline
342{
343 d_impl.unlock();
344}
345
346} // close package namespace
347
348
349#endif
350
351// ----------------------------------------------------------------------------
352// Copyright 2023 Bloomberg Finance L.P.
353//
354// Licensed under the Apache License, Version 2.0 (the "License");
355// you may not use this file except in compliance with the License.
356// You may obtain a copy of the License at
357//
358// http://www.apache.org/licenses/LICENSE-2.0
359//
360// Unless required by applicable law or agreed to in writing, software
361// distributed under the License is distributed on an "AS IS" BASIS,
362// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
363// See the License for the specific language governing permissions and
364// limitations under the License.
365// ----------------------------- END-OF-FILE ----------------------------------
366
367/** @} */
368/** @} */
369/** @} */
Definition bslmt_rwmutex.h:148
int tryLockWrite()
Definition bslmt_rwmutex.h:335
int tryLockRead()
Definition bslmt_rwmutex.h:329
RWMutex()
Create an RW mutex initialized to an unlocked state.
Definition bslmt_rwmutex.h:306
~RWMutex()
Definition bslmt_rwmutex.h:311
void lockRead()
Definition bslmt_rwmutex.h:317
void unlock()
Definition bslmt_rwmutex.h:341
void lockWrite()
Definition bslmt_rwmutex.h:323
Definition bslmt_readerwriterlock.h:294
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1917
#define BSLS_ASSERT_INVOKE_NORETURN(X)
Definition bsls_assert.h:2101
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bslmt_barrier.h:344
Definition bslmt_platform.h:83
Definition bslmt_rwmutex.h:93