1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340 | /* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met:|
| |
| 1. Redistributions of source code must retain the above copyright notice, |
| this list of conditions and the following disclaimer. |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------+
*/
#include "gate/threadpools.h"
#include "gate/synchronization.h"
#include "gate/arrays.h"
#include "gate/results.h"
struct gate_threadpool_class
{
gate_mutex_t lock;
gate_synccondition_t cond;
gate_atomic_int_t state;
gate_uint32_t max_pending_tasks;
gate_uint32_t max_threads;
gate_arraylist_t tasks;
gate_thread_t threads[1];
};
#define GATE_THREADPOOL_STATE_OFFLINE 0
#define GATE_THREADPOOL_STATE_STARTING 1
#define GATE_THREADPOOL_STATE_ONLINE 2
#define GATE_THREADPOOL_STATE_STOPPING 3
static gate_bool_t is_starting(gate_threadpool_t pool)
{
return gate_atomic_int_get(&pool->state) == GATE_THREADPOOL_STATE_STARTING;
}
static gate_bool_t is_online(gate_threadpool_t pool)
{
return gate_atomic_int_get(&pool->state) == GATE_THREADPOOL_STATE_ONLINE;
}
static gate_result_t gate_threadpool_code(void* param)
{
gate_result_t ret = GATE_RESULT_OK;
gate_threadpool_t pool = (gate_threadpool_t)param;
void* ptr_entry;
gate_runnable_t* task;<--- The scope of the variable 'task' can be reduced. [+]The scope of the variable 'task' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
{
ret = gate_mutex_acquire(&pool->lock);
if (GATE_FAILED(ret))
{
return ret;
}
while (is_starting(pool))
{
gate_synccondition_timed_wait(&pool->cond, &pool->lock, 200);
}
gate_mutex_release(&pool->lock);
}
while (is_online(pool))
{
task = NULL;
{
ret = gate_mutex_acquire(&pool->lock);
if (GATE_FAILED(ret))
{
break;
}
do
{
while ((gate_arraylist_length(pool->tasks) == 0) && is_online(pool))
{
gate_synccondition_timed_wait(&pool->cond, &pool->lock, 1000);
}
if (!is_online(pool))
{
break;
}
if (gate_arraylist_length(pool->tasks) > 0)
{
ptr_entry = gate_arraylist_get(pool->tasks, 0);
}
else
{
ptr_entry = NULL;
}
if (ptr_entry != NULL)
{
task = *(gate_runnable_t**)ptr_entry;
if (task != NULL)
{
gate_object_retain(task);
}
gate_arraylist_remove(pool->tasks, 0, 1);
}
} while (0);
gate_mutex_release(&pool->lock);
}
if (task != NULL)
{
gate_runnable_run(task);
gate_object_release(task);
}
}
return ret;
}
gate_result_t gate_threadpool_create(gate_threadpool_t* ptr_pool, gate_uint32_t thread_count, gate_uint32_t max_tasks)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_threadpool_t pool = NULL;
do
{
if (thread_count == 0)
{
thread_count = 1;
}
if (max_tasks == 0)
{
max_tasks = 1;
}
pool = gate_mem_alloc(sizeof(struct gate_threadpool_class) + sizeof(gate_thread_t) * thread_count);
if (pool == NULL) break;
gate_mem_clear(pool, sizeof(struct gate_threadpool_class) + sizeof(gate_thread_t) * thread_count);
gate_atomic_int_set(&pool->state, GATE_THREADPOOL_STATE_OFFLINE);
ret = gate_mutex_create(&pool->lock);
if (GATE_FAILED(ret))
{
gate_mem_dealloc(pool);
pool = NULL;
break;
}
ret = gate_synccondition_create(&pool->cond);
GATE_BREAK_IF_FAILED(ret);
pool->tasks = gate_arraylist_create(sizeof(gate_runnable_t*), NULL, 16, &gate_object_ptr_copyctor, &gate_object_ptr_dtor);
if (pool->tasks == NULL)
{
break;
}
pool->max_threads = thread_count;
pool->max_pending_tasks = max_tasks;
*ptr_pool = pool;
pool = NULL;
ret = GATE_RESULT_OK;
} while (0);
if (pool != NULL)
{
if (pool->tasks != NULL)
{
gate_arraylist_release(pool->tasks);
}
gate_synccondition_destroy(&pool->cond);
gate_mutex_destroy(&pool->lock);
}
return ret;
}
gate_result_t gate_threadpool_destroy(gate_threadpool_t pool)
{
gate_result_t ret = GATE_RESULT_OK;
gate_result_t result;
gate_threadpool_stop(pool);
gate_arraylist_release(pool->tasks);
result = gate_synccondition_destroy(&pool->cond);
if (GATE_FAILED(result)) ret = result;
result = gate_mutex_destroy(&pool->lock);
if (GATE_FAILED(result)) ret = result;
gate_mem_dealloc(pool);
return ret;
}
gate_result_t gate_threadpool_start(gate_threadpool_t pool)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_size_t ndx = 0;
if (GATE_THREADPOOL_STATE_OFFLINE != gate_atomic_int_xchg_if(
&pool->state, GATE_THREADPOOL_STATE_OFFLINE, GATE_THREADPOOL_STATE_STARTING))
{
ret = GATE_RESULT_INVALIDSTATE;
}
else
{
for (ndx = 0; ndx != pool->max_threads; ++ndx)
{
ret = gate_thread_start_code(&gate_threadpool_code, pool, &pool->threads[ndx], NULL);
if (GATE_FAILED(ret))
{
gate_atomic_int_set(&pool->state, GATE_THREADPOOL_STATE_STOPPING);
gate_synccondition_signal_all(&pool->cond);
while (ndx-- != 0)
{
gate_thread_join(&pool->threads[ndx], NULL);
}
break;
}
}
if (GATE_SUCCEEDED(ret))
{
gate_atomic_int_set(&pool->state, GATE_THREADPOOL_STATE_ONLINE);
gate_synccondition_signal_all(&pool->cond);
}
else
{
gate_atomic_int_set(&pool->state, GATE_THREADPOOL_STATE_OFFLINE);
}
}
return ret;
}
gate_result_t gate_threadpool_stop(gate_threadpool_t pool)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_size_t ndx;
if (GATE_THREADPOOL_STATE_ONLINE != gate_atomic_int_xchg_if(
&pool->state, GATE_THREADPOOL_STATE_ONLINE, GATE_THREADPOOL_STATE_STOPPING))
{
ret = GATE_RESULT_INVALIDSTATE;
}
else
{
gate_synccondition_signal_all(&pool->cond);
for (ndx = 0; ndx != pool->max_threads; ++ndx)
{
gate_thread_join(&pool->threads[ndx], NULL);
}
gate_atomic_int_set(&pool->state, GATE_THREADPOOL_STATE_OFFLINE);
ret = GATE_RESULT_OK;
}
return ret;
}
gate_result_t gate_threadpool_add_task(gate_threadpool_t pool, gate_runnable_t* task)
{
gate_result_t ret = GATE_RESULT_FAILED;
do
{
ret = gate_mutex_acquire(&pool->lock);
GATE_BREAK_IF_FAILED(ret);
if (gate_arraylist_length(pool->tasks) >= pool->max_pending_tasks)
{
ret = GATE_RESULT_OUTOFBOUNDS;
}
else if (NULL != gate_arraylist_add(pool->tasks, &task))
{
ret = GATE_RESULT_OK;
}
else
{
ret = GATE_RESULT_FAILED;
}
gate_mutex_release(&pool->lock);
gate_synccondition_signal_all(&pool->cond);
} while (0);
return ret;
}
static gate_bool_t gate_threadpool_remove_task_condition(void const* item, void* param)
{
gate_runnable_t* const* src = (gate_runnable_t* const*)item;
gate_runnable_t* dst = (gate_runnable_t*)param;
return *src == dst;
}
gate_result_t gate_threadpool_remove_task(gate_threadpool_t pool, gate_runnable_t* task)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_size_t count1, count2;
do
{
ret = gate_mutex_acquire(&pool->lock);
GATE_BREAK_IF_FAILED(ret);
count1 = gate_arraylist_length(pool->tasks);
ret = gate_arraylist_remove_if(pool->tasks, gate_threadpool_remove_task_condition, task);
if (GATE_SUCCEEDED(ret))
{
count2 = gate_arraylist_length(pool->tasks);
if (count2 == count1)
{
/* nothing was removed, signal "unchanged" OK-state */
ret = GATE_RESULT_OK_UNCHANGED;
}
}
gate_mutex_release(&pool->lock);
} while (0);
return ret;
}
gate_size_t gate_threadpool_pending_tasks(gate_threadpool_t pool)
{
gate_size_t ret = 0;
gate_result_t result = gate_mutex_acquire(&pool->lock);
if (GATE_SUCCEEDED(result))
{
ret = gate_arraylist_length(pool->tasks);
gate_mutex_release(&pool->lock);
}
return ret;
}
|