File: TestThreadExitThread.c

package info (click to toggle)
freerdp2 2.0.0~git20190204.1.2693389a%2Bdfsg1-1%2Bdeb10u2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 24,348 kB
  • sloc: ansic: 308,081; xml: 1,676; sh: 770; perl: 231; makefile: 158; python: 65
file content (53 lines) | stat: -rw-r--r-- 1,218 bytes parent folder | download
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
// Copyright © 2015 Hewlett-Packard Development Company, L.P.

#include <winpr/file.h>
#include <winpr/synch.h>
#include <winpr/thread.h>

static DWORD WINAPI thread_func(LPVOID arg)
{
	/* exists of the thread the quickest as possible */
	ExitThread(0);
	return 0;
}

int TestThreadExitThread(int argc, char* argv[])
{
	HANDLE thread; 
	DWORD waitResult;
	int i;

	/* FIXME: create some noise to better guaranty the test validity and
         * decrease the number of loops */
	for (i=0; i<50000; i++)
	{
		thread = CreateThread(NULL,
				0,
				thread_func,
				NULL,
				0,
				NULL);

		if (thread == INVALID_HANDLE_VALUE)
		{
			fprintf(stderr, "Got an invalid thread!\n");
			return -1;
		}

		waitResult = WaitForSingleObject(thread, 1000);
		if (waitResult != WAIT_OBJECT_0)
		{
			/* When the thread exits before the internal thread_list
			 * was updated, ExitThread() is not able to retrieve the
			 * related WINPR_THREAD object and is not able to signal
			 * the end of the thread. Therefore WaitForSingleObject
			 * never get the signal.
			 */
			fprintf(stderr, "1 second should have been enough for the thread to be in a signaled state\n");
			return -1;
		}

		CloseHandle(thread);
	}
	return 0;
}