BDE 4.39.x 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 private:
158 // NOT IMPLEMENTED
159 ConditionImpl(const ConditionImpl&);
160 ConditionImpl& operator=(const ConditionImpl&);
161
162 public:
163 // TYPES
164 enum { e_TIMED_OUT = -1 };
165 // The value 'timedWait' returns when a timeout occurs.
166
167 // CREATORS
168 explicit
169 ConditionImpl(bsls::SystemClockType::Enum clockType
171 // Create a condition variable object. Optionally specify a
172 // 'clockType' indicating the type of the system clock against which
173 // the 'bsls::TimeInterval' 'absTime' timeouts passed to the
174 // 'timedWait' method are to be interpreted (see {Supported
175 // Clock-Types} in the component-level documentation). If 'clockType'
176 // is not specified then the realtime system clock is used.
177
178 ~ConditionImpl();
179 // Destroy condition variable this object.
180
181 // MANIPULATORS
182 void broadcast();
183 // Signal this condition object, by waking up *all* threads that are
184 // currently waiting on this condition.
185
186 void signal();
187 // Signal this condition object, by waking up a single thread that is
188 // currently waiting on this condition.
189
190 int timedWait(Mutex *mutex, const bsls::TimeInterval& absTime);
191 // Atomically unlock the specified 'mutex' and suspend execution of the
192 // current thread until this condition object is "signaled" (i.e., one
193 // of the 'signal' or 'broadcast' methods is invoked on this object) or
194 // until the specified 'absTime' timeout expires, then re-acquire a
195 // lock on the 'mutex'. 'absTime' is an *absolute* time represented as
196 // an interval from some epoch, which is determined by the clock
197 // indicated at construction (see {Supported Clock-Types} in the
198 // component-level documentation), and is the earliest time at which
199 // the timeout may occur. The 'mutex' remains locked by the calling
200 // thread upon returning from this function. Return 0 on success,
201 // 'e_TIMED_OUT' on timeout, and a non-zero value different from
202 // 'e_TIMED_OUT' if an error occurs. The behavior is undefined unless
203 // 'mutex' is locked by the calling thread prior to calling this
204 // method. Note that spurious wakeups are rare but possible, i.e.,
205 // this method may succeed return 0) and return control to the thread
206 // without the condition object being signaled. Also note that the
207 // actual time of the timeout depends on many factors including system
208 // scheduling and system timer resolution, and may be significantly
209 // later than the time requested.
210
211 int wait(Mutex *mutex);
212 // Atomically unlock the specified 'mutex' and suspend execution of the
213 // current thread until this condition object is "signaled" (i.e.,
214 // either 'signal' or 'broadcast' is invoked on this object in another
215 // thread), then re-acquire a lock on the 'mutex'. Return 0 on
216 // success, and a non-zero value otherwise. Spurious wakeups are rare
217 // but possible; i.e., this method may succeed (return 0), and return
218 // control to the thread without the condition object being signaled.
219 // The behavior is undefined unless 'mutex' is locked by the calling
220 // thread prior to calling this method. Note that 'mutex' remains
221 // locked by the calling thread upon return from this function.
222
223 // ACCESSORS
224 bsls::SystemClockType::Enum clockType() const;
225 // Return the clock type used for timeouts.
226};
227
228// ============================================================================
229// INLINE DEFINITIONS
230// ============================================================================
231
232 // -------------------------------------------
233 // class ConditionImpl<Platform::Win32Threads>
234 // -------------------------------------------
235
236// CREATORS
237inline
238ConditionImpl<bslmt::Platform::Win32Threads>::ConditionImpl(
240: d_clockType(clockType)
241{
242 InitializeConditionVariable(
243 reinterpret_cast<_RTL_CONDITION_VARIABLE *>(&d_cond));
244}
245
246inline
247ConditionImpl<bslmt::Platform::Win32Threads>::~ConditionImpl()
248{
249}
250
251// MANIPULATORS
252inline
253void ConditionImpl<bslmt::Platform::Win32Threads>::broadcast()
254{
255 WakeAllConditionVariable(
256 reinterpret_cast<_RTL_CONDITION_VARIABLE *>(&d_cond));
257}
258
259inline
260void ConditionImpl<bslmt::Platform::Win32Threads>::signal()
261{
262 WakeConditionVariable(
263 reinterpret_cast<_RTL_CONDITION_VARIABLE *>(&d_cond));
264}
265
266// ACCESSORS
267inline
269ConditionImpl<bslmt::Platform::Win32Threads>::clockType() const
270{
271 return d_clockType;
272}
273
274} // close package namespace
275
276
277#endif // BSLMT_PLATFORM_WIN32_THREADS
278
279#endif
280
281// ----------------------------------------------------------------------------
282// Copyright 2019 Bloomberg Finance L.P.
283//
284// Licensed under the Apache License, Version 2.0 (the "License");
285// you may not use this file except in compliance with the License.
286// You may obtain a copy of the License at
287//
288// http://www.apache.org/licenses/LICENSE-2.0
289//
290// Unless required by applicable law or agreed to in writing, software
291// distributed under the License is distributed on an "AS IS" BASIS,
292// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
293// See the License for the specific language governing permissions and
294// limitations under the License.
295// ----------------------------- END-OF-FILE ----------------------------------
296
297/** @} */
298/** @} */
299/** @} */
Definition bsls_timeinterval.h:307
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bslmt_barrier.h:344
Enum
Definition bsls_systemclocktype.h:119
@ e_REALTIME
Definition bsls_systemclocktype.h:122