C-Thread-Pool/thpool.c

572 lines
14 KiB
C
Raw Permalink Normal View History

2011-11-05 16:35:31 +04:00
/* ********************************
2015-01-09 22:30:29 +03:00
* Author: Johan Hanssen Seferidis
* License: MIT
2014-12-29 14:48:08 +03:00
* Description: Library providing a threading pool where you can add
2015-01-18 15:42:37 +03:00
* work. For usage, check the thpool.h file or README.md
2014-12-29 14:48:08 +03:00
*
2011-11-05 16:35:31 +04:00
*//** @file thpool.h *//*
2017-01-17 22:59:29 +03:00
*
2011-11-05 16:35:31 +04:00
********************************/
2022-09-22 16:53:38 +03:00
#if defined(__APPLE__)
#include <AvailabilityMacros.h>
#else
#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200809L
2022-09-22 16:53:38 +03:00
#endif
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 500
#endif
2022-09-22 16:53:38 +03:00
#endif
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>
2017-01-17 22:59:29 +03:00
#include <time.h>
#if defined(__linux__)
#include <sys/prctl.h>
#endif
2023-12-18 17:04:46 +03:00
#if defined(__FreeBSD__) || defined(__OpenBSD__)
2021-03-17 03:58:16 +03:00
#include <pthread_np.h>
#endif
2015-01-16 21:10:25 +03:00
#include "thpool.h"
2014-12-30 15:56:19 +03:00
2015-03-31 18:02:21 +03:00
#ifdef THPOOL_DEBUG
#define THPOOL_DEBUG 1
#else
#define THPOOL_DEBUG 0
#endif
#if !defined(DISABLE_PRINT) || defined(THPOOL_DEBUG)
#define err(str) fprintf(stderr, str)
#else
#define err(str)
#endif
#ifndef THPOOL_THREAD_NAME
#define THPOOL_THREAD_NAME thpool
#endif
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
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
2015-01-15 12:12:26 +03:00
/* ========================== STRUCTURES ============================ */
/* Binary semaphore */
2015-01-16 21:10:25 +03:00
typedef struct bsem {
2015-01-15 12:12:26 +03:00
pthread_mutex_t mutex;
pthread_cond_t cond;
int v;
2015-01-16 21:10:25 +03:00
} bsem;
2015-01-15 12:12:26 +03:00
/* Job */
2015-01-16 21:10:25 +03:00
typedef struct job{
2015-01-18 15:42:37 +03:00
struct job* prev; /* pointer to previous job */
void (*function)(void* arg); /* function pointer */
2015-01-18 15:42:37 +03:00
void* arg; /* function's argument */
2015-01-16 21:10:25 +03:00
} job;
2015-01-15 12:12:26 +03:00
/* Job queue */
2015-01-16 21:10:25 +03:00
typedef struct jobqueue{
pthread_mutex_t rwmutex; /* used for queue r/w access */
job *front; /* pointer to front of queue */
job *rear; /* pointer to rear of queue */
bsem *has_jobs; /* flag as binary semaphore */
int len; /* number of jobs in queue */
} jobqueue;
2015-01-15 12:12:26 +03:00
/* Thread */
2015-01-16 21:10:25 +03:00
typedef struct thread{
2015-01-18 15:42:37 +03:00
int id; /* friendly id */
pthread_t pthread; /* pointer to actual thread */
struct thpool_* thpool_p; /* access to thpool */
2015-01-16 21:10:25 +03:00
} thread;
2015-01-15 12:12:26 +03:00
/* Threadpool */
2015-01-16 21:10:25 +03:00
typedef struct thpool_{
2015-01-18 15:42:37 +03:00
thread** threads; /* pointer to threads */
volatile int num_threads_alive; /* threads currently alive */
volatile int num_threads_working; /* threads currently working */
2015-01-18 15:42:37 +03:00
pthread_mutex_t thcount_lock; /* used for thread count etc */
pthread_cond_t threads_all_idle; /* signal to thpool_wait */
jobqueue jobqueue; /* job queue */
2015-01-16 21:10:25 +03:00
} thpool_;
2015-01-15 12:12:26 +03:00
/* ========================== PROTOTYPES ============================ */
2015-12-23 04:46:30 +03:00
static int thread_init(thpool_* thpool_p, struct thread** thread_p, int id);
2015-01-16 21:10:25 +03:00
static void* thread_do(struct thread* thread_p);
2017-01-29 05:16:33 +03:00
static void thread_hold(int sig_id);
2015-01-16 21:10:25 +03:00
static void thread_destroy(struct thread* thread_p);
static int jobqueue_init(jobqueue* jobqueue_p);
static void jobqueue_clear(jobqueue* jobqueue_p);
static void jobqueue_push(jobqueue* jobqueue_p, struct job* newjob_p);
static struct job* jobqueue_pull(jobqueue* jobqueue_p);
static void jobqueue_destroy(jobqueue* jobqueue_p);
2015-01-15 12:12:26 +03:00
2015-01-16 21:10:25 +03:00
static void bsem_init(struct bsem *bsem_p, int value);
static void bsem_reset(struct bsem *bsem_p);
static void bsem_post(struct bsem *bsem_p);
static void bsem_post_all(struct bsem *bsem_p);
static void bsem_wait(struct bsem *bsem_p);
2015-01-15 12:12:26 +03:00
2015-01-02 22:23:22 +03:00
/* ========================== THREADPOOL ============================ */
2015-01-16 21:10:25 +03:00
2011-11-05 16:35:31 +04:00
/* Initialise thread pool */
2015-01-16 21:10:25 +03:00
struct thpool_* thpool_init(int num_threads){
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;
2016-01-27 09:31:10 +03:00
if (num_threads < 0){
2015-01-16 21:10:25 +03:00
num_threads = 0;
2014-12-31 15:09:10 +03:00
}
2015-01-02 22:23:22 +03:00
2011-11-05 16:35:31 +04:00
/* Make new thread pool */
2015-01-16 21:10:25 +03:00
thpool_* thpool_p;
thpool_p = (struct thpool_*)malloc(sizeof(struct thpool_));
if (thpool_p == NULL){
err("thpool_init(): Could not allocate memory for thread pool\n");
return NULL;
2011-11-05 16:35:31 +04:00
}
2015-01-18 15:42:37 +03:00
thpool_p->num_threads_alive = 0;
thpool_p->num_threads_working = 0;
2014-12-31 15:09:10 +03:00
2011-11-05 16:35:31 +04:00
/* Initialise the job queue */
if (jobqueue_init(&thpool_p->jobqueue) == -1){
err("thpool_init(): Could not allocate memory for job queue\n");
free(thpool_p);
return NULL;
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-12-18 21:17:32 +03:00
thpool_p->threads = (struct thread**)malloc(num_threads * sizeof(struct thread *));
if (thpool_p->threads == NULL){
err("thpool_init(): Could not allocate memory for threads\n");
jobqueue_destroy(&thpool_p->jobqueue);
2015-06-26 18:00:45 +03:00
free(thpool_p);
return NULL;
}
pthread_mutex_init(&(thpool_p->thcount_lock), NULL);
2015-12-02 18:22:51 +03:00
pthread_cond_init(&thpool_p->threads_all_idle, NULL);
2017-01-17 22:59:29 +03:00
2015-01-02 22:23:22 +03:00
/* Thread init */
2014-12-31 15:09:10 +03:00
int n;
2015-01-16 21:10:25 +03:00
for (n=0; n<num_threads; n++){
thread_init(thpool_p, &thpool_p->threads[n], n);
#if THPOOL_DEBUG
2015-03-31 18:02:21 +03:00
printf("THPOOL_DEBUG: Created thread %d in pool \n", n);
#endif
2011-11-05 16:35:31 +04:00
}
2017-01-17 22:59:29 +03:00
2015-01-02 22:23:22 +03:00
/* Wait for threads to initialize */
2015-01-16 21:10:25 +03:00
while (thpool_p->num_threads_alive != num_threads) {}
2015-01-14 20:52:22 +03:00
2015-01-16 21:10:25 +03:00
return thpool_p;
2011-11-05 16:35:31 +04:00
}
/* Add work to the thread pool */
int thpool_add_work(thpool_* thpool_p, void (*function_p)(void*), void* arg_p){
2015-01-16 21:10:25 +03:00
job* newjob;
2015-01-02 22:23:22 +03:00
2015-01-16 21:10:25 +03:00
newjob=(struct job*)malloc(sizeof(struct job));
2014-12-31 15:09:10 +03:00
if (newjob==NULL){
err("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 */
jobqueue_push(&thpool_p->jobqueue, newjob);
return 0;
}
2015-01-18 15:42:37 +03:00
/* Wait until all jobs have finished */
2015-01-16 21:10:25 +03:00
void thpool_wait(thpool_* thpool_p){
pthread_mutex_lock(&thpool_p->thcount_lock);
while (thpool_p->jobqueue.len || thpool_p->num_threads_working) {
pthread_cond_wait(&thpool_p->threads_all_idle, &thpool_p->thcount_lock);
}
pthread_mutex_unlock(&thpool_p->thcount_lock);
2011-11-05 16:35:31 +04:00
}
/* Destroy the threadpool */
2015-01-16 21:10:25 +03:00
void thpool_destroy(thpool_* thpool_p){
2022-12-26 01:50:33 +03:00
/* No need to destroy if it's NULL */
if (thpool_p == NULL) return ;
2015-01-16 21:10:25 +03:00
volatile int threads_total = thpool_p->num_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;
2017-01-17 22:59:29 +03:00
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;
2015-02-13 01:28:46 +03:00
double tpassed = 0.0;
2014-12-31 15:09:10 +03:00
time (&start);
2015-01-16 21:10:25 +03:00
while (tpassed < TIMEOUT && thpool_p->num_threads_alive){
bsem_post_all(thpool_p->jobqueue.has_jobs);
2015-01-02 22:23:22 +03:00
time (&end);
tpassed = difftime(end,start);
}
2017-01-17 22:59:29 +03:00
2015-01-02 22:23:22 +03:00
/* Poll remaining threads */
2015-01-16 21:10:25 +03:00
while (thpool_p->num_threads_alive){
bsem_post_all(thpool_p->jobqueue.has_jobs);
2015-01-02 22:23:22 +03:00
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_p->jobqueue);
2015-01-09 17:40:40 +03:00
/* Deallocs */
int n;
for (n=0; n < threads_total; n++){
2015-01-16 21:10:25 +03:00
thread_destroy(thpool_p->threads[n]);
2015-01-09 17:40:40 +03:00
}
2015-01-16 21:10:25 +03:00
free(thpool_p->threads);
free(thpool_p);
2015-01-01 15:02:06 +03:00
}
2015-01-16 21:10:25 +03:00
/* Pause all threads in threadpool */
void thpool_pause(thpool_* thpool_p) {
2015-01-02 22:23:22 +03:00
int n;
2015-01-16 21:10:25 +03:00
for (n=0; n < thpool_p->num_threads_alive; n++){
pthread_kill(thpool_p->threads[n]->pthread, SIGUSR1);
2015-01-02 22:23:22 +03:00
}
2015-01-01 15:02:06 +03:00
}
2015-01-16 21:10:25 +03:00
/* Resume all threads in threadpool */
void thpool_resume(thpool_* thpool_p) {
// resuming a single threadpool hasn't been
2022-12-26 01:50:33 +03:00
// implemented yet, meanwhile this suppresses
// the warnings
2017-01-29 05:16:33 +03:00
(void)thpool_p;
2015-01-02 22:23:22 +03:00
threads_on_hold = 0;
2015-01-01 15:02:06 +03:00
}
int thpool_num_threads_working(thpool_* thpool_p){
return thpool_p->num_threads_working;
}
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-15 12:12:26 +03:00
2015-01-16 21:10:25 +03:00
/* Initialize a thread in the thread pool
2017-01-17 22:59:29 +03:00
*
2015-01-15 12:12:26 +03:00
* @param thread address to the pointer of the thread to be created
* @param id id to be given to the thread
2015-12-23 04:46:30 +03:00
* @return 0 on success, -1 otherwise.
2015-01-15 12:12:26 +03:00
*/
2015-12-23 04:46:30 +03:00
static int thread_init (thpool_* thpool_p, struct thread** thread_p, int id){
2017-01-17 22:59:29 +03:00
2015-01-16 21:10:25 +03:00
*thread_p = (struct thread*)malloc(sizeof(struct thread));
if (*thread_p == NULL){
err("thread_init(): Could not allocate memory for thread\n");
2015-12-23 04:46:30 +03:00
return -1;
2015-01-01 15:02:06 +03:00
}
2015-01-16 21:10:25 +03:00
(*thread_p)->thpool_p = thpool_p;
(*thread_p)->id = id;
2015-01-01 15:02:06 +03:00
pthread_create(&(*thread_p)->pthread, NULL, (void * (*)(void *)) thread_do, (*thread_p));
2015-01-16 21:10:25 +03:00
pthread_detach((*thread_p)->pthread);
2015-12-23 04:46:30 +03:00
return 0;
2015-01-01 15:02:06 +03:00
}
2015-01-15 12:12:26 +03:00
2015-01-16 21:10:25 +03:00
/* Sets the calling thread on hold */
2017-01-29 05:16:33 +03:00
static void thread_hold(int sig_id) {
(void)sig_id;
2015-01-02 22:23:22 +03:00
threads_on_hold = 1;
while (threads_on_hold){
2015-01-01 15:02:06 +03:00
sleep(1);
}
}
2015-01-16 21:10:25 +03:00
/* What each thread is doing
2017-01-17 22:59:29 +03:00
*
2023-06-28 01:44:47 +03:00
* In principle this is an endless loop. The only time this loop gets interrupted is once
2015-01-15 12:12:26 +03:00
* thpool_destroy() is invoked or the program exits.
2017-01-17 22:59:29 +03:00
*
2015-01-15 12:12:26 +03:00
* @param thread thread that will run this function
* @return nothing
*/
2015-01-16 21:10:25 +03:00
static void* thread_do(struct thread* thread_p){
2022-12-26 01:50:33 +03:00
/* Set thread name for profiling and debugging */
char thread_name[16] = {0};
snprintf(thread_name, 16, TOSTRING(THPOOL_THREAD_NAME) "-%d", thread_p->id);
#if defined(__linux__)
/* Use prctl instead to prevent using _GNU_SOURCE flag and implicit declaration */
prctl(PR_SET_NAME, thread_name);
#elif defined(__APPLE__) && defined(__MACH__)
pthread_setname_np(thread_name);
2023-12-18 17:04:46 +03:00
#elif defined(__FreeBSD__) || defined(__OpenBSD__)
pthread_set_name_np(thread_p->pthread, thread_name);
#else
err("thread_do(): pthread_setname_np is not supported on this system");
#endif
2015-01-01 15:02:06 +03:00
/* Assure all threads have been created before starting serving */
2015-01-16 21:10:25 +03:00
thpool_* thpool_p = thread_p->thpool_p;
2017-01-17 22:59:29 +03:00
2015-01-01 15:02:06 +03:00
/* Register signal handler */
struct sigaction act;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_ONSTACK;
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) {
err("thread_do(): cannot handle SIGUSR1");
2015-01-01 15:02:06 +03:00
}
2017-01-17 22:59:29 +03:00
2015-01-02 22:23:22 +03:00
/* Mark thread as alive (initialized) */
2015-01-16 21:10:25 +03:00
pthread_mutex_lock(&thpool_p->thcount_lock);
thpool_p->num_threads_alive += 1;
pthread_mutex_unlock(&thpool_p->thcount_lock);
2015-01-02 22:23:22 +03:00
2015-01-01 15:02:06 +03:00
while(threads_keepalive){
bsem_wait(thpool_p->jobqueue.has_jobs);
2015-01-01 15:02:06 +03:00
if (threads_keepalive){
2017-01-17 22:59:29 +03:00
2015-01-18 15:42:37 +03:00
pthread_mutex_lock(&thpool_p->thcount_lock);
thpool_p->num_threads_working++;
pthread_mutex_unlock(&thpool_p->thcount_lock);
2017-01-17 22:59:29 +03:00
2015-01-01 15:02:06 +03:00
/* Read job from queue and execute it */
2017-01-18 13:37:04 +03:00
void (*func_buff)(void*);
2015-01-01 15:02:06 +03:00
void* arg_buff;
job* job_p = jobqueue_pull(&thpool_p->jobqueue);
2015-01-16 21:10:25 +03:00
if (job_p) {
func_buff = job_p->function;
arg_buff = job_p->arg;
2015-01-01 15:02:06 +03:00
func_buff(arg_buff);
2015-01-16 21:10:25 +03:00
free(job_p);
2015-01-01 15:02:06 +03:00
}
2017-01-17 22:59:29 +03:00
2015-01-18 15:42:37 +03:00
pthread_mutex_lock(&thpool_p->thcount_lock);
thpool_p->num_threads_working--;
if (!thpool_p->num_threads_working) {
pthread_cond_signal(&thpool_p->threads_all_idle);
}
2015-01-18 15:42:37 +03:00
pthread_mutex_unlock(&thpool_p->thcount_lock);
2015-01-02 22:23:22 +03:00
2015-01-01 15:02:06 +03:00
}
}
2015-01-16 21:10:25 +03:00
pthread_mutex_lock(&thpool_p->thcount_lock);
thpool_p->num_threads_alive --;
pthread_mutex_unlock(&thpool_p->thcount_lock);
2015-01-09 21:06:37 +03:00
2015-01-09 20:35:26 +03:00
return NULL;
2015-01-02 22:23:22 +03:00
}
2015-01-16 21:10:25 +03:00
/* Frees a thread */
static void thread_destroy (thread* thread_p){
free(thread_p);
2011-11-05 16:35:31 +04:00
}
2015-01-16 21:10:25 +03: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
2015-01-16 21:10:25 +03:00
/* Initialize queue */
static int jobqueue_init(jobqueue* jobqueue_p){
jobqueue_p->len = 0;
jobqueue_p->front = NULL;
jobqueue_p->rear = NULL;
2017-01-17 22:59:29 +03:00
jobqueue_p->has_jobs = (struct bsem*)malloc(sizeof(struct bsem));
if (jobqueue_p->has_jobs == NULL){
2014-12-31 15:09:10 +03:00
return -1;
}
pthread_mutex_init(&(jobqueue_p->rwmutex), NULL);
bsem_init(jobqueue_p->has_jobs, 0);
2014-12-31 15:09:10 +03:00
return 0;
}
2015-01-16 21:10:25 +03:00
/* Clear the queue */
static void jobqueue_clear(jobqueue* jobqueue_p){
2014-12-31 15:34:52 +03:00
while(jobqueue_p->len){
free(jobqueue_pull(jobqueue_p));
2014-12-31 15:34:52 +03:00
}
2015-01-16 21:10:25 +03:00
jobqueue_p->front = NULL;
jobqueue_p->rear = NULL;
bsem_reset(jobqueue_p->has_jobs);
jobqueue_p->len = 0;
2015-01-18 15:42:37 +03:00
2011-11-05 16:35:31 +04:00
}
2015-01-16 21:10:25 +03:00
/* Add (allocated) job to queue
2015-01-15 12:12:26 +03:00
*/
static void jobqueue_push(jobqueue* jobqueue_p, struct job* newjob){
pthread_mutex_lock(&jobqueue_p->rwmutex);
2014-12-31 15:09:10 +03:00
newjob->prev = NULL;
2011-11-05 16:35:31 +04:00
switch(jobqueue_p->len){
2014-11-23 14:38:34 +03:00
2014-12-31 15:34:52 +03:00
case 0: /* if no jobs in queue */
jobqueue_p->front = newjob;
jobqueue_p->rear = newjob;
2011-11-05 16:35:31 +04:00
break;
2014-12-31 15:34:52 +03:00
default: /* if jobs in queue */
jobqueue_p->rear->prev = newjob;
jobqueue_p->rear = newjob;
2017-01-18 13:38:05 +03:00
2014-11-23 14:38:34 +03:00
}
jobqueue_p->len++;
2017-01-18 13:38:05 +03:00
bsem_post(jobqueue_p->has_jobs);
pthread_mutex_unlock(&jobqueue_p->rwmutex);
2011-11-05 16:35:31 +04:00
}
2015-01-16 21:10:25 +03:00
/* Get first job from queue(removes it from queue)
* Notice: Caller MUST hold a mutex
2015-01-15 12:12:26 +03:00
*/
static struct job* jobqueue_pull(jobqueue* jobqueue_p){
2014-12-31 15:09:10 +03:00
pthread_mutex_lock(&jobqueue_p->rwmutex);
job* job_p = jobqueue_p->front;
2017-01-17 22:59:29 +03:00
switch(jobqueue_p->len){
2017-01-18 13:38:05 +03:00
2014-12-31 15:34:52 +03:00
case 0: /* if no jobs in queue */
break;
2017-01-17 22:59:29 +03:00
2014-12-31 15:34:52 +03:00
case 1: /* if one job in queue */
jobqueue_p->front = NULL;
jobqueue_p->rear = NULL;
jobqueue_p->len = 0;
2011-11-05 16:35:31 +04:00
break;
2017-01-17 22:59:29 +03:00
2014-12-31 15:34:52 +03:00
default: /* if >1 jobs in queue */
jobqueue_p->front = job_p->prev;
jobqueue_p->len--;
/* more than one job in queue -> post it */
bsem_post(jobqueue_p->has_jobs);
2017-01-18 13:38:05 +03:00
2011-11-05 16:35:31 +04:00
}
2017-01-17 22:59:29 +03:00
pthread_mutex_unlock(&jobqueue_p->rwmutex);
2015-01-16 21:10:25 +03:00
return job_p;
2011-11-05 16:35:31 +04:00
}
2014-11-23 14:38:34 +03:00
2015-01-16 21:10:25 +03:00
/* Free all queue resources back to the system */
static void jobqueue_destroy(jobqueue* jobqueue_p){
jobqueue_clear(jobqueue_p);
free(jobqueue_p->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-16 21:10:25 +03:00
/* Init semaphore to 1 or 0 */
static void bsem_init(bsem *bsem_p, int value) {
2015-01-09 17:40:40 +03:00
if (value < 0 || value > 1) {
err("bsem_init(): Binary semaphore can take only values 1 or 0");
2015-01-09 17:40:40 +03:00
exit(1);
}
2015-03-12 22:00:03 +03:00
pthread_mutex_init(&(bsem_p->mutex), NULL);
pthread_cond_init(&(bsem_p->cond), NULL);
2015-01-16 21:10:25 +03:00
bsem_p->v = value;
2015-01-02 22:23:22 +03:00
}
2015-01-16 21:10:25 +03:00
/* Reset semaphore to 0 */
static void bsem_reset(bsem *bsem_p) {
pthread_mutex_destroy(&(bsem_p->mutex));
pthread_cond_destroy(&(bsem_p->cond));
2015-01-16 21:10:25 +03:00
bsem_init(bsem_p, 0);
2015-01-02 22:23:22 +03:00
}
2015-01-16 21:10:25 +03:00
/* Post to at least one thread */
static void bsem_post(bsem *bsem_p) {
pthread_mutex_lock(&bsem_p->mutex);
bsem_p->v = 1;
pthread_cond_signal(&bsem_p->cond);
pthread_mutex_unlock(&bsem_p->mutex);
}
2015-01-16 21:10:25 +03:00
/* Post to all threads */
static void bsem_post_all(bsem *bsem_p) {
pthread_mutex_lock(&bsem_p->mutex);
bsem_p->v = 1;
pthread_cond_broadcast(&bsem_p->cond);
pthread_mutex_unlock(&bsem_p->mutex);
2015-01-02 22:23:22 +03:00
}
2015-01-16 21:10:25 +03:00
/* Wait on semaphore until semaphore has value 0 */
static void bsem_wait(bsem* bsem_p) {
pthread_mutex_lock(&bsem_p->mutex);
while (bsem_p->v != 1) {
pthread_cond_wait(&bsem_p->cond, &bsem_p->mutex);
2014-12-30 15:28:43 +03:00
}
2015-01-16 21:10:25 +03:00
bsem_p->v = 0;
pthread_mutex_unlock(&bsem_p->mutex);
}