2013-11-15 14:38:59 +04:00
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/synch.h>
|
2013-11-15 14:43:01 +04:00
|
|
|
#include <winpr/thread.h>
|
2013-11-15 14:38:59 +04:00
|
|
|
|
2018-03-07 14:03:10 +03:00
|
|
|
static DWORD WINAPI test_thread(LPVOID arg)
|
2013-11-15 14:38:59 +04:00
|
|
|
{
|
|
|
|
Sleep(1000);
|
|
|
|
ExitThread(0);
|
2018-03-07 14:03:10 +03:00
|
|
|
return 0;
|
2013-11-15 14:38:59 +04:00
|
|
|
}
|
|
|
|
|
2014-07-16 13:58:45 +04:00
|
|
|
int TestSynchThread(int argc, char *argv[])
|
2013-11-15 14:38:59 +04:00
|
|
|
{
|
|
|
|
DWORD rc;
|
|
|
|
HANDLE thread;
|
2018-03-07 14:03:10 +03:00
|
|
|
thread = CreateThread(NULL, 0, test_thread, NULL, 0, NULL);
|
2014-07-16 13:58:45 +04:00
|
|
|
|
2013-11-15 14:38:59 +04:00
|
|
|
if (!thread)
|
|
|
|
{
|
|
|
|
printf("CreateThread failure\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TryJoin should now fail. */
|
|
|
|
rc = WaitForSingleObject(thread, 0);
|
2014-07-16 13:58:45 +04:00
|
|
|
|
2013-11-15 14:38:59 +04:00
|
|
|
if (WAIT_TIMEOUT != rc)
|
|
|
|
{
|
2016-12-14 00:47:08 +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);
|
2014-07-16 13:58:45 +04:00
|
|
|
|
2013-11-15 14:38:59 +04:00
|
|
|
if (WAIT_OBJECT_0 != rc)
|
|
|
|
{
|
2016-12-14 00:47:08 +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);
|
2014-07-16 13:58:45 +04:00
|
|
|
|
2013-11-15 14:38:59 +04:00
|
|
|
if (WAIT_OBJECT_0 != rc)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("Timed WaitForSingleObject on dead thread failed with %"PRIu32"\n", rc);
|
2013-11-15 14:38:59 +04:00
|
|
|
return -5;
|
|
|
|
}
|
|
|
|
|
2014-07-16 13:58:45 +04:00
|
|
|
if (!CloseHandle(thread))
|
|
|
|
{
|
|
|
|
printf("CloseHandle failed!");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-03-07 14:03:10 +03:00
|
|
|
thread = CreateThread(NULL, 0, test_thread, NULL, 0, NULL);
|
2014-07-16 13:58:45 +04:00
|
|
|
|
|
|
|
if (!thread)
|
|
|
|
{
|
|
|
|
printf("CreateThread failure\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TryJoin should now fail. */
|
|
|
|
rc = WaitForSingleObject(thread, 50);
|
|
|
|
|
|
|
|
if (WAIT_TIMEOUT != rc)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("Timed WaitForSingleObject on running thread failed with %"PRIu32"\n", rc);
|
2014-07-16 13:58:45 +04:00
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Join the thread */
|
|
|
|
rc = WaitForSingleObject(thread, INFINITE);
|
|
|
|
|
|
|
|
if (WAIT_OBJECT_0 != rc)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("WaitForSingleObject on thread failed with %"PRIu32"\n", rc);
|
2014-07-16 13:58:45 +04:00
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TimedJoin should now succeed. */
|
|
|
|
rc = WaitForSingleObject(thread, 0);
|
|
|
|
|
|
|
|
if (WAIT_OBJECT_0 != rc)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("Timed WaitForSingleObject on dead thread failed with %"PRIu32"\n", rc);
|
2014-07-16 13:58:45 +04:00
|
|
|
return -5;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CloseHandle(thread))
|
|
|
|
{
|
|
|
|
printf("CloseHandle failed!");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-07-16 14:03:46 +04:00
|
|
|
/* Thread detach test */
|
2018-03-07 14:03:10 +03:00
|
|
|
thread = CreateThread(NULL, 0, test_thread, NULL, 0, NULL);
|
2014-07-16 13:58:45 +04:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|