diff options
author | Tobias Brunner <tobias@strongswan.org> | 2008-04-03 09:19:12 +0000 |
---|---|---|
committer | Tobias Brunner <tobias@strongswan.org> | 2008-04-03 09:19:12 +0000 |
commit | 8e91a36314de3c5547705bfe46197b8e0530ee6a (patch) | |
tree | d83493de28a69b8e3208e051e3daa93222f78274 /src/charon/processing/processor.c | |
parent | 6af29ccf33a3e2c0feb5724a60591b8abeb9339b (diff) | |
download | strongswan-8e91a36314de3c5547705bfe46197b8e0530ee6a.tar.bz2 strongswan-8e91a36314de3c5547705bfe46197b8e0530ee6a.tar.xz |
thread locking for sender and processor optimized
Diffstat (limited to 'src/charon/processing/processor.c')
-rw-r--r-- | src/charon/processing/processor.c | 36 |
1 files changed, 27 insertions, 9 deletions
diff --git a/src/charon/processing/processor.c b/src/charon/processing/processor.c index e14679b14..336a28b45 100644 --- a/src/charon/processing/processor.c +++ b/src/charon/processing/processor.c @@ -66,7 +66,12 @@ struct private_processor_t { /** * Condvar to wait for new jobs */ - pthread_cond_t condvar; + pthread_cond_t jobadded; + + /** + * Condvar to wait for terminated threads + */ + pthread_cond_t threadterminated; }; static void process_jobs(private_processor_t *this); @@ -80,7 +85,10 @@ static void restart(private_processor_t *this) if (pthread_create(&thread, NULL, (void*)process_jobs, this) != 0) { + pthread_mutex_lock(&this->mutex); this->total_threads--; + pthread_cond_broadcast(&this->threadterminated); + pthread_mutex_unlock(&this->mutex); } } @@ -103,7 +111,7 @@ static void process_jobs(private_processor_t *this) if (this->list->get_count(this->list) == 0) { this->idle_threads++; - pthread_cond_wait(&this->condvar, &this->mutex); + pthread_cond_wait(&this->jobadded, &this->mutex); this->idle_threads--; continue; } @@ -116,7 +124,7 @@ static void process_jobs(private_processor_t *this) pthread_mutex_lock(&this->mutex); } this->total_threads--; - pthread_cond_broadcast(&this->condvar); + pthread_cond_signal(&this->threadterminated); pthread_mutex_unlock(&this->mutex); } @@ -125,7 +133,11 @@ static void process_jobs(private_processor_t *this) */ static u_int get_total_threads(private_processor_t *this) { - return this->total_threads; + u_int count; + pthread_mutex_lock(&this->mutex); + count = this->total_threads; + pthread_mutex_unlock(&this->mutex); + return count; } /** @@ -133,7 +145,11 @@ static u_int get_total_threads(private_processor_t *this) */ static u_int get_idle_threads(private_processor_t *this) { - return this->idle_threads; + u_int count; + pthread_mutex_lock(&this->mutex); + count = this->idle_threads; + pthread_mutex_unlock(&this->mutex); + return count; } /** @@ -155,8 +171,8 @@ static void queue_job(private_processor_t *this, job_t *job) { pthread_mutex_lock(&this->mutex); this->list->insert_last(this->list, job); + pthread_cond_signal(&this->jobadded); pthread_mutex_unlock(&this->mutex); - pthread_cond_signal(&this->condvar); } /** @@ -184,6 +200,7 @@ static void set_threads(private_processor_t *this, u_int count) { /* decrease thread count */ this->desired_threads = count; } + pthread_cond_broadcast(&this->jobadded); pthread_mutex_unlock(&this->mutex); } @@ -196,8 +213,8 @@ static void destroy(private_processor_t *this) pthread_mutex_lock(&this->mutex); while (this->total_threads > 0) { - pthread_cond_broadcast(&this->condvar); - pthread_cond_wait(&this->condvar, &this->mutex); + pthread_cond_broadcast(&this->jobadded); + pthread_cond_wait(&this->threadterminated, &this->mutex); } pthread_mutex_unlock(&this->mutex); this->list->destroy_offset(this->list, offsetof(job_t, destroy)); @@ -220,7 +237,8 @@ processor_t *processor_create(size_t pool_size) this->list = linked_list_create(); pthread_mutex_init(&this->mutex, NULL); - pthread_cond_init(&this->condvar, NULL); + pthread_cond_init(&this->jobadded, NULL); + pthread_cond_init(&this->threadterminated, NULL); this->total_threads = 0; this->desired_threads = 0; this->idle_threads = 0; |