libresman

diff src/threadpool.c @ 18:711698580eb0

fixed visual studio build directories fixed debug id problem with the thread pool
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 12 Feb 2014 06:53:30 +0200
parents 2b8281a146af
children fe0dbdfbe403
line diff
     1.1 --- a/src/threadpool.c	Tue Feb 11 18:48:24 2014 +0200
     1.2 +++ b/src/threadpool.c	Wed Feb 12 06:53:30 2014 +0200
     1.3 @@ -17,6 +17,9 @@
     1.4  	pthread_mutex_t work_lock;
     1.5  	pthread_cond_t work_cond;
     1.6  
     1.7 +	int start;
     1.8 +	pthread_cond_t start_cond;
     1.9 +
    1.10  	tpool_work_func work_func;
    1.11  	void *cls;
    1.12  
    1.13 @@ -55,6 +58,9 @@
    1.14  		return -1;
    1.15  	}
    1.16  
    1.17 +	/* this start condvar is pretty useless */
    1.18 +	pthread_cond_init(&tpool->start_cond, 0);
    1.19 +
    1.20  	for(i=0; i<num_threads; i++) {
    1.21  		if(pthread_create(tpool->workers + i, 0, thread_func, tpool) == -1) {
    1.22  			fprintf(stderr, "%s: failed to create thread %d\n", __FUNCTION__, i);
    1.23 @@ -62,6 +68,8 @@
    1.24  			return -1;
    1.25  		}
    1.26  	}
    1.27 +	tpool->start = 1;
    1.28 +	pthread_cond_broadcast(&tpool->start_cond);
    1.29  	return 0;
    1.30  }
    1.31  
    1.32 @@ -142,8 +150,15 @@
    1.33  	struct thread_pool *tpool = tp;
    1.34  	pthread_t tid = pthread_self();
    1.35  
    1.36 +	/* wait for the start signal :) */
    1.37 +	pthread_mutex_lock(&tpool->work_lock);
    1.38 +	while(!tpool->start) {
    1.39 +		pthread_cond_wait(&tpool->start_cond, &tpool->work_lock);
    1.40 +	}
    1.41 +	pthread_mutex_unlock(&tpool->work_lock);
    1.42 +
    1.43  	for(i=0; i<tpool->num_workers; i++) {
    1.44 -		if(tpool[i].workers[i] == tid) {
    1.45 +		if(pthread_equal(tpool->workers[i], tid)) {
    1.46  			tidx = i;
    1.47  			break;
    1.48  		}
    1.49 @@ -157,13 +172,11 @@
    1.50  			continue;	/* spurious wakeup, go back to sleep */
    1.51  		}
    1.52  
    1.53 -		printf("TPOOL: worker %d start job: %d\n", tidx, job->id);
    1.54 -
    1.55  		job = tpool->work_list;
    1.56  		tpool->work_list = tpool->work_list->next;
    1.57  
    1.58 +		printf("TPOOL: worker %d start job: %d\n", tidx, job->id);
    1.59  		tpool->work_func(job->data, tpool->cls);
    1.60 -
    1.61  		printf("TPOOL: worker %d completed job: %d\n", tidx, job->id);
    1.62  		free_node(job);
    1.63  	}