20#include <winpr/config.h>
23#include <winpr/sysinfo.h>
24#include <winpr/pool.h>
25#include <winpr/library.h>
29#ifdef WINPR_THREAD_POOL
32static INIT_ONCE init_once_module = INIT_ONCE_STATIC_INIT;
33static PTP_POOL(WINAPI* pCreateThreadpool)(PVOID reserved);
34static VOID(WINAPI* pCloseThreadpool)(PTP_POOL ptpp);
35static BOOL(WINAPI* pSetThreadpoolThreadMinimum)(PTP_POOL ptpp, DWORD cthrdMic);
36static VOID(WINAPI* pSetThreadpoolThreadMaximum)(PTP_POOL ptpp, DWORD cthrdMost);
38static BOOL CALLBACK init_module(
PINIT_ONCE once, PVOID param, PVOID* context)
40 HMODULE kernel32 = LoadLibraryA(
"kernel32.dll");
43 pCreateThreadpool = GetProcAddressAs(kernel32,
"CreateThreadpool",
void*);
44 pCloseThreadpool = GetProcAddressAs(kernel32,
"CloseThreadpool",
void*);
45 pSetThreadpoolThreadMinimum =
46 GetProcAddressAs(kernel32,
"SetThreadpoolThreadMinimum",
void*);
47 pSetThreadpoolThreadMaximum =
48 GetProcAddressAs(kernel32,
"SetThreadpoolThreadMaximum",
void*);
54static TP_POOL DEFAULT_POOL = {
63static DWORD WINAPI thread_pool_work_func(LPVOID arg)
69 PTP_CALLBACK_INSTANCE callbackInstance = NULL;
73 events[0] = pool->TerminateEvent;
74 events[1] = Queue_Event(pool->PendingQueue);
78 status = WaitForMultipleObjects(2, events, FALSE, INFINITE);
80 if (status == WAIT_OBJECT_0)
83 if (status != (WAIT_OBJECT_0 + 1))
86 callbackInstance = (PTP_CALLBACK_INSTANCE)Queue_Dequeue(pool->PendingQueue);
90 work = callbackInstance->Work;
91 work->WorkCallback(callbackInstance, work->CallbackParameter, work);
92 CountdownEvent_Signal(pool->WorkComplete, 1);
93 free(callbackInstance);
101static void threads_close(
void* thread)
103 (void)WaitForSingleObject(thread, INFINITE);
104 (void)CloseHandle(thread);
107static BOOL InitializeThreadpool(PTP_POOL pool)
115 if (!(pool->PendingQueue = Queue_New(TRUE, -1, -1)))
118 if (!(pool->WorkComplete = CountdownEvent_New(0)))
121 if (!(pool->TerminateEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
124 if (!(pool->Threads = ArrayList_New(TRUE)))
127 obj = ArrayList_Object(pool->Threads);
128 obj->fnObjectFree = threads_close;
131 GetSystemInfo(&info);
132 if (info.dwNumberOfProcessors < 1)
133 info.dwNumberOfProcessors = 1;
134 if (!SetThreadpoolThreadMinimum(pool, info.dwNumberOfProcessors))
137#if !defined(WINPR_THREADPOOL_DEFAULT_MAX_COUNT)
138#error "WINPR_THREADPOOL_DEFAULT_MAX_COUNT must be defined"
140 DWORD max = info.dwNumberOfProcessors;
141 if (max > WINPR_THREADPOOL_DEFAULT_MAX_COUNT)
142 max = WINPR_THREADPOOL_DEFAULT_MAX_COUNT;
143 SetThreadpoolThreadMaximum(pool, max);
151PTP_POOL GetDefaultThreadpool(
void)
153 PTP_POOL pool = &DEFAULT_POOL;
155 if (!InitializeThreadpool(pool))
161PTP_POOL winpr_CreateThreadpool(PVOID reserved)
163 PTP_POOL pool = NULL;
165 InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
166 if (pCreateThreadpool)
167 return pCreateThreadpool(reserved);
169 WINPR_UNUSED(reserved);
171 if (!(pool = (PTP_POOL)calloc(1,
sizeof(TP_POOL))))
174 if (!InitializeThreadpool(pool))
176 winpr_CloseThreadpool(pool);
183VOID winpr_CloseThreadpool(PTP_POOL ptpp)
186 InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
187 if (pCloseThreadpool)
189 pCloseThreadpool(ptpp);
193 (void)SetEvent(ptpp->TerminateEvent);
195 ArrayList_Free(ptpp->Threads);
196 Queue_Free(ptpp->PendingQueue);
197 CountdownEvent_Free(ptpp->WorkComplete);
198 (void)CloseHandle(ptpp->TerminateEvent);
201 TP_POOL empty = { 0 };
205 if (ptpp != &DEFAULT_POOL)
209BOOL winpr_SetThreadpoolThreadMinimum(PTP_POOL ptpp, DWORD cthrdMic)
213 InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
214 if (pSetThreadpoolThreadMinimum)
215 return pSetThreadpoolThreadMinimum(ptpp, cthrdMic);
217 ptpp->Minimum = cthrdMic;
219 ArrayList_Lock(ptpp->Threads);
220 while (ArrayList_Count(ptpp->Threads) < ptpp->Minimum)
222 HANDLE thread = CreateThread(NULL, 0, thread_pool_work_func, (
void*)ptpp, 0, NULL);
226 if (!ArrayList_Append(ptpp->Threads, thread))
228 (void)CloseHandle(thread);
235 ArrayList_Unlock(ptpp->Threads);
240VOID winpr_SetThreadpoolThreadMaximum(PTP_POOL ptpp, DWORD cthrdMost)
243 InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
244 if (pSetThreadpoolThreadMaximum)
246 pSetThreadpoolThreadMaximum(ptpp, cthrdMost);
250 ptpp->Maximum = cthrdMost;
252 ArrayList_Lock(ptpp->Threads);
253 if (ArrayList_Count(ptpp->Threads) > ptpp->Maximum)
255 (void)SetEvent(ptpp->TerminateEvent);
256 ArrayList_Clear(ptpp->Threads);
257 (void)ResetEvent(ptpp->TerminateEvent);
259 ArrayList_Unlock(ptpp->Threads);
260 winpr_SetThreadpoolThreadMinimum(ptpp, ptpp->Minimum);
This struct contains function pointer to initialize/free objects.