C-Thread-Pool/thpool.c

403 lines
8.6 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 *//*
********************************/
2014-12-31 15:09:10 +03:00
#include <unistd.h>
2015-01-01 15:02:06 +03:00
#include <signal.h>
2011-11-05 16:35:31 +04:00
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
2014-12-31 15:09:10 +03:00
#include <time.h>
2011-11-05 16:35:31 +04:00
2014-12-30 15:28:43 +03:00
#include "thpool.h" /* here you can also find the interface to each function */
2014-12-30 15:56:19 +03:00
#define POLLING_INTERVAL 1
2014-12-30 15:56:19 +03:00
2015-01-09 17:40:40 +03:00
static volatile int threads_keepalive;
static volatile int threads_on_hold;
2011-11-05 16:35:31 +04:00
2014-12-30 15:56:19 +03:00
2015-01-02 22:23:22 +03:00
/* ========================== THREADPOOL ============================ */
2011-11-05 16:35:31 +04:00
/* Initialise thread pool */
thpool_t* thpool_init(int threadsN){
2014-12-31 15:09:10 +03:00
2015-01-02 22:23:22 +03:00
threads_on_hold = 0;
2015-01-01 15:02:06 +03:00
threads_keepalive = 1;
2015-01-02 22:23:22 +03:00
if ( threadsN < 0){
2014-12-31 15:09:10 +03:00
threadsN = 0;
}
2015-01-02 22:23:22 +03:00
2011-11-05 16:35:31 +04:00
/* Make new thread pool */
2014-12-31 15:09:10 +03:00
thpool_t* thpool;
thpool = (thpool_t*)malloc(sizeof(thpool_t));
2014-12-30 15:32:15 +03:00
if (thpool==NULL){
2011-11-05 16:35:31 +04:00
fprintf(stderr, "thpool_init(): Could not allocate memory for thread pool\n");
2015-01-02 22:23:22 +03:00
exit(1);
2011-11-05 16:35:31 +04:00
}
2015-01-02 22:23:22 +03:00
thpool->threads_alive = 0;
2014-12-31 15:09:10 +03:00
2011-11-05 16:35:31 +04:00
/* Initialise the job queue */
2014-12-30 15:32:15 +03:00
if (jobqueue_init(thpool)==-1){
2011-11-05 16:35:31 +04:00
fprintf(stderr, "thpool_init(): Could not allocate memory for job queue\n");
2015-01-02 22:23:22 +03:00
exit(1);
2011-11-05 16:35:31 +04:00
}
2014-12-31 15:09:10 +03:00
2011-11-05 16:35:31 +04:00
/* Make threads in pool */
2015-01-02 22:23:22 +03:00
thpool->threads = (thread_t**)malloc(threadsN * sizeof(thread_t));
if (thpool->threads==NULL){
fprintf(stderr, "thpool_init(): Could not allocate memory for threads\n");
2015-01-02 22:23:22 +03:00
exit(1);
}
2015-01-02 22:23:22 +03:00
/* Thread init */
2014-12-31 15:09:10 +03:00
int n;
for (n=0; n<threadsN; n++){
2015-01-01 15:02:06 +03:00
2015-01-09 17:40:40 +03:00
//thread_init(thpool, &thpool->threads[n], n);
//printf("Created thread %d in pool \n", n);
thpool->threads[n] = malloc(sizeof(thread_t));
thpool->threads[n]->thpool = thpool;
thpool->threads[n]->id = n;
pthread_create(&thpool->threads[n]->pthread, NULL, (void *)thread_do, thpool->threads[n]);
pthread_detach(thpool->threads[n]->pthread);
2015-01-02 22:23:22 +03:00
2015-01-09 17:40:40 +03:00
2011-11-05 16:35:31 +04:00
}
2015-01-02 22:23:22 +03:00
/* Wait for threads to initialize */
while (thpool->threads_alive != threadsN) {}
2015-01-01 15:02:06 +03:00
return thpool;
2011-11-05 16:35:31 +04:00
}
/* Add work to the thread pool */
2014-12-30 15:32:15 +03:00
int thpool_add_work(thpool_t* thpool, void *(*function_p)(void*), void* arg_p){
2014-12-31 15:09:10 +03:00
job_t* newjob;
2015-01-02 22:23:22 +03:00
2014-12-31 15:09:10 +03:00
newjob=(job_t*)malloc(sizeof(job_t));
if (newjob==NULL){
2011-11-05 16:35:31 +04:00
fprintf(stderr, "thpool_add_work(): Could not allocate memory for new job\n");
2014-12-31 15:09:10 +03:00
return -1;
2011-11-05 16:35:31 +04:00
}
2015-01-02 22:23:22 +03:00
2011-11-05 16:35:31 +04:00
/* add function and argument */
2014-12-31 15:09:10 +03:00
newjob->function=function_p;
newjob->arg=arg_p;
2015-01-02 22:23:22 +03:00
2011-11-05 16:35:31 +04:00
/* add job to queue */
2015-01-02 22:23:22 +03:00
pthread_mutex_lock(&thpool->jobqueue->rwmutex);
2014-12-31 15:09:10 +03:00
jobqueue_push(thpool, newjob);
2015-01-02 22:23:22 +03:00
pthread_mutex_unlock(&thpool->jobqueue->rwmutex);
return 0;
}
2014-12-29 19:42:27 +03:00
/* Wait until all jobs in queue have finished */
2014-12-30 15:32:15 +03:00
void thpool_wait(thpool_t* thpool){
2015-01-01 15:02:06 +03:00
while (thpool->jobqueue->len) {
sleep(POLLING_INTERVAL);
}
2011-11-05 16:35:31 +04:00
}
/* Destroy the threadpool */
2014-12-30 15:32:15 +03:00
void thpool_destroy(thpool_t* thpool){
2015-01-09 17:40:40 +03:00
int threads_total = thpool->threads_alive;
2014-12-30 15:28:43 +03:00
2015-01-01 15:02:06 +03:00
/* End each thread 's infinite loop */
threads_keepalive = 0;
2015-01-02 22:23:22 +03:00
/* Give one second to kill idle threads */
2014-12-31 15:09:10 +03:00
double TIMEOUT = 1.0;
2014-12-31 15:34:52 +03:00
time_t start, end;
2014-12-31 15:09:10 +03:00
double tpassed;
time (&start);
2015-01-02 22:23:22 +03:00
while (tpassed < TIMEOUT && thpool->threads_alive){
2014-12-31 15:09:10 +03:00
bsem_post(thpool->jobqueue->has_jobs);
2015-01-02 22:23:22 +03:00
time (&end);
tpassed = difftime(end,start);
}
/* Poll remaining threads */
while (thpool->threads_alive){
bsem_post(thpool->jobqueue->has_jobs);
sleep(1);
2015-01-01 15:02:06 +03:00
}
2014-12-29 19:42:27 +03:00
2014-12-31 15:09:10 +03:00
/* Job queue cleanup */
jobqueue_destroy(thpool);
2014-12-31 15:34:52 +03:00
free(thpool->jobqueue);
2014-12-31 15:09:10 +03:00
2015-01-09 17:40:40 +03:00
/* Deallocs */
int n;
for (n=0; n < threads_total; n++){
puts("FREEING THREAD");
//thread_destroy(thpool->threads[n]);
free(thpool->threads[n]);
}
2014-12-31 18:17:11 +03:00
free(thpool->threads);
free(thpool);
2015-01-01 15:02:06 +03:00
}
void thpool_pause(thpool_t* thpool) {
2015-01-02 22:23:22 +03:00
int n;
for (n=0; n < thpool->threads_alive; n++){
pthread_kill(thpool->threads[n]->pthread, SIGUSR1);
}
2015-01-01 15:02:06 +03:00
}
2015-01-09 17:40:40 +03:00
void thpool_resume(thpool_t* thpool) {
2015-01-02 22:23:22 +03:00
threads_on_hold = 0;
2015-01-01 15:02:06 +03:00
}
2015-01-02 22:23:22 +03:00
/* ============================ THREAD ============================== */
2015-01-01 15:02:06 +03:00
2015-01-09 17:40:40 +03:00
void thread_init (thpool_t *thpool, thread_t **thread, int id){
2015-01-02 22:23:22 +03:00
2015-01-09 17:40:40 +03:00
*thread = (thread_t*)malloc(sizeof(thread_t));
2015-01-02 22:23:22 +03:00
if (thread == NULL){
fprintf(stderr, "thpool_init(): Could not allocate memory for thread\n");
exit(1);
2015-01-01 15:02:06 +03:00
}
2015-01-09 17:40:40 +03:00
(*thread)->thpool = thpool;
(*thread)->id = id;
2015-01-01 15:02:06 +03:00
2015-01-09 17:40:40 +03:00
pthread_create(&(*thread)->pthread, NULL, (void *)thread_do, (*thread));
pthread_detach((*thread)->pthread);
2015-01-02 22:23:22 +03:00
2015-01-01 15:02:06 +03:00
}
2015-01-02 22:23:22 +03:00
static void thread_hold () {
threads_on_hold = 1;
while (threads_on_hold){
2015-01-01 15:02:06 +03:00
sleep(1);
}
}
2015-01-02 22:23:22 +03:00
static void* thread_do(thread_t* thread){
2015-01-01 15:02:06 +03:00
/* Assure all threads have been created before starting serving */
2015-01-02 22:23:22 +03:00
thpool_t* thpool = thread->thpool;
2015-01-01 15:02:06 +03:00
/* Register signal handler */
struct sigaction act;
2015-01-02 22:23:22 +03:00
act.sa_handler = thread_hold;
2015-01-01 15:02:06 +03:00
if (sigaction(SIGUSR1, &act, NULL) == -1) {
perror("Error: cannot handle SIGUSR1");
}
2015-01-02 22:23:22 +03:00
/* Mark thread as alive (initialized) */
pthread_mutex_lock(&thpool->thcount_lock);
thpool->threads_alive += 1;
pthread_mutex_unlock(&thpool->thcount_lock);
//printf("Thread (%u) initialized init?: %d\n", (int)pthread_self(), (*thread).initialized);
//printf("Thread (%u) init adr: %p\n", (int)pthread_self(), &(*thread).initialized);
//printf("Thread (%u) id: %d\n", (int)pthread_self(), (*thread).id);
2015-01-01 15:02:06 +03:00
while(threads_keepalive){
bsem_wait(thpool->jobqueue->has_jobs);
if (threads_keepalive){
/* Read job from queue and execute it */
void*(*func_buff)(void* arg);
void* arg_buff;
job_t* job;
2015-01-02 22:23:22 +03:00
pthread_mutex_lock(&thpool->jobqueue->rwmutex);
2015-01-01 15:02:06 +03:00
job = jobqueue_pull(thpool);
2015-01-02 22:23:22 +03:00
pthread_mutex_unlock(&thpool->jobqueue->rwmutex);
2015-01-01 15:02:06 +03:00
if (job) {
func_buff = job->function;
arg_buff = job->arg;
func_buff(arg_buff);
free(job);
}
2015-01-02 22:23:22 +03:00
2015-01-01 15:02:06 +03:00
}
}
2015-01-02 22:23:22 +03:00
pthread_mutex_lock(&thpool->thcount_lock);
thpool->threads_alive --;
pthread_mutex_unlock(&thpool->thcount_lock);
2015-01-01 15:02:06 +03:00
printf("Thread %d exiting\n", (*thread).id);
2015-01-09 20:35:26 +03:00
//pthread_exit(NULL);
return NULL;
2015-01-02 22:23:22 +03:00
}
static void thread_destroy (thread_t* thread){
2015-01-09 17:40:40 +03:00
//puts("Destroying thread");
2015-01-02 22:23:22 +03:00
free(thread);
2011-11-05 16:35:31 +04:00
}
2015-01-02 22:23:22 +03:00
/* ============================ JOB QUEUE =========================== */
2014-12-30 15:56:19 +03:00
2011-11-05 16:35:31 +04:00
/* Initialise queue */
2014-12-30 15:32:15 +03:00
static int jobqueue_init(thpool_t* thpool){
thpool->jobqueue=(jobqueue_t*)malloc(sizeof(jobqueue_t));
2014-12-31 15:09:10 +03:00
if (thpool->jobqueue==NULL){
return -1;
}
2014-12-30 15:32:15 +03:00
thpool->jobqueue->has_jobs = (bsem_t*)malloc(sizeof(bsem_t));
2015-01-02 22:23:22 +03:00
bsem_init(thpool->jobqueue->has_jobs, 0);
2014-12-31 15:09:10 +03:00
jobqueue_clear(thpool);
return 0;
}
/* Clear queue */
static void jobqueue_clear(thpool_t* thpool){
2014-12-31 15:34:52 +03:00
job_t* job;
while(thpool->jobqueue->len){
free(jobqueue_pull(thpool));
}
2014-12-31 15:09:10 +03:00
thpool->jobqueue->front = NULL;
2014-12-31 15:34:52 +03:00
thpool->jobqueue->rear = NULL;
2014-12-31 15:09:10 +03:00
thpool->jobqueue->has_jobs->v = 0;
2014-12-30 15:32:15 +03:00
thpool->jobqueue->len = 0;
2014-12-31 15:09:10 +03:00
2011-11-05 16:35:31 +04:00
}
/* Add job to queue */
2014-12-30 15:32:15 +03:00
static void jobqueue_push(thpool_t* thpool, job_t* newjob){ /* remember that job prev and next point to NULL */
2014-12-31 15:09:10 +03:00
newjob->prev = NULL;
2011-11-05 16:35:31 +04:00
2014-12-30 15:32:15 +03:00
switch(thpool->jobqueue->len){
2014-11-23 14:38:34 +03:00
2014-12-31 15:34:52 +03:00
case 0: /* if no jobs in queue */
2014-12-31 15:09:10 +03:00
thpool->jobqueue->front = newjob;
thpool->jobqueue->rear = newjob;
2011-11-05 16:35:31 +04:00
break;
2014-12-31 15:34:52 +03:00
default: /* if jobs in queue */
2014-12-31 15:09:10 +03:00
thpool->jobqueue->rear->prev = newjob;
thpool->jobqueue->rear = newjob;
2014-11-23 14:38:34 +03:00
}
2014-12-30 15:32:15 +03:00
thpool->jobqueue->len++;
bsem_post(thpool->jobqueue->has_jobs);
2011-11-05 16:35:31 +04:00
}
2014-11-23 14:38:34 +03:00
/* Get first element from queue */
2014-12-30 15:32:15 +03:00
static job_t* jobqueue_pull(thpool_t* thpool){
2014-12-31 15:09:10 +03:00
2014-12-30 15:32:15 +03:00
job_t* job;
2014-12-31 15:09:10 +03:00
job = thpool->jobqueue->front;
2014-11-23 14:38:34 +03:00
2014-12-30 15:32:15 +03:00
switch(thpool->jobqueue->len){
2011-11-05 16:35:31 +04:00
2014-12-31 15:34:52 +03:00
case 0: /* if no jobs in queue */
2014-11-23 14:38:34 +03:00
return NULL;
2011-11-05 16:35:31 +04:00
2014-12-31 15:34:52 +03:00
case 1: /* if one job in queue */
2014-12-31 15:09:10 +03:00
thpool->jobqueue->front = NULL;
thpool->jobqueue->rear = NULL;
2011-11-05 16:35:31 +04:00
break;
2014-12-31 15:09:10 +03:00
2014-12-31 15:34:52 +03:00
default: /* if >1 jobs in queue */
2014-12-31 15:09:10 +03:00
thpool->jobqueue->front = job->prev;
2011-11-05 16:35:31 +04:00
}
2014-12-30 15:32:15 +03:00
thpool->jobqueue->len--;
2014-12-31 15:34:52 +03:00
/* Make sure has_jobs has right value */
2014-12-30 15:32:15 +03:00
if (thpool->jobqueue->len > 0) {
bsem_post(thpool->jobqueue->has_jobs);
}
2014-12-30 15:28:43 +03:00
2014-12-30 15:32:15 +03:00
return job;
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-31 15:09:10 +03:00
static void jobqueue_destroy(thpool_t* thpool){
jobqueue_clear(thpool);
2014-12-30 15:32:15 +03:00
free(thpool->jobqueue->has_jobs);
2011-11-05 16:35:31 +04:00
}
2014-12-30 15:56:19 +03:00
2014-12-29 19:42:27 +03:00
/* ======================== SYNCHRONISATION ========================= */
2015-01-02 22:23:22 +03:00
/* Binary semaphore init */
static void bsem_init(bsem_t *bsem, int value) {
2015-01-09 17:40:40 +03:00
if (value < 0 || value > 1) {
printf("ERROR: bsem_init(): Binary semaphore can take only values 1 or 0");
exit(1);
}
2015-01-02 22:23:22 +03:00
bsem->v = value;
}
/* Binary semaphore reset */
static void bsem_reset(bsem_t *bsem) {
bsem_init(bsem, 0);
}
2014-12-30 15:56:19 +03:00
/* Binary semaphore post */
2014-12-29 19:42:27 +03:00
static void bsem_post(bsem_t *bsem) {
2014-12-30 15:28:43 +03:00
pthread_mutex_lock(&bsem->mutex);
bsem->v = 1;
pthread_cond_signal(&bsem->cond);
pthread_mutex_unlock(&bsem->mutex);
}
2015-01-02 22:23:22 +03:00
/* Binary semaphore post */
static void bsem_post_all(bsem_t *bsem) {
pthread_mutex_lock(&bsem->mutex);
bsem->v = 1;
pthread_cond_broadcast(&bsem->cond);
pthread_mutex_unlock(&bsem->mutex);
}
2014-12-30 15:56:19 +03:00
/* Binary semaphore wait */
2014-12-29 19:42:27 +03:00
static void bsem_wait(bsem_t *bsem) {
2014-12-30 15:28:43 +03:00
pthread_mutex_lock(&bsem->mutex);
while (bsem->v != 1) {
pthread_cond_wait(&bsem->cond, &bsem->mutex);
}
bsem->v = 0;
pthread_mutex_unlock(&bsem->mutex);
}