summaryrefslogtreecommitdiff
path: root/src/irmd/main.c
diff options
context:
space:
mode:
authordimitri staessens <dimitri.staessens@ugent.be>2017-04-04 17:51:22 +0200
committerdimitri staessens <dimitri.staessens@ugent.be>2017-04-04 19:54:26 +0200
commitbf5c0ce8d90613a39df2f6fec79071250b79d1d1 (patch)
tree1efb856c6ce5dbf2c31448042edde0f5040c05a2 /src/irmd/main.c
parent5ac15427a1a2274dfb678ec569ce1698c6a89c77 (diff)
downloadouroboros-bf5c0ce8d90613a39df2f6fec79071250b79d1d1.tar.gz
ouroboros-bf5c0ce8d90613a39df2f6fec79071250b79d1d1.zip
ipcpd, irmd: Fix cleanup of thread resources
Diffstat (limited to 'src/irmd/main.c')
-rw-r--r--src/irmd/main.c40
1 files changed, 17 insertions, 23 deletions
diff --git a/src/irmd/main.c b/src/irmd/main.c
index e6647285..fa58e218 100644
--- a/src/irmd/main.c
+++ b/src/irmd/main.c
@@ -1815,6 +1815,7 @@ void * mainloop(void * o)
if (irmd->state != IRMD_RUNNING || thread_check()) {
thread_exit(id);
pthread_rwlock_unlock(&irmd->state_lock);
+ log_dbg("Thread %zd exited.", id);
break;
}
@@ -2009,27 +2010,19 @@ void * mainloop(void * o)
return (void *) 0;
}
-static bool is_thread_alive(ssize_t id)
-{
- bool ret;
- pthread_mutex_lock(&irmd->threads_lock);
-
- ret = bmp_is_id_used(irmd->thread_ids, id);
-
- pthread_mutex_unlock(&irmd->threads_lock);
-
- return ret;
-}
-
void * threadpoolmgr(void * o)
{
+ pthread_attr_t pattr;
+ struct timespec dl;
struct timespec to = {(IRMD_TPM_TIMEOUT / 1000),
(IRMD_TPM_TIMEOUT % 1000) * MILLION};
- struct timespec dl;
- size_t t;
-
(void) o;
+ if (pthread_attr_init(&pattr))
+ return (void *) -1;
+
+ pthread_attr_setdetachstate(&pattr, PTHREAD_CREATE_DETACHED);
+
while (true) {
clock_gettime(PTHREAD_COND_CLOCK, &dl);
ts_add(&dl, &to, &dl);
@@ -2037,13 +2030,13 @@ void * threadpoolmgr(void * o)
pthread_rwlock_rdlock(&irmd->state_lock);
if (irmd->state != IRMD_RUNNING) {
pthread_rwlock_unlock(&irmd->state_lock);
- log_dbg("Threadpool manager exiting.");
- for (t = 0; t < IRMD_MAX_THREADS; ++t)
- if (is_thread_alive(t)) {
- log_dbg("Waiting for thread %zd.", t);
- pthread_join(irmd->threadpool[t], NULL);
- }
-
+ pthread_attr_destroy(&pattr);
+ log_dbg("Waiting for threads exit.");
+ pthread_mutex_lock(&irmd->threads_lock);
+ while (irmd->threads > 0)
+ pthread_cond_wait(&irmd->threads_cond,
+ &irmd->threads_lock);
+ pthread_mutex_unlock(&irmd->threads_lock);
log_dbg("Threadpool manager done.");
break;
}
@@ -2064,7 +2057,8 @@ void * threadpoolmgr(void * o)
}
if (pthread_create(&irmd->threadpool[id],
- NULL, mainloop, (void *) id))
+ &pattr, mainloop,
+ (void *) id))
log_warn("Failed to start new thread.");
else
++irmd->threads;