FreeRDP/winpr/libwinpr/synch/test/TestSynchThread.c

133 lines
2.4 KiB
C
Raw Normal View History

2013-11-15 14:38:59 +04:00
#include <winpr/crt.h>
#include <winpr/synch.h>
#include <winpr/thread.h>
2013-11-15 14:38:59 +04:00
static DWORD WINAPI test_thread(LPVOID arg)
2013-11-15 14:38:59 +04:00
{
2021-06-02 16:46:27 +03:00
WINPR_UNUSED(arg);
Sleep(100);
2013-11-15 14:38:59 +04:00
ExitThread(0);
return 0;
2013-11-15 14:38:59 +04:00
}
2019-11-06 17:24:51 +03:00
int TestSynchThread(int argc, char* argv[])
2013-11-15 14:38:59 +04:00
{
DWORD rc;
HANDLE thread;
int i;
2021-06-02 16:46:27 +03:00
WINPR_UNUSED(argc);
WINPR_UNUSED(argv);
thread = CreateThread(NULL, 0, test_thread, NULL, 0, NULL);
2013-11-15 14:38:59 +04:00
if (!thread)
{
printf("CreateThread failure\n");
return -1;
}
/* TryJoin should now fail. */
rc = WaitForSingleObject(thread, 0);
2013-11-15 14:38:59 +04:00
if (WAIT_TIMEOUT != rc)
{
2019-11-06 17:24:51 +03:00
printf("Timed WaitForSingleObject on running thread failed with %" PRIu32 "\n", rc);
2013-11-15 14:38:59 +04:00
return -3;
}
/* Join the thread */
rc = WaitForSingleObject(thread, INFINITE);
2013-11-15 14:38:59 +04:00
if (WAIT_OBJECT_0 != rc)
{
2019-11-06 17:24:51 +03:00
printf("WaitForSingleObject on thread failed with %" PRIu32 "\n", rc);
2013-11-15 14:38:59 +04:00
return -2;
}
/* TimedJoin should now succeed. */
rc = WaitForSingleObject(thread, 0);
2013-11-15 14:38:59 +04:00
if (WAIT_OBJECT_0 != rc)
{
2019-11-06 17:24:51 +03:00
printf("Timed WaitForSingleObject on dead thread failed with %" PRIu32 "\n", rc);
2013-11-15 14:38:59 +04:00
return -5;
}
/* check that WaitForSingleObject works multiple times on a terminated thread */
for (i = 0; i < 4; i++)
{
rc = WaitForSingleObject(thread, 0);
if (WAIT_OBJECT_0 != rc)
{
printf("Timed WaitForSingleObject on dead thread failed with %" PRIu32 "\n", rc);
return -6;
}
}
if (!CloseHandle(thread))
{
printf("CloseHandle failed!");
return -1;
}
thread = CreateThread(NULL, 0, test_thread, NULL, 0, NULL);
if (!thread)
{
printf("CreateThread failure\n");
return -1;
}
/* TryJoin should now fail. */
2021-06-02 16:46:27 +03:00
rc = WaitForSingleObject(thread, 10);
if (WAIT_TIMEOUT != rc)
{
2019-11-06 17:24:51 +03:00
printf("Timed WaitForSingleObject on running thread failed with %" PRIu32 "\n", rc);
return -3;
}
/* Join the thread */
rc = WaitForSingleObject(thread, INFINITE);
if (WAIT_OBJECT_0 != rc)
{
2019-11-06 17:24:51 +03:00
printf("WaitForSingleObject on thread failed with %" PRIu32 "\n", rc);
return -2;
}
/* TimedJoin should now succeed. */
rc = WaitForSingleObject(thread, 0);
if (WAIT_OBJECT_0 != rc)
{
2019-11-06 17:24:51 +03:00
printf("Timed WaitForSingleObject on dead thread failed with %" PRIu32 "\n", rc);
return -5;
}
if (!CloseHandle(thread))
{
printf("CloseHandle failed!");
return -1;
}
2014-07-16 14:03:46 +04:00
/* Thread detach test */
thread = CreateThread(NULL, 0, test_thread, NULL, 0, NULL);
if (!thread)
{
printf("CreateThread failure\n");
return -1;
}
if (!CloseHandle(thread))
{
printf("CloseHandle failed!");
return -1;
}
2013-11-15 14:38:59 +04:00
return 0;
}