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
|
/**************************************************************************
A/V Stream Camera Sample
Copyright (c) 2014, Microsoft Corporation.
File:
timer.h
Abstract:
Contains the definition of a KTimer and KPassiveTimer.
KTimer wraps a kernel timer object. KPassiveTimer uses a passive
callback through a KWorkItem.
History:
created 5/30/2014
**************************************************************************/
#pragma once
typedef void (*PKTimerCallback)( PVOID Context ) ;
class KTimer : public KWaitable, public Cancelable
{
public:
KTimer( TIMER_TYPE type=NotificationTimer );
virtual
~KTimer();
public:
//
// Override function that is invoked when a timer expires
//
virtual
void
Func() ;
//
// Cancels the timer
//
virtual
BOOLEAN
Cancel();
//
// Returns TRUE is if the timer is signalled
//
BOOLEAN
State()
{
return KeReadStateTimer( &m_Timer ) ;
}
//
// Resets (Cancels) the timer and DPC.
//
// Reset() returns TRUE if the KTIMER or the KDPC are dequeued.
// If Reset() returns FALSE the DPC may still be executing on another
// processor.
//
BOOLEAN
Reset()
{
// Try cancelling the timer.
if( !KeCancelTimer( &m_Timer ) )
{
// If the timer couldn't be cancelled, try removing the DPC.
return KeRemoveQueueDpc( &m_Dpc ) ;
}
return TRUE;
}
//
// Sets the timer
//
BOOLEAN
Set(
_In_ LARGE_INTEGER DueTime,
_In_opt_ PKTimerCallback Callback=NULL,
_In_opt_ PVOID Context=NULL,
_In_ LONG Period=0
)
{
m_Callback = Callback ;
m_Context = Context ;
m_Period = Period ;
return KeSetTimerEx( &m_Timer, DueTime, Period, &m_Dpc ) ;
}
//
// Sets the timer
//
BOOLEAN
Set(
_In_ LONGLONG DueTime,
_In_opt_ PKTimerCallback Callback=NULL,
_In_opt_ PVOID Context=NULL,
_In_ LONG Period=0
)
{
m_Callback = Callback ;
m_Context = Context ;
m_Period = Period ;
LARGE_INTEGER Due;
Due.QuadPart = DueTime;
return KeSetTimerEx( &m_Timer, Due, Period, &m_Dpc ) ;
}
//
// Support for waitable class
//
virtual
PVOID
GetDispatchObject();
private:
//
// Dpc Thunking function for KeSetTimerEx
//
_Function_class_(KDEFERRED_ROUTINE)
static
VOID
DpcThunk(
_In_ PKDPC Dpc,
_Inout_opt_ PVOID DeferredContext,
_Inout_opt_ PVOID Arg1,
_Inout_opt_ PVOID Arg2
) ;
TIMER_TYPE GetType() const
{
return m_type;
}
private:
KTIMER m_Timer ;
TIMER_TYPE m_type;
KDPC m_Dpc ;
LONG m_Period ;
protected:
PKTimerCallback m_Callback ;
PVOID m_Context ;
};
class KPassiveTimer : public KTimer
{
public:
KPassiveTimer(
_In_ PDEVICE_OBJECT DeviceObj,
_In_ TIMER_TYPE type=NotificationTimer
)
: KTimer( type )
, m_WorkItem( DeviceObj, WorkThunk, this )
{}
virtual
~KPassiveTimer();
//
// Override function that is invoked when a timer expires
//
virtual
void
Func() ;
//
// Cancels the timer
//
virtual
BOOLEAN
Cancel();
//
// Make sure it's in a valid state.
//
virtual
BOOLEAN
IsValid()
{
return m_WorkItem.IsValid();
}
private:
KWorkItem m_WorkItem;
//
// Dpc Thunking function for KeSetTimerEx
//
static
IO_WORKITEM_ROUTINE
WorkThunk;
};
|