2013-07-29 21:07:39 +04:00
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/synch.h>
|
|
|
|
|
|
|
|
int TestSynchMutex(int argc, char* argv[])
|
|
|
|
{
|
2013-11-15 11:38:59 +04:00
|
|
|
DWORD rc;
|
2013-07-29 21:07:39 +04:00
|
|
|
HANDLE mutex;
|
|
|
|
|
|
|
|
mutex = CreateMutex(NULL, FALSE, NULL);
|
|
|
|
if (!mutex)
|
|
|
|
{
|
|
|
|
printf("CreateMutex failure\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-11-15 11:38:59 +04:00
|
|
|
/* Lock the mutex */
|
|
|
|
rc = WaitForSingleObject(mutex, INFINITE);
|
|
|
|
if (WAIT_OBJECT_0 != rc)
|
|
|
|
{
|
|
|
|
printf("WaitForSingleObject on mutex failed with %d\n", rc);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TryLock should now fail. */
|
|
|
|
rc = WaitForSingleObject(mutex, 0);
|
|
|
|
if (WAIT_TIMEOUT != rc)
|
|
|
|
{
|
|
|
|
printf("Timed WaitForSingleObject on locked mutex failed with %d\n", rc);
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unlock the mutex */
|
|
|
|
rc = ReleaseMutex(mutex);
|
|
|
|
if (!rc)
|
|
|
|
{
|
|
|
|
printf("ReleaseMutex failed.\n");
|
|
|
|
return -4;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TryLock should now succeed. */
|
|
|
|
rc = WaitForSingleObject(mutex, 0);
|
|
|
|
if (WAIT_OBJECT_0 != rc)
|
|
|
|
{
|
|
|
|
printf("Timed WaitForSingleObject on free mutex failed with %d\n", rc);
|
|
|
|
return -5;
|
|
|
|
}
|
|
|
|
|
2013-07-29 21:07:39 +04:00
|
|
|
CloseHandle(mutex);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|