Blame winpr/libwinpr/thread/test/TestThreadExitThread.c

Packit Service fa4841
// Copyright © 2015 Hewlett-Packard Development Company, L.P.
Packit Service fa4841
Packit Service fa4841
#include <winpr/file.h>
Packit Service fa4841
#include <winpr/synch.h>
Packit Service fa4841
#include <winpr/thread.h>
Packit Service fa4841
Packit Service fa4841
static DWORD WINAPI thread_func(LPVOID arg)
Packit Service fa4841
{
Packit Service fa4841
	/* exists of the thread the quickest as possible */
Packit Service fa4841
	ExitThread(0);
Packit Service fa4841
	return 0;
Packit Service fa4841
}
Packit Service fa4841
Packit Service fa4841
int TestThreadExitThread(int argc, char* argv[])
Packit Service fa4841
{
Packit Service fa4841
	HANDLE thread;
Packit Service fa4841
	DWORD waitResult;
Packit Service fa4841
	int i;
Packit Service fa4841
Packit Service fa4841
	/* FIXME: create some noise to better guaranty the test validity and
Packit Service fa4841
	 * decrease the number of loops */
Packit Service fa4841
	for (i = 0; i < 50000; i++)
Packit Service fa4841
	{
Packit Service fa4841
		thread = CreateThread(NULL, 0, thread_func, NULL, 0, NULL);
Packit Service fa4841
Packit Service fa4841
		if (thread == INVALID_HANDLE_VALUE)
Packit Service fa4841
		{
Packit Service fa4841
			fprintf(stderr, "Got an invalid thread!\n");
Packit Service fa4841
			return -1;
Packit Service fa4841
		}
Packit Service fa4841
Packit Service fa4841
		waitResult = WaitForSingleObject(thread, 1000);
Packit Service fa4841
		if (waitResult != WAIT_OBJECT_0)
Packit Service fa4841
		{
Packit Service fa4841
			/* When the thread exits before the internal thread_list
Packit Service fa4841
			 * was updated, ExitThread() is not able to retrieve the
Packit Service fa4841
			 * related WINPR_THREAD object and is not able to signal
Packit Service fa4841
			 * the end of the thread. Therefore WaitForSingleObject
Packit Service fa4841
			 * never get the signal.
Packit Service fa4841
			 */
Packit Service fa4841
			fprintf(stderr,
Packit Service fa4841
			        "1 second should have been enough for the thread to be in a signaled state\n");
Packit Service fa4841
			return -1;
Packit Service fa4841
		}
Packit Service fa4841
Packit Service fa4841
		CloseHandle(thread);
Packit Service fa4841
	}
Packit Service fa4841
	return 0;
Packit Service fa4841
}