2013-07-29 21:07:39 +04:00
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/synch.h>
|
2016-05-24 14:35:11 +03:00
|
|
|
#include <winpr/thread.h>
|
2013-07-29 21:07:39 +04:00
|
|
|
|
2016-05-24 14:35:11 +03:00
|
|
|
|
|
|
|
BOOL test_mutex_basic()
|
2013-07-29 21:07:39 +04:00
|
|
|
{
|
|
|
|
HANDLE mutex;
|
2016-05-24 14:35:11 +03:00
|
|
|
DWORD rc;
|
2013-07-29 21:07:39 +04:00
|
|
|
|
2016-05-24 14:35:11 +03:00
|
|
|
if (!(mutex = CreateMutex(NULL, FALSE, NULL)))
|
2013-07-29 21:07:39 +04:00
|
|
|
{
|
2016-05-24 14:35:11 +03:00
|
|
|
printf("%s: CreateMutex failed\n", __FUNCTION__);
|
|
|
|
return FALSE;
|
2013-07-29 21:07:39 +04:00
|
|
|
}
|
|
|
|
|
2013-11-15 11:38:59 +04:00
|
|
|
rc = WaitForSingleObject(mutex, INFINITE);
|
2016-05-24 14:35:11 +03:00
|
|
|
if (rc != WAIT_OBJECT_0)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("%s: WaitForSingleObject on mutex failed with %"PRIu32"\n", __FUNCTION__, rc);
|
2016-05-24 14:35:11 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ReleaseMutex(mutex))
|
|
|
|
{
|
|
|
|
printf("%s: ReleaseMutex failed\n", __FUNCTION__);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ReleaseMutex(mutex))
|
|
|
|
{
|
|
|
|
printf("%s: ReleaseMutex unexpectedly succeeded on released mutex\n", __FUNCTION__);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CloseHandle(mutex))
|
|
|
|
{
|
|
|
|
printf("%s: CloseHandle on mutex failed\n", __FUNCTION__);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL test_mutex_recursive()
|
|
|
|
{
|
|
|
|
HANDLE mutex;
|
|
|
|
DWORD rc, i, cnt = 50;
|
|
|
|
|
|
|
|
if (!(mutex = CreateMutex(NULL, TRUE, NULL)))
|
|
|
|
{
|
|
|
|
printf("%s: CreateMutex failed\n", __FUNCTION__);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < cnt; i++)
|
|
|
|
{
|
|
|
|
rc = WaitForSingleObject(mutex, INFINITE);
|
|
|
|
if (rc != WAIT_OBJECT_0)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("%s: WaitForSingleObject #%"PRIu32" on mutex failed with %"PRIu32"\n", __FUNCTION__, i, rc);
|
2016-05-24 14:35:11 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < cnt; i++)
|
2013-11-15 11:38:59 +04:00
|
|
|
{
|
2016-05-24 14:35:11 +03:00
|
|
|
if (!ReleaseMutex(mutex))
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("%s: ReleaseMutex #%"PRIu32" failed\n", __FUNCTION__, i);
|
2016-05-24 14:35:11 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
2013-11-15 11:38:59 +04:00
|
|
|
}
|
|
|
|
|
2016-05-24 14:35:11 +03:00
|
|
|
if (!ReleaseMutex(mutex))
|
2013-11-15 11:38:59 +04:00
|
|
|
{
|
2016-05-24 14:35:11 +03:00
|
|
|
/* Note: The mutex was initially owned ! */
|
|
|
|
printf("%s: Final ReleaseMutex failed\n", __FUNCTION__);
|
|
|
|
return FALSE;
|
2013-11-15 11:38:59 +04:00
|
|
|
}
|
|
|
|
|
2016-05-24 14:35:11 +03:00
|
|
|
if (ReleaseMutex(mutex))
|
2013-11-15 11:38:59 +04:00
|
|
|
{
|
2016-05-24 14:35:11 +03:00
|
|
|
printf("%s: ReleaseMutex unexpectedly succeeded on released mutex\n", __FUNCTION__);
|
|
|
|
return FALSE;
|
2013-11-15 11:38:59 +04:00
|
|
|
}
|
|
|
|
|
2016-05-24 14:35:11 +03:00
|
|
|
if (!CloseHandle(mutex))
|
2013-11-15 11:38:59 +04:00
|
|
|
{
|
2016-05-24 14:35:11 +03:00
|
|
|
printf("%s: CloseHandle on mutex failed\n", __FUNCTION__);
|
|
|
|
return FALSE;
|
2013-11-15 11:38:59 +04:00
|
|
|
}
|
2016-05-24 14:35:11 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
2013-11-15 11:38:59 +04:00
|
|
|
|
2016-05-24 14:35:11 +03:00
|
|
|
|
|
|
|
HANDLE thread1_mutex1 = NULL;
|
|
|
|
HANDLE thread1_mutex2 = NULL;
|
|
|
|
BOOL thread1_failed = TRUE;
|
|
|
|
|
|
|
|
DWORD WINAPI test_mutex_thread1(LPVOID lpParam)
|
|
|
|
{
|
|
|
|
HANDLE hStartEvent = (HANDLE)lpParam;
|
|
|
|
DWORD rc = 0;
|
|
|
|
if (WaitForSingleObject(hStartEvent, INFINITE) != WAIT_OBJECT_0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: failed to wait for start event\n", __FUNCTION__);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* at this point:
|
|
|
|
* thread1_mutex1 is expected to be locked
|
|
|
|
* thread1_mutex2 is expected to be unlocked
|
|
|
|
* defined task:
|
|
|
|
* try to lock thread1_mutex1 (expected to fail)
|
|
|
|
* lock and unlock thread1_mutex2 (expected to work)
|
|
|
|
*/
|
|
|
|
|
|
|
|
rc = WaitForSingleObject(thread1_mutex1, 10);
|
|
|
|
if (rc != WAIT_TIMEOUT)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
fprintf(stderr, "%s: WaitForSingleObject on thread1_mutex1 unexpectedly returned %"PRIu32" instead of WAIT_TIMEOUT (%u)\n",
|
2016-05-24 14:35:11 +03:00
|
|
|
__FUNCTION__, rc, WAIT_TIMEOUT);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = WaitForSingleObject(thread1_mutex2, 10);
|
|
|
|
if (rc != WAIT_OBJECT_0)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
fprintf(stderr, "%s: WaitForSingleObject on thread1_mutex2 unexpectedly returned %"PRIu32" instead of WAIT_OBJECT_0\n",
|
2016-05-24 14:35:11 +03:00
|
|
|
__FUNCTION__, rc);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ReleaseMutex(thread1_mutex2)) {
|
|
|
|
fprintf(stderr, "%s: ReleaseMutex failed on thread1_mutex2\n", __FUNCTION__);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
thread1_failed = FALSE;
|
2013-07-29 21:07:39 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-24 14:35:11 +03:00
|
|
|
BOOL test_mutex_threading()
|
|
|
|
{
|
|
|
|
HANDLE hThread = NULL;
|
|
|
|
HANDLE hStartEvent = NULL;
|
|
|
|
|
|
|
|
if (!(thread1_mutex1 = CreateMutex(NULL, TRUE, NULL)))
|
|
|
|
{
|
|
|
|
printf("%s: CreateMutex thread1_mutex1 failed\n", __FUNCTION__);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(thread1_mutex2 = CreateMutex(NULL, FALSE, NULL)))
|
|
|
|
{
|
|
|
|
printf("%s: CreateMutex thread1_mutex2 failed\n", __FUNCTION__);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(hStartEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: error creating start event\n", __FUNCTION__);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
thread1_failed = TRUE;
|
|
|
|
|
|
|
|
if (!(hThread = CreateThread(NULL, 0, test_mutex_thread1, (LPVOID)hStartEvent, 0, NULL)))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: error creating test_mutex_thread_1\n", __FUNCTION__);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
Sleep(100);
|
|
|
|
|
|
|
|
if (!thread1_failed)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: thread1 premature success\n", __FUNCTION__);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetEvent(hStartEvent);
|
|
|
|
|
|
|
|
if (WaitForSingleObject(hThread, 2000) != WAIT_OBJECT_0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: thread1 premature success\n", __FUNCTION__);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (thread1_failed)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: thread1 has not reported success\n", __FUNCTION__);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* - thread1 must not have succeeded to lock thread1_mutex1
|
|
|
|
* - thread1 must have locked and unlocked thread1_mutex2
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!ReleaseMutex(thread1_mutex1))
|
|
|
|
{
|
|
|
|
printf("%s: ReleaseMutex unexpectedly failed on thread1_mutex1\n", __FUNCTION__);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ReleaseMutex(thread1_mutex2))
|
|
|
|
{
|
|
|
|
printf("%s: ReleaseMutex unexpectedly succeeded on thread1_mutex2\n", __FUNCTION__);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseHandle(hThread);
|
|
|
|
CloseHandle(hStartEvent);
|
|
|
|
CloseHandle(thread1_mutex1);
|
|
|
|
CloseHandle(thread1_mutex2);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
ReleaseMutex(thread1_mutex1);
|
|
|
|
ReleaseMutex(thread1_mutex2);
|
|
|
|
CloseHandle(thread1_mutex1);
|
|
|
|
CloseHandle(thread1_mutex2);
|
|
|
|
CloseHandle(hStartEvent);
|
|
|
|
CloseHandle(hThread);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TestSynchMutex(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
if (!test_mutex_basic())
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (!test_mutex_recursive())
|
|
|
|
return 2;
|
|
|
|
|
|
|
|
if (!test_mutex_threading())
|
|
|
|
return 3;
|
|
|
|
|
|
|
|
printf("TestSynchMutex succeeded\n");
|
|
|
|
return 0;
|
|
|
|
}
|