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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
|
/*
* Copyright (c) 2017 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
*/
#include <config.h>
#include <stdio.h>
#include <parc/algol/parc_Object.h>
#include <parc/algol/parc_DisplayIndented.h>
#include <parc/algol/parc_Memory.h>
#include <parc/algol/parc_SortedList.h>
#include <parc/algol/parc_LinkedList.h>
#include <parc/concurrent/parc_AtomicUint64.h>
#include <parc/concurrent/parc_ThreadPool.h>
#include <parc/concurrent/parc_Thread.h>
struct PARCThreadPool {
bool continueExistingPeriodicTasksAfterShutdown;
bool executeExistingDelayedTasksAfterShutdown;
bool removeOnCancel;
PARCLinkedList *workQueue;
PARCLinkedList *threads;
int poolSize;
int maximumPoolSize;
long taskCount;
bool isShutdown;
bool isTerminated;
bool isTerminating;
PARCAtomicUint64 *completedTaskCount;
};
static void *
_parcThreadPool_Worker(const PARCThread *thread, const PARCThreadPool *pool)
{
while (parcThread_IsCancelled(thread) == false && pool->isTerminated == false) {
if (parcLinkedList_Lock(pool->workQueue)) {
PARCFutureTask *task = parcLinkedList_RemoveFirst(pool->workQueue);
if (task != NULL) {
parcAtomicUint64_Increment(pool->completedTaskCount);
parcLinkedList_Unlock(pool->workQueue);
parcFutureTask_Run(task);
parcFutureTask_Release(&task);
parcLinkedList_Lock(pool->workQueue);
parcLinkedList_Notify(pool->workQueue);
} else {
parcLinkedList_WaitFor(pool->workQueue, 1000000000);
}
parcLinkedList_Unlock(pool->workQueue);
}
}
return NULL;
}
static void
_parcThreadPool_CancelAll(const PARCThreadPool *pool)
{
PARCIterator *iterator = parcLinkedList_CreateIterator(pool->threads);
while (parcIterator_HasNext(iterator)) {
PARCThread *thread = parcIterator_Next(iterator);
parcThread_Cancel(thread);
}
parcIterator_Release(&iterator);
}
static void
_parcThreadPool_JoinAll(const PARCThreadPool *pool)
{
PARCIterator *iterator = parcLinkedList_CreateIterator(pool->threads);
while (parcIterator_HasNext(iterator)) {
PARCThread *thread = parcIterator_Next(iterator);
parcThread_Join(thread);
}
parcIterator_Release(&iterator);
}
static bool
_parcThreadPool_Destructor(PARCThreadPool **instancePtr)
{
assertNotNull(instancePtr, "Parameter must be a non-null pointer to a PARCThreadPool pointer.");
PARCThreadPool *pool = *instancePtr;
if (pool->isShutdown == false) {
_parcThreadPool_CancelAll(pool);
_parcThreadPool_JoinAll(pool);
}
parcAtomicUint64_Release(&pool->completedTaskCount);
parcLinkedList_Release(&pool->threads);
if (parcObject_Lock(pool->workQueue)) {
parcLinkedList_Release(&pool->workQueue);
}
return true;
}
parcObject_ImplementAcquire(parcThreadPool, PARCThreadPool);
parcObject_ImplementRelease(parcThreadPool, PARCThreadPool);
parcObject_Override(PARCThreadPool, PARCObject,
.isLockable = true,
.destructor = (PARCObjectDestructor *) _parcThreadPool_Destructor,
.copy = (PARCObjectCopy *) parcThreadPool_Copy,
.toString = (PARCObjectToString *) parcThreadPool_ToString,
.equals = (PARCObjectEquals *) parcThreadPool_Equals,
.compare = (PARCObjectCompare *) parcThreadPool_Compare,
.hashCode = (PARCObjectHashCode *) parcThreadPool_HashCode);
void
parcThreadPool_AssertValid(const PARCThreadPool *instance)
{
assertTrue(parcThreadPool_IsValid(instance),
"PARCThreadPool is not valid.");
}
PARCThreadPool *
parcThreadPool_Create(int poolSize)
{
PARCThreadPool *result = parcObject_CreateInstance(PARCThreadPool);
if (result != NULL) {
result->poolSize = poolSize;
result->maximumPoolSize = poolSize;
result->taskCount = 0;
result->isShutdown = false;
result->isTerminated = false;
result->isTerminating = false;
result->workQueue = parcLinkedList_Create();
result->threads = parcLinkedList_Create();
result->completedTaskCount = parcAtomicUint64_Create(0);
result->continueExistingPeriodicTasksAfterShutdown = false;
result->executeExistingDelayedTasksAfterShutdown = false;
result->removeOnCancel = true;
if (parcObject_Lock(result)) {
for (int i = 0; i < poolSize; i++) {
PARCThread *thread = parcThread_Create((void *(*)(PARCThread *, PARCObject *))_parcThreadPool_Worker, (PARCObject *) result);
parcLinkedList_Append(result->threads, thread);
parcThread_Start(thread);
parcThread_Release(&thread);
}
parcObject_Unlock(result);
}
}
return result;
}
int
parcThreadPool_Compare(const PARCThreadPool *instance, const PARCThreadPool *other)
{
int result = 0;
return result;
}
PARCThreadPool *
parcThreadPool_Copy(const PARCThreadPool *original)
{
PARCThreadPool *result = parcThreadPool_Create(original->poolSize);
return result;
}
void
parcThreadPool_Display(const PARCThreadPool *instance, int indentation)
{
parcDisplayIndented_PrintLine(indentation, "PARCThreadPool@%p {", instance);
/* Call Display() functions for the fields here. */
parcDisplayIndented_PrintLine(indentation, "}");
}
bool
parcThreadPool_Equals(const PARCThreadPool *x, const PARCThreadPool *y)
{
bool result = false;
if (x == y) {
result = true;
} else if (x == NULL || y == NULL) {
result = false;
} else {
/* perform instance specific equality tests here. */
if (x->poolSize == y->poolSize) {
result = true;
}
}
return result;
}
PARCHashCode
parcThreadPool_HashCode(const PARCThreadPool *instance)
{
PARCHashCode result = 0;
return result;
}
bool
parcThreadPool_IsValid(const PARCThreadPool *instance)
{
bool result = false;
if (instance != NULL) {
result = true;
}
return result;
}
PARCJSON *
parcThreadPool_ToJSON(const PARCThreadPool *instance)
{
PARCJSON *result = parcJSON_Create();
if (result != NULL) {
}
return result;
}
char *
parcThreadPool_ToString(const PARCThreadPool *instance)
{
char *result = parcMemory_Format("PARCThreadPool@%p\n", instance);
return result;
}
void
parcThreadPool_SetAllowCoreThreadTimeOut(PARCThreadPool *pool, bool value)
{
}
bool
parcThreadPool_GetAllowsCoreThreadTimeOut(const PARCThreadPool *pool)
{
return false;
}
bool
parcThreadPool_AwaitTermination(PARCThreadPool *pool, PARCTimeout *timeout)
{
bool result = false;
if (pool->isTerminating) {
if (parcLinkedList_Lock(pool->workQueue)) {
while (parcLinkedList_Size(pool->workQueue) > 0) {
if (parcTimeout_IsNever(timeout)) {
parcLinkedList_Wait(pool->workQueue);
} else {
// This is not accurate as this will continue the delay, rather than keep a cumulative amount of delay.
uint64_t delay = parcTimeout_InNanoSeconds(timeout);
parcLinkedList_WaitFor(pool->workQueue, delay);
}
}
result = true;
parcLinkedList_Unlock(pool->workQueue);
}
parcThreadPool_ShutdownNow(pool);
}
return result;
}
bool
parcThreadPool_Execute(PARCThreadPool *pool, PARCFutureTask *task)
{
bool result = false;
if (parcThreadPool_Lock(pool)) {
if (pool->isShutdown == false) {
parcThreadPool_Unlock(pool);
if (parcLinkedList_Lock(pool->workQueue)) {
parcLinkedList_Append(pool->workQueue, task);
parcLinkedList_Notify(pool->workQueue);
parcLinkedList_Unlock(pool->workQueue);
result = true;
}
} else {
parcThreadPool_Unlock(pool);
}
}
return result;
}
int
parcThreadPool_GetActiveCount(const PARCThreadPool *pool)
{
return pool->poolSize;
}
uint64_t
parcThreadPool_GetCompletedTaskCount(const PARCThreadPool *pool)
{
return parcAtomicUint64_GetValue(pool->completedTaskCount);
}
int
parcThreadPool_GetCorePoolSize(const PARCThreadPool *pool)
{
return pool->poolSize;
}
PARCTimeout *
parcThreadPool_GetKeepAliveTime(const PARCThreadPool *pool)
{
return PARCTimeout_Never;
}
int
parcThreadPool_GetLargestPoolSize(const PARCThreadPool *pool)
{
return pool->poolSize;
}
int
parcThreadPool_GetMaximumPoolSize(const PARCThreadPool *pool)
{
return pool->maximumPoolSize;
}
int
parcThreadPool_GetPoolSize(const PARCThreadPool *pool)
{
return pool->poolSize;
}
PARCLinkedList *
parcThreadPool_GetQueue(const PARCThreadPool *pool)
{
return pool->workQueue;
}
long
parcThreadPool_GetTaskCount(const PARCThreadPool *pool)
{
return pool->taskCount;
}
bool
parcThreadPool_IsShutdown(const PARCThreadPool *pool)
{
return pool->isShutdown;
}
bool
parcThreadPool_IsTerminated(const PARCThreadPool *pool)
{
return pool->isTerminated;
}
bool
parcThreadPool_IsTerminating(const PARCThreadPool *pool)
{
return pool->isTerminating;
}
int
parcThreadPool_PrestartAllCoreThreads(PARCThreadPool *pool)
{
return 0;
}
bool
parcThreadPool_PrestartCoreThread(PARCThreadPool *pool)
{
return 0;
}
void
parcThreadPool_Purge(PARCThreadPool *pool)
{
}
bool
parcThreadPool_Remove(PARCThreadPool *pool, PARCFutureTask *task)
{
return false;
}
void
parcThreadPool_SetCorePoolSize(PARCThreadPool *pool, int corePoolSize)
{
}
void
parcThreadPool_SetKeepAliveTime(PARCThreadPool *pool, PARCTimeout *timeout)
{
}
void
parcThreadPool_SetMaximumPoolSize(PARCThreadPool *pool, int maximumPoolSize)
{
}
void
parcThreadPool_Shutdown(PARCThreadPool *pool)
{
if (parcThreadPool_Lock(pool)) {
pool->isShutdown = true;
pool->isTerminating = true;
parcThreadPool_Unlock(pool);
}
}
PARCLinkedList *
parcThreadPool_ShutdownNow(PARCThreadPool *pool)
{
parcThreadPool_Shutdown(pool);
// Cause all of the worker threads to exit.
_parcThreadPool_CancelAll(pool);
// Wake them all up so they detect that they are cancelled.
if (parcThreadPool_Lock(pool)) {
parcThreadPool_NotifyAll(pool);
parcThreadPool_Unlock(pool);
}
if (parcLinkedList_Lock(pool->workQueue)) {
parcLinkedList_NotifyAll(pool->workQueue);
parcLinkedList_Unlock(pool->workQueue);
}
// Join with all of them, thereby cleaning up all of them.
_parcThreadPool_JoinAll(pool);
pool->isTerminated = true;
return NULL;
}
|