summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/threading/WaitForSingleObject/WFSOMutexTest/WFSOMutexTest.c
blob: 5ecf517c2a7fc05e04d142250e17af756d1ed468 (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
181
182
183
184
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/*============================================================
**
** Source: WFSOMutexTest.c 
**
** Purpose: Test for WaitForSingleObjectTest. 
**	Create Mutex Object
**	Create Two Threads, Each Threads does WFSO for the Mutex Object
**			Increments Counter
**			Releases Mutex
**	Test Passes if the above operations are successful
**	
**
**
**=========================================================*/



#include <palsuite.h>


#define NUMBER_OF_WORKER_THREADS 2

//Declaring Variables
HANDLE hMutex = NULL;
unsigned int globalcounter =0;
int testReturnCode = PASS;

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



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

	//Declare local variables
		int i =0;

	// 2 dimensional array to hold thread handles for each worker thread
		HANDLE hThread[NUMBER_OF_WORKER_THREADS];
		DWORD dwThreadId=0; 
    int returnCode = 0;

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

   //Create Mutex
		hMutex = CreateMutex(NULL,      // no security attributes
                             FALSE,     // initially not owned
                             NULL);     // name of mutex

   //Check for Mutex Creation

		if (hMutex == NULL) 
		{
		 	Fail("Create Mutex Failed, GetLastError: %d\n", GetLastError());
		}


  //Spawn 2 worker threads
  for (i=0;i<NUMBER_OF_WORKER_THREADS;i++)
  	{
  		//Create Thread

		hThread[i] = CreateThread(
		NULL,         
		0,            
		WFSOMutexTest,     
		NULL,     
		0,           
		&dwThreadId);

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

    /* Test running */
    returnCode = WaitForMultipleObjects( NUMBER_OF_WORKER_THREADS, hThread, TRUE, 5000);  
    if( WAIT_OBJECT_0 != returnCode )
    {
        Trace("Wait for Object(s) returned %d, and GetLastError value is %d\n", returnCode, GetLastError());
        testReturnCode = FAIL;
    }

//Close thread handles
for (i=0;i<NUMBER_OF_WORKER_THREADS;i++)
  	{

	if (0==CloseHandle(hThread[i]))
		 {
		    	Trace("Could not Close thread handle\n"); 
			Fail ( "GetLastError returned %d\n", GetLastError());  
	    	}
	}

//Close Mutex Handle 
if (0==CloseHandle(hMutex))
	    	{
	    		Trace("Could not close mutex handle\n"); 
		Fail ( "GetLastError returned %d\n", GetLastError());  
    	}


PAL_TerminateEx(testReturnCode);
return ( testReturnCode );

}


void incrementCounter(void)
{
	if (INT_MAX == globalcounter)
		{
			globalcounter = 0;
		}
	
	globalcounter++;	
	Trace("Global Counter Value: %d \n", globalcounter);
}


DWORD WFSOMutexTest(LPVOID params)
{

     DWORD dwWaitResult; 

    // Request ownership of mutex.
 
    dwWaitResult = WaitForSingleObject( 
        hMutex,   // handle to mutex
        5000L);   // five-second time-out interval

    switch (dwWaitResult) 
    {
        // The thread got mutex ownership.
        case WAIT_OBJECT_0: 
            	  		{

				incrementCounter();

				//Release ownership of the mutex object.
				if (! ReleaseMutex(hMutex)) 
				{ 
				   Fail ( "ReleaseMutex() returned NULL.  Failing test.\n"
		       			"GetLastError returned %d\n", GetLastError());    
				} 

				break; 
    			        } 

        // Cannot get mutex ownership due to time-out.
        case WAIT_TIMEOUT: 
				{
					Fail ( "Cannot get mutex ownership due to time-out.  Failing test.\n"
		       			"GetLastError returned %d\n", GetLastError());  
					return FALSE;
        			}

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

    return 1;
}