BDE 4.14.0 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 // NOT IMPLEMENTED
113 RecursiveMutexImpl(const RecursiveMutexImpl&);
114 RecursiveMutexImpl& operator=(const RecursiveMutexImpl&);
115
116 public:
117 // CREATORS
118
119 /// Create a recursive mutex initialized to an unlocked state. This
120 /// method does not return normally unless there are sufficient system
121 /// resources to construct the object.
122 RecursiveMutexImpl();
123
124 /// Destroy this recursive mutex object.
125 ~RecursiveMutexImpl();
126
127 // MANIPULATORS
128
129 /// Acquire a lock on this mutex object. If this object is currently
130 /// locked by a different thread, then suspend execution of the current
131 /// thread until a lock can be acquired. Otherwise, if it unlocked, or
132 /// locked by the calling thread, then grant ownership of the lock
133 /// immediately and return. Note that when this object is recursively
134 /// locked by a thread, `unlock` must be called an equal number of times
135 /// before the lock is actually released.
136 void lock();
137
138 /// Attempt to acquire a lock on this mutex object. If this object is
139 /// unlocked, or locked by the calling thread, then grant ownership of
140 /// the lock immediately and return 0. Otherwise If this object is
141 /// currently locked by a different thread or if an error occurs, then
142 /// return a non-zero value. Note that when this object is recursively
143 /// locked by a thread, `unlock` must be called an equal number of times
144 /// before the lock is actually released.
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 /// Note that when this object is recursively locked by a thread,
151 /// `unlock` must be called an equal number of times before the lock is
152 /// actually released.
153 void unlock();
154};
155
156// ============================================================================
157// INLINE DEFINITIONS
158// ============================================================================
159
160 // ------------------------------------------------
161 // class RecursiveMutexImpl<Platform::PosixThreads>
162 // ------------------------------------------------
163
164// CREATORS
165inline
166RecursiveMutexImpl<bslmt::Platform::PosixThreads>::~RecursiveMutexImpl()
167{
168 pthread_mutex_destroy(&d_lock);
169}
170
171} // close package namespace
172
173
174#endif // BSLMT_PLATFORM_POSIX_THREADS
175
176#endif
177
178// ----------------------------------------------------------------------------
179// Copyright 2023 Bloomberg Finance L.P.
180//
181// Licensed under the Apache License, Version 2.0 (the "License");
182// you may not use this file except in compliance with the License.
183// You may obtain a copy of the License at
184//
185// http://www.apache.org/licenses/LICENSE-2.0
186//
187// Unless required by applicable law or agreed to in writing, software
188// distributed under the License is distributed on an "AS IS" BASIS,
189// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
190// See the License for the specific language governing permissions and
191// limitations under the License.
192// ----------------------------- END-OF-FILE ----------------------------------
193
194/** @} */
195/** @} */
196/** @} */
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bslmt_barrier.h:344
Definition bsls_spinlock.h:381