Fix whitespace

This commit is contained in:
Pithikos 2017-01-17 19:59:29 +00:00
parent 48ae25d35a
commit 2ba0bd59fa
4 changed files with 53 additions and 54 deletions

View File

@ -37,7 +37,6 @@ you can use `thpool_wait(thpool);`. If you want to destroy the pool you can use
`thpool_destroy(thpool);`.
## API
For a deeper look into the documentation check in the [thpool.h](https://github.com/Pithikos/C-Thread-Pool/blob/master/thpool.h) file. Below is a fast practical overview.

0
tests/funcs.sh Normal file → Executable file
View File

View File

@ -5,7 +5,7 @@
* work. For usage, check the thpool.h file or README.md
*
*//** @file thpool.h *//*
*
*
********************************/
#include <unistd.h>
@ -14,7 +14,7 @@
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
#include <time.h>
#include <time.h>
#if defined(__linux__)
#include <sys/prctl.h>
#endif
@ -75,7 +75,7 @@ typedef struct thpool_{
volatile int num_threads_working; /* threads currently working */
pthread_mutex_t thcount_lock; /* used for thread count etc */
pthread_cond_t threads_all_idle; /* signal to thpool_wait */
jobqueue* jobqueue_p; /* pointer to the job queue */
jobqueue* jobqueue_p; /* pointer to the job queue */
} thpool_;
@ -148,7 +148,7 @@ struct thpool_* thpool_init(int num_threads){
pthread_mutex_init(&(thpool_p->thcount_lock), NULL);
pthread_cond_init(&thpool_p->threads_all_idle, NULL);
/* Thread init */
int n;
for (n=0; n<num_threads; n++){
@ -156,7 +156,7 @@ struct thpool_* thpool_init(int num_threads){
if (THPOOL_DEBUG)
printf("THPOOL_DEBUG: Created thread %d in pool \n", n);
}
/* Wait for threads to initialize */
while (thpool_p->num_threads_alive != num_threads) {}
@ -206,7 +206,7 @@ void thpool_destroy(thpool_* thpool_p){
/* End each thread 's infinite loop */
threads_keepalive = 0;
/* Give one second to kill idle threads */
double TIMEOUT = 1.0;
time_t start, end;
@ -217,7 +217,7 @@ void thpool_destroy(thpool_* thpool_p){
time (&end);
tpassed = difftime(end,start);
}
/* Poll remaining threads */
while (thpool_p->num_threads_alive){
bsem_post_all(thpool_p->jobqueue_p->has_jobs);
@ -227,7 +227,7 @@ void thpool_destroy(thpool_* thpool_p){
/* Job queue cleanup */
jobqueue_destroy(thpool_p);
free(thpool_p->jobqueue_p);
/* Deallocs */
int n;
for (n=0; n < threads_total; n++){
@ -265,13 +265,13 @@ int thpool_num_threads_working(thpool_* thpool_p){
/* Initialize a thread in the thread pool
*
*
* @param thread address to the pointer of the thread to be created
* @param id id to be given to the thread
* @return 0 on success, -1 otherwise.
*/
static int thread_init (thpool_* thpool_p, struct thread** thread_p, int id){
*thread_p = (struct thread*)malloc(sizeof(struct thread));
if (thread_p == NULL){
fprintf(stderr, "thread_init(): Could not allocate memory for thread\n");
@ -297,10 +297,10 @@ static void thread_hold () {
/* What each thread is doing
*
*
* In principle this is an endless loop. The only time this loop gets interuppted is once
* thpool_destroy() is invoked or the program exits.
*
*
* @param thread thread that will run this function
* @return nothing
*/
@ -321,7 +321,7 @@ static void* thread_do(struct thread* thread_p){
/* Assure all threads have been created before starting serving */
thpool_* thpool_p = thread_p->thpool_p;
/* Register signal handler */
struct sigaction act;
sigemptyset(&act.sa_mask);
@ -330,7 +330,7 @@ static void* thread_do(struct thread* thread_p){
if (sigaction(SIGUSR1, &act, NULL) == -1) {
fprintf(stderr, "thread_do(): cannot handle SIGUSR1");
}
/* Mark thread as alive (initialized) */
pthread_mutex_lock(&thpool_p->thcount_lock);
thpool_p->num_threads_alive += 1;
@ -341,11 +341,11 @@ static void* thread_do(struct thread* thread_p){
bsem_wait(thpool_p->jobqueue_p->has_jobs);
if (threads_keepalive){
pthread_mutex_lock(&thpool_p->thcount_lock);
thpool_p->num_threads_working++;
pthread_mutex_unlock(&thpool_p->thcount_lock);
/* Read job from queue and execute it */
void (*func_buff)(void* arg);
void* arg_buff;
@ -359,7 +359,7 @@ static void* thread_do(struct thread* thread_p){
func_buff(arg_buff);
free(job_p);
}
pthread_mutex_lock(&thpool_p->thcount_lock);
thpool_p->num_threads_working--;
if (!thpool_p->num_threads_working) {
@ -391,7 +391,7 @@ static void thread_destroy (thread* thread_p){
/* Initialize queue */
static int jobqueue_init(thpool_* thpool_p){
thpool_p->jobqueue_p = (struct jobqueue*)malloc(sizeof(struct jobqueue));
if (thpool_p->jobqueue_p == NULL){
return -1;
@ -445,16 +445,16 @@ static void jobqueue_push(thpool_* thpool_p, struct job* newjob){
default: /* if jobs in queue */
thpool_p->jobqueue_p->rear->prev = newjob;
thpool_p->jobqueue_p->rear = newjob;
}
thpool_p->jobqueue_p->len++;
bsem_post(thpool_p->jobqueue_p->has_jobs);
}
/* Get first job from queue(removes it from queue)
*
*
* Notice: Caller MUST hold a mutex
*/
static struct job* jobqueue_pull(thpool_* thpool_p){
@ -463,24 +463,24 @@ static struct job* jobqueue_pull(thpool_* thpool_p){
job_p = thpool_p->jobqueue_p->front;
switch(thpool_p->jobqueue_p->len){
case 0: /* if no jobs in queue */
break;
case 1: /* if one job in queue */
thpool_p->jobqueue_p->front = NULL;
thpool_p->jobqueue_p->rear = NULL;
thpool_p->jobqueue_p->len = 0;
break;
default: /* if >1 jobs in queue */
thpool_p->jobqueue_p->front = job_p->prev;
thpool_p->jobqueue_p->len--;
/* more than one job in queue -> post it */
bsem_post(thpool_p->jobqueue_p->has_jobs);
}
return job_p;
}

View File

@ -1,7 +1,7 @@
/**********************************
/**********************************
* @author Johan Hanssen Seferidis
* License: MIT
*
*
**********************************/
#ifndef _THPOOL_
@ -19,17 +19,17 @@ typedef struct thpool_* threadpool;
/**
* @brief Initialize threadpool
*
*
* Initializes a threadpool. This function will not return untill all
* threads have initialized successfully.
*
*
* @example
*
*
* ..
* threadpool thpool; //First we declare a threadpool
* thpool = thpool_init(4); //then we initialize it to 4 threads
* ..
*
*
* @param num_threads number of threads to be created in the threadpool
* @return threadpool created threadpool on success,
* NULL on error
@ -39,26 +39,26 @@ threadpool thpool_init(int num_threads);
/**
* @brief Add work to the job queue
*
*
* Takes an action and its argument and adds it to the threadpool's job queue.
* If you want to add to work a function with more than one arguments then
* a way to implement this is by passing a pointer to a structure.
*
*
* NOTICE: You have to cast both the function and argument to not get warnings.
*
*
* @example
*
*
* void print_num(int num){
* printf("%d\n", num);
* }
*
*
* int main() {
* ..
* int a = 10;
* thpool_add_work(thpool, (void*)print_num, (void*)a);
* ..
* }
*
*
* @param threadpool threadpool to which the work will be added
* @param function_p pointer to function to add as work
* @param arg_p pointer to an argument
@ -69,19 +69,19 @@ int thpool_add_work(threadpool, void (*function_p)(void*), void* arg_p);
/**
* @brief Wait for all queued jobs to finish
*
*
* Will wait for all jobs - both queued and currently running to finish.
* Once the queue is empty and all work has completed, the calling thread
* (probably the main program) will continue.
*
*
* Smart polling is used in wait. The polling is initially 0 - meaning that
* there is virtually no polling at all. If after 1 seconds the threads
* haven't finished, the polling interval starts growing exponentially
* haven't finished, the polling interval starts growing exponentially
* untill it reaches max_secs seconds. Then it jumps down to a maximum polling
* interval assuming that heavy processing is being used in the threadpool.
*
* @example
*
*
* ..
* threadpool thpool = thpool_init(4);
* ..
@ -90,7 +90,7 @@ int thpool_add_work(threadpool, void (*function_p)(void*), void* arg_p);
* thpool_wait(thpool);
* puts("All added work has finished");
* ..
*
*
* @param threadpool the threadpool to wait for
* @return nothing
*/
@ -99,22 +99,22 @@ void thpool_wait(threadpool);
/**
* @brief Pauses all threads immediately
*
*
* The threads will be paused no matter if they are idle or working.
* The threads return to their previous states once thpool_resume
* is called.
*
*
* While the thread is being paused, new work can be added.
*
*
* @example
*
*
* threadpool thpool = thpool_init(4);
* thpool_pause(thpool);
* ..
* // Add a bunch of work
* ..
* thpool_resume(thpool); // Let the threads start their magic
*
*
* @param threadpool the threadpool where the threads should be paused
* @return nothing
*/
@ -123,14 +123,14 @@ void thpool_pause(threadpool);
/**
* @brief Unpauses all threads if they are paused
*
*
* @example
* ..
* thpool_pause(thpool);
* sleep(10); // Delay execution 10 seconds
* thpool_resume(thpool);
* ..
*
*
* @param threadpool the threadpool where the threads should be unpaused
* @return nothing
*/
@ -139,10 +139,10 @@ void thpool_resume(threadpool);
/**
* @brief Destroy the threadpool
*
*
* This will wait for the currently active threads to finish and then 'kill'
* the whole threadpool to free up memory.
*
*
* @example
* int main() {
* threadpool thpool1 = thpool_init(2);
@ -152,7 +152,7 @@ void thpool_resume(threadpool);
* ..
* return 0;
* }
*
*
* @param threadpool the threadpool to destroy
* @return nothing
*/