/src/FreeRDP/winpr/libwinpr/pool/pool.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /**  | 
2  |  |  * WinPR: Windows Portable Runtime  | 
3  |  |  * Thread Pool API (Pool)  | 
4  |  |  *  | 
5  |  |  * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>  | 
6  |  |  *  | 
7  |  |  * Licensed under the Apache License, Version 2.0 (the "License");  | 
8  |  |  * you may not use this file except in compliance with the License.  | 
9  |  |  * You may obtain a copy of the License at  | 
10  |  |  *  | 
11  |  |  *     http://www.apache.org/licenses/LICENSE-2.0  | 
12  |  |  *  | 
13  |  |  * Unless required by applicable law or agreed to in writing, software  | 
14  |  |  * distributed under the License is distributed on an "AS IS" BASIS,  | 
15  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
16  |  |  * See the License for the specific language governing permissions and  | 
17  |  |  * limitations under the License.  | 
18  |  |  */  | 
19  |  |  | 
20  |  | #include <winpr/config.h>  | 
21  |  |  | 
22  |  | #include <winpr/crt.h>  | 
23  |  | #include <winpr/sysinfo.h>  | 
24  |  | #include <winpr/pool.h>  | 
25  |  | #include <winpr/library.h>  | 
26  |  |  | 
27  |  | #include "pool.h"  | 
28  |  |  | 
29  |  | #ifdef WINPR_THREAD_POOL  | 
30  |  |  | 
31  |  | #ifdef _WIN32  | 
32  |  | static INIT_ONCE init_once_module = INIT_ONCE_STATIC_INIT;  | 
33  |  | static PTP_POOL(WINAPI* pCreateThreadpool)(PVOID reserved);  | 
34  |  | static VOID(WINAPI* pCloseThreadpool)(PTP_POOL ptpp);  | 
35  |  | static BOOL(WINAPI* pSetThreadpoolThreadMinimum)(PTP_POOL ptpp, DWORD cthrdMic);  | 
36  |  | static VOID(WINAPI* pSetThreadpoolThreadMaximum)(PTP_POOL ptpp, DWORD cthrdMost);  | 
37  |  |  | 
38  |  | static BOOL CALLBACK init_module(PINIT_ONCE once, PVOID param, PVOID* context)  | 
39  |  | { | 
40  |  |   HMODULE kernel32 = LoadLibraryA("kernel32.dll"); | 
41  |  |   if (kernel32)  | 
42  |  |   { | 
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*);  | 
49  |  |   }  | 
50  |  |   return TRUE;  | 
51  |  | }  | 
52  |  | #endif  | 
53  |  |  | 
54  |  | static TP_POOL DEFAULT_POOL = { | 
55  |  |   0,    /* DWORD Minimum */  | 
56  |  |   500,  /* DWORD Maximum */  | 
57  |  |   NULL, /* wArrayList* Threads */  | 
58  |  |   NULL, /* wQueue* PendingQueue */  | 
59  |  |   NULL, /* HANDLE TerminateEvent */  | 
60  |  |   NULL, /* wCountdownEvent* WorkComplete */  | 
61  |  | };  | 
62  |  |  | 
63  |  | static DWORD WINAPI thread_pool_work_func(LPVOID arg)  | 
64  | 0  | { | 
65  | 0  |   DWORD status = 0;  | 
66  | 0  |   PTP_POOL pool = NULL;  | 
67  | 0  |   PTP_WORK work = NULL;  | 
68  | 0  |   HANDLE events[2];  | 
69  | 0  |   PTP_CALLBACK_INSTANCE callbackInstance = NULL;  | 
70  |  | 
  | 
71  | 0  |   pool = (PTP_POOL)arg;  | 
72  |  | 
  | 
73  | 0  |   events[0] = pool->TerminateEvent;  | 
74  | 0  |   events[1] = Queue_Event(pool->PendingQueue);  | 
75  |  | 
  | 
76  | 0  |   while (1)  | 
77  | 0  |   { | 
78  | 0  |     status = WaitForMultipleObjects(2, events, FALSE, INFINITE);  | 
79  |  | 
  | 
80  | 0  |     if (status == WAIT_OBJECT_0)  | 
81  | 0  |       break;  | 
82  |  |  | 
83  | 0  |     if (status != (WAIT_OBJECT_0 + 1))  | 
84  | 0  |       break;  | 
85  |  |  | 
86  | 0  |     callbackInstance = (PTP_CALLBACK_INSTANCE)Queue_Dequeue(pool->PendingQueue);  | 
87  |  | 
  | 
88  | 0  |     if (callbackInstance)  | 
89  | 0  |     { | 
90  | 0  |       work = callbackInstance->Work;  | 
91  | 0  |       work->WorkCallback(callbackInstance, work->CallbackParameter, work);  | 
92  | 0  |       CountdownEvent_Signal(pool->WorkComplete, 1);  | 
93  | 0  |       free(callbackInstance);  | 
94  | 0  |     }  | 
95  | 0  |   }  | 
96  |  | 
  | 
97  | 0  |   ExitThread(0);  | 
98  | 0  |   return 0;  | 
99  | 0  | }  | 
100  |  |  | 
101  |  | static void threads_close(void* thread)  | 
102  | 0  | { | 
103  | 0  |   WaitForSingleObject(thread, INFINITE);  | 
104  | 0  |   CloseHandle(thread);  | 
105  | 0  | }  | 
106  |  |  | 
107  |  | static BOOL InitializeThreadpool(PTP_POOL pool)  | 
108  | 0  | { | 
109  | 0  |   BOOL rc = FALSE;  | 
110  | 0  |   wObject* obj = NULL;  | 
111  |  | 
  | 
112  | 0  |   if (pool->Threads)  | 
113  | 0  |     return TRUE;  | 
114  |  |  | 
115  | 0  |   if (!(pool->PendingQueue = Queue_New(TRUE, -1, -1)))  | 
116  | 0  |     goto fail;  | 
117  |  |  | 
118  | 0  |   if (!(pool->WorkComplete = CountdownEvent_New(0)))  | 
119  | 0  |     goto fail;  | 
120  |  |  | 
121  | 0  |   if (!(pool->TerminateEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))  | 
122  | 0  |     goto fail;  | 
123  |  |  | 
124  | 0  |   if (!(pool->Threads = ArrayList_New(TRUE)))  | 
125  | 0  |     goto fail;  | 
126  |  |  | 
127  | 0  |   obj = ArrayList_Object(pool->Threads);  | 
128  | 0  |   obj->fnObjectFree = threads_close;  | 
129  |  | 
  | 
130  | 0  |   SYSTEM_INFO info = { 0 }; | 
131  | 0  |   GetSystemInfo(&info);  | 
132  | 0  |   if (info.dwNumberOfProcessors < 1)  | 
133  | 0  |     info.dwNumberOfProcessors = 1;  | 
134  | 0  |   if (!SetThreadpoolThreadMinimum(pool, info.dwNumberOfProcessors))  | 
135  | 0  |     goto fail;  | 
136  | 0  |   SetThreadpoolThreadMaximum(pool, info.dwNumberOfProcessors);  | 
137  |  | 
  | 
138  | 0  |   rc = TRUE;  | 
139  |  | 
  | 
140  | 0  | fail:  | 
141  | 0  |   return rc;  | 
142  | 0  | }  | 
143  |  |  | 
144  |  | PTP_POOL GetDefaultThreadpool(void)  | 
145  | 0  | { | 
146  | 0  |   PTP_POOL pool = NULL;  | 
147  |  | 
  | 
148  | 0  |   pool = &DEFAULT_POOL;  | 
149  |  | 
  | 
150  | 0  |   if (!InitializeThreadpool(pool))  | 
151  | 0  |     return NULL;  | 
152  |  |  | 
153  | 0  |   return pool;  | 
154  | 0  | }  | 
155  |  |  | 
156  |  | PTP_POOL winpr_CreateThreadpool(PVOID reserved)  | 
157  | 0  | { | 
158  | 0  |   PTP_POOL pool = NULL;  | 
159  |  | #ifdef _WIN32  | 
160  |  |   InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);  | 
161  |  |   if (pCreateThreadpool)  | 
162  |  |     return pCreateThreadpool(reserved);  | 
163  |  | #else  | 
164  | 0  |   WINPR_UNUSED(reserved);  | 
165  | 0  | #endif  | 
166  | 0  |   if (!(pool = (PTP_POOL)calloc(1, sizeof(TP_POOL))))  | 
167  | 0  |     return NULL;  | 
168  |  |  | 
169  | 0  |   if (!InitializeThreadpool(pool))  | 
170  | 0  |   { | 
171  | 0  |     winpr_CloseThreadpool(pool);  | 
172  | 0  |     return NULL;  | 
173  | 0  |   }  | 
174  |  |  | 
175  | 0  |   return pool;  | 
176  | 0  | }  | 
177  |  |  | 
178  |  | VOID winpr_CloseThreadpool(PTP_POOL ptpp)  | 
179  | 0  | { | 
180  |  | #ifdef _WIN32  | 
181  |  |   InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);  | 
182  |  |   if (pCloseThreadpool)  | 
183  |  |   { | 
184  |  |     pCloseThreadpool(ptpp);  | 
185  |  |     return;  | 
186  |  |   }  | 
187  |  | #endif  | 
188  | 0  |   SetEvent(ptpp->TerminateEvent);  | 
189  |  | 
  | 
