20 #include <winpr/config.h>
22 #include <winpr/assert.h>
23 #include <winpr/crt.h>
24 #include <winpr/pool.h>
25 #include <winpr/library.h>
29 #define TAG WINPR_TAG("pool")
31 #ifdef WINPR_THREAD_POOL
34 static INIT_ONCE init_once_module = INIT_ONCE_STATIC_INIT;
35 static PTP_WORK(WINAPI* pCreateThreadpoolWork)(PTP_WORK_CALLBACK pfnwk, PVOID pv,
37 static VOID(WINAPI* pCloseThreadpoolWork)(PTP_WORK pwk);
38 static VOID(WINAPI* pSubmitThreadpoolWork)(PTP_WORK pwk);
39 static BOOL(WINAPI* pTrySubmitThreadpoolCallback)(PTP_SIMPLE_CALLBACK pfns, PVOID pv,
41 static VOID(WINAPI* pWaitForThreadpoolWorkCallbacks)(PTP_WORK pwk, BOOL fCancelPendingCallbacks);
43 static BOOL CALLBACK init_module(
PINIT_ONCE once, PVOID param, PVOID* context)
45 HMODULE kernel32 = LoadLibraryA(
"kernel32.dll");
49 pCreateThreadpoolWork = GetProcAddressAs(kernel32,
"CreateThreadpoolWork",
void*);
50 pCloseThreadpoolWork = GetProcAddressAs(kernel32,
"CloseThreadpoolWork",
void*);
51 pSubmitThreadpoolWork = GetProcAddressAs(kernel32,
"SubmitThreadpoolWork",
void*);
52 pTrySubmitThreadpoolCallback =
53 GetProcAddressAs(kernel32,
"TrySubmitThreadpoolCallback",
void*);
54 pWaitForThreadpoolWorkCallbacks =
55 GetProcAddressAs(kernel32,
"WaitForThreadpoolWorkCallbacks",
void*);
72 PTP_WORK winpr_CreateThreadpoolWork(PTP_WORK_CALLBACK pfnwk, PVOID pv,
PTP_CALLBACK_ENVIRON pcbe)
76 InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
78 if (pCreateThreadpoolWork)
79 return pCreateThreadpoolWork(pfnwk, pv, pcbe);
82 work = (PTP_WORK)calloc(1,
sizeof(TP_WORK));
88 pcbe = &DEFAULT_CALLBACK_ENVIRONMENT;
89 pcbe->Pool = GetDefaultThreadpool();
92 work->CallbackEnvironment = pcbe;
93 work->WorkCallback = pfnwk;
94 work->CallbackParameter = pv;
97 if (pcbe->CleanupGroup)
98 ArrayList_Append(pcbe->CleanupGroup->groups, work);
106 VOID winpr_CloseThreadpoolWork(PTP_WORK pwk)
109 InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
111 if (pCloseThreadpoolWork)
113 pCloseThreadpoolWork(pwk);
120 WINPR_ASSERT(pwk->CallbackEnvironment);
121 if (pwk->CallbackEnvironment->CleanupGroup)
122 ArrayList_Remove(pwk->CallbackEnvironment->CleanupGroup->groups, pwk);
128 VOID winpr_SubmitThreadpoolWork(PTP_WORK pwk)
130 PTP_POOL pool = NULL;
131 PTP_CALLBACK_INSTANCE callbackInstance = NULL;
133 InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
135 if (pSubmitThreadpoolWork)
137 pSubmitThreadpoolWork(pwk);
144 WINPR_ASSERT(pwk->CallbackEnvironment);
145 pool = pwk->CallbackEnvironment->Pool;
146 callbackInstance = (PTP_CALLBACK_INSTANCE)calloc(1,
sizeof(TP_CALLBACK_INSTANCE));
148 if (callbackInstance)
150 callbackInstance->Work = pwk;
151 CountdownEvent_AddCount(pool->WorkComplete, 1);
152 if (!Queue_Enqueue(pool->PendingQueue, callbackInstance))
153 free(callbackInstance);
158 BOOL winpr_TrySubmitThreadpoolCallback(PTP_SIMPLE_CALLBACK pfns, PVOID pv,
162 InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
164 if (pTrySubmitThreadpoolCallback)
165 return pTrySubmitThreadpoolCallback(pfns, pv, pcbe);
168 WLog_ERR(TAG,
"TrySubmitThreadpoolCallback is not implemented");
172 VOID winpr_WaitForThreadpoolWorkCallbacks(PTP_WORK pwk, BOOL fCancelPendingCallbacks)
175 PTP_POOL pool = NULL;
178 InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
180 if (pWaitForThreadpoolWorkCallbacks)
182 pWaitForThreadpoolWorkCallbacks(pwk, fCancelPendingCallbacks);
188 WINPR_ASSERT(pwk->CallbackEnvironment);
190 pool = pwk->CallbackEnvironment->Pool;
193 event = CountdownEvent_WaitHandle(pool->WorkComplete);
195 if (WaitForSingleObject(event, INFINITE) != WAIT_OBJECT_0)
196 WLog_ERR(TAG,
"error waiting on work completion");