BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslmt_muteximpl_win32.h
Go to the documentation of this file.
1/// @file bslmt_muteximpl_win32.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmt_muteximpl_win32.h -*-C++-*-
8#ifndef INCLUDED_BSLMT_MUTEXIMPL_WIN32
9#define INCLUDED_BSLMT_MUTEXIMPL_WIN32
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmt_muteximpl_win32 bslmt_muteximpl_win32
15/// @brief Provide a win32 implementation of `bslmt::Mutex`.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmt
19/// @{
20/// @addtogroup bslmt_muteximpl_win32
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmt_muteximpl_win32-purpose"> Purpose</a>
25/// * <a href="#bslmt_muteximpl_win32-classes"> Classes </a>
26/// * <a href="#bslmt_muteximpl_win32-description"> Description </a>
27/// * <a href="#bslmt_muteximpl_win32-usage"> Usage </a>
28///
29/// # Purpose {#bslmt_muteximpl_win32-purpose}
30/// Provide a win32 implementation of `bslmt::Mutex`.
31///
32/// # Classes {#bslmt_muteximpl_win32-classes}
33///
34/// - bslmt::MutexImpl<Platform::Win32Threads>: win32 specialization
35///
36/// @see bslmt_mutex
37///
38/// # Description {#bslmt_muteximpl_win32-description}
39/// This component provides an implementation of `bslmt::Mutex` for
40/// Windows (win32), `bslmt::MutexImpl<Platform::Win32Threads>`, via the
41/// template specialization:
42/// @code
43/// bslmt::MutexImpl<Platform::Win32Threads>
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_win32-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_win32
65 * @{
66 */
67
68#include <bslscm_version.h>
69
70#include <bslmt_platform.h>
71
72#include <bsls_platform.h>
73
74#ifdef BSLMT_PLATFORM_WIN32_THREADS
75
76// Platform-specific implementation starts here.
77
78// Rather than setting 'WINVER' or 'NTDDI_VERSION', just forward declare the
79// Windows 2000 functions that are used.
80
81struct _RTL_CRITICAL_SECTION;
82
83typedef struct _RTL_CRITICAL_SECTION CRITICAL_SECTION, *LPCRITICAL_SECTION;
84typedef int BOOL;
85typedef unsigned long DWORD;
86
87extern "C" {
88 __declspec(dllimport) BOOL __stdcall InitializeCriticalSectionAndSpinCount(
89 LPCRITICAL_SECTION lpCriticalSection,
90 DWORD dwSpinCount);
91
92 __declspec(dllimport) void __stdcall DeleteCriticalSection(
93 LPCRITICAL_SECTION lpCriticalSection);
94
95 __declspec(dllimport) BOOL __stdcall TryEnterCriticalSection(
96 LPCRITICAL_SECTION lpCriticalSection);
97
98 __declspec(dllimport) void __stdcall EnterCriticalSection(
99 LPCRITICAL_SECTION lpCriticalSection);
100
101 __declspec(dllimport) void __stdcall LeaveCriticalSection(
102 LPCRITICAL_SECTION lpCriticalSection);
103
104} // extern "C"
105
106
107namespace bslmt {
108
109template <class THREAD_POLICY>
110class MutexImpl;
111
112 // =======================================
113 // class MutexImpl<Platform::Win32Threads>
114 // =======================================
115
116/// This class provides a full specialization of `MutexImpl` for Windows.
117/// It provides an efficient proxy for Windows critical sections, and related operations.
118///
119/// \note Note that the mutex implemented in this class is
120/// *not* error checking, and is non-recursive.
121/// TYPES
122template <>
123class MutexImpl<Platform::Win32Threads> {
124
125 public:
126 enum {
127 // Size of the buffer allocated for the critical section, in
128 // pointer-sized elements. We have to make it public so we could
129 // access it in a .cpp file to verify the size.
130
131#ifdef BSLS_PLATFORM_CPU_64_BIT
132 // 5*8 = 40 bytes
133 k_CRITICAL_SECTION_BUFFER_SIZE = 5
134#else
135 // 6*4 = 24 bytes
136 k_CRITICAL_SECTION_BUFFER_SIZE = 6
137#endif
138 };
139
140 private:
141 enum {
142 // A Windows critical section has a configurable spin count. A lock
143 // operation spins this many iterations (on, presumably, some atomic
144 // integer) before sleeping on the underlying primitive.
145
146 k_SPIN_COUNT = 30
147 };
148
149 // DATA
150 void *d_lock[k_CRITICAL_SECTION_BUFFER_SIZE];
151
152 private:
153 // NOT IMPLEMENTED
154 MutexImpl(const MutexImpl&);
155 MutexImpl& operator=(const MutexImpl&);
156
157 public:
158 // PUBLIC TYPES
159
160 /// The underlying OS-level type. Exposed so that other bslmt components
161 /// can operate directly on this mutex.
162 typedef _RTL_CRITICAL_SECTION NativeType;
163
164 // CREATORS
165
166 /// Create a mutex initialized to an unlocked state.
167 MutexImpl();
168
169 /// Destroy this mutex object.
170 ~MutexImpl();
171
172 // MANIPULATORS
173
174 /// Acquire a lock on this mutex object. If this object is currently
175 /// locked, then suspend execution of the current thread until a lock can be acquired.
176 ///
177 /// \note Note that the behavior is undefined if the calling
178 /// thread already owns the lock on this mutex, and will likely result
179 /// in a deadlock.
180 void lock();
181
182 /// Return a reference to the modifiable OS-level mutex underlying this
183 /// object. This method is intended only to support other bslmt
184 /// components that must operate directly on this mutex.
185 NativeType& nativeMutex();
186
187 /// Attempt to acquire a lock on this mutex object. Return 0 on
188 /// success, and a non-zero value of this object is already locked, or
189 /// if an error occurs.
190 int tryLock();
191
192 /// Release a lock on this mutex that was previously acquired through a
193 /// successful call to `lock`, or `tryLock`. The behavior is undefined,
194 /// unless the calling thread currently owns the lock on this mutex.
195 void unlock();
196};
197
198// ============================================================================
199// INLINE DEFINITIONS
200// ============================================================================
201
202 // ---------------------------------------
203 // class MutexImpl<Platform::Win32Threads>
204 // ---------------------------------------
205
206// CREATORS
207inline
208MutexImpl<bslmt::Platform::Win32Threads>::MutexImpl()
209{
210 InitializeCriticalSectionAndSpinCount(
211 reinterpret_cast<_RTL_CRITICAL_SECTION *>(d_lock), k_SPIN_COUNT);
212}
213
214inline
215MutexImpl<bslmt::Platform::Win32Threads>::~MutexImpl()
216{
217 DeleteCriticalSection(
218 reinterpret_cast<_RTL_CRITICAL_SECTION*>(d_lock));
219}
220
221// MANIPULATORS
222inline
223void MutexImpl<bslmt::Platform::Win32Threads>::lock()
224{
225 EnterCriticalSection(
226 reinterpret_cast<_RTL_CRITICAL_SECTION*>(d_lock));
227}
228
229inline
230MutexImpl<bslmt::Platform::Win32Threads>::NativeType&
231MutexImpl<bslmt::Platform::Win32Threads>::nativeMutex()
232{
233 return *reinterpret_cast<_RTL_CRITICAL_SECTION*>(d_lock);
234}
235
236inline
237int MutexImpl<bslmt::Platform::Win32Threads>::tryLock()
238{
239 return !TryEnterCriticalSection(
240 reinterpret_cast<_RTL_CRITICAL_SECTION*>(d_lock));
241}
242
243inline
244void MutexImpl<bslmt::Platform::Win32Threads>::unlock()
245{
246 LeaveCriticalSection(
247 reinterpret_cast<_RTL_CRITICAL_SECTION*>(d_lock));
248}
249
250} // close package namespace
251
252
253#endif // BSLMT_PLATFORM_WIN32_THREADS
254
255#endif
256
257// ----------------------------------------------------------------------------
258// Copyright 2015 Bloomberg Finance L.P.
259//
260// Licensed under the Apache License, Version 2.0 (the "License");
261// you may not use this file except in compliance with the License.
262// You may obtain a copy of the License at
263//
264// http://www.apache.org/licenses/LICENSE-2.0
265//
266// Unless required by applicable law or agreed to in writing, software
267// distributed under the License is distributed on an "AS IS" BASIS,
268// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
269// See the License for the specific language governing permissions and
270// limitations under the License.
271// ----------------------------- END-OF-FILE ----------------------------------
272
273/** @} */
274/** @} */
275/** @} */
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bslmt_barrier.h:344