2012-12-22 03:30:33 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Thread Pool API (Work)
|
|
|
|
*
|
|
|
|
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/pool.h>
|
2016-02-06 00:28:45 +03:00
|
|
|
#include <winpr/library.h>
|
2012-12-22 03:30:33 +04:00
|
|
|
|
2013-01-22 03:33:00 +04:00
|
|
|
#include "pool.h"
|
2014-08-18 19:22:22 +04:00
|
|
|
#include "../log.h"
|
2014-08-18 20:57:08 +04:00
|
|
|
#define TAG WINPR_TAG("pool")
|
2013-01-22 03:33:00 +04:00
|
|
|
|
2016-06-04 18:04:12 +03:00
|
|
|
#ifdef WINPR_THREAD_POOL
|
2013-01-21 07:39:32 +04:00
|
|
|
|
2016-06-04 18:04:12 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
static INIT_ONCE init_once_module = INIT_ONCE_STATIC_INIT;
|
2017-11-15 11:11:12 +03:00
|
|
|
static PTP_WORK(WINAPI* pCreateThreadpoolWork)(PTP_WORK_CALLBACK pfnwk, PVOID pv,
|
2019-11-06 17:24:51 +03:00
|
|
|
PTP_CALLBACK_ENVIRON pcbe);
|
|
|
|
static VOID(WINAPI* pCloseThreadpoolWork)(PTP_WORK pwk);
|
|
|
|
static VOID(WINAPI* pSubmitThreadpoolWork)(PTP_WORK pwk);
|
|
|
|
static BOOL(WINAPI* pTrySubmitThreadpoolCallback)(PTP_SIMPLE_CALLBACK pfns, PVOID pv,
|
|
|
|
PTP_CALLBACK_ENVIRON pcbe);
|
|
|
|
static VOID(WINAPI* pWaitForThreadpoolWorkCallbacks)(PTP_WORK pwk, BOOL fCancelPendingCallbacks);
|
2013-01-21 07:39:32 +04:00
|
|
|
|
2017-11-15 11:11:12 +03:00
|
|
|
static BOOL CALLBACK init_module(PINIT_ONCE once, PVOID param, PVOID* context)
|
2013-01-21 07:39:32 +04:00
|
|
|
{
|
2016-06-04 18:04:12 +03:00
|
|
|
HMODULE kernel32 = LoadLibraryA("kernel32.dll");
|
2017-11-15 11:11:12 +03:00
|
|
|
|
2016-06-04 18:04:12 +03:00
|
|
|
if (kernel32)
|
|
|
|
{
|
|
|
|
pCreateThreadpoolWork = (void*)GetProcAddress(kernel32, "CreateThreadpoolWork");
|
|
|
|
pCloseThreadpoolWork = (void*)GetProcAddress(kernel32, "CloseThreadpoolWork");
|
|
|
|
pSubmitThreadpoolWork = (void*)GetProcAddress(kernel32, "SubmitThreadpoolWork");
|
2019-11-06 17:24:51 +03:00
|
|
|
pTrySubmitThreadpoolCallback =
|
|
|
|
(void*)GetProcAddress(kernel32, "TrySubmitThreadpoolCallback");
|
|
|
|
pWaitForThreadpoolWorkCallbacks =
|
|
|
|
(void*)GetProcAddress(kernel32, "WaitForThreadpoolWorkCallbacks");
|
2016-06-04 18:04:12 +03:00
|
|
|
}
|
2017-11-15 11:11:12 +03:00
|
|
|
|
2016-06-04 18:04:12 +03:00
|
|
|
return TRUE;
|
2013-01-21 07:39:32 +04:00
|
|
|
}
|
|
|
|
#endif
|
2012-12-22 03:30:33 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static TP_CALLBACK_ENVIRON DEFAULT_CALLBACK_ENVIRONMENT = {
|
|
|
|
1, /* Version */
|
2016-06-04 18:04:12 +03:00
|
|
|
NULL, /* Pool */
|
|
|
|
NULL, /* CleanupGroup */
|
|
|
|
NULL, /* CleanupGroupCancelCallback */
|
|
|
|
NULL, /* RaceDll */
|
|
|
|
NULL, /* ActivationContext */
|
|
|
|
NULL, /* FinalizationCallback */
|
|
|
|
{ 0 } /* Flags */
|
|
|
|
};
|
2014-07-17 23:11:04 +04:00
|
|
|
|
2016-06-16 12:47:33 +03:00
|
|
|
PTP_WORK winpr_CreateThreadpoolWork(PTP_WORK_CALLBACK pfnwk, PVOID pv, PTP_CALLBACK_ENVIRON pcbe)
|
2012-12-22 03:30:33 +04:00
|
|
|
{
|
2013-01-22 03:33:00 +04:00
|
|
|
PTP_WORK work = NULL;
|
2013-01-21 07:39:32 +04:00
|
|
|
#ifdef _WIN32
|
2016-06-04 18:04:12 +03:00
|
|
|
InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
|
2017-11-15 11:11:12 +03:00
|
|
|
|
2013-01-21 07:39:32 +04:00
|
|
|
if (pCreateThreadpoolWork)
|
|
|
|
return pCreateThreadpoolWork(pfnwk, pv, pcbe);
|
2017-11-15 11:11:12 +03:00
|
|
|
|
2016-06-04 18:04:12 +03:00
|
|
|
#endif
|
2019-11-06 17:24:51 +03:00
|
|
|
work = (PTP_WORK)calloc(1, sizeof(TP_WORK));
|
2013-01-22 03:33:00 +04:00
|
|
|
|
|
|
|
if (work)
|
|
|
|
{
|
|
|
|
if (!pcbe)
|
2016-06-04 18:04:12 +03:00
|
|
|
{
|
|
|
|
pcbe = &DEFAULT_CALLBACK_ENVIRONMENT;
|
|
|
|
pcbe->Pool = GetDefaultThreadpool();
|
|
|
|
}
|
2017-11-15 11:11:12 +03:00
|
|
|
|
2013-01-22 03:33:00 +04:00
|
|
|
work->CallbackEnvironment = pcbe;
|
2016-06-04 18:04:12 +03:00
|
|
|
work->WorkCallback = pfnwk;
|
|
|
|
work->CallbackParameter = pv;
|
2017-11-15 11:11:12 +03:00
|
|
|
#ifndef _WIN32
|
|
|
|
|
|
|
|
if (pcbe->CleanupGroup)
|
2021-05-17 13:30:18 +03:00
|
|
|
ArrayList_Append(pcbe->CleanupGroup->groups, work);
|
2017-11-15 11:11:12 +03:00
|
|
|
|
|
|
|
#endif
|
2013-01-22 03:33:00 +04:00
|
|
|
}
|
2013-01-21 07:39:32 +04:00
|
|
|
|
2013-01-22 03:33:00 +04:00
|
|
|
return work;
|
2012-12-22 03:30:33 +04:00
|
|
|
}
|
|
|
|
|
2016-06-16 12:47:33 +03:00
|
|
|
VOID winpr_CloseThreadpoolWork(PTP_WORK pwk)
|
2012-12-22 03:30:33 +04:00
|
|
|
{
|
2013-01-21 07:39:32 +04:00
|
|
|
#ifdef _WIN32
|
2016-06-04 18:04:12 +03:00
|
|
|
InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
|
2017-11-15 11:11:12 +03:00
|
|
|
|
2013-01-21 07:39:32 +04:00
|
|
|
if (pCloseThreadpoolWork)
|
2016-06-04 18:04:12 +03:00
|
|
|
{
|
2013-01-21 07:39:32 +04:00
|
|
|
pCloseThreadpoolWork(pwk);
|
2016-06-04 18:04:12 +03:00
|
|
|
return;
|
|
|
|
}
|
2017-11-15 11:11:12 +03:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
if (pwk->CallbackEnvironment->CleanupGroup)
|
|
|
|
ArrayList_Remove(pwk->CallbackEnvironment->CleanupGroup->groups, pwk);
|
|
|
|
|
2013-01-21 07:39:32 +04:00
|
|
|
#endif
|
2016-06-04 18:04:12 +03:00
|
|
|
free(pwk);
|
2012-12-22 03:30:33 +04:00
|
|
|
}
|
|
|
|
|
2016-06-16 12:47:33 +03:00
|
|
|
VOID winpr_SubmitThreadpoolWork(PTP_WORK pwk)
|
2012-12-22 03:30:33 +04:00
|
|
|
{
|
2016-06-04 18:04:12 +03:00
|
|
|
PTP_POOL pool;
|
|
|
|
PTP_CALLBACK_INSTANCE callbackInstance;
|
2013-01-21 07:39:32 +04:00
|
|
|
#ifdef _WIN32
|
2016-06-04 18:04:12 +03:00
|
|
|
InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
|
2017-11-15 11:11:12 +03:00
|
|
|
|
2013-01-21 07:39:32 +04:00
|
|
|
if (pSubmitThreadpoolWork)
|
2016-06-04 18:04:12 +03:00
|
|
|
{
|
2013-01-21 07:39:32 +04:00
|
|
|
pSubmitThreadpoolWork(pwk);
|
2016-06-04 18:04:12 +03:00
|
|
|
return;
|
|
|
|
}
|
2017-11-15 11:11:12 +03:00
|
|
|
|
2016-06-04 18:04:12 +03:00
|
|
|
#endif
|
2013-01-22 03:33:00 +04:00
|
|
|
pool = pwk->CallbackEnvironment->Pool;
|
2019-11-06 17:24:51 +03:00
|
|
|
callbackInstance = (PTP_CALLBACK_INSTANCE)calloc(1, sizeof(TP_CALLBACK_INSTANCE));
|
2013-01-22 03:33:00 +04:00
|
|
|
|
|
|
|
if (callbackInstance)
|
|
|
|
{
|
|
|
|
callbackInstance->Work = pwk;
|
2013-01-22 04:22:08 +04:00
|
|
|
CountdownEvent_AddCount(pool->WorkComplete, 1);
|
2013-01-22 03:33:00 +04:00
|
|
|
Queue_Enqueue(pool->PendingQueue, callbackInstance);
|
|
|
|
}
|
2012-12-22 03:30:33 +04:00
|
|
|
}
|
|
|
|
|
2017-11-15 11:11:12 +03:00
|
|
|
BOOL winpr_TrySubmitThreadpoolCallback(PTP_SIMPLE_CALLBACK pfns, PVOID pv,
|
|
|
|
PTP_CALLBACK_ENVIRON pcbe)
|
2012-12-22 03:30:33 +04:00
|
|
|
{
|
2013-01-21 07:39:32 +04:00
|
|
|
#ifdef _WIN32
|
2016-06-04 18:04:12 +03:00
|
|
|
InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
|
2017-11-15 11:11:12 +03:00
|
|
|
|
2013-01-21 07:39:32 +04:00
|
|
|
if (pTrySubmitThreadpoolCallback)
|
|
|
|
return pTrySubmitThreadpoolCallback(pfns, pv, pcbe);
|
2017-11-15 11:11:12 +03:00
|
|
|
|
2013-01-21 07:39:32 +04:00
|
|
|
#endif
|
2016-06-04 18:04:12 +03:00
|
|
|
WLog_ERR(TAG, "TrySubmitThreadpoolCallback is not implemented");
|
2012-12-22 03:30:33 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2016-06-16 12:47:33 +03:00
|
|
|
VOID winpr_WaitForThreadpoolWorkCallbacks(PTP_WORK pwk, BOOL fCancelPendingCallbacks)
|
2012-12-22 03:30:33 +04:00
|
|
|
{
|
2016-06-04 18:04:12 +03:00
|
|
|
HANDLE event;
|
|
|
|
PTP_POOL pool;
|
2013-01-21 07:39:32 +04:00
|
|
|
#ifdef _WIN32
|
2016-06-04 18:04:12 +03:00
|
|
|
InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
|
2017-11-15 11:11:12 +03:00
|
|
|
|
2013-01-21 07:39:32 +04:00
|
|
|
if (pWaitForThreadpoolWorkCallbacks)
|
2016-06-04 18:04:12 +03:00
|
|
|
{
|
2013-01-21 07:39:32 +04:00
|
|
|
pWaitForThreadpoolWorkCallbacks(pwk, fCancelPendingCallbacks);
|
2016-06-04 18:04:12 +03:00
|
|
|
return;
|
|
|
|
}
|
2017-11-15 11:11:12 +03:00
|
|
|
|
2016-06-04 18:04:12 +03:00
|
|
|
#endif
|
2013-01-22 03:33:00 +04:00
|
|
|
pool = pwk->CallbackEnvironment->Pool;
|
2013-01-22 04:22:08 +04:00
|
|
|
event = CountdownEvent_WaitHandle(pool->WorkComplete);
|
2012-12-22 03:30:33 +04:00
|
|
|
|
2013-01-22 04:22:08 +04:00
|
|
|
if (WaitForSingleObject(event, INFINITE) != WAIT_OBJECT_0)
|
2014-08-18 19:22:22 +04:00
|
|
|
WLog_ERR(TAG, "error waiting on work completion");
|
2013-01-22 03:33:00 +04:00
|
|
|
}
|
2014-07-17 23:11:04 +04:00
|
|
|
|
2016-06-04 18:04:12 +03:00
|
|
|
#endif /* WINPR_THREAD_POOL defined */
|