From 9405ad97e20686f74c06bcbac9523a8b4f10272e Mon Sep 17 00:00:00 2001 From: dimitri staessens Date: Sat, 30 Sep 2017 17:58:18 +0200 Subject: lib: Cancel tpm threads instead of marking exit This makes the threadpool use pthread_cancel instead of setting an exit flag that threadpool managed threads check periodically. This drastically reduces CPU consumption in the irmd when running a lot of applications. It requires cancellation handlers in the ipcp and irmd to be implemented to ensure safe cancellation during operation and shutdown. --- src/irmd/irm_flow.c | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) (limited to 'src/irmd/irm_flow.c') diff --git a/src/irmd/irm_flow.c b/src/irmd/irm_flow.c index e335ef48..991644c9 100644 --- a/src/irmd/irm_flow.c +++ b/src/irmd/irm_flow.c @@ -88,6 +88,21 @@ struct irm_flow * irm_flow_create(pid_t n_api, return f; } +static void cancel_irm_destroy(void * o) +{ + struct irm_flow * f = (struct irm_flow *) o; + + pthread_mutex_unlock(&f->state_lock); + + pthread_cond_destroy(&f->state_cond); + pthread_mutex_destroy(&f->state_lock); + + shm_rbuff_destroy(f->n_rb); + shm_rbuff_destroy(f->n_1_rb); + + free(f); +} + void irm_flow_destroy(struct irm_flow * f) { assert(f); @@ -106,18 +121,12 @@ void irm_flow_destroy(struct irm_flow * f) pthread_cond_signal(&f->state_cond); + pthread_cleanup_push(cancel_irm_destroy, f); + while (f->state != FLOW_NULL) pthread_cond_wait(&f->state_cond, &f->state_lock); - pthread_mutex_unlock(&f->state_lock); - - pthread_cond_destroy(&f->state_cond); - pthread_mutex_destroy(&f->state_lock); - - shm_rbuff_destroy(f->n_rb); - shm_rbuff_destroy(f->n_1_rb); - - free(f); + pthread_cleanup_pop(true); } enum flow_state irm_flow_get_state(struct irm_flow * f) @@ -172,6 +181,9 @@ int irm_flow_wait_state(struct irm_flow * f, assert(f->state != FLOW_NULL); + pthread_cleanup_push((void *)(void *) pthread_mutex_unlock, + &f->state_lock); + while (!(f->state == state || f->state == FLOW_DESTROY || f->state == FLOW_DEALLOC_PENDING) && @@ -194,7 +206,7 @@ int irm_flow_wait_state(struct irm_flow * f, s = f->state; - pthread_mutex_unlock(&f->state_lock); + pthread_cleanup_pop(true); return ret ? ret : s; } -- cgit v1.2.3