1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5/*=====================================================================
6**
7** Source: test3.c
8**
9** Purpose: Tests that waiting on an open mutex will a return
10** WAIT_OBJECT_0. Does this by creating a child thread that
11** acquires the mutex, releases it, and exits.
12**
13**
14**===================================================================*/
15
16#include <palsuite.h>
17
18
19const int ChildThreadWaitTime = 1000;
20const int ParentDelayTime = 2000;
21
22DWORD PALAPI AcquiringProc(LPVOID lpParameter);
23
24int __cdecl main( int argc, char **argv)
25{
26 HANDLE Mutex;
27 HANDLE hThread = 0;
28 DWORD dwThreadId = 0;
29 int ret;
30
31 if (0 != (PAL_Initialize(argc, argv)))
32 {
33 return FAIL;
34 }
35
36 Mutex = CreateMutexW(NULL, FALSE, NULL);
37 if (Mutex == NULL)
38 {
39 Fail("Unable to create the mutex. GetLastError returned %d\n",
40 GetLastError());
41 }
42
43 hThread = CreateThread( NULL,
44 0,
45 (LPTHREAD_START_ROUTINE)AcquiringProc,
46 (LPVOID) Mutex,
47 0,
48 &dwThreadId);
49
50 if (hThread == NULL)
51 {
52 Fail("ERROR: Was not able to create the thread to test!\n"
53 "GetLastError returned %d\n", GetLastError());
54 }
55
56 Sleep(ParentDelayTime);
57
58 ret = WaitForMultipleObjectsEx(1, &Mutex, FALSE, INFINITE, FALSE);
59 if (ret != WAIT_OBJECT_0)
60 {
61 Fail("Expected WaitForMultipleObjectsEx to return WAIT_OBJECT_0\n"
62 "Got %d\n", ret);
63 }
64
65 if (!CloseHandle(Mutex))
66 {
67 Fail("CloseHandle on the mutex failed!\n");
68 }
69
70 if (!CloseHandle(hThread))
71 {
72 Fail("CloseHandle on the thread failed!\n");
73 }
74
75 PAL_Terminate();
76 return PASS;
77}
78
79/*
80 * Entry Point for child thread. Acquries a mutex, releases it, and exits.
81 */
82DWORD PALAPI AcquiringProc(LPVOID lpParameter)
83{
84 HANDLE Mutex;
85 DWORD ret;
86
87 Mutex = (HANDLE) lpParameter;
88
89 Sleep(ChildThreadWaitTime);
90
91 ret = WaitForSingleObject(Mutex, 0);
92 if (ret != WAIT_OBJECT_0)
93 {
94 Fail("Expected the WaitForSingleObject call on the mutex to succeed\n"
95 "Expected return of WAIT_OBJECT_0, got %d\n", ret);
96 }
97
98 ret = ReleaseMutex(Mutex);
99 if (!ret)
100 {
101 Fail("Unable to release mutex! GetLastError returned %d\n",
102 GetLastError());
103 }
104
105 return 0;
106}
107