BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bsls_bsllockimpl_pthread.h
Go to the documentation of this file.
1/// @file bsls_bsllockimpl_pthread.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bsls_bsllockimpl_pthread.h -*-C++-*-
8#ifndef INCLUDED_BSLS_BSLLOCKIMPL_PTHREAD
9#define INCLUDED_BSLS_BSLLOCKIMPL_PTHREAD
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bsls_bsllockimpl_pthread bsls_bsllockimpl_pthread
15/// @brief Provide a mutex for use below `bslmt`.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bsls
19/// @{
20/// @addtogroup bsls_bsllockimpl_pthread
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bsls_bsllockimpl_pthread-purpose"> Purpose</a>
25/// * <a href="#bsls_bsllockimpl_pthread-classes"> Classes </a>
26/// * <a href="#bsls_bsllockimpl_pthread-description"> Description </a>
27/// * <a href="#bsls_bsllockimpl_pthread-usage"> Usage </a>
28///
29/// # Purpose {#bsls_bsllockimpl_pthread-purpose}
30/// Provide a mutex for use below `bslmt`.
31///
32/// # Classes {#bsls_bsllockimpl_pthread-classes}
33///
34/// - bsls::BslLockImpl_pthread: pthread mutex
35///
36/// @see bslmt_mutex
37///
38/// # Description {#bsls_bsllockimpl_pthread-description}
39/// This component provides a mutually exclusive lock primitive
40/// ("mutex") by wrapping a suitable platform-specific mechanism. The
41/// `bsls::BslLockImpl_pthread` class provides `lock` and `unlock` operations.
42/// Note that `bsls::BslLockImpl_pthread` is not intended for direct client use;
43/// see @ref bslmt_mutex instead. Also note that `bsls::BslLockImpl_pthread` is
44/// not recursive.
45///
46/// ## Usage {#bsls_bsllockimpl_pthread-usage}
47///
48///
49/// This component is an implementation detail of `bsls` and is *not* intended
50/// for direct client use. It is subject to change without notice. As such, a
51/// usage example is not provided.
52/// @}
53/** @} */
54/** @} */
55
56/** @addtogroup bsl
57 * @{
58 */
59/** @addtogroup bsls
60 * @{
61 */
62/** @addtogroup bsls_bsllockimpl_pthread
63 * @{
64 */
65
66#include <bsls_buildtarget.h>
67#include <bsls_platform.h>
68
69#ifdef BSLS_PLATFORM_OS_UNIX
70
71#include <pthread.h>
72
73#ifdef BDE_BUILD_TARGET_SAFE
74// This component needs to be below bsls_assert in the physical hierarchy, so
75// 'BSLS_ASSERT' macros can't be used here. To workaround this issue, we use
76// the C 'assert' instead.
77
78#include <assert.h>
79
80#define BSLS_BSLLOCKIMPL_PTHREAD_ASSERT_SAFE(x) assert((x))
81#else
82#define BSLS_BSLLOCKIMPL_PTHREAD_ASSERT_SAFE(x)
83#endif
84
85
86namespace bsls {
87
88 // =========================
89 // class BslLockImpl_pthread
90 // =========================
91
92/// This `class` implements a light-weight wrapper of an OS-level mutex to
93/// support intra-process synchronization. The mutex implemented by this class is *non*-recursive.
94///
95/// \note Note that `BslLockImpl_pthread` is *not*
96/// intended for direct use by client code; it is meant for internal use
97/// only.
98///
99/// See @ref bsls_bsllockimpl_pthread
100class BslLockImpl_pthread {
101
102 // DATA
103 pthread_mutex_t d_lock; // 'pthreads' mutex object
104
105 private:
106 // NOT IMPLEMENTED
107 BslLockImpl_pthread(const BslLockImpl_pthread&); // = delete
108 BslLockImpl_pthread& operator=(const BslLockImpl_pthread&); // = delete
109
110 public:
111 // CREATORS
112
113 /// Create a lock object initialized to the unlocked state.
114 BslLockImpl_pthread();
115
116 /// Destroy this lock object.
117 /// \pre The behavior is undefined unless this
118 /// object is in the unlocked state.
119 ~BslLockImpl_pthread();
120
121 // MANIPULATORS
122
123 /// Acquire the lock on this object. If the lock on this object is
124 /// currently held by another thread, then suspend execution of the
125 /// calling thread until the lock can be acquired.
126 ///
127 /// \pre The behavior is undefined unless the calling thread does not already hold the lock on this object.
128 ///
129 /// \note Note that deadlock may result if this method is
130 /// invoked while the calling thread holds the lock on the object.
131 void lock();
132
133 /// Release the lock on this object that was previously acquired through
134 /// a call to `lock`, enabling another thread to acquire the lock.
135 ///
136 /// \pre The behavior is undefined unless the calling thread holds the lock on
137 /// this object.
138 void unlock();
139};
140
141// ============================================================================
142// INLINE FUNCTION DEFINITIONS
143// ============================================================================
144
145 // -------------------------
146 // class BslLockImpl_pthread
147 // -------------------------
148
149// CREATORS
150inline
151BslLockImpl_pthread::BslLockImpl_pthread()
152{
153 const int status = pthread_mutex_init(&d_lock, 0);
154 (void)status;
155 BSLS_BSLLOCKIMPL_PTHREAD_ASSERT_SAFE(0 == status);
156}
157
158inline
159BslLockImpl_pthread::~BslLockImpl_pthread()
160{
161 const int status = pthread_mutex_destroy(&d_lock);
162 (void)status;
163 BSLS_BSLLOCKIMPL_PTHREAD_ASSERT_SAFE(0 == status);
164}
165
166// MANIPULATORS
167inline
168void BslLockImpl_pthread::lock()
169{
170 const int status = pthread_mutex_lock(&d_lock);
171 (void)status;
172 BSLS_BSLLOCKIMPL_PTHREAD_ASSERT_SAFE(0 == status);
173}
174
175inline
176void BslLockImpl_pthread::unlock()
177{
178 const int status = pthread_mutex_unlock(&d_lock);
179 (void)status;
180 BSLS_BSLLOCKIMPL_PTHREAD_ASSERT_SAFE(0 == status);
181}
182
183
184} // close package namespace
185
186
187#endif // BSLS_PLATFORM_OS_UNIX
188
189#endif
190
191// ----------------------------------------------------------------------------
192// Copyright 2021 Bloomberg Finance L.P.
193//
194// Licensed under the Apache License, Version 2.0 (the "License");
195// you may not use this file except in compliance with the License.
196// You may obtain a copy of the License at
197//
198// http://www.apache.org/licenses/LICENSE-2.0
199//
200// Unless required by applicable law or agreed to in writing, software
201// distributed under the License is distributed on an "AS IS" BASIS,
202// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
203// See the License for the specific language governing permissions and
204// limitations under the License.
205// ----------------------------- END-OF-FILE ----------------------------------
206
207/** @} */
208/** @} */
209/** @} */
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlt_iso8601util.h:707