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

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