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
|
|
|
{
|
2021-06-02 16:46:27 +03:00
|
|
|
WINPR_UNUSED(arg);
|
|
|
|
Sleep(100);
|
2013-11-15 14:38:59 +04:00
|
|
|
ExitThread(0);
|
2018-03-07 14:03:10 +03:00
|
|
|
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;
|
2022-08-02 12:40:55 +03:00
|
|
|
int i;
|
2021-06-02 16:46:27 +03:00
|
|
|
|
|
|
|
WINPR_UNUSED(argc);
|
|
|
|
WINPR_UNUSED(argv);
|
|
|
|
|
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)
|
|
|
|
{
|
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);
|
2014-07-16 13:58:45 +04:00
|
|
|
|
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);
|
2014-07-16 13:58:45 +04:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-08-02 12:40:55 +03:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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. */
|
2021-06-02 16:46:27 +03:00
|
|
|
rc = WaitForSingleObject(thread, 10);
|
2014-07-16 13:58:45 +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);
|
2014-07-16 13:58:45 +04:00
|
|
|
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);
|
2014-07-16 13:58:45 +04:00
|
|
|
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);
|
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;
|
|
|
|
}
|