BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bsls_bsllockimpl_win32.h
Go to the documentation of this file.
1/// @file bsls_bsllockimpl_win32.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bsls_bsllockimpl_win32.h -*-C++-*-
8#ifndef INCLUDED_BSLS_BSLLOCKIMPL_WIN32
9#define INCLUDED_BSLS_BSLLOCKIMPL_WIN32
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bsls_bsllockimpl_win32 bsls_bsllockimpl_win32
15/// @brief Provide a mutex for use below `bslmt` for windows platform.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bsls
19/// @{
20/// @addtogroup bsls_bsllockimpl_win32
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bsls_bsllockimpl_win32-purpose"> Purpose</a>
25/// * <a href="#bsls_bsllockimpl_win32-classes"> Classes </a>
26/// * <a href="#bsls_bsllockimpl_win32-description"> Description </a>
27/// * <a href="#bsls_bsllockimpl_win32-usage"> Usage </a>
28///
29/// # Purpose {#bsls_bsllockimpl_win32-purpose}
30/// Provide a mutex for use below `bslmt` for windows platform.
31///
32/// # Classes {#bsls_bsllockimpl_win32-classes}
33///
34/// - bsls::BslLockImpl_win32: windows mutex
35///
36/// @see bslmt_mutex
37///
38/// # Description {#bsls_bsllockimpl_win32-description}
39/// This component provides a mutually exclusive lock primitive
40/// ("mutex") by wrapping a suitable platform-specific mechanism. The
41/// `bsls::BslLockImpl_win32` class provides `lock` and `unlock` operations.
42/// Note that `bsls::BslLockImpl_win32` is not intended for direct client use;
43/// see @ref bslmt_mutex instead. Also note that `bsls::BslLockImpl_win32` is not
44/// recursive.
45///
46/// ## Usage {#bsls_bsllockimpl_win32-usage}
47///
48///
49/// This component is an implementation detail of `bsls` and is *not* intended
50/// for direct client use. It is subject to change without notice. As such, a
51/// usage example is not provided.
52/// @}
53/** @} */
54/** @} */
55
56/** @addtogroup bsl
57 * @{
58 */
59/** @addtogroup bsls
60 * @{
61 */
62/** @addtogroup bsls_bsllockimpl_win32
63 * @{
64 */
65
66#include <bsls_platform.h>
67
68#ifdef BSLS_PLATFORM_OS_WINDOWS
69
70// This section replaces #include <windows.h> by forward declaring necessary
71// structures and API. See {DRQS 147858260}.
72
73struct _RTL_CRITICAL_SECTION;
74
75typedef struct _RTL_CRITICAL_SECTION CRITICAL_SECTION, *LPCRITICAL_SECTION;
76typedef int BOOL;
77typedef unsigned long DWORD;
78
79extern "C" {
80 __declspec(dllimport) BOOL __stdcall InitializeCriticalSectionAndSpinCount(
81 LPCRITICAL_SECTION lpCriticalSection,
82 DWORD dwSpinCount);
83
84 __declspec(dllimport) void __stdcall DeleteCriticalSection(
85 LPCRITICAL_SECTION lpCriticalSection);
86
87 __declspec(dllimport) void __stdcall EnterCriticalSection(
88 LPCRITICAL_SECTION lpCriticalSection);
89
90 __declspec(dllimport) void __stdcall LeaveCriticalSection(
91 LPCRITICAL_SECTION lpCriticalSection);
92
93} // extern "C"
94
95
96namespace bsls {
97
98 // =======================
99 // class BslLockImpl_win32
100 // =======================
101
102class BslLockImpl_win32 {
103 // This 'class' implements a light-weight wrapper of an OS-level mutex to
104 // support intra-process synchronization. The mutex implemented by this
105 // class is *non*-recursive. Note that 'BslLockImpl_win32' is *not*
106 // intended for direct use by client code; it is meant for internal use
107 // only.
108
109 public:
110 enum {
111 // Size of the buffer allocated for the critical section, in
112 // pointer-sized elements. We have to make it public so we could
113 // access it in a .cpp file to verify the size.
114
115#ifdef BSLS_PLATFORM_CPU_64_BIT
116 // 5*8 = 40 bytes
117 k_CRITICAL_SECTION_BUFFER_SIZE = 5
118#else
119 // 6*4 = 24 bytes
120 k_CRITICAL_SECTION_BUFFER_SIZE = 6
121#endif
122 };
123
124 private:
125 enum {
126 // A Windows critical section has a configurable spin count. A lock
127 // operation spins this many iterations (on, presumably, some atomic
128 // integer) before sleeping on the underlying primitive.
129
130 k_SPIN_COUNT = 30
131 };
132
133 // DATA
134 void *d_lock[k_CRITICAL_SECTION_BUFFER_SIZE];
135
136 private:
137 // NOT IMPLEMENTED
138 BslLockImpl_win32(const BslLockImpl_win32&); // = delete
139 BslLockImpl_win32& operator=(const BslLockImpl_win32&); // = delete
140
141 public:
142 // CREATORS
143 BslLockImpl_win32();
144 // Create a lock object initialized to the unlocked state.
145
146 ~BslLockImpl_win32();
147 // Destroy this lock object. The behavior is undefined unless this
148 // object is in the unlocked state.
149
150 // MANIPULATORS
151 void lock();
152 // Acquire the lock on this object. If the lock on this object is
153 // currently held by another thread, then suspend execution of the
154 // calling thread until the lock can be acquired. The behavior is
155 // undefined unless the calling thread does not already hold the lock
156 // on this object. Note that deadlock may result if this method is
157 // invoked while the calling thread holds the lock on the object.
158
159 void unlock();
160 // Release the lock on this object that was previously acquired through
161 // a call to 'lock', enabling another thread to acquire the lock. The
162 // behavior is undefined unless the calling thread holds the lock on
163 // this object.
164};
165
166// ============================================================================
167// INLINE FUNCTION DEFINITIONS
168// ============================================================================
169
170 // -----------------------
171 // class BslLockImpl_win32
172 // -----------------------
173
174// CREATORS
175inline
176BslLockImpl_win32::BslLockImpl_win32()
177{
178 InitializeCriticalSectionAndSpinCount(
179 reinterpret_cast<_RTL_CRITICAL_SECTION *>(d_lock), k_SPIN_COUNT);
180}
181
182inline
183BslLockImpl_win32::~BslLockImpl_win32()
184{
185 DeleteCriticalSection(
186 reinterpret_cast<_RTL_CRITICAL_SECTION*>(d_lock));
187}
188
189// MANIPULATORS
190inline
191void BslLockImpl_win32::lock()
192{
193 EnterCriticalSection(
194 reinterpret_cast<_RTL_CRITICAL_SECTION*>(d_lock));
195}
196
197inline
198void BslLockImpl_win32::unlock()
199{
200 LeaveCriticalSection(
201 reinterpret_cast<_RTL_CRITICAL_SECTION*>(d_lock));
202}
203
204
205} // close package namespace
206
207
208#endif // BSLS_PLATFORM_OS_WINDOWS
209
210#endif
211
212// ----------------------------------------------------------------------------
213// Copyright 2021 Bloomberg Finance L.P.
214//
215// Licensed under the Apache License, Version 2.0 (the "License");
216// you may not use this file except in compliance with the License.
217// You may obtain a copy of the License at
218//
219// http://www.apache.org/licenses/LICENSE-2.0
220//
221// Unless required by applicable law or agreed to in writing, software
222// distributed under the License is distributed on an "AS IS" BASIS,
223// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
224// See the License for the specific language governing permissions and
225// limitations under the License.
226// ----------------------------- END-OF-FILE ----------------------------------
227
228/** @} */
229/** @} */
230/** @} */
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlt_iso8601util.h:707