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>
|
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"
|
|
|
|
|
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;
|
2013-01-21 07:39:32 +04:00
|
|
|
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
|
|
|
|
2016-06-04 18:04: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");
|
|
|
|
if (kernel32)
|
|
|
|
{
|
|
|
|
pCreateThreadpool = (void*)GetProcAddress(kernel32, "CreateThreadpool");
|
|
|
|
pCloseThreadpool = (void*)GetProcAddress(kernel32, "CloseThreadpool");
|
|
|
|
pSetThreadpoolThreadMinimum = (void*)GetProcAddress(kernel32, "SetThreadpoolThreadMinimum");
|
|
|
|
pSetThreadpoolThreadMaximum = (void*)GetProcAddress(kernel32, "SetThreadpoolThreadMaximum");
|
|
|
|
}
|
|
|
|
return TRUE;
|
2013-01-22 03:33:00 +04:00
|
|
|
}
|
2016-06-04 18:04:12 +03:00
|
|
|
#endif
|
2013-01-22 03:33:00 +04:00
|
|
|
|
|
|
|
static TP_POOL DEFAULT_POOL =
|
|
|
|
{
|
2015-04-28 18:00:41 +03:00
|
|
|
0, /* DWORD Minimum */
|
|
|
|
500, /* DWORD Maximum */
|
|
|
|
NULL, /* wArrayList* Threads */
|
|
|
|
NULL, /* wQueue* PendingQueue */
|
|
|
|
NULL, /* HANDLE TerminateEvent */
|
|
|
|
NULL, /* wCountdownEvent* WorkComplete */
|
2013-01-22 03:33:00 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static void* thread_pool_work_func(void* arg)
|
|
|
|
{
|
2013-01-23 01:19:32 +04:00
|
|
|
DWORD status;
|
2013-01-22 03:33:00 +04:00
|
|
|
PTP_POOL pool;
|
|
|
|
PTP_WORK work;
|
2013-01-23 01:19:32 +04:00
|
|
|
HANDLE events[2];
|
2013-01-22 03:33:00 +04:00
|
|
|
PTP_CALLBACK_INSTANCE callbackInstance;
|
|
|
|
|
|
|
|
pool = (PTP_POOL) arg;
|
|
|
|
|
2013-01-23 01:19:32 +04:00
|
|
|
events[0] = pool->TerminateEvent;
|
|
|
|
events[1] = Queue_Event(pool->PendingQueue);
|
|
|
|
|
|
|
|
while (1)
|
2013-01-22 03:33:00 +04:00
|
|
|
{
|
2013-01-23 01:19:32 +04:00
|
|
|
status = WaitForMultipleObjects(2, events, FALSE, INFINITE);
|
|
|
|
|
|
|
|
if (status == WAIT_OBJECT_0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (status != (WAIT_OBJECT_0 + 1))
|
|
|
|
break;
|
|
|
|
|
2013-01-22 03:33:00 +04:00
|
|
|
callbackInstance = (PTP_CALLBACK_INSTANCE) Queue_Dequeue(pool->PendingQueue);
|
|
|
|
|
|
|
|
if (callbackInstance)
|
|
|
|
{
|
|
|
|
work = callbackInstance->Work;
|
|
|
|
work->WorkCallback(callbackInstance, work->CallbackParameter, work);
|
2013-01-22 04:22:08 +04:00
|
|
|
CountdownEvent_Signal(pool->WorkComplete, 1);
|
2013-01-22 03:33:00 +04:00
|
|
|
free(callbackInstance);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-14 21:36:31 +04:00
|
|
|
ExitThread(0);
|
2013-01-22 03:33:00 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-04-23 18:59:14 +04:00
|
|
|
static void threads_close(void *thread)
|
|
|
|
{
|
2014-07-14 21:36:31 +04:00
|
|
|
WaitForSingleObject(thread, INFINITE);
|
2014-04-23 18:59:14 +04:00
|
|
|
CloseHandle(thread);
|
|
|
|
}
|
|
|
|
|
2015-04-28 18:00:41 +03:00
|
|
|
static BOOL InitializeThreadpool(PTP_POOL pool)
|
2013-01-22 03:33:00 +04:00
|
|
|
{
|
|
|
|
int index;
|
2013-01-23 01:19:32 +04:00
|
|
|
HANDLE thread;
|
2013-01-22 03:33:00 +04:00
|
|
|
|
2015-04-28 18:00:41 +03:00
|
|
|
if (pool->Threads)
|
|
|
|
return TRUE;
|
2013-01-23 01:19:32 +04:00
|
|
|
|
2015-04-28 18:00:41 +03:00
|
|
|
pool->Minimum = 0;
|
|
|
|
pool->Maximum = 500;
|
2013-01-22 03:33:00 +04:00
|
|
|
|
2015-04-28 18:00:41 +03:00
|
|
|
if (!(pool->PendingQueue = Queue_New(TRUE, -1, -1)))
|
|
|
|
goto fail_queue_new;
|
2013-01-22 03:33:00 +04:00
|
|
|
|
2015-04-28 18:00:41 +03:00
|
|
|
if (!(pool->WorkComplete = CountdownEvent_New(0)))
|
|
|
|
goto fail_countdown_event;
|
2013-01-23 01:19:32 +04:00
|
|
|
|
2015-04-28 18:00:41 +03:00
|
|
|
if (!(pool->TerminateEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
|
|
|
|
goto fail_terminate_event;
|
2013-01-23 01:19:32 +04:00
|
|
|
|
2015-04-28 18:00:41 +03:00
|
|
|
if (!(pool->Threads = ArrayList_New(TRUE)))
|
|
|
|
goto fail_thread_array;
|
|
|
|
|
|
|
|
pool->Threads->object.fnObjectFree = threads_close;
|
|
|
|
|
|
|
|
for (index = 0; index < 4; index++)
|
|
|
|
{
|
|
|
|
if (!(thread = CreateThread(NULL, 0,
|
|
|
|
(LPTHREAD_START_ROUTINE) thread_pool_work_func,
|
|
|
|
(void*) pool, 0, NULL)))
|
|
|
|
{
|
|
|
|
goto fail_create_threads;
|
2013-01-22 03:33:00 +04:00
|
|
|
}
|
2015-04-28 18:00:41 +03:00
|
|
|
|
2015-05-18 12:28:00 +03:00
|
|
|
if (ArrayList_Add(pool->Threads, thread) < 0)
|
|
|
|
goto fail_create_threads;
|
2013-01-22 03:33:00 +04:00
|
|
|
}
|
2015-04-28 18:00:41 +03:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
fail_create_threads:
|
|
|
|
SetEvent(pool->TerminateEvent);
|
|
|
|
ArrayList_Free(pool->Threads);
|
|
|
|
pool->Threads = NULL;
|
|
|
|
fail_thread_array:
|
|
|
|
CloseHandle(pool->TerminateEvent);
|
|
|
|
pool->TerminateEvent = NULL;
|
|
|
|
fail_terminate_event:
|
|
|
|
CountdownEvent_Free(pool->WorkComplete);
|
|
|
|
pool->WorkComplete = NULL;
|
|
|
|
fail_countdown_event:
|
|
|
|
Queue_Free(pool->PendingQueue);
|
|
|
|
pool->WorkComplete = NULL;
|
|
|
|
fail_queue_new:
|
|
|
|
|
|
|
|
return FALSE;
|
2013-01-23 01:19:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
PTP_POOL GetDefaultThreadpool()
|
|
|
|
{
|
|
|
|
PTP_POOL pool = NULL;
|
|
|
|
|
|
|
|
pool = &DEFAULT_POOL;
|
|
|
|
|
2015-04-28 18:00:41 +03:00
|
|
|
if (!InitializeThreadpool(pool))
|
|
|
|
return NULL;
|
2013-01-22 03:33:00 +04:00
|
|
|
|
|
|
|
return pool;
|
2013-01-21 07:39:32 +04:00
|
|
|
}
|
|
|
|
|
2016-06-16 12:47:33 +03:00
|
|
|
PTP_POOL winpr_CreateThreadpool(PVOID reserved)
|
2012-12-22 03:30:33 +04:00
|
|
|
{
|
2013-01-22 03:33:00 +04:00
|
|
|
PTP_POOL pool = 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);
|
2013-01-21 07:39:32 +04:00
|
|
|
if (pCreateThreadpool)
|
|
|
|
return pCreateThreadpool(reserved);
|
2016-06-04 18:04:12 +03:00
|
|
|
#endif
|
2015-04-28 18:00:41 +03:00
|
|
|
if (!(pool = (PTP_POOL) calloc(1, sizeof(TP_POOL))))
|
|
|
|
return NULL;
|
2013-01-22 03:33:00 +04:00
|
|
|
|
2015-04-28 18:00:41 +03:00
|
|
|
if (!InitializeThreadpool(pool))
|
|
|
|
{
|
|
|
|
free(pool);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-01-21 07:39:32 +04:00
|
|
|
|
2013-01-22 03:33:00 +04:00
|
|
|
return pool;
|
2012-12-22 03:30:33 +04:00
|
|
|
}
|
|
|
|
|
2016-06-16 12:47:33 +03:00
|
|
|
VOID winpr_CloseThreadpool(PTP_POOL ptpp)
|
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);
|
2013-01-21 07:39:32 +04:00
|
|
|
if (pCloseThreadpool)
|
2016-06-04 18:04:12 +03:00
|
|
|
{
|
2013-01-21 07:39:32 +04:00
|
|
|
pCloseThreadpool(ptpp);
|
2016-06-04 18:04:12 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2013-01-23 01:19:32 +04:00
|
|
|
SetEvent(ptpp->TerminateEvent);
|
|
|
|
|
|
|
|
ArrayList_Free(ptpp->Threads);
|
|
|
|
Queue_Free(ptpp->PendingQueue);
|
|
|
|
CountdownEvent_Free(ptpp->WorkComplete);
|
|
|
|
CloseHandle(ptpp->TerminateEvent);
|
|
|
|
|
2015-04-28 18:00:41 +03:00
|
|
|
if (ptpp == &DEFAULT_POOL)
|
|
|
|
{
|
|
|
|
ptpp->Threads = NULL;
|
|
|
|
ptpp->PendingQueue = NULL;
|
|
|
|
ptpp->WorkComplete = NULL;
|
|
|
|
ptpp->TerminateEvent = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
free(ptpp);
|
|
|
|
}
|
2012-12-22 03:30:33 +04:00
|
|
|
}
|
|
|
|
|
2016-06-16 12:47:33 +03:00
|
|
|
BOOL winpr_SetThreadpoolThreadMinimum(PTP_POOL ptpp, DWORD cthrdMic)
|
2012-12-22 03:30:33 +04:00
|
|
|
{
|
2016-06-04 18:04:12 +03:00
|
|
|
HANDLE thread;
|
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);
|
2013-01-22 03:33:00 +04:00
|
|
|
if (pSetThreadpoolThreadMinimum)
|
|
|
|
return pSetThreadpoolThreadMinimum(ptpp, cthrdMic);
|
2016-06-04 18:04:12 +03:00
|
|
|
#endif
|
2013-01-22 03:33:00 +04:00
|
|
|
ptpp->Minimum = cthrdMic;
|
2013-01-23 06:54:13 +04:00
|
|
|
|
|
|
|
while (ArrayList_Count(ptpp->Threads) < ptpp->Minimum)
|
|
|
|
{
|
2015-05-05 14:55:48 +03:00
|
|
|
if (!(thread = CreateThread(NULL, 0,
|
2013-01-23 06:54:13 +04:00
|
|
|
(LPTHREAD_START_ROUTINE) thread_pool_work_func,
|
2015-05-05 14:55:48 +03:00
|
|
|
(void*) ptpp, 0, NULL)))
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2013-01-23 06:54:13 +04:00
|
|
|
|
2015-05-18 12:28:00 +03:00
|
|
|
if (ArrayList_Add(ptpp->Threads, thread) < 0)
|
|
|
|
return FALSE;
|
2013-01-23 06:54:13 +04:00
|
|
|
}
|
2016-06-04 18:04:12 +03:00
|
|
|
|
2013-01-22 03:33:00 +04:00
|
|
|
return TRUE;
|
2012-12-22 03:30:33 +04:00
|
|
|
}
|
|
|
|
|
2016-06-16 12:47:33 +03:00
|
|
|
VOID winpr_SetThreadpoolThreadMaximum(PTP_POOL ptpp, DWORD cthrdMost)
|
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);
|
2013-01-22 03:33:00 +04:00
|
|
|
if (pSetThreadpoolThreadMaximum)
|
2016-06-04 18:04:12 +03:00
|
|
|
{
|
2013-01-22 03:33:00 +04:00
|
|
|
pSetThreadpoolThreadMaximum(ptpp, cthrdMost);
|
2016-06-04 18:04:12 +03:00
|
|
|
return;
|
|
|
|
}
|
2013-01-21 07:39:32 +04:00
|
|
|
#endif
|
2016-06-04 18:04:12 +03:00
|
|
|
ptpp->Maximum = cthrdMost;
|
2012-12-22 03:30:33 +04:00
|
|
|
}
|
|
|
|
|
2016-06-04 18:04:12 +03:00
|
|
|
#endif /* WINPR_THREAD_POOL defined */
|