BDE 4.14.0 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
118/// related operations. Note that the mutex implemented in this class is
119/// *not* error checking, and is non-recursive.
120/// TYPES
121template <>
122class MutexImpl<Platform::Win32Threads> {
123
124 public:
125 enum {
126 // Size of the buffer allocated for the critical section, in
127 // pointer-sized elements. We have to make it public so we could
128 // access it in a .cpp file to verify the size.
129
130#ifdef BSLS_PLATFORM_CPU_64_BIT
131 // 5*8 = 40 bytes
132 k_CRITICAL_SECTION_BUFFER_SIZE = 5
133#else
134 // 6*4 = 24 bytes
135 k_CRITICAL_SECTION_BUFFER_SIZE = 6
136#endif
137 };
138
139 private:
140 enum {
141 // A Windows critical section has a configurable spin count. A lock
142 // operation spins this many iterations (on, presumably, some atomic
143 // integer) before sleeping on the underlying primitive.
144
145 k_SPIN_COUNT = 30
146 };
147
148 // DATA
149 void *d_lock[k_CRITICAL_SECTION_BUFFER_SIZE];
150
151 // NOT IMPLEMENTED
152 MutexImpl(const MutexImpl&);
153 MutexImpl& operator=(const MutexImpl&);
154
155 public:
156 // PUBLIC TYPES
157
158 /// The underlying OS-level type. Exposed so that other bslmt components
159 /// can operate directly on this mutex.
160 typedef _RTL_CRITICAL_SECTION NativeType;
161
162 // CREATORS
163
164 /// Create a mutex initialized to an unlocked state.
165 MutexImpl();
166
167 /// Destroy this mutex object.
168 ~MutexImpl();
169
170 // MANIPULATORS
171
172 /// Acquire a lock on this mutex object. If this object is currently
173 /// locked, then suspend execution of the current thread until a lock
174 /// can be acquired. Note that the behavior is undefined if the calling
175 /// thread already owns the lock on this mutex, and will likely result
176 /// in a deadlock.
177 void lock();
178
179 /// Return a reference to the modifiable OS-level mutex underlying this
180 /// object. This method is intended only to support other bslmt
181 /// components that must operate directly on this mutex.
182 NativeType& nativeMutex();
183
184 /// Attempt to acquire a lock on this mutex object. Return 0 on
185 /// success, and a non-zero value of this object is already locked, or
186 /// if an error occurs.
187 int tryLock();
188
189 /// Release a lock on this mutex that was previously acquired through a
190 /// successful call to `lock`, or `tryLock`. The behavior is undefined,
191 /// unless the calling thread currently owns the lock on this mutex.
192 void unlock();
193};
194
195// ============================================================================
196// INLINE DEFINITIONS
197// ============================================================================
198
199 // ---------------------------------------
200 // class MutexImpl<Platform::Win32Threads>
201 // ---------------------------------------
202
203// CREATORS
204inline
205MutexImpl<bslmt::Platform::Win32Threads>::MutexImpl()
206{
207 InitializeCriticalSectionAndSpinCount(
208 reinterpret_cast<_RTL_CRITICAL_SECTION *>(d_lock), k_SPIN_COUNT);
209}
210
211inline
212MutexImpl<bslmt::Platform::Win32Threads>::~MutexImpl()
213{
214 DeleteCriticalSection(
215 reinterpret_cast<_RTL_CRITICAL_SECTION*>(d_lock));
216}
217
218// MANIPULATORS
219inline
220void MutexImpl<bslmt::Platform::Win32Threads>::lock()
221{
222 EnterCriticalSection(
223 reinterpret_cast<_RTL_CRITICAL_SECTION*>(d_lock));
224}
225
226inline
227MutexImpl<bslmt::Platform::Win32Threads>::NativeType&
228MutexImpl<bslmt::Platform::Win32Threads>::nativeMutex()
229{
230 return *reinterpret_cast<_RTL_CRITICAL_SECTION*>(d_lock);
231}
232
233inline
234int MutexImpl<bslmt::Platform::Win32Threads>::tryLock()
235{
236 return !TryEnterCriticalSection(
237 reinterpret_cast<_RTL_CRITICAL_SECTION*>(d_lock));
238}
239
240inline
241void MutexImpl<bslmt::Platform::Win32Threads>::unlock()
242{
243 LeaveCriticalSection(
244 reinterpret_cast<_RTL_CRITICAL_SECTION*>(d_lock));
245}
246
247} // close package namespace
248
249
250#endif // BSLMT_PLATFORM_WIN32_THREADS
251
252#endif
253
254// ----------------------------------------------------------------------------
255// Copyright 2015 Bloomberg Finance L.P.
256//
257// Licensed under the Apache License, Version 2.0 (the "License");
258// you may not use this file except in compliance with the License.
259// You may obtain a copy of the License at
260//
261// http://www.apache.org/licenses/LICENSE-2.0
262//
263// Unless required by applicable law or agreed to in writing, software
264// distributed under the License is distributed on an "AS IS" BASIS,
265// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
266// See the License for the specific language governing permissions and
267// limitations under the License.
268// ----------------------------- END-OF-FILE ----------------------------------
269
270/** @} */
271/** @} */
272/** @} */
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bslmt_barrier.h:344