summaryrefslogtreecommitdiff
path: root/src/lib/tpm.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/tpm.c')
-rw-r--r--src/lib/tpm.c51
1 files changed, 32 insertions, 19 deletions
diff --git a/src/lib/tpm.c b/src/lib/tpm.c
index ec2b8a90..0ef1fda8 100644
--- a/src/lib/tpm.c
+++ b/src/lib/tpm.c
@@ -1,10 +1,10 @@
/*
- * Ouroboros - Copyright (C) 2016 - 2018
+ * Ouroboros - Copyright (C) 2016 - 2024
*
* Threadpool management
*
- * Dimitri Staessens <dimitri.staessens@ugent.be>
- * Sander Vrijders <sander.vrijders@ugent.be>
+ * Dimitri Staessens <dimitri@ouroboros.rocks>
+ * Sander Vrijders <sander@ouroboros.rocks>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
@@ -26,12 +26,12 @@
#include <ouroboros/errno.h>
#include <ouroboros/list.h>
-#include <ouroboros/time_utils.h>
+#include <ouroboros/time.h>
#include <ouroboros/tpm.h>
+#include <assert.h>
#include <pthread.h>
#include <stdlib.h>
-#include <assert.h>
#define TPM_TIMEOUT 1000
@@ -82,11 +82,19 @@ static void tpm_join(struct tpm * tpm)
--tpm->cur;
}
}
+ }
+ list_for_each_safe(p, h, &tpm->pool) {
+ struct pthr_el * e = list_entry(p, struct pthr_el, next);
if (e->kill) {
- pthread_join(e->thr, NULL);
+ pthread_t thr = e->thr;
list_del(&e->next);
free(e);
+ pthread_mutex_unlock(&tpm->lock);
+
+ pthread_join(thr, NULL);
+
+ pthread_mutex_lock(&tpm->lock);
}
}
}
@@ -109,8 +117,7 @@ static void tpm_kill(struct tpm * tpm)
static void * tpmgr(void * o)
{
struct timespec dl;
- struct timespec to = {(TPM_TIMEOUT / 1000),
- (TPM_TIMEOUT % 1000) * MILLION};
+ struct timespec to = TIMESPEC_INIT_MS(TPM_TIMEOUT);
struct tpm * tpm = (struct tpm *) o;
while (true) {
@@ -151,7 +158,7 @@ static void * tpmgr(void * o)
if (pthread_cond_timedwait(&tpm->cond, &tpm->lock, &dl)
== ETIMEDOUT)
- if (tpm->cur > tpm->min)
+ if (tpm->cur - tpm->wrk > tpm->min)
tpm_kill(tpm);
pthread_mutex_unlock(&tpm->lock);
@@ -231,12 +238,12 @@ void tpm_stop(struct tpm * tpm)
tpm->state = TPM_NULL;
pthread_mutex_unlock(&tpm->lock);
+
+ pthread_join(tpm->mgr, NULL);
}
void tpm_destroy(struct tpm * tpm)
{
- pthread_join(tpm->mgr, NULL);
-
pthread_mutex_destroy(&tpm->lock);
pthread_cond_destroy(&tpm->cond);
@@ -256,29 +263,35 @@ static struct pthr_el * tpm_pthr_el(struct tpm * tpm,
}
- assert(false);
-
return NULL;
}
void tpm_inc(struct tpm * tpm)
{
- pthread_mutex_lock(&tpm->lock);
+ struct pthr_el * e;
- tpm_pthr_el(tpm, pthread_self())->busy = false;
+ pthread_mutex_lock(&tpm->lock);
- --tpm->wrk;
+ e = tpm_pthr_el(tpm, pthread_self());
+ if (e != NULL) {
+ e->busy = false;
+ --tpm->wrk;
+ }
pthread_mutex_unlock(&tpm->lock);
}
void tpm_dec(struct tpm * tpm)
{
- pthread_mutex_lock(&tpm->lock);
+ struct pthr_el * e;
- tpm_pthr_el(tpm, pthread_self())->busy = true;
+ pthread_mutex_lock(&tpm->lock);
- ++tpm->wrk;
+ e = tpm_pthr_el(tpm, pthread_self());
+ if (e != NULL) {
+ e->busy = true;
+ ++tpm->wrk;
+ }
pthread_cond_signal(&tpm->cond);