BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslmt_muteximpl_pthread.h
Go to the documentation of this file.
1/// @file bslmt_muteximpl_pthread.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmt_muteximpl_pthread.h -*-C++-*-
8#ifndef INCLUDED_BSLMT_MUTEXIMPL_PTHREAD
9#define INCLUDED_BSLMT_MUTEXIMPL_PTHREAD
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmt_muteximpl_pthread bslmt_muteximpl_pthread
15/// @brief Provide a POSIX implementation of `bslmt::Mutex`.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmt
19/// @{
20/// @addtogroup bslmt_muteximpl_pthread
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmt_muteximpl_pthread-purpose"> Purpose</a>
25/// * <a href="#bslmt_muteximpl_pthread-classes"> Classes </a>
26/// * <a href="#bslmt_muteximpl_pthread-description"> Description </a>
27/// * <a href="#bslmt_muteximpl_pthread-usage"> Usage </a>
28///
29/// # Purpose {#bslmt_muteximpl_pthread-purpose}
30/// Provide a POSIX implementation of `bslmt::Mutex`.
31///
32/// # Classes {#bslmt_muteximpl_pthread-classes}
33///
34/// - bslmt::MutexImpl<Platform::PosixThreads>: POSIX specialization
35///
36/// @see bslmt_mutex
37///
38/// # Description {#bslmt_muteximpl_pthread-description}
39/// This component provides an implementation of `bslmt::Mutex` for
40/// POSIX threads ("pthreads"), `bslmt::MutexImpl<Platform::PosixThreads>`, via
41/// the template specialization:
42/// @code
43/// bslmt::MutexImpl<Platform::PosixThreads>
44/// @endcode
45/// This template class should not be used (directly) by client code. Clients
46/// should instead use `bslmt::Mutex`.
47///
48/// ## Usage {#bslmt_muteximpl_pthread-usage}
49///
50///
51/// This component is an implementation detail of `bslmt` and is *not* intended
52/// for direct client use. It is subject to change without notice. As such, a
53/// usage example is not provided.
54/// @}
55/** @} */
56/** @} */
57
58/** @addtogroup bsl
59 * @{
60 */
61/** @addtogroup bslmt
62 * @{
63 */
64/** @addtogroup bslmt_muteximpl_pthread
65 * @{
66 */
67
68#include <bslscm_version.h>
69
70#include <bslmt_platform.h>
71
72#ifdef BSLMT_PLATFORM_POSIX_THREADS
73
74// Platform-specific implementation starts here.
75
76#include <bsls_assert.h>
77
78#include <bsl_cstring.h>
79
80#include <pthread.h>
81
82
83namespace bslmt {
84
85template <class THREAD_POLICY>
86class MutexImpl;
87
88 // =======================================
89 // class MutexImpl<Platform::PosixThreads>
90 // =======================================
91
92/// This class provides a full specialization of `MutexImpl` for pthreads.
93/// It provides a efficient proxy for the @ref pthread_mutex_t pthreads type, and related operations.
94///
95/// \note Note that the mutex implemented in this class
96/// is *not* error checking, and is non-recursive.
97template <>
98class MutexImpl<Platform::PosixThreads> {
99
100 // DATA
101 pthread_mutex_t d_lock; // TBD doc
102
103 private:
104 // NOT IMPLEMENTED
105 MutexImpl(const MutexImpl&);
106 MutexImpl& operator=(const MutexImpl&);
107
108 public:
109 // PUBLIC TYPES
110
111 /// The underlying OS-level type. Exposed so that other `bslmt`
112 /// components can operate directly on this mutex.
113 typedef pthread_mutex_t NativeType;
114
115 // CREATORS
116
117 /// Create a mutex initialized to an unlocked state. This method does
118 /// not return normally unless there are sufficient system resources to
119 /// construct the object.
120 MutexImpl();
121
122 /// Destroy this mutex object.
123 /// \pre The behavior is undefined if the mutex
124 /// is in a locked state.
125 ~MutexImpl();
126
127 // MANIPULATORS
128
129 /// Acquire a lock on this mutex object. If this object is currently
130 /// locked, then suspend execution of the current thread until a lock can be acquired.
131 ///
132 /// \note Note that the behavior is undefined if the calling
133 /// thread already owns the lock on this mutex, and will likely result
134 /// in a deadlock.
135 void lock();
136
137 /// Return a reference to the modifiable OS-level mutex underlying this
138 /// object. This method is intended only to support other `bslmt`
139 /// components that must operate directly on this mutex.
140 NativeType& nativeMutex();
141
142 /// Attempt to acquire a lock on this mutex object. Return 0 on
143 /// success, and a non-zero value of this object is already locked, or
144 /// if an error occurs.
145 int tryLock();
146
147 /// Release a lock on this mutex that was previously acquired through a
148 /// successful call to `lock`, or `tryLock`. The behavior is undefined,
149 /// unless the calling thread currently owns the lock on this mutex.
150 void unlock();
151};
152
153// ============================================================================
154// INLINE DEFINITIONS
155// ============================================================================
156
157 // ---------------------------------------
158 // class MutexImpl<Platform::PosixThreads>
159 // ---------------------------------------
160
161// CREATORS
162inline
163MutexImpl<bslmt::Platform::PosixThreads>::MutexImpl()
164{
165 const int status = pthread_mutex_init(&d_lock, 0);
166 if (status) {
167 BSLS_ASSERT_INVOKE_NORETURN("'pthread_mutex_init' failed");
168 }
169}
170
171// MANIPULATORS
172inline
173void MutexImpl<bslmt::Platform::PosixThreads>::lock()
174{
175 BSLS_ASSERT_SAFE(0xdeadbeef !=
176 *reinterpret_cast<const unsigned *>(&d_lock));
177
178 const int status = pthread_mutex_lock(&d_lock);
179 (void) status;
180 BSLS_ASSERT_SAFE(0 == status);
181}
182
183inline
184int MutexImpl<bslmt::Platform::PosixThreads>::tryLock()
185{
186 BSLS_ASSERT_SAFE(0xdeadbeef !=
187 *reinterpret_cast<const unsigned *>(&d_lock));
188
189 return pthread_mutex_trylock(&d_lock);
190}
191
192inline
193void MutexImpl<bslmt::Platform::PosixThreads>::unlock()
194{
195 BSLS_ASSERT_SAFE(0xdeadbeef !=
196 *reinterpret_cast<const unsigned *>(&d_lock));
197
198 const int status = pthread_mutex_unlock(&d_lock);
199 (void) status;
200 BSLS_ASSERT_SAFE(0 == status);
201}
202
203inline
204MutexImpl<bslmt::Platform::PosixThreads>::NativeType&
205MutexImpl<bslmt::Platform::PosixThreads>::nativeMutex()
206{
207 return d_lock;
208}
209
210} // close package namespace
211
212
213#endif // BSLMT_PLATFORM_POSIX_THREADS
214
215#endif
216
217// ----------------------------------------------------------------------------
218// Copyright 2023 Bloomberg Finance L.P.
219//
220// Licensed under the Apache License, Version 2.0 (the "License");
221// you may not use this file except in compliance with the License.
222// You may obtain a copy of the License at
223//
224// http://www.apache.org/licenses/LICENSE-2.0
225//
226// Unless required by applicable law or agreed to in writing, software
227// distributed under the License is distributed on an "AS IS" BASIS,
228// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
229// See the License for the specific language governing permissions and
230// limitations under the License.
231// ----------------------------- END-OF-FILE ----------------------------------
232
233/** @} */
234/** @} */
235/** @} */
#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