winpr/thread: fix test compilation on windows
[platform/upstream/freerdp.git] / winpr / libwinpr / thread / test / TestThreadExitThread.c
1 // Copyright © 2015 Hewlett-Packard Development Company, L.P.
2
3 #include <winpr/file.h>
4 #include <winpr/synch.h>
5 #include <winpr/thread.h>
6
7 static void* thread_func(void* arg)
8 {
9         /* exists of the thread the quickest as possible */
10         ExitThread(0);
11         return NULL;
12 }
13
14 int TestThreadExitThread(int argc, char* argv[])
15 {
16         HANDLE thread; 
17         DWORD waitResult;
18         int i;
19
20         /* FIXME: create some noise to better guaranty the test validity and
21          * decrease the number of loops */
22         for (i=0; i<50000; i++)
23         {
24                 thread = CreateThread(NULL,
25                                 0,
26                                 (LPTHREAD_START_ROUTINE)thread_func,
27                                 NULL,
28                                 0,
29                                 NULL);
30
31                 if (thread == INVALID_HANDLE_VALUE)
32                 {
33                         fprintf(stderr, "Got an invalid thread!\n");
34                         return -1;
35                 }
36
37                 waitResult = WaitForSingleObject(thread, 1000);
38                 if (waitResult != WAIT_OBJECT_0)
39                 {
40                         /* When the thread exits before the internal thread_list
41                          * was updated, ExitThread() is not able to retrieve the
42                          * related WINPR_THREAD object and is not able to signal
43                          * the end of the thread. Therefore WaitForSingleObject
44                          * never get the signal.
45                          */
46                         fprintf(stderr, "1 second should have been enough for the thread to be in a signaled state\n");
47                         return -1;
48                 }
49
50                 CloseHandle(thread);
51         }
52         return 0;
53 }