summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/threading/WaitForMultipleObjectsEx/test3/test3.c
blob: b78b0540dcfbe3dcf86d6693ab421078945f38a7 (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
// 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:  test3.c
**
** Purpose: Tests that waiting on an open mutex will a return 
**          WAIT_OBJECT_0.  Does this by creating a child thread that 
**          acquires the mutex, releases it, and exits.
**
**
**===================================================================*/

#include <palsuite.h>


const int ChildThreadWaitTime = 1000;
const int ParentDelayTime = 2000; 

DWORD PALAPI AcquiringProc(LPVOID lpParameter);

int __cdecl main( int argc, char **argv) 
{
    HANDLE Mutex;
    HANDLE hThread = 0;
    DWORD dwThreadId = 0;
    int ret;

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

    Mutex = CreateMutexW(NULL, FALSE, NULL);
    if (Mutex == NULL)
    {
        Fail("Unable to create the mutex.  GetLastError returned %d\n", 
            GetLastError());
    }

    hThread = CreateThread( NULL, 
                            0, 
                            (LPTHREAD_START_ROUTINE)AcquiringProc,
                            (LPVOID) Mutex,
                            0,
                            &dwThreadId);

    if (hThread == NULL)
    {
        Fail("ERROR: Was not able to create the thread to test!\n"
            "GetLastError returned %d\n", GetLastError());
    }

    Sleep(ParentDelayTime);

    ret = WaitForMultipleObjectsEx(1, &Mutex, FALSE, INFINITE, FALSE);
    if (ret != WAIT_OBJECT_0)
    {
        Fail("Expected WaitForMultipleObjectsEx to return WAIT_OBJECT_0\n"
            "Got %d\n", ret);
    }

    if (!CloseHandle(Mutex))
    {
        Fail("CloseHandle on the mutex failed!\n");
    }

    if (!CloseHandle(hThread))
    {
        Fail("CloseHandle on the thread failed!\n");
    }

    PAL_Terminate();
    return PASS;
}

/* 
 * Entry Point for child thread. Acquries a mutex, releases it, and exits.
 */
DWORD PALAPI AcquiringProc(LPVOID lpParameter)
{
    HANDLE Mutex;
    DWORD ret;

    Mutex = (HANDLE) lpParameter;
    
    Sleep(ChildThreadWaitTime);

    ret = WaitForSingleObject(Mutex, 0);
    if (ret != WAIT_OBJECT_0)
    {
        Fail("Expected the WaitForSingleObject call on the mutex to succeed\n"
            "Expected return of WAIT_OBJECT_0, got %d\n", ret);
    }

    ret = ReleaseMutex(Mutex);
    if (!ret)
    {
        Fail("Unable to release mutex!  GetLastError returned %d\n", 
            GetLastError());
    }

    return 0;
}