BDE 4.14.0 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 // NOT IMPLEMENTED
137 BslLockImpl_win32(const BslLockImpl_win32&); // = delete
138 BslLockImpl_win32& operator=(const BslLockImpl_win32&); // = delete
139
140 public:
141 // CREATORS
142 BslLockImpl_win32();
143 // Create a lock object initialized to the unlocked state.
144
145 ~BslLockImpl_win32();
146 // Destroy this lock object. The behavior is undefined unless this
147 // object is in the unlocked state.
148
149 // MANIPULATORS
150 void lock();
151 // Acquire the lock on this object. If the lock on this object is
152 // currently held by another thread, then suspend execution of the
153 // calling thread until the lock can be acquired. The behavior is
154 // undefined unless the calling thread does not already hold the lock
155 // on this object. Note that deadlock may result if this method is
156 // invoked while the calling thread holds the lock on the object.
157
158 void unlock();
159 // Release the lock on this object that was previously acquired through
160 // a call to 'lock', enabling another thread to acquire the lock. The
161 // behavior is undefined unless the calling thread holds the lock on
162 // this object.
163};
164
165// ============================================================================
166// INLINE FUNCTION DEFINITIONS
167// ============================================================================
168
169 // -----------------------
170 // class BslLockImpl_win32
171 // -----------------------
172
173// CREATORS
174inline
175BslLockImpl_win32::BslLockImpl_win32()
176{
177 InitializeCriticalSectionAndSpinCount(
178 reinterpret_cast<_RTL_CRITICAL_SECTION *>(d_lock), k_SPIN_COUNT);
179}
180
181inline
182BslLockImpl_win32::~BslLockImpl_win32()
183{
184 DeleteCriticalSection(
185 reinterpret_cast<_RTL_CRITICAL_SECTION*>(d_lock));
186}
187
188// MANIPULATORS
189inline
190void BslLockImpl_win32::lock()
191{
192 EnterCriticalSection(
193 reinterpret_cast<_RTL_CRITICAL_SECTION*>(d_lock));
194}
195
196inline
197void BslLockImpl_win32::unlock()
198{
199 LeaveCriticalSection(
200 reinterpret_cast<_RTL_CRITICAL_SECTION*>(d_lock));
201}
202
203
204} // close package namespace
205
206
207#endif // BSLS_PLATFORM_OS_WINDOWS
208
209#endif
210
211// ----------------------------------------------------------------------------
212// Copyright 2021 Bloomberg Finance L.P.
213//
214// Licensed under the Apache License, Version 2.0 (the "License");
215// you may not use this file except in compliance with the License.
216// You may obtain a copy of the License at
217//
218// http://www.apache.org/licenses/LICENSE-2.0
219//
220// Unless required by applicable law or agreed to in writing, software
221// distributed under the License is distributed on an "AS IS" BASIS,
222// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
223// See the License for the specific language governing permissions and
224// limitations under the License.
225// ----------------------------- END-OF-FILE ----------------------------------
226
227/** @} */
228/** @} */
229/** @} */
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlt_iso8601util.h:691