summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/threading/WaitForSingleObject/WFSOThreadTest/WFSOThreadTest.c
blob: a2e5a87645acf059cae82fa8d64d972e73239a94 (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information. 
//

/*============================================================
**
** Source: WFSOThreadTest.c 
**
** Purpose: Test for WaitForSingleObjectTest. 
**			Create One Thread and do some work
**			Use WFSO For the Thread to finish 
**			
** Test Passes if the above operations are successful
**	
**
**
**=========================================================*/



#include <palsuite.h>


//Declaring Variables
HANDLE hThread = NULL;
HANDLE hEvent = NULL;

unsigned int globalcounter =0;

//Declaring Function Prototypes
DWORD incrementCounter(LPVOID params);

int __cdecl main(int argc, char **argv)
{

	//Declare local variables
	DWORD dwThreadId=0; 
	DWORD dwWaitResult=0; 

	//Initialize PAL 
	if(0 != (PAL_Initialize(argc, argv)))
	    {
	        return ( FAIL );
	    }


	//Create Event
	hEvent = CreateEvent(NULL,TRUE,FALSE, NULL);
	if(hEvent == NULL)
	{
		Fail("Create Event Failed\n"
			"GetLastError returned %d\n", GetLastError());
	}

	
	//Create Thread
	hThread = CreateThread(
		NULL,         
		0,            
		incrementCounter,     
		NULL,     
		0,           
		&dwThreadId);

	    if ( NULL == hThread ) 
	    {
		Fail ( "CreateThread() returned NULL.  Failing test.\n"
		       "GetLastError returned %d\n", GetLastError());   
	    }


	//Wait For Thread to signal start  
	dwWaitResult  = WaitForSingleObject(hEvent,INFINITE);
	
	switch (dwWaitResult) 
    	{
        // The thread wait was successful
        case WAIT_OBJECT_0: 
            	  		{

				Trace ("Wait for Single Object (hEvent) was successful.\n");
			       break; 
    			        } 

	// Time-out.
        case WAIT_TIMEOUT: 
				{
					Fail ( "Time -out.  Failing test.\n"
		       			"GetLastError returned %d\n", GetLastError());  
					return FALSE;
        			}

        // Got ownership of the abandoned event object.
        case WAIT_ABANDONED: 
				{
					Fail ( "Got ownership of the abandoned event object.  Failing test.\n"
		       			"GetLastError returned %d\n", GetLastError());  
					return FALSE; 
        			}

    	}

		
	//Wait for Thread to finish 
	dwWaitResult = WaitForSingleObject( 
	        hThread,   //handle to thread
	        5000L);     //Wait Indefinitely

       
	switch (dwWaitResult) 
    	{
        // The thread wait was successful
        case WAIT_OBJECT_0: 
            	  		{

				Trace("Wait for thread was successful\n");
			
				break; 
    			        } 

        // Time-out.
        case WAIT_TIMEOUT: 
				{
					Fail ( "Time -out.  Failing test.\n"
		       			"GetLastError returned %d\n", GetLastError());  
					return FALSE;
        			}

        // Got ownership of the abandoned thread object.
        case WAIT_ABANDONED: 
				{
					Fail ( "Got ownership of the abandoned thread object.  Failing test.\n"
		       			"GetLastError returned %d\n", GetLastError());  
					return FALSE; 
        			}

    }


//Close Handles
if (0==CloseHandle(hEvent))
		 {
		    	Trace("Could not Close event handle\n"); 
			Fail ( "GetLastError returned %d\n", GetLastError());  
	    	}
if (0==CloseHandle(hThread))
		 {
		    	Trace("Could not Close thread handle\n"); 
			Fail ( "GetLastError returned %d\n", GetLastError());  
	    	}

PAL_Terminate();
return ( PASS );

}

DWORD incrementCounter(LPVOID params)
{

	//Signal Event so that main thread can start to wait for thread object
	if (0==SetEvent(hEvent))
	{
		Fail ( "SetEvent returned Zero.  Failing test.\n"
		       "GetLastError returned %d\n", GetLastError());  
	}

	for (globalcounter=0;globalcounter<100000;globalcounter++);

	//Sleep(5000);
	
	Trace("Global Counter Value: %d \n", globalcounter);
	return 0;
}