BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslmt_conditionimpl_win32.h
Go to the documentation of this file.
1/// @file bslmt_conditionimpl_win32.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmt_conditionimpl_win32.h -*-C++-*-
8#ifndef INCLUDED_BSLMT_CONDITIONIMPL_WIN32
9#define INCLUDED_BSLMT_CONDITIONIMPL_WIN32
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmt_conditionimpl_win32 bslmt_conditionimpl_win32
15/// @brief Provide a win32 implementation of `bslmt::Condition`.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmt
19/// @{
20/// @addtogroup bslmt_conditionimpl_win32
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmt_conditionimpl_win32-purpose"> Purpose</a>
25/// * <a href="#bslmt_conditionimpl_win32-classes"> Classes </a>
26/// * <a href="#bslmt_conditionimpl_win32-description"> Description </a>
27/// * <a href="#bslmt_conditionimpl_win32-supported-clock-types"> Supported Clock-Types </a>
28/// * <a href="#bslmt_conditionimpl_win32-usage"> Usage </a>
29///
30/// # Purpose {#bslmt_conditionimpl_win32-purpose}
31/// Provide a win32 implementation of `bslmt::Condition`.
32///
33/// # Classes {#bslmt_conditionimpl_win32-classes}
34///
35/// - bslmt::ConditionImpl<Win32Threads>: win32 specialization
36///
37/// @see bslmt_condition
38///
39/// # Description {#bslmt_conditionimpl_win32-description}
40/// This component provides an implementation of `bslmt::Condition`
41/// for Windows (win32), `bslmt::ConditionImpl<Win32Threads>`, via the template
42/// specialization:
43/// @code
44/// bslmt::ConditionImpl<Platform::Win32Threads>
45/// @endcode
46/// This template class should not be used (directly) by client code. Clients
47/// should instead use `bslmt::Condition`.
48///
49/// ## Supported Clock-Types {#bslmt_conditionimpl_win32-supported-clock-types}
50///
51///
52/// `bsls::SystemClockType` supplies the enumeration indicating the system clock
53/// on which timeouts supplied to other methods should be based. If the clock
54/// type indicated at construction is `bsls::SystemClockType::e_REALTIME`, the
55/// `absTime` argument passed to the `timedWait` method should be expressed as
56/// an *absolute* offset since 00:00:00 UTC, January 1, 1970 (which matches the
57/// epoch used in `bsls::SystemTime::now(bsls::SystemClockType::e_REALTIME)`.
58/// If the clock type indicated at construction is
59/// `bsls::SystemClockType::e_MONOTONIC`, the `absTime` argument passed to the
60/// `timedWait` method should be expressed as an *absolute* offset since the
61/// epoch of this clock (which matches the epoch used in
62/// `bsls::SystemTime::now(bsls::SystemClockType::e_MONOTONIC)`.
63///
64/// ## Usage {#bslmt_conditionimpl_win32-usage}
65///
66///
67/// This component is an implementation detail of `bslmt` and is *not* intended
68/// for direct client use. It is subject to change without notice. As such, a
69/// usage example is not provided.
70/// @}
71/** @} */
72/** @} */
73
74/** @addtogroup bsl
75 * @{
76 */
77/** @addtogroup bslmt
78 * @{
79 */
80/** @addtogroup bslmt_conditionimpl_win32
81 * @{
82 */
83
84#include <bslscm_version.h>
85
86#include <bslmt_platform.h>
87
88#ifdef BSLMT_PLATFORM_WIN32_THREADS
89
90// Platform-specific implementation starts here.
91
93
95
96#ifndef BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
97#include <bslmt_sluice.h>
98#endif // BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
99
100// Rather than setting 'WINVER' or 'NTDDI_VERSION', just forward declare the
101// Windows 2000 functions that are used.
102
103struct _RTL_CONDITION_VARIABLE;
104struct _RTL_CRITICAL_SECTION;
105
106typedef struct _RTL_CONDITION_VARIABLE CONDITION_VARIABLE,
107 *PCONDITION_VARIABLE;
108typedef struct _RTL_CRITICAL_SECTION CRITICAL_SECTION, *LPCRITICAL_SECTION;
109typedef int BOOL;
110typedef unsigned long DWORD;
111
112extern "C" {
113 __declspec(dllimport) BOOL __stdcall SleepConditionVariableCS(
114 PCONDITION_VARIABLE ConditionVariable,
115 LPCRITICAL_SECTION CriticalSection,
116 DWORD dwMilliseconds);
117
118
119 __declspec(dllimport) void __stdcall InitializeConditionVariable(
120 PCONDITION_VARIABLE ConditionVariable);
121
122 __declspec(dllimport) void __stdcall WakeConditionVariable(
123 PCONDITION_VARIABLE ConditionVariable);
124
125 __declspec(dllimport) void __stdcall WakeAllConditionVariable(
126 PCONDITION_VARIABLE ConditionVariable);
127
128 __declspec(dllimport) DWORD __stdcall GetLastError();
129
130} // extern "C"
131
132
133namespace bslmt {
134
135template <class THREAD_POLICY>
136class ConditionImpl;
137
138class Mutex;
139
140 // ===========================================
141 // class ConditionImpl<Platform::Win32Threads>
142 // ===========================================
143
144template <>
145class ConditionImpl<Platform::Win32Threads> {
146 // This class provides a full specialization of 'ConditionImpl' for win32.
147 // The implementation provided here defines an efficient POSIX like
148 // condition variable.
149
150 private:
151 // DATA
152 void *d_cond; // Condition variable is the size
153 // of a pointer.
154
155 bsls::SystemClockType::Enum d_clockType; // clock type
156
157 // NOT IMPLEMENTED
158 ConditionImpl(const ConditionImpl&);
159 ConditionImpl& operator=(const ConditionImpl&);
160
161 public:
162 // TYPES
163 enum { e_TIMED_OUT = -1 };
164 // The value 'timedWait' returns when a timeout occurs.
165
166 // CREATORS
167 explicit
168 ConditionImpl(bsls::SystemClockType::Enum clockType
170 // Create a condition variable object. Optionally specify a
171 // 'clockType' indicating the type of the system clock against which
172 // the 'bsls::TimeInterval' 'absTime' timeouts passed to the
173 // 'timedWait' method are to be interpreted (see {Supported
174 // Clock-Types} in the component-level documentation). If 'clockType'
175 // is not specified then the realtime system clock is used.
176
177 ~ConditionImpl();
178 // Destroy condition variable this object.
179
180 // MANIPULATORS
181 void broadcast();
182 // Signal this condition object, by waking up *all* threads that are
183 // currently waiting on this condition.
184
185 void signal();
186 // Signal this condition object, by waking up a single thread that is
187 // currently waiting on this condition.
188
189 int timedWait(Mutex *mutex, const bsls::TimeInterval& absTime);
190 // Atomically unlock the specified 'mutex' and suspend execution of the
191 // current thread until this condition object is "signaled" (i.e., one
192 // of the 'signal' or 'broadcast' methods is invoked on this object) or
193 // until the specified 'absTime' timeout expires, then re-acquire a
194 // lock on the 'mutex'. 'absTime' is an *absolute* time represented as
195 // an interval from some epoch, which is determined by the clock
196 // indicated at construction (see {Supported Clock-Types} in the
197 // component-level documentation), and is the earliest time at which
198 // the timeout may occur. The 'mutex' remains locked by the calling
199 // thread upon returning from this function. Return 0 on success,
200 // 'e_TIMED_OUT' on timeout, and a non-zero value different from
201 // 'e_TIMED_OUT' if an error occurs. The behavior is undefined unless
202 // 'mutex' is locked by the calling thread prior to calling this
203 // method. Note that spurious wakeups are rare but possible, i.e.,
204 // this method may succeed return 0) and return control to the thread
205 // without the condition object being signaled. Also note that the
206 // actual time of the timeout depends on many factors including system
207 // scheduling and system timer resolution, and may be significantly
208 // later than the time requested.
209
210 int wait(Mutex *mutex);
211 // Atomically unlock the specified 'mutex' and suspend execution of the
212 // current thread until this condition object is "signaled" (i.e.,
213 // either 'signal' or 'broadcast' is invoked on this object in another
214 // thread), then re-acquire a lock on the 'mutex'. Return 0 on
215 // success, and a non-zero value otherwise. Spurious wakeups are rare
216 // but possible; i.e., this method may succeed (return 0), and return
217 // control to the thread without the condition object being signaled.
218 // The behavior is undefined unless 'mutex' is locked by the calling
219 // thread prior to calling this method. Note that 'mutex' remains
220 // locked by the calling thread upon return from this function.
221
222 // ACCESSORS
223 bsls::SystemClockType::Enum clockType() const;
224 // Return the clock type used for timeouts.
225};
226
227// ============================================================================
228// INLINE DEFINITIONS
229// ============================================================================
230
231 // -------------------------------------------
232 // class ConditionImpl<Platform::Win32Threads>
233 // -------------------------------------------
234
235// CREATORS
236inline
237ConditionImpl<bslmt::Platform::Win32Threads>::ConditionImpl(
239: d_clockType(clockType)
240{
241 InitializeConditionVariable(
242 reinterpret_cast<_RTL_CONDITION_VARIABLE *>(&d_cond));
243}
244
245inline
246ConditionImpl<bslmt::Platform::Win32Threads>::~ConditionImpl()
247{
248}
249
250// MANIPULATORS
251inline
252void ConditionImpl<bslmt::Platform::Win32Threads>::broadcast()
253{
254 WakeAllConditionVariable(
255 reinterpret_cast<_RTL_CONDITION_VARIABLE *>(&d_cond));
256}
257
258inline
259void ConditionImpl<bslmt::Platform::Win32Threads>::signal()
260{
261 WakeConditionVariable(
262 reinterpret_cast<_RTL_CONDITION_VARIABLE *>(&d_cond));
263}
264
265// ACCESSORS
266inline
268ConditionImpl<bslmt::Platform::Win32Threads>::clockType() const
269{
270 return d_clockType;
271}
272
273} // close package namespace
274
275
276#endif // BSLMT_PLATFORM_WIN32_THREADS
277
278#endif
279
280// ----------------------------------------------------------------------------
281// Copyright 2019 Bloomberg Finance L.P.
282//
283// Licensed under the Apache License, Version 2.0 (the "License");
284// you may not use this file except in compliance with the License.
285// You may obtain a copy of the License at
286//
287// http://www.apache.org/licenses/LICENSE-2.0
288//
289// Unless required by applicable law or agreed to in writing, software
290// distributed under the License is distributed on an "AS IS" BASIS,
291// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
292// See the License for the specific language governing permissions and
293// limitations under the License.
294// ----------------------------- END-OF-FILE ----------------------------------
295
296/** @} */
297/** @} */
298/** @} */
Definition bsls_timeinterval.h:301
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bslmt_barrier.h:344
Enum
Definition bsls_systemclocktype.h:117
@ e_REALTIME
Definition bsls_systemclocktype.h:120