summaryrefslogtreecommitdiff
path: root/avstream/avscamera/sys/Timer.cpp
blob: 5345a361a9ba6d63b96c798db14fd5b77dd9f0f7 (plain)
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
/**************************************************************************

    A/V Stream Camera Sample

    Copyright (c) 2014, Microsoft Corporation.

    File:

        timer.cpp

    Abstract:

        Contains the implementation of a KTimer and KPassiveTimer.

        KTimer wraps a kernel timer object.  KPassiveTimer uses a passive
        callback through a KWorkItem.

    History:

        created 5/30/2014

**************************************************************************/

#include "Common.h"

KTimer::
KTimer( TIMER_TYPE  type )
    : m_Period(0)
    , m_Context(NULL)
    , m_Callback(NULL)
{
    KeInitializeTimerEx( &m_Timer,type ) ;
    KeInitializeDpc( &m_Dpc, KTimer::DpcThunk, this ) ;
}

KTimer::
~KTimer()
{
    Cancel();
}

//
//  Cancels the timer and ensures no DPC is executing.
//
BOOLEAN
KTimer::
Cancel()
{
    //  Clear the timer and dequeue the DPC.
    if( !Reset() )
    {
        //  If the item wasn't found, flush all the DPC queues.
        KeFlushQueuedDpcs() ;

        //  Return false because we lost the race.
        return FALSE;
    }
    return TRUE;
}

//
//  Support for waitable class
//
PVOID
KTimer::
GetDispatchObject()
{
    return &m_Timer;
}

//
//  Dpc Thunking function for KeSetTimerEx
//
_Function_class_(KDEFERRED_ROUTINE)
VOID
KTimer::
DpcThunk(
    _In_        PKDPC   /*Dpc*/,
    _Inout_opt_ PVOID   DeferredContext,
    _Inout_opt_ PVOID   /*Arg1*/,
    _Inout_opt_ PVOID   /*Arg2*/
)
{
    KTimer *pKTimer = (KTimer *) DeferredContext ;
    pKTimer->Func() ;
}

//
//  Function invoked when a timer expires
//
//  IRQL <= DISPATCH_LEVEL
//
void
KTimer::
Func()
{
    if( m_Callback )
    {
        m_Callback( m_Context ) ;
    }
}

//
//  Dtor
//
//  Calls Cancel() here to clean up.
//
//  Note: KTimer will also call Cancel(), but it will invoke KTimer::Cancel().
//        Since KPassiveTimer::Cancel() first calls KTimer::Cancel() this second
//        call is redudant; but preventing it would probably require an explicit
//        call to Cancel() before destruction.  This way you can just delete
//        timers when they are no longer needed, but it make take a bit longer.
//
KPassiveTimer::
~KPassiveTimer()
{
    Cancel();
}

//
//  Function invoked when a timer expires
//
//  IRQL == DISPATCH_LEVEL
//
void
KPassiveTimer::
Func()
{
    if( m_Callback )
    {
        //  Start the work item.  If it fails, there's not much we can do since
        //  it can only fail if the owner of this timer is trying to cancel it.
        m_WorkItem.Start();
    }
}

VOID
KPassiveTimer::
WorkThunk(
    _In_ PDEVICE_OBJECT DeviceObject,
    _In_opt_ PVOID Context
)
{
    UNREFERENCED_PARAMETER(DeviceObject);

    if( Context )
    {
        ((KPassiveTimer *) Context)->KTimer::Func();
    }
}

//
//  Cancels the timer
//
BOOLEAN
KPassiveTimer::
Cancel()
{
    //  Kill the timer.
    if( !KTimer::Cancel() )
    {
        //  Wait until the work item is idle.
        m_WorkItem.Wait();

        //  Reset the WorkItem so we can use it again.
        m_WorkItem.Reset();

        return FALSE;
    }
    return TRUE;
}