2014-01-25 02:48:55 +04:00
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
2014-01-27 02:21:12 +04:00
|
|
|
#include <winpr/sysinfo.h>
|
2016-06-01 17:26:26 +03:00
|
|
|
#include <winpr/file.h>
|
2014-01-25 02:48:55 +04:00
|
|
|
#include <winpr/synch.h>
|
|
|
|
|
2014-01-27 02:21:12 +04:00
|
|
|
#define FIRE_COUNT 5
|
2014-01-27 05:39:13 +04:00
|
|
|
#define TIMER_COUNT 5
|
2014-01-27 02:21:12 +04:00
|
|
|
|
|
|
|
struct apc_data
|
|
|
|
{
|
2016-05-24 23:33:27 +03:00
|
|
|
DWORD TimerId;
|
|
|
|
DWORD FireCount;
|
2014-01-27 05:39:13 +04:00
|
|
|
DWORD DueTime;
|
|
|
|
DWORD Period;
|
2014-01-27 02:21:12 +04:00
|
|
|
UINT32 StartTime;
|
2016-05-24 23:33:27 +03:00
|
|
|
DWORD MaxFireCount;
|
|
|
|
HANDLE CompletionEvent;
|
2014-01-27 02:21:12 +04:00
|
|
|
};
|
|
|
|
typedef struct apc_data APC_DATA;
|
2014-01-25 02:48:55 +04:00
|
|
|
|
|
|
|
VOID CALLBACK TimerRoutine(PVOID lpParam, BOOLEAN TimerOrWaitFired)
|
|
|
|
{
|
2014-01-27 02:21:12 +04:00
|
|
|
UINT32 TimerTime;
|
|
|
|
APC_DATA* apcData;
|
2014-01-27 05:39:13 +04:00
|
|
|
UINT32 expectedTime;
|
2014-01-27 02:21:12 +04:00
|
|
|
UINT32 CurrentTime = GetTickCount();
|
2014-01-25 02:48:55 +04:00
|
|
|
|
|
|
|
if (!lpParam)
|
|
|
|
return;
|
|
|
|
|
2014-01-27 02:21:12 +04:00
|
|
|
apcData = (APC_DATA*) lpParam;
|
|
|
|
|
|
|
|
TimerTime = CurrentTime - apcData->StartTime;
|
2014-01-27 05:39:13 +04:00
|
|
|
expectedTime = apcData->DueTime + (apcData->Period * apcData->FireCount);
|
2014-01-27 02:21:12 +04:00
|
|
|
|
2014-01-27 05:39:13 +04:00
|
|
|
apcData->FireCount++;
|
2014-01-25 02:48:55 +04:00
|
|
|
|
2014-01-27 06:56:07 +04:00
|
|
|
printf("TimerRoutine: TimerId: %d FireCount: %d ActualTime: %d ExpectedTime: %d Discrepancy: %d\n",
|
|
|
|
apcData->TimerId, apcData->FireCount, TimerTime, expectedTime, TimerTime - expectedTime);
|
|
|
|
|
2016-05-24 23:33:27 +03:00
|
|
|
Sleep(50);
|
|
|
|
|
|
|
|
if (apcData->FireCount == apcData->MaxFireCount)
|
2014-01-27 02:21:12 +04:00
|
|
|
{
|
2016-05-24 23:33:27 +03:00
|
|
|
SetEvent(apcData->CompletionEvent);
|
2014-01-27 02:21:12 +04:00
|
|
|
}
|
2014-01-25 02:48:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int TestSynchTimerQueue(int argc, char* argv[])
|
|
|
|
{
|
2016-05-24 23:33:27 +03:00
|
|
|
DWORD index;
|
2014-01-27 06:56:07 +04:00
|
|
|
HANDLE hTimerQueue;
|
|
|
|
HANDLE hTimers[TIMER_COUNT];
|
2014-01-27 02:21:12 +04:00
|
|
|
APC_DATA apcData[TIMER_COUNT];
|
2014-01-25 02:48:55 +04:00
|
|
|
|
|
|
|
hTimerQueue = CreateTimerQueue();
|
|
|
|
|
|
|
|
if (!hTimerQueue)
|
|
|
|
{
|
2016-05-24 23:33:27 +03:00
|
|
|
printf("CreateTimerQueue failed (%u)\n", GetLastError());
|
2014-01-25 02:48:55 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-01-27 02:21:12 +04:00
|
|
|
for (index = 0; index < TIMER_COUNT; index++)
|
2014-01-25 02:48:55 +04:00
|
|
|
{
|
2014-01-27 02:21:12 +04:00
|
|
|
apcData[index].TimerId = index;
|
|
|
|
apcData[index].StartTime = GetTickCount();
|
2014-01-27 05:39:13 +04:00
|
|
|
apcData[index].DueTime = (index * 100) + 500;
|
|
|
|
apcData[index].Period = 1000;
|
|
|
|
apcData[index].FireCount = 0;
|
2016-05-24 23:33:27 +03:00
|
|
|
apcData[index].MaxFireCount = FIRE_COUNT;
|
|
|
|
|
|
|
|
if (!(apcData[index].CompletionEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
|
|
|
|
{
|
|
|
|
printf("Failed to create apcData[%u] event (%u)\n", index, GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
2014-01-27 02:21:12 +04:00
|
|
|
|
2014-01-27 06:56:07 +04:00
|
|
|
if (!CreateTimerQueueTimer(&hTimers[index], hTimerQueue, (WAITORTIMERCALLBACK) TimerRoutine,
|
2014-01-27 05:39:13 +04:00
|
|
|
&apcData[index], apcData[index].DueTime, apcData[index].Period, 0))
|
2014-01-27 02:21:12 +04:00
|
|
|
{
|
2016-05-24 23:33:27 +03:00
|
|
|
printf("CreateTimerQueueTimer failed (%u)\n", GetLastError());
|
2014-01-27 02:21:12 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2014-01-25 02:48:55 +04:00
|
|
|
}
|
|
|
|
|
2016-05-24 23:33:27 +03:00
|
|
|
for (index = 0; index < TIMER_COUNT; index++)
|
2014-01-25 02:48:55 +04:00
|
|
|
{
|
2016-05-24 23:33:27 +03:00
|
|
|
if (WaitForSingleObject(apcData[index].CompletionEvent, 20000) != WAIT_OBJECT_0)
|
|
|
|
{
|
|
|
|
printf("Failed to wait for timer queue timer #%u (%u)\n", index, GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
2014-01-25 02:48:55 +04:00
|
|
|
}
|
|
|
|
|
2014-01-27 06:56:07 +04:00
|
|
|
for (index = 0; index < TIMER_COUNT; index++)
|
2014-01-26 21:37:32 +04:00
|
|
|
{
|
2016-06-01 17:26:26 +03:00
|
|
|
/**
|
|
|
|
* 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))
|
2014-01-27 06:56:07 +04:00
|
|
|
{
|
2016-05-24 23:33:27 +03:00
|
|
|
printf("DeleteTimerQueueTimer failed (%u)\n", GetLastError());
|
2014-01-27 06:56:07 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2016-06-01 17:26:26 +03:00
|
|
|
CloseHandle(apcData[index].CompletionEvent);
|
2014-01-26 21:37:32 +04:00
|
|
|
}
|
2016-05-24 23:33:27 +03:00
|
|
|
|
2014-01-25 02:48:55 +04:00
|
|
|
if (!DeleteTimerQueue(hTimerQueue))
|
|
|
|
{
|
2016-05-24 23:33:27 +03:00
|
|
|
printf("DeleteTimerQueue failed (%u)\n", GetLastError());
|
2014-01-25 02:48:55 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|