| 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: test4.c |
| 8 | ** |
| 9 | ** Purpose: Tests that waiting on an abandonded mutex will a return |
| 10 | ** WAIT_ABANDONED_0. Does this by creating a child thread that |
| 11 | ** acquires the mutex and exits. |
| 12 | ** |
| 13 | ** |
| 14 | **===================================================================*/ |
| 15 | |
| 16 | #include <palsuite.h> |
| 17 | |
| 18 | |
| 19 | const int ChildThreadWaitTime = 1000; |
| 20 | const int ParentDelayTime = 2000; |
| 21 | |
| 22 | DWORD PALAPI AbandoningProc(LPVOID lpParameter); |
| 23 | |
| 24 | int __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)AbandoningProc, |
| 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_ABANDONED_0) |
| 60 | { |
| 61 | Fail("Expected WaitForMultipleObjectsEx to return WAIT_ABANDONED_0\n" |
| 62 | "Got %d\n" , ret); |
| 63 | } |
| 64 | |
| 65 | ReleaseMutex(Mutex); |
| 66 | if (!CloseHandle(Mutex)) |
| 67 | { |
| 68 | Fail("CloseHandle on the mutex failed!\n" ); |
| 69 | } |
| 70 | |
| 71 | if (!CloseHandle(hThread)) |
| 72 | { |
| 73 | Fail("CloseHandle on the thread failed!\n" ); |
| 74 | } |
| 75 | |
| 76 | PAL_Terminate(); |
| 77 | return PASS; |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Entry Point for child thread. Acquries a mutex and exit's without |
| 82 | * releasing it. |
| 83 | */ |
| 84 | DWORD PALAPI AbandoningProc(LPVOID lpParameter) |
| 85 | { |
| 86 | HANDLE Mutex; |
| 87 | DWORD ret; |
| 88 | |
| 89 | Mutex = (HANDLE) lpParameter; |
| 90 | |
| 91 | Sleep(ChildThreadWaitTime); |
| 92 | |
| 93 | ret = WaitForSingleObject(Mutex, 0); |
| 94 | if (ret != WAIT_OBJECT_0) |
| 95 | { |
| 96 | Fail("Expected the WaitForSingleObject call on the mutex to succeed\n" |
| 97 | "Expected return of WAIT_OBJECT_0, got %d\n" , ret); |
| 98 | } |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |