FreeRDP
work.c
1 
20 #include <winpr/config.h>
21 
22 #include <winpr/assert.h>
23 #include <winpr/crt.h>
24 #include <winpr/pool.h>
25 #include <winpr/library.h>
26 
27 #include "pool.h"
28 #include "../log.h"
29 #define TAG WINPR_TAG("pool")
30 
31 #ifdef WINPR_THREAD_POOL
32 
33 #ifdef _WIN32
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);
42 
43 static BOOL CALLBACK init_module(PINIT_ONCE once, PVOID param, PVOID* context)
44 {
45  HMODULE kernel32 = LoadLibraryA("kernel32.dll");
46 
47  if (kernel32)
48  {
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*);
56  }
57 
58  return TRUE;
59 }
60 #endif
61 
62 static TP_CALLBACK_ENVIRON DEFAULT_CALLBACK_ENVIRONMENT = {
63  1, /* Version */
64  NULL, /* Pool */
65  NULL, /* CleanupGroup */
66  NULL, /* CleanupGroupCancelCallback */
67  NULL, /* RaceDll */
68  NULL, /* FinalizationCallback */
69  { 0 } /* Flags */
70 };
71 
72 PTP_WORK winpr_CreateThreadpoolWork(PTP_WORK_CALLBACK pfnwk, PVOID pv, PTP_CALLBACK_ENVIRON pcbe)
73 {
74  PTP_WORK work = NULL;
75 #ifdef _WIN32
76  InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
77 
78  if (pCreateThreadpoolWork)
79  return pCreateThreadpoolWork(pfnwk, pv, pcbe);
80 
81 #endif
82  work = (PTP_WORK)calloc(1, sizeof(TP_WORK));
83 
84  if (work)
85  {
86  if (!pcbe)
87  {
88  pcbe = &DEFAULT_CALLBACK_ENVIRONMENT;
89  pcbe->Pool = GetDefaultThreadpool();
90  }
91 
92  work->CallbackEnvironment = pcbe;
93  work->WorkCallback = pfnwk;
94  work->CallbackParameter = pv;
95 #ifndef _WIN32
96 
97  if (pcbe->CleanupGroup)
98  ArrayList_Append(pcbe->CleanupGroup->groups, work);
99 
100 #endif
101  }
102 
103  return work;
104 }
105 
106 VOID winpr_CloseThreadpoolWork(PTP_WORK pwk)
107 {
108 #ifdef _WIN32
109  InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
110 
111  if (pCloseThreadpoolWork)
112  {
113  pCloseThreadpoolWork(pwk);
114  return;
115  }
116 
117 #else
118 
119  WINPR_ASSERT(pwk);
120  WINPR_ASSERT(pwk->CallbackEnvironment);
121  if (pwk->CallbackEnvironment->CleanupGroup)
122  ArrayList_Remove(pwk->CallbackEnvironment->CleanupGroup->groups, pwk);
123 
124 #endif
125  free(pwk);
126 }
127 
128 VOID winpr_SubmitThreadpoolWork(PTP_WORK pwk)
129 {
130  PTP_POOL pool = NULL;
131  PTP_CALLBACK_INSTANCE callbackInstance = NULL;
132 #ifdef _WIN32
133  InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
134 
135  if (pSubmitThreadpoolWork)
136  {
137  pSubmitThreadpoolWork(pwk);
138  return;
139  }
140 
141 #endif
142 
143  WINPR_ASSERT(pwk);
144  WINPR_ASSERT(pwk->CallbackEnvironment);
145  pool = pwk->CallbackEnvironment->Pool;
146  callbackInstance = (PTP_CALLBACK_INSTANCE)calloc(1, sizeof(TP_CALLBACK_INSTANCE));
147 
148  if (callbackInstance)
149  {
150  callbackInstance->Work = pwk;
151  CountdownEvent_AddCount(pool->WorkComplete, 1);
152  if (!Queue_Enqueue(pool->PendingQueue, callbackInstance))
153  free(callbackInstance);
154  }
155  // NOLINTNEXTLINE(clang-analyzer-unix.Malloc): Queue_Enqueue takes ownership of callbackInstance
156 }
157 
158 BOOL winpr_TrySubmitThreadpoolCallback(PTP_SIMPLE_CALLBACK pfns, PVOID pv,
160 {
161 #ifdef _WIN32
162  InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
163 
164  if (pTrySubmitThreadpoolCallback)
165  return pTrySubmitThreadpoolCallback(pfns, pv, pcbe);
166 
167 #endif
168  WLog_ERR(TAG, "TrySubmitThreadpoolCallback is not implemented");
169  return FALSE;
170 }
171 
172 VOID winpr_WaitForThreadpoolWorkCallbacks(PTP_WORK pwk, BOOL fCancelPendingCallbacks)
173 {
174  HANDLE event = NULL;
175  PTP_POOL pool = NULL;
176 
177 #ifdef _WIN32
178  InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
179 
180  if (pWaitForThreadpoolWorkCallbacks)
181  {
182  pWaitForThreadpoolWorkCallbacks(pwk, fCancelPendingCallbacks);
183  return;
184  }
185 
186 #endif
187  WINPR_ASSERT(pwk);
188  WINPR_ASSERT(pwk->CallbackEnvironment);
189 
190  pool = pwk->CallbackEnvironment->Pool;
191  WINPR_ASSERT(pool);
192 
193  event = CountdownEvent_WaitHandle(pool->WorkComplete);
194 
195  if (WaitForSingleObject(event, INFINITE) != WAIT_OBJECT_0)
196  WLog_ERR(TAG, "error waiting on work completion");
197 }
198 
199 #endif /* WINPR_THREAD_POOL defined */