summaryrefslogtreecommitdiffstats
path: root/winpr/libwinpr/synch/test/TestSynchWaitableTimer.c
blob: f61bbc141d12c5e7c65eb616ee2f6b62bcb004de (plain)
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
#include <winpr/crt.h>
#include <winpr/synch.h>

int TestSynchWaitableTimer(int argc, char* argv[])
{
	DWORD status = 0;
	HANDLE timer = NULL;
	LONG period = 0;
	LARGE_INTEGER due;
	int result = -1;
	WINPR_UNUSED(argc);
	WINPR_UNUSED(argv);
	timer = CreateWaitableTimer(NULL, FALSE, NULL);

	if (!timer)
	{
		printf("CreateWaitableTimer failure\n");
		goto out;
	}

	due.QuadPart = -1500000LL; /* 0.15 seconds */

	if (!SetWaitableTimer(timer, &due, 0, NULL, NULL, 0))
	{
		printf("SetWaitableTimer failure\n");
		goto out;
	}

	status = WaitForSingleObject(timer, INFINITE);

	if (status != WAIT_OBJECT_0)
	{
		printf("WaitForSingleObject(timer, INFINITE) failure\n");
		goto out;
	}

	printf("Timer Signaled\n");
	status = WaitForSingleObject(timer, 200);

	if (status != WAIT_TIMEOUT)
	{
		printf("WaitForSingleObject(timer, 200) failure: Actual: 0x%08" PRIX32
		       ", Expected: 0x%08X\n",
		       status, WAIT_TIMEOUT);
		goto out;
	}

	due.QuadPart = 0;
	period = 120; /* 0.12 seconds */

	if (!SetWaitableTimer(timer, &due, period, NULL, NULL, 0))
	{
		printf("SetWaitableTimer failure\n");
		goto out;
	}

	if (WaitForSingleObject(timer, INFINITE) != WAIT_OBJECT_0)
	{
		printf("WaitForSingleObject(timer, INFINITE) failure\n");
		goto out;
	}

	printf("Timer Signaled\n");

	if (!SetWaitableTimer(timer, &due, period, NULL, NULL, 0))
	{
		printf("SetWaitableTimer failure\n");
		goto out;
	}

	if (WaitForMultipleObjects(1, &timer, FALSE, INFINITE) != WAIT_OBJECT_0)
	{
		printf("WaitForMultipleObjects(timer, INFINITE) failure\n");
		goto out;
	}

	printf("Timer Signaled\n");
	result = 0;
out:
	CloseHandle(timer);
	return result;
}