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>
|
|
|
|
|
2013-01-22 03:33:00 +04:00
|
|
|
#include "pool.h"
|
|
|
|
|
2013-01-21 07:39:32 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
static BOOL module_initialized = FALSE;
|
|
|
|
static BOOL module_available = FALSE;
|
|
|
|
static HMODULE kernel32_module = NULL;
|
|
|
|
|
|
|
|
static PTP_WORK (WINAPI * pCreateThreadpoolWork)(PTP_WORK_CALLBACK pfnwk, PVOID pv, 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);
|
|
|
|
|
|
|
|
static void module_init()
|
|
|
|
{
|
|
|
|
if (module_initialized)
|
|
|
|
return;
|
|
|
|
|
|
|
|
kernel32_module = LoadLibraryA("kernel32.dll");
|
2013-01-22 08:34:46 +04:00
|
|
|
module_initialized = TRUE;
|
2013-01-21 07:39:32 +04:00
|
|
|
|
|
|
|
if (!kernel32_module)
|
|
|
|
return;
|
|
|
|
|
|
|
|
module_available = TRUE;
|
|
|
|
|
|
|
|
pCreateThreadpoolWork = (void*) GetProcAddress(kernel32_module, "CreateThreadpoolWork");
|
|
|
|
pCloseThreadpoolWork = (void*) GetProcAddress(kernel32_module, "CloseThreadpoolWork");
|
|
|
|
pSubmitThreadpoolWork = (void*) GetProcAddress(kernel32_module, "SubmitThreadpoolWork");
|
|
|
|
pTrySubmitThreadpoolCallback = (void*) GetProcAddress(kernel32_module, "TrySubmitThreadpoolCallback");
|
|
|
|
pWaitForThreadpoolWorkCallbacks = (void*) GetProcAddress(kernel32_module, "WaitForThreadpoolWorkCallbacks");
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2012-12-22 03:30:33 +04:00
|
|
|
|
|
|
|
PTP_WORK CreateThreadpoolWork(PTP_WORK_CALLBACK pfnwk, PVOID pv, PTP_CALLBACK_ENVIRON pcbe)
|
|
|
|
{
|
2013-01-22 03:33:00 +04:00
|
|
|
PTP_WORK work = NULL;
|
|
|
|
|
2013-01-21 07:39:32 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
module_init();
|
|
|
|
|
|
|
|
if (pCreateThreadpoolWork)
|
|
|
|
return pCreateThreadpoolWork(pfnwk, pv, pcbe);
|
2013-01-22 03:33:00 +04:00
|
|
|
#else
|
|
|
|
work = (PTP_WORK) malloc(sizeof(TP_WORK));
|
|
|
|
|
|
|
|
if (work)
|
|
|
|
{
|
|
|
|
work->WorkCallback = pfnwk;
|
|
|
|
work->CallbackParameter = pv;
|
|
|
|
|
|
|
|
if (!pcbe)
|
|
|
|
pcbe = GetDefaultThreadpoolEnvironment();
|
|
|
|
|
|
|
|
work->CallbackEnvironment = pcbe;
|
|
|
|
}
|
2013-01-21 07:39:32 +04:00
|
|
|
#endif
|
|
|
|
|
2013-01-22 03:33:00 +04:00
|
|
|
return work;
|
2012-12-22 03:30:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID CloseThreadpoolWork(PTP_WORK pwk)
|
|
|
|
{
|
2013-01-21 07:39:32 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
module_init();
|
2012-12-22 03:30:33 +04:00
|
|
|
|
2013-01-21 07:39:32 +04:00
|
|
|
if (pCloseThreadpoolWork)
|
|
|
|
pCloseThreadpoolWork(pwk);
|
2013-01-22 03:33:00 +04:00
|
|
|
#else
|
|
|
|
free(pwk);
|
2013-01-21 07:39:32 +04:00
|
|
|
#endif
|
2012-12-22 03:30:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID SubmitThreadpoolWork(PTP_WORK pwk)
|
|
|
|
{
|
2013-01-21 07:39:32 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
module_init();
|
2012-12-22 03:30:33 +04:00
|
|
|
|
2013-01-21 07:39:32 +04:00
|
|
|
if (pSubmitThreadpoolWork)
|
|
|
|
pSubmitThreadpoolWork(pwk);
|
2013-01-22 03:33:00 +04:00
|
|
|
#else
|
|
|
|
PTP_POOL pool;
|
|
|
|
PTP_CALLBACK_INSTANCE callbackInstance;
|
|
|
|
|
|
|
|
pool = pwk->CallbackEnvironment->Pool;
|
|
|
|
|
|
|
|
callbackInstance = (PTP_CALLBACK_INSTANCE) malloc(sizeof(TP_CALLBACK_INSTANCE));
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2013-01-21 07:39:32 +04:00
|
|
|
#endif
|
2012-12-22 03:30:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL TrySubmitThreadpoolCallback(PTP_SIMPLE_CALLBACK pfns, PVOID pv, PTP_CALLBACK_ENVIRON pcbe)
|
|
|
|
{
|
2013-01-21 07:39:32 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
module_init();
|
|
|
|
|
|
|
|
if (pTrySubmitThreadpoolCallback)
|
|
|
|
return pTrySubmitThreadpoolCallback(pfns, pv, pcbe);
|
2013-01-22 03:33:00 +04:00
|
|
|
#else
|
2013-01-21 07:39:32 +04:00
|
|
|
#endif
|
2012-12-22 03:30:33 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID WaitForThreadpoolWorkCallbacks(PTP_WORK pwk, BOOL fCancelPendingCallbacks)
|
|
|
|
{
|
2013-01-21 07:39:32 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
module_init();
|
2012-12-22 03:30:33 +04:00
|
|
|
|
2013-01-21 07:39:32 +04:00
|
|
|
if (pWaitForThreadpoolWorkCallbacks)
|
|
|
|
pWaitForThreadpoolWorkCallbacks(pwk, fCancelPendingCallbacks);
|
2013-01-22 03:33:00 +04:00
|
|
|
#else
|
2013-01-22 04:22:08 +04:00
|
|
|
HANDLE event;
|
2013-01-22 03:33:00 +04:00
|
|
|
PTP_POOL pool;
|
2012-12-22 03:30:33 +04:00
|
|
|
|
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)
|
|
|
|
printf("WaitForThreadpoolWorkCallbacks: error waiting on work completion\n");
|
2013-01-22 03:33:00 +04:00
|
|
|
#endif
|
|
|
|
}
|