summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMikhail Zakharov <mikhail.zakharov@cognitivesystems.com>2019-01-02 10:31:36 -0500
committerDamien George <damien.p.george@gmail.com>2019-01-27 12:39:45 +1100
commit1e7b4222268d49c6a0e7b5a0033d3b25061653a7 (patch)
tree06d6aae9de085bf3e0a66ba2277d09991daf1977
parentf8c1be85d1d80fae4b71634a411ab354756c3bda (diff)
unix/mpthreadport: Cleanup used memory on thread exit.
-rw-r--r--ports/unix/mpthreadport.c15
1 files changed, 13 insertions, 2 deletions
diff --git a/ports/unix/mpthreadport.c b/ports/unix/mpthreadport.c
index 7170379f4..4efa5925b 100644
--- a/ports/unix/mpthreadport.c
+++ b/ports/unix/mpthreadport.c
@@ -173,6 +173,11 @@ void mp_thread_create(void *(*entry)(void*), void *arg, size_t *stack_size) {
goto er;
}
+ ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+ if (ret != 0) {
+ goto er;
+ }
+
pthread_mutex_lock(&thread_mutex);
// create thread
@@ -205,12 +210,18 @@ er:
void mp_thread_finish(void) {
pthread_mutex_lock(&thread_mutex);
- // TODO unlink from list
+ thread_t *prev = NULL;
for (thread_t *th = thread; th != NULL; th = th->next) {
if (th->id == pthread_self()) {
- th->ready = 0;
+ if (prev == NULL) {
+ thread = th->next;
+ } else {
+ prev->next = th->next;
+ }
+ free(th);
break;
}
+ prev = th;
}
pthread_mutex_unlock(&thread_mutex);
}