Merge pull request #47 from GandaG/master
Make it possible to disable error message printing
This commit is contained in:
commit
35f7486784
27
thpool.c
27
thpool.c
|
@ -26,6 +26,12 @@
|
|||
#define THPOOL_DEBUG 0
|
||||
#endif
|
||||
|
||||
#if !defined(DISABLE_PRINT) || defined(THPOOL_DEBUG)
|
||||
#define print_error(msg) fprintf(stderr, msg)
|
||||
#else
|
||||
#define print_error(msg)
|
||||
#endif
|
||||
|
||||
static volatile int threads_keepalive;
|
||||
static volatile int threads_on_hold;
|
||||
|
||||
|
@ -123,7 +129,7 @@ struct thpool_* thpool_init(int num_threads){
|
|||
thpool_* thpool_p;
|
||||
thpool_p = (struct thpool_*)malloc(sizeof(struct thpool_));
|
||||
if (thpool_p == NULL){
|
||||
fprintf(stderr, "thpool_init(): Could not allocate memory for thread pool\n");
|
||||
print_error("thpool_init(): Could not allocate memory for thread pool\n");
|
||||
return NULL;
|
||||
}
|
||||
thpool_p->num_threads_alive = 0;
|
||||
|
@ -131,7 +137,7 @@ struct thpool_* thpool_init(int num_threads){
|
|||
|
||||
/* Initialise the job queue */
|
||||
if (jobqueue_init(&thpool_p->jobqueue) == -1){
|
||||
fprintf(stderr, "thpool_init(): Could not allocate memory for job queue\n");
|
||||
print_error("thpool_init(): Could not allocate memory for job queue\n");
|
||||
free(thpool_p);
|
||||
return NULL;
|
||||
}
|
||||
|
@ -139,7 +145,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){
|
||||
fprintf(stderr, "thpool_init(): Could not allocate memory for threads\n");
|
||||
print_error("thpool_init(): Could not allocate memory for threads\n");
|
||||
jobqueue_destroy(&thpool_p->jobqueue);
|
||||
free(thpool_p);
|
||||
return NULL;
|
||||
|
@ -152,8 +158,9 @@ struct thpool_* thpool_init(int num_threads){
|
|||
int n;
|
||||
for (n=0; n<num_threads; n++){
|
||||
thread_init(thpool_p, &thpool_p->threads[n], n);
|
||||
if (THPOOL_DEBUG)
|
||||
#if THPOOL_DEBUG
|
||||
printf("THPOOL_DEBUG: Created thread %d in pool \n", n);
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Wait for threads to initialize */
|
||||
|
@ -169,7 +176,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){
|
||||
fprintf(stderr, "thpool_add_work(): Could not allocate memory for new job\n");
|
||||
print_error("thpool_add_work(): Could not allocate memory for new job\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -269,7 +276,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){
|
||||
fprintf(stderr, "thread_init(): Could not allocate memory for thread\n");
|
||||
print_error("thread_init(): Could not allocate memory for thread\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -310,8 +317,8 @@ static void* thread_do(struct thread* thread_p){
|
|||
prctl(PR_SET_NAME, thread_name);
|
||||
#elif defined(__APPLE__) && defined(__MACH__)
|
||||
pthread_setname_np(thread_name);
|
||||
#elif THPOOL_DEBUG
|
||||
fprintf(stderr, "thread_do(): pthread_setname_np is not supported on this system");
|
||||
#else
|
||||
print_error("thread_do(): pthread_setname_np is not supported on this system");
|
||||
#endif
|
||||
|
||||
/* Assure all threads have been created before starting serving */
|
||||
|
@ -323,7 +330,7 @@ static void* thread_do(struct thread* thread_p){
|
|||
act.sa_flags = 0;
|
||||
act.sa_handler = thread_hold;
|
||||
if (sigaction(SIGUSR1, &act, NULL) == -1) {
|
||||
fprintf(stderr, "thread_do(): cannot handle SIGUSR1");
|
||||
print_error("thread_do(): cannot handle SIGUSR1");
|
||||
}
|
||||
|
||||
/* Mark thread as alive (initialized) */
|
||||
|
@ -492,7 +499,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) {
|
||||
fprintf(stderr, "bsem_init(): Binary semaphore can take only values 1 or 0");
|
||||
print_error("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