FreeRDP/winpr/libwinpr/synch/test/TestSynchTimerQueue.c
Norbert Federa ef1fd12b15 Fix unchecked CreateEvent calls and misc fixes
1)
Added missing checks for CreateEvent which also required the
following related changes:

- changed freerdp_context_new API to BOOL
- changed freerdp_peer_context_new API to BOOL
- changed pRdpClientNew callback to BOOL
- changed pContextNew callback to BOOL
- changed psPeerAccepted callback to BOOL
- changed psPeerContextNew callback to BOOL

2)
Fixed lots of missing alloc and error checks in the
changed code's neighbourhood.

3)
Check freerdp_client_codecs_prepare result to avoid segfaults
caused by using non-initialized codecs.

4)
Fixed deadlocks in x11 caused by missing xf_unlock_x11() calls
in some error handlers

5)
Some fixes in thread pool:
- DEFAULT_POOL assignment did not match TP_POOL definition
- don't free the pool pointer if it points to the static DEFAULT_POOL
- added error handling and cleanup in InitializeThreadpool
2015-04-29 18:18:39 +02:00

115 lines
2.4 KiB
C

#include <winpr/crt.h>
#include <winpr/sysinfo.h>
#include <winpr/synch.h>
#define FIRE_COUNT 5
#define TIMER_COUNT 5
static int g_Count = 0;
static HANDLE g_Event = NULL;
struct apc_data
{
int TimerId;
int FireCount;
DWORD DueTime;
DWORD Period;
UINT32 StartTime;
};
typedef struct apc_data APC_DATA;
VOID CALLBACK TimerRoutine(PVOID lpParam, BOOLEAN TimerOrWaitFired)
{
UINT32 TimerTime;
APC_DATA* apcData;
UINT32 expectedTime;
UINT32 CurrentTime = GetTickCount();
if (!lpParam)
return;
apcData = (APC_DATA*) lpParam;
TimerTime = CurrentTime - apcData->StartTime;
expectedTime = apcData->DueTime + (apcData->Period * apcData->FireCount);
apcData->FireCount++;
g_Count++;
printf("TimerRoutine: TimerId: %d FireCount: %d ActualTime: %d ExpectedTime: %d Discrepancy: %d\n",
apcData->TimerId, apcData->FireCount, TimerTime, expectedTime, TimerTime - expectedTime);
if (g_Count >= (TIMER_COUNT * FIRE_COUNT))
{
SetEvent(g_Event);
}
Sleep(50);
}
int TestSynchTimerQueue(int argc, char* argv[])
{
int index;
HANDLE hTimerQueue;
HANDLE hTimers[TIMER_COUNT];
APC_DATA apcData[TIMER_COUNT];
g_Event = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!g_Event)
{
printf("CreateEvent failed (%d)\n", (int) GetLastError());
return -1;
}
hTimerQueue = CreateTimerQueue();
if (!hTimerQueue)
{
printf("CreateTimerQueue failed (%d)\n", (int) GetLastError());
return -1;
}
for (index = 0; index < TIMER_COUNT; index++)
{
apcData[index].TimerId = index;
apcData[index].StartTime = GetTickCount();
apcData[index].DueTime = (index * 100) + 500;
apcData[index].Period = 1000;
apcData[index].FireCount = 0;
if (!CreateTimerQueueTimer(&hTimers[index], hTimerQueue, (WAITORTIMERCALLBACK) TimerRoutine,
&apcData[index], apcData[index].DueTime, apcData[index].Period, 0))
{
printf("CreateTimerQueueTimer failed (%d)\n", (int) GetLastError());
return -1;
}
}
if (WaitForSingleObject(g_Event, INFINITE) != WAIT_OBJECT_0)
{
printf("WaitForSingleObject failed (%d)\n", (int) GetLastError());
return -1;
}
for (index = 0; index < TIMER_COUNT; index++)
{
if (!DeleteTimerQueueTimer(hTimerQueue, hTimers[index], NULL))
{
printf("DeleteTimerQueueTimer failed (%d)\n", (int) GetLastError());
return -1;
}
}
if (!DeleteTimerQueue(hTimerQueue))
{
printf("DeleteTimerQueue failed (%d)\n", (int) GetLastError());
return -1;
}
CloseHandle(g_Event);
return 0;
}