BDE 4.14.0 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
94/// class is *non*-recursive. Note that `BslLockImpl_pthread` is *not*
95/// intended for direct use by client code; it is meant for internal use
96/// only.
97///
98/// See @ref bsls_bsllockimpl_pthread
99class BslLockImpl_pthread {
100
101 // DATA
102 pthread_mutex_t d_lock; // 'pthreads' mutex object
103
104 private:
105 // NOT IMPLEMENTED
106 BslLockImpl_pthread(const BslLockImpl_pthread&); // = delete
107 BslLockImpl_pthread& operator=(const BslLockImpl_pthread&); // = delete
108
109 public:
110 // CREATORS
111
112 /// Create a lock object initialized to the unlocked state.
113 BslLockImpl_pthread();
114
115 /// Destroy this lock object. The behavior is undefined unless this
116 /// object is in the unlocked state.
117 ~BslLockImpl_pthread();
118
119 // MANIPULATORS
120
121 /// Acquire the lock on this object. If the lock on this object is
122 /// currently held by another thread, then suspend execution of the
123 /// calling thread until the lock can be acquired. The behavior is
124 /// undefined unless the calling thread does not already hold the lock
125 /// on this object. Note that deadlock may result if this method is
126 /// invoked while the calling thread holds the lock on the object.
127 void lock();
128
129 /// Release the lock on this object that was previously acquired through
130 /// a call to `lock`, enabling another thread to acquire the lock. The
131 /// behavior is undefined unless the calling thread holds the lock on
132 /// this object.
133 void unlock();
134};
135
136// ============================================================================
137// INLINE FUNCTION DEFINITIONS
138// ============================================================================
139
140 // -------------------------
141 // class BslLockImpl_pthread
142 // -------------------------
143
144// CREATORS
145inline
146BslLockImpl_pthread::BslLockImpl_pthread()
147{
148 const int status = pthread_mutex_init(&d_lock, 0);
149 (void)status;
150 BSLS_BSLLOCKIMPL_PTHREAD_ASSERT_SAFE(0 == status);
151}
152
153inline
154BslLockImpl_pthread::~BslLockImpl_pthread()
155{
156 const int status = pthread_mutex_destroy(&d_lock);
157 (void)status;
158 BSLS_BSLLOCKIMPL_PTHREAD_ASSERT_SAFE(0 == status);
159}
160
161// MANIPULATORS
162inline
163void BslLockImpl_pthread::lock()
164{
165 const int status = pthread_mutex_lock(&d_lock);
166 (void)status;
167 BSLS_BSLLOCKIMPL_PTHREAD_ASSERT_SAFE(0 == status);
168}
169
170inline
171void BslLockImpl_pthread::unlock()
172{
173 const int status = pthread_mutex_unlock(&d_lock);
174 (void)status;
175 BSLS_BSLLOCKIMPL_PTHREAD_ASSERT_SAFE(0 == status);
176}
177
178
179} // close package namespace
180
181
182#endif // BSLS_PLATFORM_OS_UNIX
183
184#endif
185
186// ----------------------------------------------------------------------------
187// Copyright 2021 Bloomberg Finance L.P.
188//
189// Licensed under the Apache License, Version 2.0 (the "License");
190// you may not use this file except in compliance with the License.
191// You may obtain a copy of the License at
192//
193// http://www.apache.org/licenses/LICENSE-2.0
194//
195// Unless required by applicable law or agreed to in writing, software
196// distributed under the License is distributed on an "AS IS" BASIS,
197// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
198// See the License for the specific language governing permissions and
199// limitations under the License.
200// ----------------------------- END-OF-FILE ----------------------------------
201
202/** @} */
203/** @} */
204/** @} */
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlt_iso8601util.h:691