libresman

diff src/threadpool.c @ 15:2b8281a146af

added debugging crap in the threadpool
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 11 Feb 2014 18:47:00 +0200
parents 84f55eab27cb
children 711698580eb0
line diff
     1.1 --- a/src/threadpool.c	Mon Feb 10 12:11:02 2014 +0200
     1.2 +++ b/src/threadpool.c	Tue Feb 11 18:47:00 2014 +0200
     1.3 @@ -5,6 +5,7 @@
     1.4  #include "threadpool.h"
     1.5  
     1.6  struct work_item {
     1.7 +	int id;	/* just for debugging messages */
     1.8  	void *data;
     1.9  	struct work_item *next;
    1.10  };
    1.11 @@ -106,6 +107,7 @@
    1.12  int tpool_add_work(struct thread_pool *tpool, void *data)
    1.13  {
    1.14  	struct work_item *node;
    1.15 +	static int jcounter;
    1.16  
    1.17  	if(!(node = alloc_node())) {
    1.18  		fprintf(stderr, "%s: failed to allocate new work item node\n", __FUNCTION__);
    1.19 @@ -115,6 +117,9 @@
    1.20  	node->next = 0;
    1.21  
    1.22  	pthread_mutex_lock(&tpool->work_lock);
    1.23 +	node->id = jcounter++;
    1.24 +
    1.25 +	printf("TPOOL: adding work item: %d\n", node->id);
    1.26  
    1.27  	if(!tpool->work_list) {
    1.28  		tpool->work_list = tpool->work_list_tail = node;
    1.29 @@ -132,8 +137,17 @@
    1.30  
    1.31  static void *thread_func(void *tp)
    1.32  {
    1.33 +	int i, tidx = -1;
    1.34  	struct work_item *job;
    1.35  	struct thread_pool *tpool = tp;
    1.36 +	pthread_t tid = pthread_self();
    1.37 +
    1.38 +	for(i=0; i<tpool->num_workers; i++) {
    1.39 +		if(tpool[i].workers[i] == tid) {
    1.40 +			tidx = i;
    1.41 +			break;
    1.42 +		}
    1.43 +	}
    1.44  
    1.45  	pthread_mutex_lock(&tpool->work_lock);
    1.46  	for(;;) {
    1.47 @@ -143,11 +157,14 @@
    1.48  			continue;	/* spurious wakeup, go back to sleep */
    1.49  		}
    1.50  
    1.51 +		printf("TPOOL: worker %d start job: %d\n", tidx, job->id);
    1.52 +
    1.53  		job = tpool->work_list;
    1.54  		tpool->work_list = tpool->work_list->next;
    1.55  
    1.56  		tpool->work_func(job->data, tpool->cls);
    1.57  
    1.58 +		printf("TPOOL: worker %d completed job: %d\n", tidx, job->id);
    1.59  		free_node(job);
    1.60  	}
    1.61  	pthread_mutex_unlock(&tpool->work_lock);