1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5/*============================================================
6**
7** Source: WFSOThreadTest.c
8**
9** Purpose: Test for WaitForSingleObjectTest.
10** Create One Thread and do some work
11** Use WFSO For the Thread to finish
12**
13** Test Passes if the above operations are successful
14**
15**
16**
17**=========================================================*/
18
19
20
21#include <palsuite.h>
22
23
24//Declaring Variables
25HANDLE hThread = NULL;
26HANDLE hEvent = NULL;
27
28unsigned int globalcounter =0;
29
30//Declaring Function Prototypes
31DWORD PALAPI incrementCounter(LPVOID params);
32
33int __cdecl main(int argc, char **argv)
34{
35
36 //Declare local variables
37 DWORD dwThreadId=0;
38 DWORD dwWaitResult=0;
39
40 //Initialize PAL
41 if(0 != (PAL_Initialize(argc, argv)))
42 {
43 return ( FAIL );
44 }
45
46
47 //Create Event
48 hEvent = CreateEvent(NULL,TRUE,FALSE, NULL);
49 if(hEvent == NULL)
50 {
51 Fail("Create Event Failed\n"
52 "GetLastError returned %d\n", GetLastError());
53 }
54
55
56 //Create Thread
57 hThread = CreateThread(
58 NULL,
59 0,
60 incrementCounter,
61 NULL,
62 0,
63 &dwThreadId);
64
65 if ( NULL == hThread )
66 {
67 Fail ( "CreateThread() returned NULL. Failing test.\n"
68 "GetLastError returned %d\n", GetLastError());
69 }
70
71
72 //Wait For Thread to signal start
73 dwWaitResult = WaitForSingleObject(hEvent,INFINITE);
74
75 switch (dwWaitResult)
76 {
77 // The thread wait was successful
78 case WAIT_OBJECT_0:
79 {
80
81 Trace ("Wait for Single Object (hEvent) was successful.\n");
82 break;
83 }
84
85 // Time-out.
86 case WAIT_TIMEOUT:
87 {
88 Fail ( "Time -out. Failing test.\n"
89 "GetLastError returned %d\n", GetLastError());
90 return FALSE;
91 }
92
93 // Got ownership of the abandoned event object.
94 case WAIT_ABANDONED:
95 {
96 Fail ( "Got ownership of the abandoned event object. Failing test.\n"
97 "GetLastError returned %d\n", GetLastError());
98 return FALSE;
99 }
100
101 }
102
103
104 //Wait for Thread to finish
105 dwWaitResult = WaitForSingleObject(
106 hThread, //handle to thread
107 5000L); //Wait Indefinitely
108
109
110 switch (dwWaitResult)
111 {
112 // The thread wait was successful
113 case WAIT_OBJECT_0:
114 {
115
116 Trace("Wait for thread was successful\n");
117
118 break;
119 }
120
121 // Time-out.
122 case WAIT_TIMEOUT:
123 {
124 Fail ( "Time -out. Failing test.\n"
125 "GetLastError returned %d\n", GetLastError());
126 return FALSE;
127 }
128
129 // Got ownership of the abandoned thread object.
130 case WAIT_ABANDONED:
131 {
132 Fail ( "Got ownership of the abandoned thread object. Failing test.\n"
133 "GetLastError returned %d\n", GetLastError());
134 return FALSE;
135 }
136
137 }
138
139
140//Close Handles
141if (0==CloseHandle(hEvent))
142 {
143 Trace("Could not Close event handle\n");
144 Fail ( "GetLastError returned %d\n", GetLastError());
145 }
146if (0==CloseHandle(hThread))
147 {
148 Trace("Could not Close thread handle\n");
149 Fail ( "GetLastError returned %d\n", GetLastError());
150 }
151
152PAL_Terminate();
153return ( PASS );
154
155}
156
157DWORD PALAPI incrementCounter(LPVOID params)
158{
159
160 //Signal Event so that main thread can start to wait for thread object
161 if (0==SetEvent(hEvent))
162 {
163 Fail ( "SetEvent returned Zero. Failing test.\n"
164 "GetLastError returned %d\n", GetLastError());
165 }
166
167 for (globalcounter=0;globalcounter<100000;globalcounter++);
168
169 //Sleep(5000);
170
171 Trace("Global Counter Value: %d \n", globalcounter);
172 return 0;
173}
174
175
176
177
178
179
180