Blame winpr/libwinpr/synch/test/TestSynchWaitableTimerAPC.c

Packit 1fb8d4
Packit 1fb8d4
#include <winpr/crt.h>
Packit 1fb8d4
#include <winpr/synch.h>
Packit 1fb8d4
#include <winpr/sysinfo.h>
Packit 1fb8d4
Packit 1fb8d4
static int g_Count = 0;
Packit 1fb8d4
static HANDLE g_Event = NULL;
Packit 1fb8d4
Packit 1fb8d4
struct apc_data
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 StartTime;
Packit 1fb8d4
};
Packit 1fb8d4
typedef struct apc_data APC_DATA;
Packit 1fb8d4
Packit 1fb8d4
static VOID CALLBACK TimerAPCProc(LPVOID lpArg, DWORD dwTimerLowValue, DWORD dwTimerHighValue)
Packit 1fb8d4
{
Packit 1fb8d4
	APC_DATA* apcData;
Packit 1fb8d4
	UINT32 CurrentTime = GetTickCount();
Packit Service 5a9772
	WINPR_UNUSED(dwTimerLowValue);
Packit Service 5a9772
	WINPR_UNUSED(dwTimerHighValue);
Packit 1fb8d4
Packit 1fb8d4
	if (!lpArg)
Packit 1fb8d4
		return;
Packit 1fb8d4
Packit Service 5a9772
	apcData = (APC_DATA*)lpArg;
Packit Service 5a9772
	printf("TimerAPCProc: time: %" PRIu32 "\n", CurrentTime - apcData->StartTime);
Packit 1fb8d4
	g_Count++;
Packit 1fb8d4
Packit 1fb8d4
	if (g_Count >= 5)
Packit 1fb8d4
	{
Packit 1fb8d4
		SetEvent(g_Event);
Packit 1fb8d4
	}
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
int TestSynchWaitableTimerAPC(int argc, char* argv[])
Packit 1fb8d4
{
Packit 1fb8d4
	int status = -1;
Packit 1fb8d4
	HANDLE hTimer = NULL;
Packit 1fb8d4
	BOOL bSuccess;
Packit 1fb8d4
	LARGE_INTEGER due;
Packit Service 5a9772
	APC_DATA apcData = { 0 };
Packit Service 5a9772
	WINPR_UNUSED(argc);
Packit Service 5a9772
	WINPR_UNUSED(argv);
Packit 1fb8d4
	g_Event = CreateEvent(NULL, TRUE, FALSE, NULL);
Packit Service 5a9772
Packit 1fb8d4
	if (!g_Event)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("Failed to create event\n");
Packit 1fb8d4
		goto cleanup;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	hTimer = CreateWaitableTimer(NULL, FALSE, NULL);
Packit 1fb8d4
Packit 1fb8d4
	if (!hTimer)
Packit 1fb8d4
		goto cleanup;
Packit 1fb8d4
Packit 1fb8d4
	due.QuadPart = -15000000LL; /* 1.5 seconds */
Packit Service 5a9772
	apcData.StartTime = GetTickCount();
Packit Service 5a9772
	bSuccess = SetWaitableTimer(hTimer, &due, 2000, TimerAPCProc, &apcData, FALSE);
Packit 1fb8d4
Packit 1fb8d4
	if (!bSuccess)
Packit 1fb8d4
		goto cleanup;
Packit 1fb8d4
Packit 1fb8d4
	/**
Packit Service 5a9772
	 * See Remarks at
Packit Service 5a9772
	 * https://msdn.microsoft.com/en-us/library/windows/desktop/ms686786(v=vs.85).aspx The
Packit Service 5a9772
	 * SetWaitableTimer completion routine is executed by the thread that activates the timer using
Packit Service 5a9772
	 * SetWaitableTimer. However, the thread must be in an ALERTABLE state.
Packit 1fb8d4
	 */
Packit 1fb8d4
Packit 1fb8d4
	/**
Packit 1fb8d4
	 * Note: On WIN32 we need to use WaitForSingleObjectEx with parameter bAlertable = TRUE
Packit Service 5a9772
	 * However, WinPR currently (May 2016) does not have a working WaitForSingleObjectEx
Packit Service 5a9772
	 *implementation but its non-WIN32 WaitForSingleObject implementations seem to be alertable by
Packit Service 5a9772
	 *WinPR's timer implementations.
Packit 1fb8d4
	 **/
Packit 1fb8d4
Packit Service 5a9772
	for (;;)
Packit 1fb8d4
	{
Packit 1fb8d4
		DWORD rc;
Packit 1fb8d4
#ifdef _WIN32
Packit 1fb8d4
		rc = WaitForSingleObjectEx(g_Event, INFINITE, TRUE);
Packit 1fb8d4
#else
Packit 1fb8d4
		rc = WaitForSingleObject(g_Event, INFINITE);
Packit 1fb8d4
#endif
Packit Service 5a9772
Packit 1fb8d4
		if (rc == WAIT_OBJECT_0)
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		if (rc == 0x000000C0L) /* WAIT_IO_COMPLETION */
Packit 1fb8d4
			continue;
Packit 1fb8d4
Packit Service 5a9772
		printf("Failed to wait for completion event (%" PRIu32 ")\n", GetLastError());
Packit 1fb8d4
		goto cleanup;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	status = 0;
Packit 1fb8d4
cleanup:
Packit Service 5a9772
Packit 1fb8d4
	if (hTimer)
Packit 1fb8d4
		CloseHandle(hTimer);
Packit Service 5a9772
Packit 1fb8d4
	if (g_Event)
Packit 1fb8d4
		CloseHandle(g_Event);
Packit 1fb8d4
Packit 1fb8d4
	return status;
Packit 1fb8d4
}