summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/threading/CreateMutexW_ReleaseMutex/test1/CreateMutexW.cpp
blob: c21bfb6a5055d8fe8a68d8e860bada96ef7a30f0 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// 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:  CreateMutexW_ReleaseMutex/test1/CreateMutexW.c
**
** Purpose: This test case tests whether a Mutex object created
**          with CreateMutex really works by mutually excluding
**          threads from accessing a data structure at the same
**          time.  Here we have a buffer that can be filled or
**          emptied, we use a Mutex object to ensure that one
**          operation cannot be started until the other is
**          finished.  If one operation detects that the other
**          has not finished, it fails.  There is a Producer
**          thread which will try to fill the buffer 25 times,
**          and a consumer thread which try to empty the buffer
**          25 times.  If either the fill or empty operations
**          fails because the Mutex failed to mutually exclude
**          them, the corresponding thread will set an error
**          flag and return.  This will cause the test case to
**          fail.
**
**          To increase the probability of identifying problems,
**          the Fill opeartion has been slowed dowm with a call
**          to Sleep.  This ensures that one operation will try
**          to access the shared buffer while the other is in
**          progress.
**
**          NOTE: this test case also serves as a test case for
**          WaitForSingleObject.
**
**
** Dependencies: CreateThread
**               ReleaseMutex
**               WaitForSingleObject
**               WaitForMultipleObjects
**               Sleep
**               memset
** 

**
**=========================================================*/

#define UNICODE
#include <palsuite.h>

/* Define some values that we will using many times */
#define MAIN_BUF_SIZE 40
#define NUM_OF_CYCLES 40

/* Buffer Operation return codes */
#define OP_OK   0
#define OP_ERR  1
#define OP_NONE 2


HANDLE hMutex;      /* handle to mutex */

BOOL bProdErr;      /* Producer error Flag */
BOOL bConErr;       /* Consumer error Flag */

/* Test Buffer */
char Buffer[MAIN_BUF_SIZE];

/*
 *  EmptyBuffer implements the empty operation for test buffer.
 */
int
EmptyBuffer()
{
    int i;

   if ( WaitForSingleObject(hMutex, INFINITE) == WAIT_FAILED)
   {
        Fail("ERROR: WaitForSingleObject failed.\n");
   }

    /* Check to see if the buffer is already completely empty */
    for (i=0; i<MAIN_BUF_SIZE && Buffer[i] == 0; i++);
    if (i == MAIN_BUF_SIZE)
    {
        /* Its empty so just return */
        if (ReleaseMutex(hMutex) == FALSE)
        {
            Fail("ERROR: ReleaseMutex Failed.\n");
        }
        return OP_NONE;
    }

    /* Its not empty so we must empty it. */
    for (i=0; i<MAIN_BUF_SIZE; i++)
    {
        /* Check for empty slots if we find one then the */
        /* fill operation did no finish.  return an error */
        if (Buffer[i] == 0)
        {
            if (ReleaseMutex(hMutex) == FALSE)
            {
                Fail("ERROR: ReleaseMutex Failed.\n");
            }
            return OP_ERR;
        }

        Buffer[i] = 0;
    }

    if (ReleaseMutex(hMutex) == FALSE)
    {
        Fail("ERROR: ReleaseMutex Failed.\n");
    }
    return OP_OK;
}

/*
 *  FillBuffer implements the fill operation for test buffer.
 */
int
FillBuffer()
{
    int i;

   if ( WaitForSingleObject(hMutex, INFINITE) == WAIT_FAILED)
   {
        Fail("ERROR: WaitForSingleObject failed.\n");
   }

    /* Check to see if the buffer is already completely full */
    for (i=0; i<MAIN_BUF_SIZE && Buffer[i] != 0; i++);
    if (i == MAIN_BUF_SIZE)
    {
        /* Its full so just return */
        if (ReleaseMutex(hMutex) == FALSE)
        {
            Fail("ERROR: ReleaseMutex Failed.\n");
        }
        return OP_NONE;
    }

    /* Its not full so we must fill it. */
    for (i=0; i<MAIN_BUF_SIZE; i++)
    {
        /* Check for filled slots if we find one then the */
        /* empty operation did not finish.  return an error */
        if (Buffer[i] == 1)
        {
            if (ReleaseMutex(hMutex) == FALSE)
            {
                Fail("ERROR: ReleaseMutex Failed.\n");
            }
            return OP_ERR;
        }

        Buffer[i] = 1;
        Sleep(10);
    }

    if (ReleaseMutex(hMutex) == FALSE)
    {
        Fail("ERROR: ReleaseMutex Failed.\n");
    }
    return OP_OK;
}