190  | 0  |   ArrayList_Free(ptpp->Threads);  | 
191  | 0  |   Queue_Free(ptpp->PendingQueue);  | 
192  | 0  |   CountdownEvent_Free(ptpp->WorkComplete);  | 
193  | 0  |   CloseHandle(ptpp->TerminateEvent);  | 
194  |  | 
  | 
195  | 0  |   { | 
196  | 0  |     TP_POOL empty = { 0 }; | 
197  | 0  |     *ptpp = empty;  | 
198  | 0  |   }  | 
199  |  | 
  | 
200  | 0  |   if (ptpp != &DEFAULT_POOL)  | 
201  | 0  |     free(ptpp);  | 
202  | 0  | }  | 
203  |  |  | 
204  |  | BOOL winpr_SetThreadpoolThreadMinimum(PTP_POOL ptpp, DWORD cthrdMic)  | 
205  | 0  | { | 
206  | 0  |   BOOL rc = FALSE;  | 
207  |  | #ifdef _WIN32  | 
208  |  |   InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);  | 
209  |  |   if (pSetThreadpoolThreadMinimum)  | 
210  |  |     return pSetThreadpoolThreadMinimum(ptpp, cthrdMic);  | 
211  |  | #endif  | 
212  | 0  |   ptpp->Minimum = cthrdMic;  | 
213  |  | 
  | 
