summaryrefslogtreecommitdiffstats
path: root/winpr/libwinpr/utils/test/TestMessageQueue.c
blob: b3245b1d3054c77b7d86b99c5dbbe0e8d5ee17ad (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
#include <winpr/crt.h>
#include <winpr/thread.h>
#include <winpr/collections.h>

static DWORD WINAPI message_queue_consumer_thread(LPVOID arg)
{
	wMessage message = { 0 };
	wMessageQueue* queue = (wMessageQueue*)arg;

	while (MessageQueue_Wait(queue))
	{
		if (MessageQueue_Peek(queue, &message, TRUE))
		{
			if (message.id == WMQ_QUIT)
				break;

			printf("Message.Type: %" PRIu32 "\n", message.id);
		}
	}

	return 0;
}

int TestMessageQueue(int argc, char* argv[])
{
	HANDLE thread = NULL;
	wMessageQueue* queue = NULL;

	WINPR_UNUSED(argc);
	WINPR_UNUSED(argv);

	if (!(queue = MessageQueue_New(NULL)))
	{
		printf("failed to create message queue\n");
		return 1;
	}

	if (!(thread = CreateThread(NULL, 0, message_queue_consumer_thread, (void*)queue, 0, NULL)))
	{
		printf("failed to create thread\n");
		MessageQueue_Free(queue);
		return 1;
	}

	if (!MessageQueue_Post(queue, NULL, 123, NULL, NULL) ||
	    !MessageQueue_Post(queue, NULL, 456, NULL, NULL) ||
	    !MessageQueue_Post(queue, NULL, 789, NULL, NULL) || !MessageQueue_PostQuit(queue, 0) ||
	    WaitForSingleObject(thread, INFINITE) != WAIT_OBJECT_0)
		return -1;

	MessageQueue_Free(queue);
	CloseHandle(thread);

	return 0;
}