/* 
 * Producer thread function.  
 */
DWORD 
Producer(LPVOID lpParam)
{
    int n = 0;
    int ret;

    while (n < NUM_OF_CYCLES)
    {
        if (bConErr == TRUE)
        {
            /* The consumer ran into an error so we'll stop */
            return 0;
        }

        ret = FillBuffer();

        if (ret == OP_OK)
        {
            n++;
        }
        else if (ret == OP_ERR)
        {
            bProdErr = TRUE;
            return 0;
        }
    }

    return 0;
}

/* 
 * Consumer thread function. 
 */
DWORD Consumer( LPVOID lpParam )
{
    int n = 0;
    int ret;

    while (n < NUM_OF_CYCLES)
    {
        if (bProdErr == TRUE)
        {
            /* The consumer ran into an error so we'll stop */
            return 0;
        }

        ret = EmptyBuffer();

        if (ret == OP_OK)
        {
            n++;
        }
        else if (ret == OP_ERR)
        {
            bConErr = TRUE;
            return 0;
        }
    }

    return 0;
}


int __cdecl main (int argc, char **argv) 
{
    DWORD dwThreadId;
    DWORD dwWaitRet;
    
    HANDLE hThread1;    /* handle to consumer thread */
    HANDLE hThread2;    /* handle to producer thread */
    HANDLE handleArray[2];   

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

    /* Initialize our error flags */
    bProdErr = FALSE;
    bConErr = FALSE;

    /* 
     * Initialize the Buffer to be empty
     */
    memset(Buffer, 0, MAIN_BUF_SIZE);

    /*
     * Create Mutex
     */
    hMutex = CreateMutexW (NULL, FALSE, NULL);         

    if (NULL == hMutex)
    {
        Fail("hMutex = CreateMutexW() - returned NULL\n"
             "Failing Test.\nGetLastError returned %u\n", GetLastError());
    }


    /*
     * Create the Producer thread
     */
    hThread1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)Producer,
                            0, 0, &dwThreadId);

    if ( NULL == hThread1 )
    {
        CloseHandle(hMutex);
            
        Fail("CreateThread() returned NULL.  Failing test.\n"
             "GetLastError returned %u\n", GetLastError());
    }

    /* 
     * Create the Consumer thread
     */
    hThread2 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)Consumer,
                            0, 0, &dwThreadId);

    if ( NULL == hThread2 )
    {
        CloseHandle(hMutex);
        
        /* Set the error flag and give thread1 some time to exit */
        bConErr = FALSE;
        Sleep(250);
            
        Fail("CreateThread() returned NULL.  Failing test.\n"
             "GetLastError returned %u\n", GetLastError());
    }

    /*
     * Wait for both threads to complete (Max 45 Seconds)
     */
    handleArray[0] = hThread1;
    handleArray[1] = hThread2;
    dwWaitRet = WaitForMultipleObjects (2, handleArray, TRUE, 450000);
    if (dwWaitRet == WAIT_FAILED)
    {
        Fail("ERROR: WaitForMultipleObjects failed.\n");
    }
    else if (dwWaitRet == WAIT_TIMEOUT)
    {
        /* Set the error flags and give the threads some time to exit */
        bProdErr = FALSE;
        bConErr = FALSE;
        Sleep(250);
        
        Fail("ERROR: Timeout interval exceeded.\n");
    }

    /*
     * Clean up
     */
    if (CloseHandle(hThread1) == FALSE ||
        CloseHandle(hThread2) == FALSE ||
        CloseHandle(hMutex) == FALSE)
    {
        Fail("ERROR: CloseHandle failed.\n");
    }


    /*
     * Check our error flags
     */
    if (bProdErr == TRUE || bConErr == TRUE)
    {
        Fail("ERROR: A collision occurred, so the mutex failed.\n");
    }

    PAL_Terminate();
    return ( PASS );

}