mirror of
https://github.com/Pithikos/C-Thread-Pool
synced 2024-11-25 23:09:36 +03:00
Small refactoring without affecting functionality
This commit is contained in:
parent
779938e54d
commit
e108ee5ab6
21
thpool.c
21
thpool.c
@ -18,6 +18,7 @@
|
||||
#if defined(__linux__)
|
||||
#include <sys/prctl.h>
|
||||
#endif
|
||||
|
||||
#include "thpool.h"
|
||||
|
||||
#ifdef THPOOL_DEBUG
|
||||
@ -27,9 +28,9 @@
|
||||
#endif
|
||||
|
||||
#if !defined(DISABLE_PRINT) || defined(THPOOL_DEBUG)
|
||||
#define print_error(msg) fprintf(stderr, msg)
|
||||
#define err(str) fprintf(stderr, str)
|
||||
#else
|
||||
#define print_error(msg)
|
||||
#define err(str)
|
||||
#endif
|
||||
|
||||
static volatile int threads_keepalive;
|
||||
@ -129,7 +130,7 @@ struct thpool_* thpool_init(int num_threads){
|
||||
thpool_* thpool_p;
|
||||
thpool_p = (struct thpool_*)malloc(sizeof(struct thpool_));
|
||||
if (thpool_p == NULL){
|
||||
print_error("thpool_init(): Could not allocate memory for thread pool\n");
|
||||
err("thpool_init(): Could not allocate memory for thread pool\n");
|
||||
return NULL;
|
||||
}
|
||||
thpool_p->num_threads_alive = 0;
|
||||
@ -137,7 +138,7 @@ struct thpool_* thpool_init(int num_threads){
|
||||
|
||||
/* Initialise the job queue */
|
||||
if (jobqueue_init(&thpool_p->jobqueue) == -1){
|
||||
print_error("thpool_init(): Could not allocate memory for job queue\n");
|
||||
err("thpool_init(): Could not allocate memory for job queue\n");
|
||||
free(thpool_p);
|
||||
return NULL;
|
||||
}
|
||||
@ -145,7 +146,7 @@ struct thpool_* thpool_init(int num_threads){
|
||||
/* Make threads in pool */
|
||||
thpool_p->threads = (struct thread**)malloc(num_threads * sizeof(struct thread *));
|
||||
if (thpool_p->threads == NULL){
|
||||
print_error("thpool_init(): Could not allocate memory for threads\n");
|
||||
err("thpool_init(): Could not allocate memory for threads\n");
|
||||
jobqueue_destroy(&thpool_p->jobqueue);
|
||||
free(thpool_p);
|
||||
return NULL;
|
||||
@ -176,7 +177,7 @@ int thpool_add_work(thpool_* thpool_p, void (*function_p)(void*), void* arg_p){
|
||||
|
||||
newjob=(struct job*)malloc(sizeof(struct job));
|
||||
if (newjob==NULL){
|
||||
print_error("thpool_add_work(): Could not allocate memory for new job\n");
|
||||
err("thpool_add_work(): Could not allocate memory for new job\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -276,7 +277,7 @@ 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){
|
||||
print_error("thread_init(): Could not allocate memory for thread\n");
|
||||
err("thread_init(): Could not allocate memory for thread\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -318,7 +319,7 @@ static void* thread_do(struct thread* thread_p){
|
||||
#elif defined(__APPLE__) && defined(__MACH__)
|
||||
pthread_setname_np(thread_name);
|
||||
#else
|
||||
print_error("thread_do(): pthread_setname_np is not supported on this system");
|
||||
err("thread_do(): pthread_setname_np is not supported on this system");
|
||||
#endif
|
||||
|
||||
/* Assure all threads have been created before starting serving */
|
||||
@ -330,7 +331,7 @@ static void* thread_do(struct thread* thread_p){
|
||||
act.sa_flags = 0;
|
||||
act.sa_handler = thread_hold;
|
||||
if (sigaction(SIGUSR1, &act, NULL) == -1) {
|
||||
print_error("thread_do(): cannot handle SIGUSR1");
|
||||
err("thread_do(): cannot handle SIGUSR1");
|
||||
}
|
||||
|
||||
/* Mark thread as alive (initialized) */
|
||||
@ -499,7 +500,7 @@ static void jobqueue_destroy(jobqueue* jobqueue_p){
|
||||
/* Init semaphore to 1 or 0 */
|
||||
static void bsem_init(bsem *bsem_p, int value) {
|
||||
if (value < 0 || value > 1) {
|
||||
print_error("bsem_init(): Binary semaphore can take only values 1 or 0");
|
||||
err("bsem_init(): Binary semaphore can take only values 1 or 0");
|
||||
exit(1);
|
||||
}
|
||||
pthread_mutex_init(&(bsem_p->mutex), NULL);
|
||||
|
Loading…
Reference in New Issue
Block a user