-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmythreads.c
734 lines (629 loc) · 21.7 KB
/
mythreads.c
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
/* MyThreads : A small, efficient, and fast threadpool implementation in C
* Copyright (C) 2017 Subhranil Mukherjee (https://github.com/iamsubhranil)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <pthread.h> // The thread library
#include <stdio.h> // Standard output functions in case of errors and debug
#include <stdlib.h> // Memory management functions
#include <inttypes.h> // Standard integer format specifiers
#include "mythreads.h" // API header
/* A singly linked list of threads. This list
* gives tremendous flexibility managing the
* threads at runtime.
*/
typedef struct ThreadList {
pthread_t thread; // The thread object
struct ThreadList *next; // Link to next thread
} ThreadList;
/* A singly linked list of worker functions. This
* list is implemented as a queue to manage the
* execution in the pool.
*/
typedef struct Job {
void (*function)(void *); // The worker function
void *args; // Argument to the function
struct Job *next; // Link to next Job
} Job;
/* The core pool structure. This is the only
* user accessible structure in the API. It contains
* all the primitives necessary to provide
* synchronization between the threads, along with
* dynamic management and execution control.
*/
struct ThreadPool {
/* The FRONT of the thread queue in the pool.
* It typically points to the first thread
* created in the pool.
*/
ThreadList * threads;
/* The REAR of the thread queue in the pool.
* Points to the last, and most young thread
* added to the pool.
*/
ThreadList * rearThreads;
/* Number of threads in the pool. As this can
* grow dynamically, access and modification
* of it is bounded by a mutex.
*/
uint64_t numThreads;
/* The indicator which indicates the number
* of threads to remove. If this is equal to
* N, then N threads will be removed from the
* pool when they are idle. All threads
* typically check the value of this variable
* before executing a job, and if finds the
* value >0, immediately exits.
*/
uint64_t removeThreads;
/* Denotes the number of idle threads in the
* pool at any given instant of time. This value
* is used to check if all threads are idle,
* and thus triggering the end of job queue or
* the initialization of the pool, whichever
* applicable.
*/
volatile uint64_t waitingThreads;
/* Denotes whether the pool is presently
* initalized or not. This variable is used to
* busy wait after the creation of the pool
* to ensure all threads are in waiting state.
*/
volatile uint8_t isInitialized;
/* The main mutex for the job queue. All
* operations on the queue is done after locking
* this mutex to ensure consistency.
*/
pthread_mutex_t queuemutex;
/* This mutex indicates whether a thread is
* presently in idle state or not, and is used
* in conjunction with the conditional below.
*/
pthread_mutex_t condmutex;
/* Conditional to ensure conditional wait.
* When idle, each thread waits on this
* conditional, which is signaled by various
* methods to indicate the wake of the thread.
*/
pthread_cond_t conditional;
/* Ensures pool state. When the pool is running,
* this is set to 1. All the threads loop on
* this condition, and exits immediately when
* it is set to 0, which happens when the pool
* is destroyed.
*/
_Atomic uint8_t run;
/* Used to assign unique thread IDs to each
* running threads. It is an always incremental
* counter.
*/
uint64_t threadID;
/* The FRONT of the job queue, which typically
* points to the job to be executed next.
*/
Job *FRONT;
/* The REAR of the job queue, which points
* to the job last added in the pool.
*/
Job *REAR;
/* Mutex used to denote the end of the job
* queue, which triggers the function
* waitForComplete.
*/
pthread_mutex_t endmutex;
/* Conditional to signal the end of the job
* queue.
*/
pthread_cond_t endconditional;
/* Variable to impose and withdraw
* the suspend state.
*/
uint8_t suspend;
/* Counter to the number of jobs
* present in the job queue
*/
_Atomic uint64_t jobCount;
};
/* The core function which is executed in each thread.
* A pointer to the pool is passed as the argument,
* which controls the flow of execution of the thread.
*/
static void *threadExecutor(void *pl){
ThreadPool *pool = (ThreadPool *)pl; // Get the pool
pthread_mutex_lock(&pool->queuemutex); // Lock the mutex
++pool->threadID; // Get an id
#ifdef DEBUG
uint64_t id = pool->threadID;
#endif
pthread_mutex_unlock(&pool->queuemutex); // Release the mutex
#ifdef DEBUG
printf("\n[THREADPOOL:THREAD%" PRIu64 ":INFO] Starting execution loop!", id);
#endif
//Start the core execution loop
while(pool->run){ // run==1, we should get going
#ifdef DEBUG
printf("\n[THREADPOOL:THREAD%" PRIu64 ":INFO] Trying to lock the mutex!", id);
#endif
pthread_mutex_lock(&pool->queuemutex); //Lock the queue mutex
if(pool->removeThreads>0){ // A thread is needed to be removed
#ifdef DEBUG
printf("\n[THREADPOOL:THREAD%" PRIu64 ":INFO] Removal signalled! Exiting the execution loop!", id);
#endif
pthread_mutex_lock(&pool->condmutex);
pool->numThreads--;
pthread_mutex_unlock(&pool->condmutex);
break; // Exit the loop
}
Job *presentJob = pool->FRONT; // Get the first job
if(presentJob==NULL || pool->suspend){ // Queue is empty!
#ifdef DEBUG
if(presentJob==NULL)
printf("\n[THREADPOOL:THREAD%" PRIu64 ":INFO] Queue is empty! Unlocking the mutex!", id);
else
printf("\n[THREADPOOL:THREAD%" PRIu64 ":INFO] Suspending thread!", id);
#endif
pthread_mutex_unlock(&pool->queuemutex); // Unlock the mutex
pthread_mutex_lock(&pool->condmutex); // Hold the conditional mutex
pool->waitingThreads++; // Add yourself as a waiting thread
#ifdef DEBUG
printf("\n[THREADPOOL:THREAD%" PRIu64 ":INFO] Waiting threads %" PRIu64 "!", id, pool->waitingThreads);
#endif
if(!pool->suspend && pool->waitingThreads==pool->numThreads){ // All threads are idle
#ifdef DEBUG
printf("\n[THREADPOOL:THREAD%" PRIu64 ":INFO] All threads are idle now!", id);
#endif
if(pool->isInitialized){ // Pool is initialized, time to trigger the end conditional
#ifdef DEBUG
printf("\n[THREADPOOL:THREAD%" PRIu64 ":INFO] Signaling endconditional!" ,id);
fflush(stdout);
#endif
pthread_mutex_lock(&pool->endmutex); // Lock the mutex
pthread_cond_signal(&pool->endconditional); // Signal the end
pthread_mutex_unlock(&pool->endmutex); // Release the mutex
#ifdef DEBUG
printf("\n[THREADPOOL:THREAD%" PRIu64 ":INFO] Signalled any monitor!", id);
#endif
}
else // We are initializing the pool
pool->isInitialized = 1; // Break the busy wait
}
#ifdef DEBUG
printf("\n[THREADPOOL:THREAD%" PRIu64 ":INFO] Going to conditional wait!", id);
fflush(stdout);
#endif
pthread_cond_wait(&pool->conditional, &pool->condmutex); // Idle wait on conditional
/* Woke up! */
if(pool->waitingThreads>0) // Unregister youself as a waiting thread
pool->waitingThreads--;
pthread_mutex_unlock(&pool->condmutex); // Woke up! Release the mutex
#ifdef DEBUG
printf("\n[THREADPOOL:THREAD%" PRIu64 ":INFO] Woke up from conditional wait!", id);
#endif
}
else{ // There is a job in the pool
pool->FRONT = pool->FRONT->next; // Shift FRONT to right
pool->jobCount--; // Decrement the count
if(pool->FRONT==NULL) // No jobs next
pool->REAR = NULL; // Reset the REAR
#ifdef DEBUG
else
printf("\n[THREADPOOL:THREAD%" PRIu64 ":INFO] Remaining jobs : %" PRIu64, id, pool->jobCount);
printf("\n[THREADPOOL:THREAD%" PRIu64 ":INFO] Job recieved! Unlocking the mutex!", id);
#endif
pthread_mutex_unlock(&pool->queuemutex); // Unlock the mutex
#ifdef DEBUG
printf("\n[THREADPOOL:THREAD%" PRIu64 ":INFO] Executing the job now!", id);
fflush(stdout);
#endif
presentJob->function(presentJob->args); // Execute the job
#ifdef DEBUG
printf("\n[THREADPOOL:THREAD%" PRIu64 ":INFO] Job completed! Releasing memory for the job!", id);
#endif
free(presentJob); // Release memory for the job
}
}
if(pool->run){ // We exited, but the pool is running! It must be force removal!
#ifdef DEBUG
printf("\n[THREADPOOL:THREAD%" PRIu64 ":INFO] Releasing the lock!", id);
#endif
pool->removeThreads--; // Alright, I'm shutting now
pthread_mutex_unlock(&pool->queuemutex); // We broke the loop, release the mutex now
#ifdef DEBUG
printf("\n[THREADPOOL:THREAD%" PRIu64 ":INFO] Stopping now..", id);
fflush(stdout);
#endif
}
#ifdef DEBUG
else // The pool is stopped
printf("\n[THREADPOOL:THREAD%" PRIu64 ":INFO] Pool has been stopped! Exiting now..", id);
#endif
pthread_exit((void *)COMPLETED); // Exit
}
/* This method adds 'threads' number of new threads
* to the argument pool. See header for more details.
*/
ThreadPoolStatus addThreadsToPool(ThreadPool *pool, uint64_t threads){
if(pool==NULL){ // Sanity check
printf("\n[THREADPOOL:ADD:ERROR] Pool is not initialized!");
return POOL_NOT_INITIALIZED;
}
if(!pool->run){
printf("\n[THREADPOOL:ADD:ERROR] Pool already stopped!");
return POOL_STOPPED;
}
if(threads < 1){
printf("\n[THREADPOOL:ADD:WARNING] Tried to add invalid number of threads %" PRIu64 "!", threads);
return INVALID_NUMBER;
}
int temp = 0;
ThreadPoolStatus rc = COMPLETED;
#ifdef DEBUG
printf("\n[THREADPOOL:ADD:INFO] Holding the condmutex..");
#endif
pthread_mutex_lock(&pool->condmutex);
pool->numThreads += threads; // Increment the thread count to prevent idle signal
pthread_mutex_unlock(&pool->condmutex);
#ifdef DEBUG
printf("\n[THREADPOOL:ADD:INFO] Speculative increment done!");
#endif
uint64_t i = 0;
for(i = 0;i < threads;i++){
ThreadList *newThread = (ThreadList *)malloc(sizeof(ThreadList)); // Allocate a new thread
newThread->next = NULL;
temp = pthread_create(&newThread->thread, NULL, threadExecutor, (void *)pool); // Start the thread
if(temp){
printf("\n[THREADPOOL:ADD:ERROR] Unable to create thread %" PRIu64 "(error code %d)!", (i+1), temp);
pthread_mutex_lock(&pool->condmutex);
pool->numThreads--;
pthread_mutex_unlock(&pool->condmutex);
temp = 0;
rc = THREAD_CREATION_FAILED;
}
else{
#ifdef DEBUG
printf("\n[THREADPOOL:ADD:INFO] Initialized thread %" PRIu64 "!", (i+1));
#endif
if(pool->rearThreads==NULL) // This is the first thread
pool->threads = pool->rearThreads = newThread;
else // There are threads in the pool
pool->rearThreads->next = newThread;
pool->rearThreads = newThread; // This is definitely the last thread
}
}
return rc;
}
/* This method removes one thread from the
* argument pool. See header for more details.
*/
void removeThreadFromPool(ThreadPool *pool){
if(pool==NULL || !pool->isInitialized){
printf("\n[THREADPOOL:REM:ERROR] Pool is not initialized!");
return;
}
if(!pool->run){
printf("\n[THREADPOOL:REM:WARNING] Removing thread from a stopped pool!");
return;
}
#ifdef DEBUG
printf("\n[THREADPOOL:REM:INFO] Acquiring the lock!");
#endif
pthread_mutex_lock(&pool->queuemutex); // Lock the mutex
#ifdef DEBUG
printf("\n[THREADPOOL:REM:INFO] Incrementing the removal count");
#endif
pool->removeThreads++; // Indicate the willingness of removal
pthread_mutex_unlock(&pool->queuemutex); // Unlock the mutex
#ifdef DEBUG
printf("\n[THREADPOOL:REM:INFO] Waking up any sleeping threads!");
#endif
pthread_mutex_lock(&pool->condmutex); // Lock the wait mutex
pthread_cond_signal(&pool->conditional); // Signal any idle threads
pthread_mutex_unlock(&pool->condmutex); // Release the wait mutex
#ifdef DEBUG
printf("\n[THREADPOOL:REM:INFO] Signalling complete!");
#endif
}
/* This method creates a new thread pool containing
* argument number of threads. See header for more
* details.
*/
ThreadPool * createPool(uint64_t numThreads){
ThreadPool * pool = (ThreadPool *)malloc(sizeof(ThreadPool)); // Allocate memory for the pool
if(pool==NULL){ // Oops!
printf("[THREADPOOL:INIT:ERROR] Unable to allocate memory for the pool!");
return NULL;
}
#ifdef DEBUG
printf("\n[THREADPOOL:INIT:INFO] Allocated %zu bytes for new pool!", sizeof(ThreadPool));
#endif
// Initialize members with default values
pool->numThreads = 0;
pool->FRONT = NULL;
pool->REAR = NULL;
pool->waitingThreads = 0;
pool->isInitialized = 0;
pool->removeThreads = 0;
pool->suspend = 0;
pool->rearThreads = NULL;
pool->threads = NULL;
pool->jobCount = 0;
pool->threadID = 0;
#ifdef DEBUG
printf("\n[THREADPOOL:INIT:INFO] Initializing mutexes!");
#endif
pthread_mutex_init(&pool->queuemutex, NULL); // Initialize queue mutex
pthread_mutex_init(&pool->condmutex, NULL); // Initialize idle mutex
pthread_mutex_init(&pool->endmutex, NULL); // Initialize end mutex
#ifdef DEBUG
printf("\n[THREADPOOL:INIT:INFO] Initiliazing conditionals!");
#endif
pthread_cond_init(&pool->endconditional, NULL); // Initialize end conditional
pthread_cond_init(&pool->conditional, NULL); // Initialize idle conditional
pool->run = 1; // Start the pool
#ifdef DEBUG
printf("\n[THREADPOOL:INIT:INFO] Successfully initialized all members of the pool!");
printf("\n[THREADPOOL:INIT:INFO] Initializing %" PRIu64 " threads..",numThreads);
#endif
if(numThreads<1){
printf("\n[THREADPOOL:INIT:WARNING] Starting with no threads!");
pool->isInitialized = 1;
}
else{
addThreadsToPool(pool, numThreads); // Add threads to the pool
#ifdef DEBUG
printf("\n[THREADPOOL:INIT:INFO] Waiting for all threads to start..");
#endif
}
while(!pool->isInitialized); // Busy wait till the pool is initialized
#ifdef DEBUG
printf("\n[THREADPOOL:INIT:INFO] New threadpool initialized successfully!");
#endif
return pool;
}
/* Adds a new job to pool. See header for more
* details.
*
*/
ThreadPoolStatus addJobToPool(ThreadPool *pool, void (*func)(void *args), void *args){
if(pool==NULL || !pool->isInitialized){ // Sanity check
printf("\n[THREADPOOL:EXEC:ERROR] Pool is not initialized!");
return POOL_NOT_INITIALIZED;
}
if(!pool->run){
printf("\n[THREADPOOL:EXEC:ERROR] Trying to add a job in a stopped pool!");
return POOL_STOPPED;
}
if(pool->run==2){
printf("\n[THREADPOOL:EXEC:WARNING] Another thread is waiting for the pool to complete!");
return WAIT_ISSUED;
}
Job *newJob = (Job *)malloc(sizeof(Job)); // Allocate memory
if(newJob==NULL){ // Who uses 2KB RAM nowadays?
printf("\n[THREADPOOL:EXEC:ERROR] Unable to allocate memory for new job!");
return MEMORY_UNAVAILABLE;
}
#ifdef DEBUG
printf("\n[THREADPOOL:EXEC:INFO] Allocated %zu bytes for new job!", sizeof(Job));
#endif
newJob->function = func; // Initialize the function
newJob->args = args; // Initialize the argument
newJob->next = NULL; // Reset the link
#ifdef DEBUG
printf("\n[THREADPOOL:EXEC:INFO] Locking the queue for insertion of the job!");
#endif
pthread_mutex_lock(&pool->queuemutex); // Inserting the job, lock the queue
if(pool->FRONT==NULL) // This is the first job
pool->FRONT = pool->REAR = newJob;
else // There are other jobs
pool->REAR->next = newJob;
pool->REAR = newJob; // This is the last job
pool->jobCount++; // Increment the count
#ifdef DEBUG
printf("\n[THREADPOOL:EXEC:INFO] Inserted the job at the end of the queue!");
#endif
if(pool->waitingThreads>0){ // There are some threads sleeping, wake'em up
#ifdef DEBUG
printf("\n[THREADPOOL:EXEC:INFO] Signaling any idle thread!");
#endif
pthread_mutex_lock(&pool->condmutex); // Lock the mutex
pthread_cond_signal(&pool->conditional); // Signal the conditional
pthread_mutex_unlock(&pool->condmutex); // Release the mutex
#ifdef DEBUG
printf("\n[THREADPOOL:EXEC:INFO] Signaling successful!");
#endif
}
pthread_mutex_unlock(&pool->queuemutex); // Finally, release the queue
#ifdef DEBUG
printf("\n[THREADPOOL:EXEC:INFO] Unlocked the mutex!");
#endif
return COMPLETED;
}
/* Wait for the pool to finish executing. See header
* for more details.
*/
void waitToComplete(ThreadPool *pool){
if(pool==NULL || !pool->isInitialized){ // Sanity check
printf("\n[THREADPOOL:WAIT:ERROR] Pool is not initialized!");
return;
}
if(!pool->run){
printf("\n[THREADPOOL:WAIT:ERROR] Pool already stopped!");
return;
}
pool->run = 2;
pthread_mutex_lock(&pool->condmutex);
if(pool->numThreads==pool->waitingThreads){
#ifdef DEBUG
printf("\n[THREADPOOL:WAIT:INFO] All threads are already idle!");
#endif
pthread_mutex_unlock(&pool->condmutex);
pool->run = 1;
return;
}
pthread_mutex_unlock(&pool->condmutex);
#ifdef DEBUG
printf("\n[THREADPOOL:WAIT:INFO] Waiting for all threads to become idle..");
#endif
pthread_mutex_lock(&pool->endmutex); // Lock the mutex
pthread_cond_wait(&pool->endconditional, &pool->endmutex); // Wait for end signal
pthread_mutex_unlock(&pool->endmutex); // Unlock the mutex
#ifdef DEBUG
printf("\n[THREADPOOL:WAIT:INFO] All threads are idle now!");
#endif
pool->run = 1;
}
/* Suspend all active threads in a pool. See header
* for more details.
*/
void suspendPool(ThreadPool *pool){
if(pool==NULL || !pool->isInitialized){ // Sanity check
printf("\n[THREADPOOL:SUSP:ERROR] Pool is not initialized!");
return;
}
if(!pool->run){ // Pool is stopped
printf("\n[THREADPOOL:SUSP:ERROR] Pool already stopped!");
return;
}
if(pool->suspend){ // Pool is already suspended
printf("\n[THREADPOOL:SUSP:ERROR] Pool already suspended!");
return;
}
#ifdef DEBUG
printf("\n[THREADPOOL:SUSP:INFO] Initiating suspend..");
#endif
pthread_mutex_lock(&pool->queuemutex); // Lock the queue
pool->suspend = 1; // Present the wish for suspension
pthread_mutex_unlock(&pool->queuemutex); // Release the queue
#ifdef DEBUG
printf("\n[THREADPOOL:SUSP:INFO] Waiting for all threads to be idle..");
fflush(stdout);
#endif
while(pool->waitingThreads<pool->numThreads); // Busy wait till all threads are idle
#ifdef DEBUG
printf("\n[THREADPOOL:SUSP:INFO] Successfully suspended all threads!");
#endif
}
/* Resume a suspended pool. See header for more
* details.
*/
void resumePool(ThreadPool *pool){
if(pool==NULL || !pool->isInitialized){ // Sanity check
printf("\n[THREADPOOL:RESM:ERROR] Pool is not initialized!");
return;
}
if(!pool->run){ // Pool stopped
printf("\n[THREADPOOL:RESM:ERROR] Pool is not running!");
return;
}
if(!pool->suspend){ // Pool is not suspended
printf("\n[THREADPOOL:RESM:WARNING] Pool is not suspended!");
return;
}
#ifdef DEBUG
printf("\n[THREADPOOL:RESM:INFO] Initiating resume..");
#endif
pthread_mutex_lock(&pool->condmutex); // Lock the conditional
pool->suspend = 0; // Reset the state
#ifdef DEBUG
printf("\n[THREADPOOL:RESM:INFO] Waking up all threads..");
#endif
pthread_cond_broadcast(&pool->conditional); // Wake up all threads
pthread_mutex_unlock(&pool->condmutex); // Release the mutex
#ifdef DEBUG
printf("\n[THREADPOOL:RESM:INFO] Resume complete!");
#endif
}
/* Returns number of pending jobs in the pool. See
* header for more details
*/
uint64_t getJobCount(ThreadPool *pool){
return pool->jobCount;
}
/* Returns the number of threads in the pool. See
* header for more details.
*/
uint64_t getThreadCount(ThreadPool *pool){
return pool->numThreads;
}
/* Destroy the pool. See header for more details.
*
*/
void destroyPool(ThreadPool *pool){
if(pool==NULL || !pool->isInitialized){ // Sanity check
printf("\n[THREADPOOL:EXIT:ERROR] Pool is not initialized!");
return;
}
#ifdef DEBUG
printf("\n[THREADPOOL:EXIT:INFO] Trying to wakeup all waiting threads..");
#endif
pool->run = 0; // Stop the pool
pthread_mutex_lock(&pool->condmutex);
pthread_cond_broadcast(&pool->conditional); // Wake up all idle threads
pthread_mutex_unlock(&pool->condmutex);
int rc;
#ifdef DEBUG
printf("\n[THREADPOOL:EXIT:INFO] Waiting for all threads to exit..");
#endif
ThreadList *list = pool->threads, *backup = NULL; // For travsersal
uint64_t i = 0;
while(list != NULL){
#ifdef DEBUG
printf("\n[THREADPOOL:EXIT:INFO] Joining thread %" PRIu64 "..", i);
#endif
rc = pthread_join(list->thread, NULL); // Wait for ith thread to join
if(rc)
printf("\n[THREADPOOL:EXIT:WARNING] Unable to join THREAD%" PRIu64 "!", i);
#ifdef DEBUG
else
printf("\n[THREADPOOL:EXIT:INFO] THREAD%" PRIu64 " joined!", i);
#endif
backup = list;
list = list->next; // Continue
#ifdef DEBUG
printf("\n[THREADPOOL:EXIT:INFO] Releasing memory for THREAD%" PRIu64 "..", i);
#endif
free(backup); // Free ith thread
i++;
}
#ifdef DEBUG
printf("\n[THREADPOOL:EXIT:INFO] Destroying remaining jobs..");
#endif
// Delete remaining jobs
while(pool->FRONT!=NULL){
Job *j = pool->FRONT;
pool->FRONT = pool->FRONT->next;
free(j);
}
#ifdef DEBUG
printf("\n[THREADPOOL:EXIT:INFO] Destroying conditionals..");
#endif
rc = pthread_cond_destroy(&pool->conditional); // Destroying idle conditional
rc = pthread_cond_destroy(&pool->endconditional); // Destroying end conditional
if(rc)
printf("\n[THREADPOOL:EXIT:WARNING] Unable to destroy one or more conditionals (error code %d)!", rc);
#ifdef DEBUG
printf("\n[THREADPOOL:EXIT:INFO] Destroying the mutexes..");
#endif
rc = pthread_mutex_destroy(&pool->queuemutex); // Destroying queue lock
rc = pthread_mutex_destroy(&pool->condmutex); // Destroying idle lock
rc = pthread_mutex_destroy(&pool->endmutex); // Destroying end lock
if(rc)
printf("\n[THREADPOOL:EXIT:WARNING] Unable to destroy one or mutexes (error code %d)!", rc);
#ifdef DEBUG
printf("\n[THREADPOOL:EXIT:INFO] Releasing memory for the pool..");
#endif
free(pool); // Release the pool
#ifdef DEBUG
printf("\n[THREADPOOL:EXIT:INFO] Pool destruction completed!");
#endif
}