unix/mpthreadport: Cleanup used memory on thread exit.

This commit is contained in:
Mikhail Zakharov 2019-01-02 10:31:36 -05:00 committed by Damien George
parent f8c1be85d1
commit 1e7b422226

View File

@ -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);
}