Skip to content

Commit b1211e3

Browse files
committed
updt: renamed everything to be under 'mt_' namespace
1 parent 99c68aa commit b1211e3

File tree

4 files changed

+36
-36
lines changed

4 files changed

+36
-36
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
### A fast, small, efficient threadpool in c
33
This threadpool implementation is much faster, and much feature rich than other notable threadpool implementations found in GitHub. This threadpool has a very small footprint, and can be extensible upto hundreds of threads.
44
##### Features :
5-
1. Small initialization cost. When the function `createPool` returns, it is also guaranteed that all threads are fully initialized and in waiting state.
6-
2. Add hundreds of jobs, asynchronously. The function `addJobToPool` is implemented to guarantee consistency and fast response.
7-
3. Dynamically add and/or remove threads at runtime. You can add as many threads as you want using `addThreadsToPool` function, or remove a thread using `removeThreadFromPool` function, AT RUNTIME. Just remember, for these functions to work properly, the worker function needs to be as atomic as possible. No threads will be removed if all threads are presently busy. Also, to feel the effect of the new thread, there must be some work to be done.
8-
4. Easy suspend and resume. You can suspend the pool anytime using `suspendPool`, and resume where you left off using `resumePool`.
5+
1. Small initialization cost. When the function `mt_create_pool` returns, it is also guaranteed that all threads are fully initialized and in waiting state.
6+
2. Add hundreds of jobs, asynchronously. The function `mt_add_job` is implemented to guarantee consistency and fast response.
7+
3. Dynamically add and/or remove threads at runtime. You can add as many threads as you want using `mt_add_thread` function, or remove a thread using `mt_remove_thread` function, AT RUNTIME. Just remember, for these functions to work properly, the worker function needs to be as atomic as possible. No threads will be removed if all threads are presently busy. Also, to feel the effect of the new thread, there must be some work to be done.
8+
4. Easy suspend and resume. You can suspend the pool anytime using `mt_suspend`, and resume where you left off using `mt_resume`.
99
5. Flexible stopping. You can stop the pool in two ways : a) Stop recieving new jobs, but continue existing jobs and then exit and b) Stop recieving new jobs, and exit as immediately as possible. Remeber, for the latter function to work, the work needs to be atomic. No threads will be force stopped if it is currently executing a function.
1010
##### Proposed additions :
1111
1. Implement force-stop [Low priority]

mytest.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ int main() {
2828
uint64_t size;
2929
printf("\nEnter the number of threads to start from : ");
3030
scanf("%" SCNu64, &size);
31-
ThreadPool *pool = createPool(size);
31+
ThreadPool *pool = mt_create_pool(size);
3232
printf("\nEnter number of jobs : ");
3333
scanf("%" SCNu64, &size);
3434
uint64_t i = 0;
35-
for(i = 0; i < size; i++) addJobToPool(pool, &longJob, NULL);
35+
for(i = 0; i < size; i++) mt_add_job(pool, &longJob, NULL);
3636
char choice = '1';
3737
while(choice >= '0' && choice < '9') {
3838
printf("\n[CHOICE:0] Add some jobs");
@@ -50,22 +50,22 @@ int main() {
5050
case '0':
5151
printf("\n[CHOICE:INPUT] Number of jobs : ");
5252
scanf("%" SCNu64, &size);
53-
for(i = 0; i < size; i++) addJobToPool(pool, &longJob, NULL);
53+
for(i = 0; i < size; i++) mt_add_job(pool, &longJob, NULL);
5454
break;
5555
case '1':
5656
printf("\n[CHOICE:INFO] Pending jobs %" PRIu64,
57-
getJobCount(pool));
57+
mt_get_job_count(pool));
5858
break;
59-
case '2': addThreadsToPool(pool, 1); break;
60-
case '3': removeThreadFromPool(pool); break;
59+
case '2': mt_add_thread(pool, 1); break;
60+
case '3': mt_remove_thread(pool); break;
6161
case '4':
6262
printf("\n[CHOICE:INFO] Number of threads %" PRIu64,
63-
getThreadCount(pool));
63+
mt_get_thread_count(pool));
6464
break;
65-
case '5': waitToComplete(pool); break;
66-
case '6': suspendPool(pool); break;
67-
case '7': resumePool(pool); break;
68-
case '8': destroyPool(pool); return 0;
65+
case '5': mt_join(pool); break;
66+
case '6': mt_suspend(pool); break;
67+
case '7': mt_resume(pool); break;
68+
case '8': mt_destroy_pool(pool); return 0;
6969
default: break;
7070
}
7171
}

