BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslmt_recursivemuteximpl_pthread.h
Go to the documentation of this file.
1/// @file bslmt_recursivemuteximpl_pthread.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmt_recursivemuteximpl_pthread.h -*-C++-*-
8#ifndef INCLUDED_BSLMT_RECURSIVEMUTEXIMPL_PTHREAD
9#define INCLUDED_BSLMT_RECURSIVEMUTEXIMPL_PTHREAD
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmt_recursivemuteximpl_pthread bslmt_recursivemuteximpl_pthread
15/// @brief Provide a POSIX implementation of `bslmt::RecursiveMutex`.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmt
19/// @{
20/// @addtogroup bslmt_recursivemuteximpl_pthread
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmt_recursivemuteximpl_pthread-purpose"> Purpose</a>
25/// * <a href="#bslmt_recursivemuteximpl_pthread-classes"> Classes </a>
26/// * <a href="#bslmt_recursivemuteximpl_pthread-description"> Description </a>
27/// * <a href="#bslmt_recursivemuteximpl_pthread-usage"> Usage </a>
28///
29/// # Purpose {#bslmt_recursivemuteximpl_pthread-purpose}
30/// Provide a POSIX implementation of `bslmt::RecursiveMutex`.
31///
32/// # Classes {#bslmt_recursivemuteximpl_pthread-classes}
33///
34/// - bslmt::RecursiveMutexImpl<PosixThreads>: POSIX specialization
35///
36/// @see bslmt_recursivemutex
37///
38/// # Description {#bslmt_recursivemuteximpl_pthread-description}
39/// This component provides an implementation of
40/// `bslmt::RecursiveMutex` for POSIX threads ("pthreads"),
41/// `bslmt::RecursiveMutexImpl<PosixThreads>`, via the template specialization:
42/// @code
43/// bslmt::RecursiveMutexImpl<Platform::PosixThreads>
44/// @endcode
45/// This template class should not be used (directly) by client code. Clients
46/// should instead use `bslmt::RecursiveMutex`.
47///
48/// ## Usage {#bslmt_recursivemuteximpl_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_recursivemuteximpl_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 <pthread.h>
77
78#ifndef PTHREAD_MUTEX_RECURSIVE
79#include <bsls_spinlock.h>
80#endif
81
82
83namespace bslmt {
84
85template <class THREAD_POLICY>
86class RecursiveMutexImpl;
87
88 // ================================================
89 // class RecursiveMutexImpl<Platform::PosixThreads>
90 // ================================================
91
92/// This class provides a full specialization of `RecursiveMutexImpl` for
93/// pthreads. If the pthreads implementation supports the "recursive"
94/// attribute, then the native implementation is used, otherwise, a
95/// portable, efficient implementation is provided.
96template <>
97class RecursiveMutexImpl<Platform::PosixThreads> {
98
99 // DATA
100 pthread_mutex_t d_lock; // TBD doc
101
102#ifndef PTHREAD_MUTEX_RECURSIVE
103 bsls::SpinLock d_spin; // spin lock controlling access to this
104 // object
105
106 pthread_t d_owner; // thread id of thread currently owning this
107 // lock object
108
109 int d_lockCount; // current lock recursion level
110#endif
111
112 private:
113 // NOT IMPLEMENTED
114 RecursiveMutexImpl(const RecursiveMutexImpl&);
115 RecursiveMutexImpl& operator=(const RecursiveMutexImpl&);
116
117 public:
118 // CREATORS
119
120 /// Create a recursive mutex initialized to an unlocked state. This
121 /// method does not return normally unless there are sufficient system
122 /// resources to construct the object.
123 RecursiveMutexImpl();
124
125 /// Destroy this recursive mutex object.
126 ~RecursiveMutexImpl();
127
128 // MANIPULATORS
129
130 /// Acquire a lock on this mutex object. If this object is currently
131 /// locked by a different thread, then suspend execution of the current
132 /// thread until a lock can be acquired. Otherwise, if it unlocked, or
133 /// locked by the calling thread, then grant ownership of the lock immediately and return.
134 ///
135 /// \note Note that when this object is recursively
136 /// locked by a thread, `unlock` must be called an equal number of times
137 /// before the lock is actually released.
138 void lock();
139
140 /// Attempt to acquire a lock on this mutex object. If this object is
141 /// unlocked, or locked by the calling thread, then grant ownership of
142 /// the lock immediately and return 0. Otherwise If this object is
143 /// currently locked by a different thread or if an error occurs, then return a non-zero value.
144 ///
145 /// \note Note that when this object is recursively
146 /// locked by a thread, `unlock` must be called an equal number of times
147 /// before the lock is actually released.
148 int tryLock();
149
150 /// Release a lock on this mutex that was previously acquired through a
151 /// successful call to `lock`, or `tryLock`. The behavior is undefined,
152 /// unless the calling thread currently owns the lock on this mutex.
153 ///
154 /// \note Note that when this object is recursively locked by a thread,
155 /// `unlock` must be called an equal number of times before the lock is
156 /// actually released.
157 void unlock();
158};
159
160// ============================================================================
161// INLINE DEFINITIONS
162// ============================================================================
163
164 // ------------------------------------------------
165 // class RecursiveMutexImpl<Platform::PosixThreads>
166 // ------------------------------------------------
167
168// CREATORS
169inline
170RecursiveMutexImpl<bslmt::Platform::PosixThreads>::~RecursiveMutexImpl()
171{
172 pthread_mutex_destroy(&d_lock);
173}
174
175} // close package namespace
176
177
178#endif // BSLMT_PLATFORM_POSIX_THREADS
179
180#endif
181
182// ----------------------------------------------------------------------------
183// Copyright 2023 Bloomberg Finance L.P.
184//
185// Licensed under the Apache License, Version 2.0 (the "License");
186// you may not use this file except in compliance with the License.
187// You may obtain a copy of the License at
188//
189// http://www.apache.org/licenses/LICENSE-2.0
190//
191// Unless required by applicable law or agreed to in writing, software
192// distributed under the License is distributed on an "AS IS" BASIS,
193// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
194// See the License for the specific language governing permissions and
195// limitations under the License.
196// ----------------------------- END-OF-FILE ----------------------------------
197
198/** @} */
199/** @} */
200/** @} */
#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 bsls_spinlock.h:383