Repository navigation
Expand file tree
/
Copy pathevent.hpp
More file actions
438 lines (376 loc) · 12.6 KB
/
Copy pathevent.hpp
File metadata and controls
438 lines (376 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/* /////////////////////////////////////////////////////////////////////////
* File: winstl/synch/event.hpp
*
* Purpose: event class, based on Windows EVENT.
*
* Created: 3rd July 2003
* Updated: 9th November 2026
*
* Home: http://stlsoft.org/
*
* Copyright (c) 2019-2026, Matthew Wilson and Synesis Information Systems
* Copyright (c) 2003-2019, Matthew Wilson and Synesis Software
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - Neither the name(s) of Matthew Wilson and Synesis Information Systems
* nor the names of any contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ////////////////////////////////////////////////////////////////////// */
/** \file winstl/synch/event.hpp
*
* \brief [C++] Definition of the winstl::event class
* (\ref group__library__Synch "Synchronisation" Library).
*/
#ifndef WINSTL_INCL_WINSTL_SYNCH_HPP_EVENT
#define WINSTL_INCL_WINSTL_SYNCH_HPP_EVENT
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define WINSTL_VER_WINSTL_SYNCH_HPP_EVENT_MAJOR 4
# define WINSTL_VER_WINSTL_SYNCH_HPP_EVENT_MINOR 4
# define WINSTL_VER_WINSTL_SYNCH_HPP_EVENT_REVISION 8
# define WINSTL_VER_WINSTL_SYNCH_HPP_EVENT_EDIT 85
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* /////////////////////////////////////////////////////////////////////////
* includes
*/
#ifndef WINSTL_INCL_WINSTL_H_WINSTL
# include <winstl/winstl.h>
#endif /* !WINSTL_INCL_WINSTL_H_WINSTL */
#ifdef STLSOFT_TRACE_INCLUDE
# pragma message(__FILE__)
#endif /* STLSOFT_TRACE_INCLUDE */
#ifndef WINSTL_INCL_WINSTL_SYNCH_HPP_COMMON
# include <winstl/synch/common.hpp>
#endif /* !WINSTL_INCL_WINSTL_SYNCH_HPP_COMMON */
#ifndef WINSTL_INCL_WINSTL_API_external_h_ErrorHandling
# include <winstl/api/external/ErrorHandling.h>
#endif /* !WINSTL_INCL_WINSTL_API_external_h_ErrorHandling */
#ifndef WINSTL_INCL_WINSTL_API_external_h_HandleAndObject
# include <winstl/api/external/HandleAndObject.h>
#endif /* !WINSTL_INCL_WINSTL_API_external_h_HandleAndObject */
/* /////////////////////////////////////////////////////////////////////////
* namespace
*/
#ifndef WINSTL_NO_NAMESPACE
# if defined(STLSOFT_NO_NAMESPACE) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
/* There is no stlsoft namespace, so must define ::winstl */
namespace winstl
{
# else
/* Define stlsoft::winstl_project */
namespace stlsoft
{
namespace winstl_project
{
# endif /* STLSOFT_NO_NAMESPACE */
#endif /* !WINSTL_NO_NAMESPACE */
/* /////////////////////////////////////////////////////////////////////////
* classes
*/
/** Class which wraps the Win32 EVENT synchronisation object
*
* \ingroup group__library__Synch
*/
class event
: public STLSOFT_NS_QUAL(synchronisable_object_tag)
{
public:
/// This type
typedef event class_type;
typedef HANDLE synch_handle_type;
typedef HANDLE resource_type;
/// \name Construction
/// @{
public:
/// Creates the event
event(ws_bool_t bManualReset, ws_bool_t bInitialState)
: m_ev(create_event_(NULL, bManualReset, bInitialState, static_cast<ws_char_a_t const*>(0)))
, m_bOwnHandle(true)
{}
/// Creates the event with the given name
event(ws_char_a_t const* name, ws_bool_t bManualReset, ws_bool_t bInitialState)
: m_ev(create_event_(NULL, bManualReset, bInitialState, name))
, m_bOwnHandle(true)
{}
/// Creates the event with the given name
event(ws_char_w_t const* name, ws_bool_t bManualReset, ws_bool_t bInitialState)
: m_ev(create_event_(NULL, bManualReset, bInitialState, name))
, m_bOwnHandle(true)
{}
/// Creates the event with the given name and security attributes
event(ws_char_a_t const* name, ws_bool_t bManualReset, ws_bool_t bInitialState, LPSECURITY_ATTRIBUTES psa)
: m_ev(create_event_(psa, bManualReset, bInitialState, name))
, m_bOwnHandle(true)
{}
/// Creates the event with the given name and security attributes
event(ws_char_w_t const* name, ws_bool_t bManualReset, ws_bool_t bInitialState, LPSECURITY_ATTRIBUTES psa)
: m_ev(create_event_(psa, bManualReset, bInitialState, name))
, m_bOwnHandle(true)
{}
/// Destroys the event instance
~event() STLSOFT_NOEXCEPT
{
if (NULL != m_ev &&
m_bOwnHandle)
{
WINSTL_API_EXTERNAL_HandleAndObject_CloseHandle(m_ev);
}
}
private:
event(class_type const&) STLSOFT_COPY_CONSTRUCTION_PROSCRIBED;
void operator =(class_type const&) STLSOFT_COPY_ASSIGNMENT_PROSCRIBED;
/// @}
/// \name Operations
/// @{
public:
/// Sets the state of the event to signalled
void set()
{
WINSTL_ASSERT(NULL != m_ev);
if (!WINSTL_API_EXTERNAL_Synchronisation_SetEvent(m_ev))
{
DWORD const e = WINSTL_API_EXTERNAL_ErrorHandling_GetLastError();
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
STLSOFT_THROW_X(synchronisation_object_state_change_failed_exception(e, "event set operation failed", Synchronisation_EventSetFailed));
#else
WINSTL_SYNCH_OPERATION_FAILED_IN_NOX(Synchronisation_EventSetFailed, e);
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
}
}
/// Sets the state of the event to signalled
void reset()
{
WINSTL_ASSERT(NULL != m_ev);
if (!WINSTL_API_EXTERNAL_Synchronisation_ResetEvent(m_ev))
{
DWORD const e = WINSTL_API_EXTERNAL_ErrorHandling_GetLastError();
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
STLSOFT_THROW_X(synchronisation_object_state_change_failed_exception(e, "event reset operation failed", Synchronisation_EventResetFailed));
#else
WINSTL_SYNCH_OPERATION_FAILED_IN_NOX(Synchronisation_EventResetFailed, e);
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
}
}
/// @}
/// \name Accessors
/// @{
public:
/// The underlying kernel object handle
HANDLE handle()
{
return m_ev;
}
/// The underlying kernel object handle
HANDLE get()
{
return m_ev;
}
/// @}
// Implementation
private:
static HANDLE create_event_(LPSECURITY_ATTRIBUTES psa, ws_bool_t bManualReset, ws_bool_t bInitialState, ws_char_a_t const* name)
{
HANDLE h = WINSTL_API_EXTERNAL_Synchronisation_CreateEventA(psa, bManualReset, bInitialState, name);
if (NULL == h)
{
DWORD const e = WINSTL_API_EXTERNAL_ErrorHandling_GetLastError();
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
STLSOFT_THROW_X(synchronisation_creation_exception(e, "failed to create kernel event object"));
#else
WINSTL_SYNCH_OPERATION_FAILED_IN_NOX(Synchronisation_SynchronisationObjectCreationFailed, e);
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
}
return h;
}
static HANDLE create_event_(LPSECURITY_ATTRIBUTES psa, ws_bool_t bManualReset, ws_bool_t bInitialState, ws_char_w_t const* name)
{
HANDLE h = WINSTL_API_EXTERNAL_Synchronisation_CreateEventW(psa, bManualReset, bInitialState, name);
if (NULL == h)
{
DWORD const e = WINSTL_API_EXTERNAL_ErrorHandling_GetLastError();
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
STLSOFT_THROW_X(synchronisation_creation_exception(e, "failed to create kernel event object"));
#else
WINSTL_SYNCH_OPERATION_FAILED_IN_NOX(Synchronisation_SynchronisationObjectCreationFailed, e);
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
}
return h;
}
// Members
private:
HANDLE m_ev;
const ws_bool_t m_bOwnHandle; // Does the instance own the handle?
};
/* /////////////////////////////////////////////////////////////////////////
* attribute shims
*/
/** Overload of the form of the winstl::get_synch_handle() shim for
* the winstl::event type.
*
* \param ev The winstl::event instance
*
* \retval The synchronisation handle of \c ev
*/
inline
HANDLE
get_synch_handle(
event &ev
)
{
return ev.get();
}
/** Overload of the form of the winstl::get_kernel_handle() shim for
* the winstl::event type.
*
* \ingroup group__library__Shims_Attribute_Windows_KernelHandle
*
* \param ev The winstl::event instance
*
* \retval The synchronisation handle of \c ev
*/
inline
HANDLE
get_kernel_handle(
event &ev
)
{
return ev.get();
}
/* /////////////////////////////////////////////////////////////////////////
* namespace
*/
#ifndef WINSTL_NO_NAMESPACE
# if defined(STLSOFT_NO_NAMESPACE) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
} // namespace winstl
# else
} // namespace winstl_project
# endif /* STLSOFT_NO_NAMESPACE */
#endif /* !WINSTL_NO_NAMESPACE */
/* /////////////////////////////////////////////////////////////////////////
* control shims
*/
/** This \ref group__concept__Shim "control shim" acquires the given event
*
* \ingroup group__concept__Shim__synchronisation_control
*
* \param ev The event to acquire
*/
inline
void
lock_instance(
WINSTL_NS_QUAL(event) &ev
)
{
HANDLE h = ev.get();
DWORD const dwRes = WINSTL_API_EXTERNAL_Synchronization_WaitForSingleObject(h, INFINITE);
if (WAIT_OBJECT_0 != dwRes)
{
DWORD const e = WINSTL_API_EXTERNAL_ErrorHandling_GetLastError();
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
STLSOFT_THROW_X(WINSTL_NS_QUAL(wait_failed_logic_exception)(e, "event wait failed"));
#else
WINSTL_SYNCH_OPERATION_FAILED_IN_NOX(WINSTL_NS_QUAL(Synchronisation_WaitFailed), e);
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
}
}
/** This \ref group__concept__Shim "control shim" releases the given event
*
* \ingroup group__concept__Shim__synchronisation_control
*
* \param ev The event to release
*/
inline
void
unlock_instance(
WINSTL_NS_QUAL(event) &ev
)
{
ev.set();
}
/* /////////////////////////////////////////////////////////////////////////
* namespace
*/
#ifndef WINSTL_NO_NAMESPACE
# if defined(STLSOFT_NO_NAMESPACE) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
namespace winstl {
# else
namespace winstl_project {
# if defined(STLSOFT_COMPILER_IS_BORLAND)
using ::stlsoft::lock_instance;
using ::stlsoft::unlock_instance;
# endif /* compiler */
# endif /* STLSOFT_NO_NAMESPACE */
#endif /* !WINSTL_NO_NAMESPACE */
/* /////////////////////////////////////////////////////////////////////////
* lock_traits
*/
// class lock_traits
/** Traits for the event class
*
* \ingroup group__library__Synch
*/
struct event_lock_traits
{
public:
/// The lockable type
typedef event lock_type;
/// This type
typedef event_lock_traits class_type;
// Operations
public:
/// Lock the given event instance
static void lock(event &c)
{
lock_instance(c);
}
/// Unlock the given event instance
static void unlock(event &c)
{
unlock_instance(c);
}
};
/* /////////////////////////////////////////////////////////////////////////
* namespace
*/
#ifndef WINSTL_NO_NAMESPACE
# if defined(STLSOFT_NO_NAMESPACE) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
} // namespace winstl
# else
} // namespace winstl_project
} // namespace stlsoft
# endif /* STLSOFT_NO_NAMESPACE */
#endif /* !WINSTL_NO_NAMESPACE */
/* /////////////////////////////////////////////////////////////////////////
* inclusion control
*/
#ifdef STLSOFT_PPF_pragma_message_SUPPORT
# pragma once
#endif /* STLSOFT_PPF_pragma_message_SUPPORT */
#endif /* !WINSTL_INCL_WINSTL_SYNCH_HPP_EVENT */
/* ///////////////////////////// end of file //////////////////////////// */