C-Thread-Pool/thpool.c

297 lines
6.5 KiB
C
Raw Normal View History

2011-11-05 16:35:31 +04:00
/* ********************************
* Author: Johan Hanssen Seferidis
* Date: 12/08/2011
* License: MIT
2014-12-29 14:48:08 +03:00
* Description: Library providing a threading pool where you can add
* work. For an example on usage refer to the main file
* found in the same package
2014-12-29 14:48:08 +03:00
*
2011-11-05 16:35:31 +04:00
*//** @file thpool.h *//*
********************************/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <errno.h>
2014-12-29 19:42:27 +03:00
#include "thpool.h" /* here you can also find the interface to each function */
#define POLLING_INTERVAL 1
2011-11-05 16:35:31 +04:00
2014-12-29 16:14:45 +03:00
static int thpool_keepalive = 1;
2011-11-05 16:35:31 +04:00
/* Create mutex variable */
2014-11-23 14:38:34 +03:00
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; /* used to serialize queue access */
2011-11-05 16:35:31 +04:00
/* Initialise thread pool */
thpool_t* thpool_init(int threadsN){
thpool_t* tp_p;
2014-11-23 14:38:34 +03:00
if (threadsN<0) threadsN=0;
2011-11-05 16:35:31 +04:00
/* Make new thread pool */
2014-12-29 19:42:27 +03:00
tp_p=(thpool_t*)malloc(sizeof(thpool_t));
2011-11-05 16:35:31 +04:00
if (tp_p==NULL){
fprintf(stderr, "thpool_init(): Could not allocate memory for thread pool\n");
return NULL;
}
2014-12-29 19:42:27 +03:00
tp_p->threads=(pthread_t*)malloc(threadsN*sizeof(pthread_t));
2011-11-05 16:35:31 +04:00
if (tp_p->threads==NULL){
fprintf(stderr, "thpool_init(): Could not allocate memory for thread IDs\n");
return NULL;
}
tp_p->threadsN=threadsN;
/* Initialise the job queue */
2014-11-23 14:38:34 +03:00
if (jobqueue_init(tp_p)==-1){
2011-11-05 16:35:31 +04:00
fprintf(stderr, "thpool_init(): Could not allocate memory for job queue\n");
return NULL;
}
/* Make threads in pool */
int t;
for (t=0; t<threadsN; t++){
printf("Created thread %d in pool \n", t);
2014-11-23 14:38:34 +03:00
pthread_create(&(tp_p->threads[t]), NULL, (void *)thpool_thread_do, (void *)tp_p);
2011-11-05 16:35:31 +04:00
}
return tp_p;
}
/* What each individual thread is doing
2014-12-29 19:42:27 +03:00
*
* There are two scenarios here. One is everything works as it should and second if
2011-11-05 16:35:31 +04:00
* the thpool is to be killed. In that manner we try to BYPASS sem_wait and end each thread. */
2014-12-29 19:42:27 +03:00
static void thpool_thread_do(thpool_t* tp_p){
2011-11-05 16:35:31 +04:00
while(thpool_keepalive){
2014-12-29 16:14:45 +03:00
bsem_wait(tp_p->jobqueue->has_jobs);
2011-11-05 16:35:31 +04:00
if (thpool_keepalive){
2014-12-29 16:14:45 +03:00
2011-11-05 16:35:31 +04:00
/* Read job from queue and execute it */
void*(*func_buff)(void* arg);
void* arg_buff;
2014-11-23 14:38:34 +03:00
job_t* job_p;
job_p = jobqueue_pull(tp_p);
2014-12-29 16:14:45 +03:00
if (job_p) {
func_buff=job_p->function;
arg_buff =job_p->arg;
func_buff(arg_buff);
2014-12-29 19:42:27 +03:00
free(job_p);
2014-12-29 16:14:45 +03:00
}
2011-11-05 16:35:31 +04:00
}
else
{
return; /* EXIT thread*/
}
}
return;
}
/* Add work to the thread pool */
int thpool_add_work(thpool_t* tp_p, void *(*function_p)(void*), void* arg_p){
2014-11-23 14:38:34 +03:00
job_t* newJob;
2011-11-05 16:35:31 +04:00
2014-12-29 19:42:27 +03:00
newJob=(job_t*)malloc(sizeof(job_t));
2011-11-05 16:35:31 +04:00
if (newJob==NULL){
fprintf(stderr, "thpool_add_work(): Could not allocate memory for new job\n");
exit(1);
}
/* add function and argument */
newJob->function=function_p;
newJob->arg=arg_p;
/* add job to queue */
2014-11-23 14:38:34 +03:00
jobqueue_push(tp_p, newJob);
return 0;
}
2014-12-29 19:42:27 +03:00
/* Wait until all jobs in queue have finished */
void thpool_wait(thpool_t* tp_p){
while (tp_p->jobqueue->len > 0) {
sleep(POLLING_INTERVAL);
}
2011-11-05 16:35:31 +04:00
}
/* Destroy the threadpool */
void thpool_destroy(thpool_t* tp_p){
int t;
/* End each thread's infinite loop */
thpool_keepalive=0;
/* Awake idle threads waiting at semaphore */
2014-12-29 19:42:27 +03:00
//for (t=0; t<(tp_p->threadsN); t++){
//bsem_post(tp_p->jobqueue->has_jobs);
/*if (){
2011-11-05 16:35:31 +04:00
fprintf(stderr, "thpool_destroy(): Could not bypass sem_wait()\n");
}*/
2014-12-29 19:42:27 +03:00
//}
2011-11-05 16:35:31 +04:00
/* Wait for threads to finish */
2014-12-29 19:42:27 +03:00
for (t=0; t < (tp_p->threadsN); t++){
2011-11-05 16:35:31 +04:00
pthread_join(tp_p->threads[t], NULL);
}
2014-12-29 19:42:27 +03:00
2014-11-23 14:38:34 +03:00
jobqueue_empty(tp_p);
2014-12-29 19:42:27 +03:00
2011-11-05 16:35:31 +04:00
/* Dealloc */
2014-12-29 19:42:27 +03:00
free(tp_p->threads);
free(tp_p->jobqueue);
free(tp_p);
2011-11-05 16:35:31 +04:00
}
/* ===================== JOB QUEUE OPERATIONS ======================= */
2011-11-05 16:35:31 +04:00
/*
* Requirements for job queue:
* - Thread-safe push/pull
* - Binary semaphore for has_jobs must be 1 if there are jobs, 0 if not
*
* */
2011-11-05 16:35:31 +04:00
/* Initialise queue */
2014-12-29 19:42:27 +03:00
static int jobqueue_init(thpool_t* tp_p){
tp_p->jobqueue=(thpool_jobqueue*)malloc(sizeof(thpool_jobqueue));
2011-11-05 16:35:31 +04:00
if (tp_p->jobqueue==NULL) return -1;
tp_p->jobqueue->tail=NULL;
tp_p->jobqueue->head=NULL;
2014-12-29 19:42:27 +03:00
tp_p->jobqueue->has_jobs=(bsem_t*)malloc(sizeof(bsem_t));
tp_p->jobqueue->len = 0;
2011-11-05 16:35:31 +04:00
return 0;
}
2014-11-23 14:38:34 +03:00
/* How many jobs currently in queue */
2014-12-29 19:42:27 +03:00
static int jobqueue_len(thpool_t* tp_p){
return tp_p->jobqueue->len;
2014-11-23 14:38:34 +03:00
}
2011-11-05 16:35:31 +04:00
/* Add job to queue */
2014-12-29 19:42:27 +03:00
static void jobqueue_push(thpool_t* tp_p, job_t* newjob_p){ /* remember that job prev and next point to NULL */
2014-12-29 16:14:45 +03:00
pthread_mutex_lock(&mutex);
newjob_p->next=NULL;
2011-11-05 16:35:31 +04:00
2014-11-23 14:38:34 +03:00
switch(jobqueue_len(tp_p)){
case 0: /* if there are no jobs in queue */
2011-11-05 16:35:31 +04:00
tp_p->jobqueue->tail=newjob_p;
tp_p->jobqueue->head=newjob_p;
break;
2014-11-23 14:38:34 +03:00
default: /* if there are already jobs in queue */
tp_p->jobqueue->tail->next=newjob_p;
newjob_p->prev=tp_p->jobqueue->tail;
tp_p->jobqueue->tail=newjob_p;
2014-11-23 14:38:34 +03:00
}
tp_p->jobqueue->len++;
bsem_post(tp_p->jobqueue->has_jobs);
2014-12-29 16:14:45 +03:00
pthread_mutex_unlock(&mutex);
2011-11-05 16:35:31 +04:00
}
2014-11-23 14:38:34 +03:00
/* Get first element from queue */
2014-12-29 19:42:27 +03:00
static job_t* jobqueue_pull(thpool_t* tp_p){
pthread_mutex_lock(&mutex);
//puts("**** PULLING");
/* get first job */
job_t* job_p;
job_p = tp_p->jobqueue->head;
2014-12-29 19:42:27 +03:00
printf(" queue length: %d\n", jobqueue_len(tp_p));
printf(" has jobs: %d\n", tp_p->jobqueue->has_jobs->v);
2014-11-23 14:38:34 +03:00
/* remove job from queue */
switch(jobqueue_len(tp_p)){
2011-11-05 16:35:31 +04:00
2014-11-23 14:38:34 +03:00
case 0: /* if there are no jobs in queue */
return NULL;
2011-11-05 16:35:31 +04:00
break;
2014-11-23 14:38:34 +03:00
case 1: /* if there is only one job in queue */
2011-11-05 16:35:31 +04:00
tp_p->jobqueue->tail=NULL;
tp_p->jobqueue->head=NULL;
break;
2014-11-23 14:38:34 +03:00
default: /* if there are more than one jobs in queue */
tp_p->jobqueue->head=job_p->next;
job_p->next->prev=tp_p->jobqueue->head;
2011-11-05 16:35:31 +04:00
}
tp_p->jobqueue->len--;
// Make sure has_jobs has right value
if (tp_p->jobqueue->len > 0) {
bsem_post(tp_p->jobqueue->has_jobs);
}
2014-12-29 19:42:27 +03:00
puts(" (after pull)");
printf(" queue length: %d\n", jobqueue_len(tp_p));
printf(" has jobs: %d\n", tp_p->jobqueue->has_jobs->v);
2014-12-29 16:14:45 +03:00
pthread_mutex_unlock(&mutex);
2014-11-23 14:38:34 +03:00
return job_p;
2011-11-05 16:35:31 +04:00
}
2014-11-23 14:38:34 +03:00
2011-11-05 16:35:31 +04:00
/* Remove and deallocate all jobs in queue */
2014-12-29 19:42:27 +03:00
static void jobqueue_empty(thpool_t* tp_p){
2011-11-05 16:35:31 +04:00
2014-11-23 14:38:34 +03:00
job_t* curjob;
2011-11-05 16:35:31 +04:00
curjob=tp_p->jobqueue->tail;
2014-11-23 14:38:34 +03:00
while(jobqueue_len(tp_p)){
2011-11-05 16:35:31 +04:00
tp_p->jobqueue->tail=curjob->prev;
free(curjob);
curjob=tp_p->jobqueue->tail;
}
/* Fix head and tail */
tp_p->jobqueue->tail=NULL;
tp_p->jobqueue->head=NULL;
2014-12-29 16:14:45 +03:00
/* Deallocs */
free(tp_p->jobqueue->has_jobs);
2011-11-05 16:35:31 +04:00
}
2014-12-29 19:42:27 +03:00
/* ======================== SYNCHRONISATION ========================= */
2014-12-29 19:42:27 +03:00
static void bsem_post(bsem_t *bsem) {
pthread_mutex_lock(&bsem->mutex);
bsem->v = 1;
pthread_cond_signal(&bsem->cond);
pthread_mutex_unlock(&bsem->mutex);
}
2014-12-29 19:42:27 +03:00
static void bsem_wait(bsem_t *bsem) {
pthread_mutex_lock(&bsem->mutex);
while (!bsem->v)
pthread_cond_wait(&bsem->cond, &bsem->mutex);
bsem->v = 0;
pthread_mutex_unlock(&bsem->mutex);
}