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
|
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
#include "stdafx.h"
#include "Metering.h"
#include <assert.h>
#include "RpcInterface_h.h"
using namespace RpcServer;
//
// Thread pool callback method for metering worker
//
void CALLBACK MeteringWorkerTpCallback(
_In_ PTP_CALLBACK_INSTANCE /*iTimerInstance*/,
_In_ PVOID pContext,
_In_ PTP_TIMER /*pTimer*/)
{
Metering *metering = static_cast<Metering *>(pContext);
if (metering != nullptr)
{
// Run the metering worker for this instance of metering
metering->MeteringWorker();
}
}
//
// ctor for Metering
//
Metering::Metering(
_In_ __int64 period)
{
tpTimer = CreateThreadpoolTimer(::MeteringWorkerTpCallback, this, nullptr);
// TODO: Handle if CreateThreadpoolTimer fails i.e tpTimer == nullptr
samplePeriod = period;
event = CreateEvent(
nullptr, // default security attributes
FALSE, // auto-reset event object
FALSE, // initial state is nonsignaled
nullptr); // unnamed object
}
//
// dtor for Metering
//
Metering::~Metering()
{
if (tpTimer != nullptr)
{
// How to close threadpool timer when there are outstanding callbacks:
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms682040(v=vs.85).aspx
SetThreadpoolTimer(tpTimer, nullptr, 0, 0);
WaitForThreadpoolTimerCallbacks(tpTimer, true);
CloseThreadpoolTimer(tpTimer);
tpTimer = nullptr;
}
CloseHandle(event);
}
//
// Set the lowest sample period
//
void Metering::SetSamplePeriod(_In_ __int64 period)
{
if (period < 1)
{
// Don't allow 0 (too fast) or negative numbers.
period = 1;
}
if (period > 1000)
{
// Don't let the period be too long, or we will be
// slow to shut down.
period = 1000;
}
samplePeriod = period;
}
//
// Get last known metering data
//
__int64 Metering::GetMeteringData() const
{
return _data;
}
//
// Metering worker that updates metering data
//
void Metering::MeteringWorker()
{
// Get the value from the imaginary driver and update the data
_data = GetTickCount();
SetEvent(event);
}
//
// Set the thread poot timer and wait for metering data.
//
void Metering::WaitForMeteringData() const
{
ULARGE_INTEGER ulDueTime;
FILETIME FileDueTime;
ulDueTime.QuadPart = static_cast<ULONGLONG>(-(1 * 10 * 1000 * samplePeriod));
FileDueTime.dwHighDateTime = ulDueTime.HighPart;
FileDueTime.dwLowDateTime = ulDueTime.LowPart;
SetThreadpoolTimer(tpTimer,
&FileDueTime,
0,
0);
WaitForSingleObject(event, INFINITE);
}
void Metering::StartMetering(
_In_ __int64 samplePeriod,
_In_ __int64 context)
{
stopMeteringRequested = false;
SetSamplePeriod(samplePeriod);
while (true)
{
WaitForMeteringData();
if (stopMeteringRequested || ShutdownRequested)
{
break;
}
MeteringDataEvent(GetMeteringData(), context);
}
}
void Metering::StopMetering()
{
stopMeteringRequested = true;
}
|