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
|
// Copyright (C) Microsoft Corporation. All rights reserved.
#pragma once
#include <KMacros.h>
#if _KERNEL_MODE
typedef wistd::integral_constant<EVENT_TYPE, SynchronizationEvent> auto_reset_event_t;
#else
typedef wistd::integral_constant<bool, false> auto_reset_event_t;
#endif
#if _KERNEL_MODE
typedef wistd::integral_constant<EVENT_TYPE, NotificationEvent> manual_reset_event_t;
#else
typedef wistd::integral_constant<bool, true> manual_reset_event_t;
#endif
#define WIN_VERIFY NT_VERIFY
#define WIN_ASSERT NT_ASSERT
template<typename TEventType>
class KWaitEventBase
{
public:
KWaitEventBase() = default;
#ifdef _KERNEL_MODE
NONPAGED ~KWaitEventBase() = default;
#else
NONPAGED ~KWaitEventBase()
{
WIN_ASSERT(m_event == nullptr);
}
#endif
KWaitEventBase(KWaitEventBase &) = delete;
KWaitEventBase(KWaitEventBase &&) = delete;
KWaitEventBase & operator=(KWaitEventBase &) = delete;
KWaitEventBase & operator=(KWaitEventBase &&) = delete;
_IRQL_requires_max_(DISPATCH_LEVEL) void Set()
{
#if _KERNEL_MODE
KeSetEvent(&m_event, 0, false);
#else
WIN_VERIFY(SetEvent(m_event));
#endif
}
_IRQL_requires_max_(DISPATCH_LEVEL) void Clear()
{
#if _KERNEL_MODE
KeClearEvent(&m_event);
#else
WIN_VERIFY(ResetEvent(m_event));
#endif
}
PAGED void Wait()
{
#if _KERNEL_MODE
// Not used at runtime, but might be useful during debugging
volatile LARGE_INTEGER SystemTime;
KeQuerySystemTime(const_cast<LARGE_INTEGER*>(&SystemTime));
NTSTATUS NtStatus = KeWaitForSingleObject(
&m_event, Executive, KernelMode, FALSE, nullptr);
NT_VERIFY(NtStatus == STATUS_SUCCESS);
#else
ULONG r = WaitForSingleObject(m_event, INFINITE);
WIN_VERIFY(r == NO_ERROR);
#endif
}
PAGED bool Test()
{
#if _KERNEL_MODE
return !!KeReadStateEvent(&m_event);
#else
ULONG r = WaitForSingleObject(m_event, 0);
WIN_VERIFY(r == WAIT_TIMEOUT || r == WAIT_OBJECT_0);
return (r == WAIT_OBJECT_0);
#endif
}
NONPAGED bool TestNP()
{
#if _KERNEL_MODE
return !!KeReadStateEvent(&m_event);
#else
ULONG r = WaitForSingleObject(m_event, 0);
WIN_VERIFY(r == WAIT_TIMEOUT || r == WAIT_OBJECT_0);
return (r == WAIT_OBJECT_0);
#endif
}
protected:
PAGED void InitializeBase()
{
#if _KERNEL_MODE
KeInitializeEvent(&m_event, TEventType(), FALSE);
#else
m_event = CreateEventW(nullptr, TEventType(), false, nullptr);
WIN_VERIFY(m_event);
#endif
}
NONPAGED void CleanupBase()
{
#ifndef _KERNEL_MODE
CloseHandle(m_event);
m_event = nullptr;
#endif
}
private:
#if _KERNEL_MODE
KEVENT m_event;
#else
HANDLE m_event;
#endif
};
class KWaitEvent : public KWaitEventBase<manual_reset_event_t>
{
public:
PAGED KWaitEvent() noexcept
{
InitializeBase();
}
NONPAGED ~KWaitEvent()
{
CleanupBase();
}
};
class KWaitEventManualConstruct : public KWaitEventBase<manual_reset_event_t>
{
public:
PAGED void Initialize()
{
InitializeBase();
}
PAGED void Cleanup()
{
CleanupBase();
}
};
class KAutoEvent : public KWaitEventBase<auto_reset_event_t>
{
public:
PAGED KAutoEvent() noexcept
{
InitializeBase();
}
NONPAGED ~KAutoEvent()
{
CleanupBase();
}
};
|