BDE 4.14.0 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 `pthread_mutex_t` pthreads type,
94/// and related operations. Note that the mutex implemented in this class
95/// is *not* error checking, and is non-recursive.
96template <>
97class MutexImpl<Platform::PosixThreads> {
98
99 // DATA
100 pthread_mutex_t d_lock; // TBD doc
101
102 // NOT IMPLEMENTED
103 MutexImpl(const MutexImpl&);
104 MutexImpl& operator=(const MutexImpl&);
105
106 public:
107 // PUBLIC TYPES
108
109 /// The underlying OS-level type. Exposed so that other `bslmt`
110 /// components can operate directly on this mutex.
111 typedef pthread_mutex_t NativeType;
112
113 // CREATORS
114
115 /// Create a mutex initialized to an unlocked state. This method does
116 /// not return normally unless there are sufficient system resources to
117 /// construct the object.
118 MutexImpl();
119
120 /// Destroy this mutex object. The behavior is undefined if the mutex
121 /// is in a locked state.
122 ~MutexImpl();
123
124 // MANIPULATORS
125
126 /// Acquire a lock on this mutex object. If this object is currently
127 /// locked, then suspend execution of the current thread until a lock
128 /// can be acquired. Note that the behavior is undefined if the calling
129 /// thread already owns the lock on this mutex, and will likely result
130 /// in a deadlock.
131 void lock();
132
133 /// Return a reference to the modifiable OS-level mutex underlying this
134 /// object. This method is intended only to support other `bslmt`
135 /// components that must operate directly on this mutex.
136 NativeType& nativeMutex();
137
138 /// Attempt to acquire a lock on this mutex object. Return 0 on
139 /// success, and a non-zero value of this object is already locked, or
140 /// if an error occurs.
141 int tryLock();
142
143 /// Release a lock on this mutex that was previously acquired through a
144 /// successful call to `lock`, or `tryLock`. The behavior is undefined,
145 /// unless the calling thread currently owns the lock on this mutex.
146 void unlock();
147};
148
149// ============================================================================
150// INLINE DEFINITIONS
151// ============================================================================
152
153 // ---------------------------------------
154 // class MutexImpl<Platform::PosixThreads>
155 // ---------------------------------------
156
157// CREATORS
158inline
159MutexImpl<bslmt::Platform::PosixThreads>::MutexImpl()
160{
161 const int status = pthread_mutex_init(&d_lock, 0);
162 if (status) {
163 BSLS_ASSERT_INVOKE_NORETURN("'pthread_mutex_init' failed");
164 }
165}
166
167// MANIPULATORS
168inline
169void MutexImpl<bslmt::Platform::PosixThreads>::lock()
170{
171 BSLS_ASSERT_SAFE(0xdeadbeef !=
172 *reinterpret_cast<const unsigned *>(&d_lock));
173
174 const int status = pthread_mutex_lock(&d_lock);
175 (void) status;
176 BSLS_ASSERT_SAFE(0 == status);
177}
178
179inline
180int MutexImpl<bslmt::Platform::PosixThreads>::tryLock()
181{
182 BSLS_ASSERT_SAFE(0xdeadbeef !=
183 *reinterpret_cast<const unsigned *>(&d_lock));
184
185 return pthread_mutex_trylock(&d_lock);
186}
187
188inline
189void MutexImpl<bslmt::Platform::PosixThreads>::unlock()
190{
191 BSLS_ASSERT_SAFE(0xdeadbeef !=
192 *reinterpret_cast<const unsigned *>(&d_lock));
193
194 const int status = pthread_mutex_unlock(&d_lock);
195 (void) status;
196 BSLS_ASSERT_SAFE(0 == status);
197}
198
199inline
200MutexImpl<bslmt::Platform::PosixThreads>::NativeType&
201MutexImpl<bslmt::Platform::PosixThreads>::nativeMutex()
202{
203 return d_lock;
204}
205
206} // close package namespace
207
208
209#endif // BSLMT_PLATFORM_POSIX_THREADS
210
211#endif
212
213// ----------------------------------------------------------------------------
214// Copyright 2023 Bloomberg Finance L.P.
215//
216// Licensed under the Apache License, Version 2.0 (the "License");
217// you may not use this file except in compliance with the License.
218// You may obtain a copy of the License at
219//
220// http://www.apache.org/licenses/LICENSE-2.0
221//
222// Unless required by applicable law or agreed to in writing, software
223// distributed under the License is distributed on an "AS IS" BASIS,
224// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
225// See the License for the specific language governing permissions and
226// limitations under the License.
227// ----------------------------- END-OF-FILE ----------------------------------
228
229/** @} */
230/** @} */
231/** @} */
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1762
#define BSLS_ASSERT_INVOKE_NORETURN(X)
Definition bsls_assert.h:1895
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bslmt_barrier.h:344