FreeRDP/winpr/libwinpr/synch/test/TestSynchTimerQueue.c

119 lines
2.9 KiB
C
Raw Normal View History

2014-01-25 02:48:55 +04:00
#include <winpr/crt.h>
#include <winpr/sysinfo.h>
#include <winpr/file.h>
2014-01-25 02:48:55 +04:00
#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;
2014-01-25 02:48:55 +04:00
VOID CALLBACK TimerRoutine(PVOID lpParam, BOOLEAN TimerOrWaitFired)
{
UINT32 TimerTime;
APC_DATA* apcData;
UINT32 expectedTime;
UINT32 CurrentTime = GetTickCount();
2014-01-25 02:48:55 +04:00
if (!lpParam)
return;
apcData = (APC_DATA*) lpParam;
TimerTime = CurrentTime - apcData->StartTime;
expectedTime = apcData->DueTime + (apcData->Period * apcData->FireCount);
apcData->FireCount++;
2014-01-25 02:48:55 +04:00
printf("TimerRoutine: TimerId: %"PRIu32" FireCount: %"PRIu32" ActualTime: %"PRIu32" ExpectedTime: %"PRIu32" Discrepancy: %"PRIu32"\n",
apcData->TimerId, apcData->FireCount, TimerTime, expectedTime, TimerTime - expectedTime);
Sleep(50);
if (apcData->FireCount == apcData->MaxFireCount)
{
SetEvent(apcData->CompletionEvent);
}
2014-01-25 02:48:55 +04:00
}
int TestSynchTimerQueue(int argc, char* argv[])
{
DWORD index;
HANDLE hTimerQueue;
HANDLE hTimers[TIMER_COUNT];
APC_DATA apcData[TIMER_COUNT];
2014-01-25 02:48:55 +04:00
hTimerQueue = CreateTimerQueue();
if (!hTimerQueue)
{
printf("CreateTimerQueue failed (%"PRIu32")\n", GetLastError());
2014-01-25 02:48:55 +04:00
return -1;
}
for (index = 0; index < TIMER_COUNT; index++)
2014-01-25 02:48:55 +04:00
{
apcData[index].TimerId = index;
apcData[index].StartTime = GetTickCount();
apcData[index].DueTime = (index * 100) + 500;
apcData[index].Period = 1000;
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, (WAITORTIMERCALLBACK) TimerRoutine,
&apcData[index], apcData[index].DueTime, apcData[index].Period, 0))
{
printf("CreateTimerQueueTimer failed (%"PRIu32")\n", GetLastError());
return -1;
}
2014-01-25 02:48:55 +04:00
}
for (index = 0; index < TIMER_COUNT; index++)
2014-01-25 02:48:55 +04:00
{
if (WaitForSingleObject(apcData[index].CompletionEvent, 20000) != WAIT_OBJECT_0)
{
printf("Failed to wait for timer queue timer #%"PRIu32" (%"PRIu32")\n", index, GetLastError());
return -1;
}
2014-01-25 02:48:55 +04:00
}
for (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);
}
2014-01-25 02:48:55 +04:00
if (!DeleteTimerQueue(hTimerQueue))
{
printf("DeleteTimerQueue failed (%"PRIu32")\n", GetLastError());
2014-01-25 02:48:55 +04:00
return -1;
}
return 0;
}