214  | 0  |   ArrayList_Lock(ptpp->Threads);  | 
215  | 0  |   while (ArrayList_Count(ptpp->Threads) < ptpp->Minimum)  | 
216  | 0  |   { | 
217  | 0  |     HANDLE thread = CreateThread(NULL, 0, thread_pool_work_func, (void*)ptpp, 0, NULL);  | 
218  | 0  |     if (!thread)  | 
219  | 0  |       goto fail;  | 
220  |  |  | 
221  | 0  |     if (!ArrayList_Append(ptpp->Threads, thread))  | 
222  | 0  |     { | 
223  | 0  |       CloseHandle(thread);  | 
224  | 0  |       goto fail;  | 
225  | 0  |     }  | 
226  | 0  |   }  | 
227  |  |  | 
228  | 0  |   rc = TRUE;  | 
229  | 0  | fail:  | 
230  | 0  |   ArrayList_Unlock(ptpp->Threads);  | 
231  |  | 
  | 
232  | 0  |   return rc;  | 
233  | 0  | }  | 
234  |  |  | 
235  |  | VOID winpr_SetThreadpoolThreadMaximum(PTP_POOL ptpp, DWORD cthrdMost)  | 
236  | 0  | { | 
237  |  | #ifdef _WIN32  | 
238  |  |   InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);  | 
239  |  |   if (pSetThreadpoolThreadMaximum)  | 
240  |  |   { | 
241  |  |     pSetThreadpoolThreadMaximum(ptpp, cthrdMost);  | 
242  |  |     return;  | 
243  |  |   }  | 
244  |  | #endif  | 
245  | 0  |   ptpp->Maximum = cthrdMost;  | 
246  |  | 
  | 
247  | 0  |   ArrayList_Lock(ptpp->Threads);  | 
248  | 0  |   if (ArrayList_Count(ptpp->Threads) > ptpp->Maximum)  | 
249  | 0  |   { | 
250  | 0  |     SetEvent(ptpp->TerminateEvent);  | 
251  | 0  |     ArrayList_Clear(ptpp->Threads);  | 
252  | 0  |     ResetEvent(ptpp->TerminateEvent);  | 
253  | 0  |   }  | 
254  | 0  |   ArrayList_Unlock(ptpp->Threads);  | 
255  | 0  |   winpr_SetThreadpoolThreadMinimum(ptpp, ptpp->Minimum);  | 
256  | 0  | }  | 
257  |  |  | 
258  |  | #endif /* WINPR_THREAD_POOL defined */  |