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
|
#include <winpr/crt.h>
#include <winpr/sysinfo.h>
#include <winpr/file.h>
#include <winpr/synch.h>
#define FIRE_COUNT 5
#define TIMER_COUNT 5
struct apc_data
{
DWORD TimerId;
DWORD FireCount;
DWORD DueTime;
DWORD Period;
UINT32 StartTime;
DWORD MaxFireCount;
HANDLE CompletionEvent;
};
typedef struct apc_data APC_DATA;
static VOID CALLBACK TimerRoutine(PVOID lpParam, BOOLEAN TimerOrWaitFired)
{
UINT32 TimerTime = 0;
APC_DATA* apcData = NULL;
UINT32 expectedTime = 0;
UINT32 CurrentTime = GetTickCount();
WINPR_UNUSED(TimerOrWaitFired);
if (!lpParam)
return;
apcData = (APC_DATA*)lpParam;
TimerTime = CurrentTime - apcData->StartTime;
expectedTime = apcData->DueTime + (apcData->Period * apcData->FireCount);
apcData->FireCount++;
printf("TimerRoutine: TimerId: %" PRIu32 " FireCount: %" PRIu32 " ActualTime: %" PRIu32
" ExpectedTime: %" PRIu32 " Discrepancy: %" PRIu32 "\n",
apcData->TimerId, apcData->FireCount, TimerTime, expectedTime, TimerTime - expectedTime);
Sleep(11);
if (apcData->FireCount == apcData->MaxFireCount)
{
SetEvent(apcData->CompletionEvent);
}
}
int TestSynchTimerQueue(int argc, char* argv[])
{
HANDLE hTimerQueue = NULL;
HANDLE hTimers[TIMER_COUNT];
APC_DATA apcData[TIMER_COUNT];
WINPR_UNUSED(argc);
WINPR_UNUSED(argv);
hTimerQueue = CreateTimerQueue();
if (!hTimerQueue)
{
printf("CreateTimerQueue failed (%" PRIu32 ")\n", GetLastError());
return -1;
}
for (DWORD index = 0; index < TIMER_COUNT; index++)
{
apcData[index].TimerId = index;
apcData[index].StartTime = GetTickCount();
apcData[index].DueTime = (index * 10) + 50;
apcData[index].Period = 100;
apcData[index].FireCount = 0;
apcData[index].MaxFireCount = FIRE_COUNT;
if (!(apcData[index].CompletionEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
{
printf("Failed to create apcData[%" PRIu32 "] event (%" PRIu32 ")\n", index,
GetLastError());
return -1;
}
if (!CreateTimerQueueTimer(&hTimers[index], hTimerQueue, TimerRoutine, &apcData[index],
apcData[index].DueTime, apcData[index].Period, 0))
{
printf("CreateTimerQueueTimer failed (%" PRIu32 ")\n", GetLastError());
return -1;
}
}
for (DWORD index = 0; index < TIMER_COUNT; index++)
{
if (WaitForSingleObject(apcData[index].CompletionEvent, 2000) != WAIT_OBJECT_0)
{
printf("Failed to wait for timer queue timer #%" PRIu32 " (%" PRIu32 ")\n", index,
GetLastError());
return -1;
}
}
for (DWORD index = 0; index < TIMER_COUNT; index++)
{
/**
* Note: If the CompletionEvent parameter is INVALID_HANDLE_VALUE, the function waits
* for any running timer callback functions to complete before returning.
*/
if (!DeleteTimerQueueTimer(hTimerQueue, hTimers[index], INVALID_HANDLE_VALUE))
{
printf("DeleteTimerQueueTimer failed (%" PRIu32 ")\n", GetLastError());
return -1;
}
CloseHandle(apcData[index].CompletionEvent);
}
if (!DeleteTimerQueue(hTimerQueue))
{
printf("DeleteTimerQueue failed (%" PRIu32 ")\n", GetLastError());
return -1;
}
return 0;
}
|