2012-12-22 03:30:33 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Thread Pool API (Pool)
|
|
|
|
*
|
|
|
|
* 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_POOL (WINAPI * pCreateThreadpool)(PVOID reserved);
|
|
|
|
static VOID (WINAPI * pCloseThreadpool)(PTP_POOL ptpp);
|
|
|
|
static BOOL (WINAPI * pSetThreadpoolThreadMinimum)(PTP_POOL ptpp, DWORD cthrdMic);
|
2013-01-22 03:33:00 +04:00
|
|
|
static VOID (WINAPI * pSetThreadpoolThreadMaximum)(PTP_POOL ptpp, DWORD cthrdMost);
|
2013-01-21 07:39:32 +04:00
|
|
|
|
|
|
|
static void module_init()
|
|
|
|
{
|
|
|
|
if (module_initialized)
|
|
|
|
return;
|
|
|
|
|
|
|
|
kernel32_module = LoadLibraryA("kernel32.dll");
|
|
|
|
|
|
|
|
if (!kernel32_module)
|
|
|
|
return;
|
|
|
|
|
|
|
|
module_available = TRUE;
|
|
|
|
|
|
|
|
pCreateThreadpool = (void*) GetProcAddress(kernel32_module, "CreateThreadpool");
|
|
|
|
pCloseThreadpool = (void*) GetProcAddress(kernel32_module, "CloseThreadpool");
|
|
|
|
pSetThreadpoolThreadMinimum = (void*) GetProcAddress(kernel32_module, "SetThreadpoolThreadMinimum");
|
2013-01-22 03:33:00 +04:00
|
|
|
pSetThreadpoolThreadMaximum = (void*) GetProcAddress(kernel32_module, "SetThreadpoolThreadMaximum");
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
static TP_POOL DEFAULT_POOL =
|
|
|
|
{
|
|
|
|
0, /* Minimum */
|
|
|
|
500, /* Maximum */
|
|
|
|
NULL, /* Threads */
|
|
|
|
0, /* ThreadCount */
|
|
|
|
};
|
|
|
|
|
|
|
|
static void* thread_pool_work_func(void* arg)
|
|
|
|
{
|
|
|
|
PTP_POOL pool;
|
|
|
|
PTP_WORK work;
|
|
|
|
PTP_CALLBACK_INSTANCE callbackInstance;
|
|
|
|
|
|
|
|
pool = (PTP_POOL) arg;
|
|
|
|
|
|
|
|
while (WaitForSingleObject(Queue_Event(pool->PendingQueue), INFINITE) == WAIT_OBJECT_0)
|
|
|
|
{
|
|
|
|
callbackInstance = (PTP_CALLBACK_INSTANCE) Queue_Dequeue(pool->PendingQueue);
|
|
|
|
|
|
|
|
if (callbackInstance)
|
|
|
|
{
|
|
|
|
work = callbackInstance->Work;
|
|
|
|
work->WorkCallback(callbackInstance, work->CallbackParameter, work);
|
|
|
|
free(callbackInstance);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
PTP_POOL GetDefaultThreadpool()
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
PTP_POOL pool = NULL;
|
|
|
|
|
|
|
|
pool = &DEFAULT_POOL;
|
|
|
|
|
|
|
|
if (!pool->Threads)
|
|
|
|
{
|
|
|
|
pool->ThreadCount = 4;
|
|
|
|
pool->Threads = (HANDLE*) malloc(pool->ThreadCount * sizeof(HANDLE));
|
|
|
|
|
|
|
|
pool->PendingQueue = Queue_New(TRUE, -1, -1);
|
|
|
|
|
|
|
|
for (index = 0; index < pool->ThreadCount; index++)
|
|
|
|
{
|
|
|
|
pool->Threads[index] = CreateThread(NULL, 0,
|
|
|
|
(LPTHREAD_START_ROUTINE) thread_pool_work_func,
|
|
|
|
(void*) pool, 0, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pool;
|
2013-01-21 07:39:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2012-12-22 03:30:33 +04:00
|
|
|
|
|
|
|
PTP_POOL CreateThreadpool(PVOID reserved)
|
|
|
|
{
|
2013-01-22 03:33:00 +04:00
|
|
|
PTP_POOL pool = NULL;
|
|
|
|
|
2013-01-21 07:39:32 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
module_init();
|
|
|
|
|
|
|
|
if (pCreateThreadpool)
|
|
|
|
return pCreateThreadpool(reserved);
|
2013-01-22 03:33:00 +04:00
|
|
|
#else
|
|
|
|
pool = (PTP_POOL) malloc(sizeof(TP_POOL));
|
|
|
|
|
|
|
|
if (pool)
|
|
|
|
{
|
|
|
|
pool->Minimum = 0;
|
|
|
|
pool->Maximum = 500;
|
|
|
|
}
|
2013-01-21 07:39:32 +04:00
|
|
|
#endif
|
|
|
|
|
2013-01-22 03:33:00 +04:00
|
|
|
return pool;
|
2012-12-22 03:30:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID CloseThreadpool(PTP_POOL ptpp)
|
|
|
|
{
|
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 (pCloseThreadpool)
|
|
|
|
pCloseThreadpool(ptpp);
|
2013-01-22 03:33:00 +04:00
|
|
|
#else
|
|
|
|
free(ptpp);
|
2013-01-21 07:39:32 +04:00
|
|
|
#endif
|
2012-12-22 03:30:33 +04:00
|
|
|
}
|
|
|
|
|
2013-01-22 03:33:00 +04:00
|
|
|
BOOL SetThreadpoolThreadMinimum(PTP_POOL ptpp, DWORD cthrdMic)
|
2012-12-22 03:30:33 +04:00
|
|
|
{
|
2013-01-21 07:39:32 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
module_init();
|
2012-12-22 03:30:33 +04:00
|
|
|
|
2013-01-22 03:33:00 +04:00
|
|
|
if (pSetThreadpoolThreadMinimum)
|
|
|
|
return pSetThreadpoolThreadMinimum(ptpp, cthrdMic);
|
|
|
|
#else
|
|
|
|
ptpp->Minimum = cthrdMic;
|
2013-01-21 07:39:32 +04:00
|
|
|
#endif
|
2013-01-22 03:33:00 +04:00
|
|
|
return TRUE;
|
2012-12-22 03:30:33 +04:00
|
|
|
}
|
|
|
|
|
2013-01-22 03:33:00 +04:00
|
|
|
VOID SetThreadpoolThreadMaximum(PTP_POOL ptpp, DWORD cthrdMost)
|
2012-12-22 03:30:33 +04:00
|
|
|
{
|
2013-01-21 07:39:32 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
module_init();
|
|
|
|
|
2013-01-22 03:33:00 +04:00
|
|
|
if (pSetThreadpoolThreadMaximum)
|
|
|
|
pSetThreadpoolThreadMaximum(ptpp, cthrdMost);
|
|
|
|
#else
|
|
|
|
ptpp->Maximum = cthrdMost;
|
2013-01-21 07:39:32 +04:00
|
|
|
#endif
|
2012-12-22 03:30:33 +04:00
|
|
|
}
|
|
|
|
|
2013-01-21 07:39:32 +04:00
|
|
|
/* dummy */
|
2012-12-22 03:30:33 +04:00
|
|
|
|
|
|
|
void winpr_pool_dummy()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|