Coverage Report

Created: 2025-10-10 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/winpr/libwinpr/pool/pool.c
Line
Count
Source
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
  (void)WaitForSingleObject(thread, INFINITE);
104
0
  (void)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
#if !defined(WINPR_THREADPOOL_DEFAULT_MIN_COUNT)
131
#error "WINPR_THREADPOOL_DEFAULT_MIN_COUNT must be defined"
132
#endif
133
#if !defined(WINPR_THREADPOOL_DEFAULT_MAX_COUNT)
134
#error "WINPR_THREADPOOL_DEFAULT_MAX_COUNT must be defined"
135
#endif
136
137
0
  SYSTEM_INFO info = { 0 };
138
0
  GetSystemInfo(&info);
139
140
0
  DWORD min = info.dwNumberOfProcessors;
141
0
  DWORD max = info.dwNumberOfProcessors;
142
0
  if (info.dwNumberOfProcessors < WINPR_THREADPOOL_DEFAULT_MIN_COUNT)
143
0
    min = WINPR_THREADPOOL_DEFAULT_MIN_COUNT;
144
0
  if (info.dwNumberOfProcessors > WINPR_THREADPOOL_DEFAULT_MAX_COUNT)
145
0
    max = WINPR_THREADPOOL_DEFAULT_MAX_COUNT;
146
0
  if (min > max)
147
0
    min = max;
148
149
0
  if (!SetThreadpoolThreadMinimum(pool, min))
150
0
    goto fail;
151
152
0
  SetThreadpoolThreadMaximum(pool, max);
153
154
0
  rc = TRUE;
155
156
0
fail:
157
0
  return rc;
158
0
}
159
160
PTP_POOL GetDefaultThreadpool(void)
161
0
{
162
0
  PTP_POOL pool = &DEFAULT_POOL;
163
164
0
  if (!InitializeThreadpool(pool))
165
0
    return NULL;
166
167
0
  return pool;
168
0
}
169
170
PTP_POOL winpr_CreateThreadpool(PVOID reserved)
171
0
{
172
0
  PTP_POOL pool = NULL;
173
#ifdef _WIN32
174
  InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
175
  if (pCreateThreadpool)
176
    return pCreateThreadpool(reserved);
177
#else
178
0
  WINPR_UNUSED(reserved);
179
0
#endif
180
0
  if (!(pool = (PTP_POOL)calloc(1, sizeof(TP_POOL))))
181
0
    return NULL;
182
183
0
  if (!InitializeThreadpool(pool))
184
0
  {
185
0
    winpr_CloseThreadpool(pool);
186
0
    return NULL;
187
0
  }
188
189
0
  return pool;
190
0
}
191
192
VOID winpr_CloseThreadpool(PTP_POOL ptpp)
193
0
{
194
#ifdef _WIN32
195
  InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
196
  if (pCloseThreadpool)
197
  {
198
    pCloseThreadpool(ptpp);
199
    return;
200
  }
201
#endif
202
0
  (void)SetEvent(ptpp->TerminateEvent);
203
204
0
  ArrayList_Free(ptpp->Threads);
205
0
  Queue_Free(ptpp->PendingQueue);
206
0
  CountdownEvent_Free(ptpp->WorkComplete);
207
0
  (void)CloseHandle(ptpp->TerminateEvent);
208
209
0
  {
210
0
    TP_POOL empty = { 0 };
211
0
    *ptpp = empty;
212
0
  }
213
214
0
  if (ptpp != &DEFAULT_POOL)
215
0
    free(ptpp);
216
0
}
217
218
BOOL winpr_SetThreadpoolThreadMinimum(PTP_POOL ptpp, DWORD cthrdMic)
219
0
{
220
0
  BOOL rc = FALSE;
221
#ifdef _WIN32
222
  InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
223
  if (pSetThreadpoolThreadMinimum)
224
    return pSetThreadpoolThreadMinimum(ptpp, cthrdMic);
225
#endif
226
0
  ptpp->Minimum = cthrdMic;
227
228
0
  ArrayList_Lock(ptpp->Threads);
229
0
  while (ArrayList_Count(ptpp->Threads) < ptpp->Minimum)
230
0
  {
231
0
    HANDLE thread = CreateThread(NULL, 0, thread_pool_work_func, (void*)ptpp, 0, NULL);
232
0
    if (!thread)
233
0
      goto fail;
234
235
0
    if (!ArrayList_Append(ptpp->Threads, thread))
236
0
    {
237
0
      (void)CloseHandle(thread);
238
0
      goto fail;
239
0
    }
240
0
  }
241
242
0
  rc = TRUE;
243
0
fail:
244
0
  ArrayList_Unlock(ptpp->Threads);
245
246
0
  return rc;
247
0
}
248
249
VOID winpr_SetThreadpoolThreadMaximum(PTP_POOL ptpp, DWORD cthrdMost)
250
0
{
251
#ifdef _WIN32
252
  InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
253
  if (pSetThreadpoolThreadMaximum)
254
  {
255
    pSetThreadpoolThreadMaximum(ptpp, cthrdMost);
256
    return;
257
  }
258
#endif
259
0
  ptpp->Maximum = cthrdMost;
260
261
0
  ArrayList_Lock(ptpp->Threads);
262
0
  if (ArrayList_Count(ptpp->Threads) > ptpp->Maximum)
263
0
  {
264
0
    (void)SetEvent(ptpp->TerminateEvent);
265
0
    ArrayList_Clear(ptpp->Threads);
266
0
    (void)ResetEvent(ptpp->TerminateEvent);
267
0
  }
268
0
  ArrayList_Unlock(ptpp->Threads);
269
0
  winpr_SetThreadpoolThreadMinimum(ptpp, ptpp->Minimum);
270
0
}
271
272
#endif /* WINPR_THREAD_POOL defined */