Coverage Report

Created: 2023-09-25 06:56

/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/pool.h>
24
#include <winpr/library.h>
25
26
#include "pool.h"
27
28
#ifdef WINPR_THREAD_POOL
29
30
#ifdef _WIN32
31
static INIT_ONCE init_once_module = INIT_ONCE_STATIC_INIT;
32
static PTP_POOL(WINAPI* pCreateThreadpool)(PVOID reserved);
33
static VOID(WINAPI* pCloseThreadpool)(PTP_POOL ptpp);
34
static BOOL(WINAPI* pSetThreadpoolThreadMinimum)(PTP_POOL ptpp, DWORD cthrdMic);
35
static VOID(WINAPI* pSetThreadpoolThreadMaximum)(PTP_POOL ptpp, DWORD cthrdMost);
36
37
static BOOL CALLBACK init_module(PINIT_ONCE once, PVOID param, PVOID* context)
38
{
39
  HMODULE kernel32 = LoadLibraryA("kernel32.dll");
40
  if (kernel32)
41
  {
42
    pCreateThreadpool = (void*)GetProcAddress(kernel32, "CreateThreadpool");
43
    pCloseThreadpool = (void*)GetProcAddress(kernel32, "CloseThreadpool");
44
    pSetThreadpoolThreadMinimum = (void*)GetProcAddress(kernel32, "SetThreadpoolThreadMinimum");
45
    pSetThreadpoolThreadMaximum = (void*)GetProcAddress(kernel32, "SetThreadpoolThreadMaximum");
46
  }
47
  return TRUE;
48
}
49
#endif
50
51
static TP_POOL DEFAULT_POOL = {
52
  0,    /* DWORD Minimum */
53
  500,  /* DWORD Maximum */
54
  NULL, /* wArrayList* Threads */
55
  NULL, /* wQueue* PendingQueue */
56
  NULL, /* HANDLE TerminateEvent */
57
  NULL, /* wCountdownEvent* WorkComplete */
58
};
59
60
static DWORD WINAPI thread_pool_work_func(LPVOID arg)
61
0
{
62
0
  DWORD status;
63
0
  PTP_POOL pool;
64
0
  PTP_WORK work;
65
0
  HANDLE events[2];
66
0
  PTP_CALLBACK_INSTANCE callbackInstance;
67
68
0
  pool = (PTP_POOL)arg;
69
70
0
  events[0] = pool->TerminateEvent;
71
0
  events[1] = Queue_Event(pool->PendingQueue);
72
73
0
  while (1)
74
0
  {
75
0
    status = WaitForMultipleObjects(2, events, FALSE, INFINITE);
76
77
0
    if (status == WAIT_OBJECT_0)
78
0
      break;
79
80
0
    if (status != (WAIT_OBJECT_0 + 1))
81
0
      break;
82
83
0
    callbackInstance = (PTP_CALLBACK_INSTANCE)Queue_Dequeue(pool->PendingQueue);
84
85
0
    if (callbackInstance)
86
0
    {
87
0
      work = callbackInstance->Work;
88
0
      work->WorkCallback(callbackInstance, work->CallbackParameter, work);
89
0
      CountdownEvent_Signal(pool->WorkComplete, 1);
90
0
      free(callbackInstance);
91
0
    }
92
0
  }
93
94
0
  ExitThread(0);
95
0
  return 0;
96
0
}
97
98
static void threads_close(void* thread)
99
0
{
100
0
  WaitForSingleObject(thread, INFINITE);
101
0
  CloseHandle(thread);
102
0
}
103
104
static BOOL InitializeThreadpool(PTP_POOL pool)
105
0
{
106
0
  BOOL rc = FALSE;
107
0
  int index;
108
0
  wObject* obj;
109
0
  HANDLE thread;
110
111
0
  if (pool->Threads)
112
0
    return TRUE;
113
114
0
  pool->Minimum = 0;
115
0
  pool->Maximum = 500;
116
117
0
  if (!(pool->PendingQueue = Queue_New(TRUE, -1, -1)))
118
0
    goto fail;
119
120
0
  if (!(pool->WorkComplete = CountdownEvent_New(0)))
121
0
    goto fail;
122
123
0
  if (!(pool->TerminateEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
124
0
    goto fail;
125
126
0
  if (!(pool->Threads = ArrayList_New(TRUE)))
127
0
    goto fail;
128
129
0
  obj = ArrayList_Object(pool->Threads);
130
0
  obj->fnObjectFree = threads_close;
131
132
0
  for (index = 0; index < 4; index++)
133
0
  {
134
0
    if (!(thread = CreateThread(NULL, 0, thread_pool_work_func, (void*)pool, 0, NULL)))
135
0
    {
136
0
      goto fail;
137
0
    }
138
139
0
    if (!ArrayList_Append(pool->Threads, thread))
140
0
    {
141
0
      CloseHandle(thread);
142
0
      goto fail;
143
0
    }
144
0
  }
145
146
0
  rc = TRUE;
147
148
0
fail:
149
0
  return rc;
150
0
}
151
152
PTP_POOL GetDefaultThreadpool(void)
153
0
{
154
0
  PTP_POOL pool = NULL;
155
156
0
  pool = &DEFAULT_POOL;
157
158
0
  if (!InitializeThreadpool(pool))
159
0
    return NULL;
160
161
0
  return pool;
162
0
}
163
164
PTP_POOL winpr_CreateThreadpool(PVOID reserved)
165
0
{
166
0
  PTP_POOL pool = NULL;
167
#ifdef _WIN32
168
  InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
169
  if (pCreateThreadpool)
170
    return pCreateThreadpool(reserved);
171
#else
172
0
  WINPR_UNUSED(reserved);
173
0
#endif
174
0
  if (!(pool = (PTP_POOL)calloc(1, sizeof(TP_POOL))))
175
0
    return NULL;
176
177
0
  if (!InitializeThreadpool(pool))
178
0
  {
179
0
    winpr_CloseThreadpool(pool);
180
0
    return NULL;
181
0
  }
182
183
0
  return pool;
184
0
}
185
186
VOID winpr_CloseThreadpool(PTP_POOL ptpp)
187
0
{
188
#ifdef _WIN32
189
  InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
190
  if (pCloseThreadpool)
191
  {
192
    pCloseThreadpool(ptpp);
193
    return;
194
  }
195
#endif
196
0
  SetEvent(ptpp->TerminateEvent);
197
198
0
  ArrayList_Free(ptpp->Threads);
199
0
  Queue_Free(ptpp->PendingQueue);
200
0
  CountdownEvent_Free(ptpp->WorkComplete);
201
0
  CloseHandle(ptpp->TerminateEvent);
202
203
0
  {
204
0
    TP_POOL empty = { 0 };
205
0
    *ptpp = empty;
206
0
  }
207
208
0
  if (ptpp != &DEFAULT_POOL)
209
0
    free(ptpp);
210
0
}
211
212
BOOL winpr_SetThreadpoolThreadMinimum(PTP_POOL ptpp, DWORD cthrdMic)
213
0
{
214
0
  HANDLE thread;
215
#ifdef _WIN32
216
  InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
217
  if (pSetThreadpoolThreadMinimum)
218
    return pSetThreadpoolThreadMinimum(ptpp, cthrdMic);
219
#endif
220
0
  ptpp->Minimum = cthrdMic;
221
222
0
  while (ArrayList_Count(ptpp->Threads) < ptpp->Minimum)
223
0
  {
224
0
    if (!(thread = CreateThread(NULL, 0, thread_pool_work_func, (void*)ptpp, 0, NULL)))
225
0
    {
226
0
      return FALSE;
227
0
    }
228
229
0
    if (!ArrayList_Append(ptpp->Threads, thread))
230
0
    {
231
0
      CloseHandle(thread);
232
0
      return FALSE;
233
0
    }
234
0
  }
235
236
0
  return TRUE;
237
0
}
238
239
VOID winpr_SetThreadpoolThreadMaximum(PTP_POOL ptpp, DWORD cthrdMost)
240
0
{
241
#ifdef _WIN32
242
  InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
243
  if (pSetThreadpoolThreadMaximum)
244
  {
245
    pSetThreadpoolThreadMaximum(ptpp, cthrdMost);
246
    return;
247
  }
248
#endif
249
0
  ptpp->Maximum = cthrdMost;
250
0
}
251
252
#endif /* WINPR_THREAD_POOL defined */