2013-01-21 06:15:55 +04:00
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/pool.h>
|
2013-01-23 02:24:57 +04:00
|
|
|
#include <winpr/interlocked.h>
|
2013-01-21 06:15:55 +04:00
|
|
|
|
2013-01-23 02:24:57 +04:00
|
|
|
static LONG count = 0;
|
2013-01-22 03:33:00 +04:00
|
|
|
|
2017-11-15 11:11:12 +03:00
|
|
|
static void CALLBACK test_WorkCallback(PTP_CALLBACK_INSTANCE instance, void* context, PTP_WORK work)
|
2013-01-21 06:15:55 +04:00
|
|
|
{
|
2013-01-23 01:19:32 +04:00
|
|
|
int index;
|
|
|
|
BYTE a[1024];
|
|
|
|
BYTE b[1024];
|
|
|
|
BYTE c[1024];
|
2019-11-06 17:24:51 +03:00
|
|
|
printf("Hello %s: %03" PRId32 " (thread: 0x%08" PRIX32 ")\n", (char*)context,
|
2017-11-15 11:11:12 +03:00
|
|
|
InterlockedIncrement(&count), GetCurrentThreadId());
|
2013-01-23 01:19:32 +04:00
|
|
|
|
|
|
|
for (index = 0; index < 100; index++)
|
|
|
|
{
|
|
|
|
ZeroMemory(a, 1024);
|
|
|
|
ZeroMemory(b, 1024);
|
|
|
|
ZeroMemory(c, 1024);
|
|
|
|
FillMemory(a, 1024, 0xAA);
|
|
|
|
FillMemory(b, 1024, 0xBB);
|
|
|
|
CopyMemory(c, a, 1024);
|
|
|
|
CopyMemory(c, b, 1024);
|
|
|
|
}
|
2013-01-21 06:15:55 +04:00
|
|
|
}
|
|
|
|
|
2017-11-15 11:11:12 +03:00
|
|
|
static BOOL test1(void)
|
2013-01-21 06:15:55 +04:00
|
|
|
{
|
2013-01-22 03:33:00 +04:00
|
|
|
int index;
|
2013-01-22 08:34:46 +04:00
|
|
|
PTP_WORK work;
|
|
|
|
printf("Global Thread Pool\n");
|
2018-03-07 14:42:17 +03:00
|
|
|
work = CreateThreadpoolWork(test_WorkCallback, "world", NULL);
|
2013-01-21 06:15:55 +04:00
|
|
|
|
|
|
|
if (!work)
|
|
|
|
{
|
|
|
|
printf("CreateThreadpoolWork failure\n");
|
2017-11-15 11:11:12 +03:00
|
|
|
return FALSE;
|
2013-01-21 06:15:55 +04:00
|
|
|
}
|
|
|
|
|
2013-01-22 03:33:00 +04:00
|
|
|
/**
|
2019-11-06 17:24:51 +03:00
|
|
|
* You can post a work object one or more times (up to MAXULONG) without waiting for prior
|
|
|
|
* callbacks to complete. The callbacks will execute in parallel. To improve efficiency, the
|
|
|
|
* thread pool may throttle the threads.
|
2013-01-22 03:33:00 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
for (index = 0; index < 10; index++)
|
|
|
|
SubmitThreadpoolWork(work);
|
|
|
|
|
2013-01-21 06:15:55 +04:00
|
|
|
WaitForThreadpoolWorkCallbacks(work, FALSE);
|
|
|
|
CloseThreadpoolWork(work);
|
2017-11-15 11:11:12 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
2013-01-21 06:15:55 +04:00
|
|
|
|
2017-11-15 11:11:12 +03:00
|
|
|
static BOOL test2(void)
|
|
|
|
{
|
|
|
|
BOOL rc = FALSE;
|
|
|
|
int index;
|
|
|
|
PTP_POOL pool;
|
|
|
|
PTP_WORK work;
|
2017-12-21 11:34:35 +03:00
|
|
|
PTP_CLEANUP_GROUP cleanupGroup = NULL;
|
2017-11-15 11:11:12 +03:00
|
|
|
TP_CALLBACK_ENVIRON environment;
|
2013-01-22 08:34:46 +04:00
|
|
|
printf("Private Thread Pool\n");
|
|
|
|
|
2015-04-28 18:00:41 +03:00
|
|
|
if (!(pool = CreateThreadpool(NULL)))
|
|
|
|
{
|
|
|
|
printf("CreateThreadpool failure\n");
|
2017-11-15 11:11:12 +03:00
|
|
|
return FALSE;
|
2015-04-28 18:00:41 +03:00
|
|
|
}
|
2013-01-22 08:34:46 +04:00
|
|
|
|
2015-05-05 14:55:48 +03:00
|
|
|
if (!SetThreadpoolThreadMinimum(pool, 4))
|
|
|
|
{
|
|
|
|
printf("SetThreadpoolThreadMinimum failure\n");
|
2017-11-15 11:11:12 +03:00
|
|
|
goto fail;
|
2015-05-05 14:55:48 +03:00
|
|
|
}
|
|
|
|
|
2013-01-22 08:34:46 +04:00
|
|
|
SetThreadpoolThreadMaximum(pool, 8);
|
|
|
|
InitializeThreadpoolEnvironment(&environment);
|
|
|
|
SetThreadpoolCallbackPool(&environment, pool);
|
|
|
|
cleanupGroup = CreateThreadpoolCleanupGroup();
|
|
|
|
|
|
|
|
if (!cleanupGroup)
|
|
|
|
{
|
|
|
|
printf("CreateThreadpoolCleanupGroup failure\n");
|
2017-11-15 11:11:12 +03:00
|
|
|
goto fail;
|
2013-01-22 08:34:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
SetThreadpoolCallbackCleanupGroup(&environment, cleanupGroup, NULL);
|
2018-03-07 14:42:17 +03:00
|
|
|
work = CreateThreadpoolWork(test_WorkCallback, "world", &environment);
|
2013-01-22 08:34:46 +04:00
|
|
|
|
|
|
|
if (!work)
|
|
|
|
{
|
|
|
|
printf("CreateThreadpoolWork failure\n");
|
2017-11-15 11:11:12 +03:00
|
|
|
goto fail;
|
2013-01-22 08:34:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (index = 0; index < 10; index++)
|
|
|
|
SubmitThreadpoolWork(work);
|
|
|
|
|
|
|
|
WaitForThreadpoolWorkCallbacks(work, FALSE);
|
2017-11-15 11:11:12 +03:00
|
|
|
rc = TRUE;
|
|
|
|
fail:
|
2013-01-22 08:34:46 +04:00
|
|
|
|
2017-11-15 11:11:12 +03:00
|
|
|
if (cleanupGroup)
|
|
|
|
{
|
|
|
|
CloseThreadpoolCleanupGroupMembers(cleanupGroup, TRUE, NULL);
|
|
|
|
CloseThreadpoolCleanupGroup(cleanupGroup);
|
|
|
|
DestroyThreadpoolEnvironment(&environment);
|
|
|
|
/**
|
2019-11-06 17:24:51 +03:00
|
|
|
* See Remarks at
|
|
|
|
* https://msdn.microsoft.com/en-us/library/windows/desktop/ms682043(v=vs.85).aspx If there
|
|
|
|
* is a cleanup group associated with the work object, it is not necessary to call
|
|
|
|
* CloseThreadpoolWork ! calling the CloseThreadpoolCleanupGroupMembers function releases
|
|
|
|
* the work, wait, and timer objects associated with the cleanup group.
|
2017-11-15 11:11:12 +03:00
|
|
|
*/
|
|
|
|
#if 0
|
|
|
|
CloseThreadpoolWork(work); // this would segfault, see comment above. */
|
|
|
|
#endif
|
|
|
|
}
|
2013-01-22 08:34:46 +04:00
|
|
|
|
2017-11-15 11:11:12 +03:00
|
|
|
CloseThreadpool(pool);
|
|
|
|
return rc;
|
|
|
|
}
|
2016-06-04 18:04:12 +03:00
|
|
|
|
2017-11-15 11:11:12 +03:00
|
|
|
int TestPoolWork(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
if (!test1())
|
|
|
|
return -1;
|
2016-06-04 18:04:12 +03:00
|
|
|
|
2017-11-15 11:11:12 +03:00
|
|
|
if (!test2())
|
|
|
|
return -1;
|
2013-01-22 08:34:46 +04:00
|
|
|
|
2013-01-21 06:15:55 +04:00
|
|
|
return 0;
|
|
|
|
}
|