mythreads.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ static void *threadExecutor(void *pl) {
333333
/* This method adds 'threads' number of new threads
334334
* to the argument pool. See header for more details.
335335
*/
336-
ThreadPoolStatus addThreadsToPool(ThreadPool *pool, uint64_t threads) {
336+
ThreadPoolStatus mt_add_thread(ThreadPool *pool, uint64_t threads) {
337337
if(pool == NULL) { // Sanity check
338338
printf("\n[THREADPOOL:ADD:ERROR] Pool is not initialized!");
339339
return POOL_NOT_INITIALIZED;
@@ -397,7 +397,7 @@ ThreadPoolStatus addThreadsToPool(ThreadPool *pool, uint64_t threads) {
397397
/* This method removes one thread from the
398398
* argument pool. See header for more details.
399399
*/
400-
void removeThreadFromPool(ThreadPool *pool) {
400+
void mt_remove_thread(ThreadPool *pool) {
401401
if(pool == NULL || !pool->isInitialized) {
402402
printf("\n[THREADPOOL:REM:ERROR] Pool is not initialized!");
403403
return;
@@ -433,7 +433,7 @@ void removeThreadFromPool(ThreadPool *pool) {
433433
* details.
434434
*/
435435

436-
ThreadPool *createPool(uint64_t numThreads) {
436+
ThreadPool *mt_create_pool(uint64_t numThreads) {
437437
ThreadPool *pool = (ThreadPool *)malloc(
438438
sizeof(ThreadPool)); // Allocate memory for the pool
439439
if(pool == NULL) { // Oops!
@@ -489,7 +489,7 @@ ThreadPool *createPool(uint64_t numThreads) {
489489
printf("\n[THREADPOOL:INIT:WARNING] Starting with no threads!");
490490
pool->isInitialized = 1;
491491
} else {
492-
addThreadsToPool(pool, numThreads); // Add threads to the pool
492+
mt_add_thread(pool, numThreads); // Add threads to the pool
493493
#ifdef DEBUG
494494
printf("\n[THREADPOOL:INIT:INFO] Waiting for all threads to start..");
495495
#endif
@@ -509,7 +509,7 @@ ThreadPool *createPool(uint64_t numThreads) {
509509
* details.
510510
*
511511
*/
512-
ThreadPoolStatus addJobToPool(ThreadPool *pool, void (*func)(void *args),
512+
ThreadPoolStatus mt_add_job(ThreadPool *pool, void (*func)(void *args),
513513
void * args) {
514514
if(pool == NULL || !pool->isInitialized) { // Sanity check
515515
printf("\n[THREADPOOL:EXEC:ERROR] Pool is not initialized!");
@@ -588,7 +588,7 @@ ThreadPoolStatus addJobToPool(ThreadPool *pool, void (*func)(void *args),
588588
/* Wait for the pool to finish executing. See header
589589
* for more details.
590590
*/
591-
void waitToComplete(ThreadPool *pool) {
591+
void mt_join(ThreadPool *pool) {
592592
if(pool == NULL || !pool->isInitialized) { // Sanity check
593593
printf("\n[THREADPOOL:WAIT:ERROR] Pool is not initialized!");
594594
return;
@@ -626,7 +626,7 @@ void waitToComplete(ThreadPool *pool) {
626626
/* Suspend all active threads in a pool. See header
627627
* for more details.
628628
*/
629-
void suspendPool(ThreadPool *pool) {
629+
void mt_suspend(ThreadPool *pool) {
630630
if(pool == NULL || !pool->isInitialized) { // Sanity check
631631
printf("\n[THREADPOOL:SUSP:ERROR] Pool is not initialized!");
632632
return;
@@ -660,7 +660,7 @@ void suspendPool(ThreadPool *pool) {
660660
/* Resume a suspended pool. See header for more
661661
* details.
662662
*/
663-
void resumePool(ThreadPool *pool) {
663+
void mt_resume(ThreadPool *pool) {
664664
if(pool == NULL || !pool->isInitialized) { // Sanity check
665665
printf("\n[THREADPOOL:RESM:ERROR] Pool is not initialized!");
666666
return;
@@ -692,21 +692,21 @@ void resumePool(ThreadPool *pool) {
692692
/* Returns number of pending jobs in the pool. See
693693
* header for more details
694694
*/
695-
uint64_t getJobCount(ThreadPool *pool) {
695+
uint64_t mt_get_job_count(ThreadPool *pool) {
696696
return pool->jobCount;
697697
}
698698

699699
/* Returns the number of threads in the pool. See
700700
* header for more details.
701701
*/
702-
uint64_t getThreadCount(ThreadPool *pool) {
702+
uint64_t mt_get_thread_count(ThreadPool *pool) {
703703
return pool->numThreads;
704704
}
705705

706706
/* Destroy the pool. See header for more details.
707707
*
708708
*/
709-
void destroyPool(ThreadPool *pool) {
709+
void mt_destroy_pool(ThreadPool *pool) {
710710
if(pool == NULL || !pool->isInitialized) { // Sanity check
711711
printf("\n[THREADPOOL:EXIT:ERROR] Pool is not initialized!");
712712
return;

mythreads.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ typedef enum Status {
5858
* in case of insufficient memory, which is rare, and a NULL
5959
* is returned in that case.
6060
*/
61-
ThreadPool *createPool(uint64_t);
61+
ThreadPool *mt_create_pool(uint64_t);
6262

6363
/* Waits till all the threads in the pool are finished.
6464
*
6565
* When this method returns, it is assured that all threads
6666
* in the pool have finished executing, and in waiting state.
6767
*/
68-
void waitToComplete(ThreadPool *);
68+
void mt_join(ThreadPool *);
6969

7070
/* Destroys the argument pool.
7171
*
@@ -80,7 +80,7 @@ void waitToComplete(ThreadPool *);
8080
* threads, destroys all synchronization objects, and frees
8181
* any remaining jobs, finally freeing the pool itself.
8282
*/
83-
void destroyPool(ThreadPool *);
83+
void mt_destroy_pool(ThreadPool *);
8484

8585
/* Add a new job to the pool.
8686
*
@@ -96,7 +96,7 @@ void destroyPool(ThreadPool *);
9696
* When all threads are idle, any one of them wakes up and
9797
* executes this function asynchronously.
9898
*/
99-
ThreadPoolStatus addJobToPool(ThreadPool *, void (*func)(void *), void *);
99+
ThreadPoolStatus mt_add_job(ThreadPool *, void (*func)(void *), void *);
100100

101101
/* Add some new threads to the pool.
102102
*
@@ -111,7 +111,7 @@ ThreadPoolStatus addJobToPool(ThreadPool *, void (*func)(void *), void *);
111111
* pthread_create, or for insufficient memory. These error
112112
* codes can be compared using the Status enum above.
113113
*/
114-
ThreadPoolStatus addThreadsToPool(ThreadPool *, uint64_t);
114+
ThreadPoolStatus mt_add_thread(ThreadPool *, uint64_t);
115115

116116
/* Suspend all currently executing threads in the pool.
117117
*
@@ -123,7 +123,7 @@ ThreadPoolStatus addThreadsToPool(ThreadPool *, uint64_t);
123123
* till the thread completes the present job, and then
124124
* halts the thread.
125125
*/
126-
void suspendPool(ThreadPool *);
126+
void mt_suspend(ThreadPool *);
127127

128128
/* Resume a suspended pool.
129129
*
@@ -133,7 +133,7 @@ void suspendPool(ThreadPool *);
133133
* wake up from suspend very soon in future. This method
134134
* fails if the pool was not previously suspended.
135135
*/
136-
void resumePool(ThreadPool *);
136+
void mt_resume(ThreadPool *);
137137

138138
/* Remove an existing thread from the pool.
139139
*
@@ -153,7 +153,7 @@ void resumePool(ThreadPool *);
153153
* the pool, the queue will automatically resume from the
154154
* position where it stopped.
155155
*/
156-
void removeThreadFromPool(ThreadPool *);
156+
void mt_remove_thread(ThreadPool *);
157157

158158
/* Returns the number of pending jobs in the pool.
159159
*
@@ -163,7 +163,7 @@ void removeThreadFromPool(ThreadPool *);
163163
* idlement if no new jobs are added to the pool from this
164164
* instant.
165165
*/
166-
uint64_t getJobCount(ThreadPool *pool);
166+
uint64_t mt_get_job_count(ThreadPool *pool);
167167

168168
/* Returns the number of threads present in the pool.
169169
*
@@ -173,6 +173,6 @@ uint64_t getJobCount(ThreadPool *pool);
173173
* executing a worker function or in idle wait, will be
174174
* returned by this method.
175175
*/
176-
uint64_t getThreadCount(ThreadPool *);
176+
uint64_t mt_get_thread_count(ThreadPool *);
177177

178178
#endif

0 commit comments

Comments
 (0)