summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/threading/WaitForSingleObject/WFSOSemaphoreTest/WFSOSemaphoreTest.cpp
blob: b743b44544acc5295098959195918b37f2900c45 (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
// 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 Semaphore Object
**	Create Two Threads, Each Threads does WFSO for the Semaphore Object
**			Increments Counter
**			Releases Semaphore
**	Test Passes if the above operations are successful
**	
**
**
**=========================================================*/



#include <palsuite.h>


#define NUMBER_OF_WORKER_THREADS 2


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

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

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

	//Declare local variables
		int i =0;
		int cMax = 2;

    int returnCode = 0;

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

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

   //Create Semaphore
		hSemaphore = CreateSemaphore( 
			NULL,   // no security attributes
			cMax,   // initial count
			cMax,   // maximum count
			NULL);  // unnamed semaphore

		if (hSemaphore == NULL) 
		{
		    // Check for error.
		    Fail("Create Semaphore 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,            
		WFSOSemaphoreTest,     
		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 Semaphore Handle 
if (0==CloseHandle(hSemaphore))
	    	{
	    		Trace("Could not close semaphore 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 PALAPI WFSOSemaphoreTest(LPVOID params)
{

     DWORD dwWaitResult; 

    // Request ownership of Semaphore
 
 dwWaitResult = WaitForSingleObject( 
        hSemaphore,   // handle to semaphore
        0L);          // zero-second time-out interval


    switch (dwWaitResult) 
    {
        // The semaphore object was signaled.
        case WAIT_OBJECT_0: 
            	  		{

				incrementCounter();
				// Increment the count of the semaphore.

				if (!ReleaseSemaphore( 
				        hSemaphore,  // handle to semaphore
				        1,           // increase count by one
				        NULL) )      // not interested in previous count
				{
				    Fail ( "ReleaseSemaphore() returned NULL.  Failing test.\n"
		       			"GetLastError returned %d\n", GetLastError());    
				}

				break; 
    			        } 

        // Semaphore was nonsignaled, so a time-out occurred.
        case WAIT_TIMEOUT: 
				{
					Fail ( "Semaphore was nonsignaled, so a time-out occurred.  Failing test.\n"
		       			"GetLastError returned %d\n", GetLastError());  
					return FALSE;
        			}
    }

    return 1;
}