summaryrefslogtreecommitdiff
path: root/src/irmd/irm_flow.c
diff options
context:
space:
mode:
authordimitri staessens <[email protected]>2017-09-30 17:58:18 +0200
committerdimitri staessens <[email protected]>2017-09-30 17:58:18 +0200
commit9405ad97e20686f74c06bcbac9523a8b4f10272e (patch)
treea0489929634ee7588de3ad77a6a1166ce11508e2 /src/irmd/irm_flow.c
parent5e974395fadc5e1922f200855c14ca0538ba50dc (diff)
downloadouroboros-9405ad97e20686f74c06bcbac9523a8b4f10272e.tar.gz
ouroboros-9405ad97e20686f74c06bcbac9523a8b4f10272e.zip
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.
Diffstat (limited to 'src/irmd/irm_flow.c')
-rw-r--r--src/irmd/irm_flow.c32
1 files changed, 22 insertions, 10 deletions
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;
}