diff --git a/src/thread/thread.c b/src/thread/thread.c index 83f8e28..c332d42 100644 --- a/src/thread/thread.c +++ b/src/thread/thread.c @@ -182,7 +182,6 @@ int thread_join(thread_t thread, void** retval) running->last_waited = malloc(sizeof(struct last_thread_t)); running->last_waited->ref = 0 ; entry->last_waited = running->last_waited; - entry->last_waited->ref++; running->last_waited->last_thread = entry; } running->last_waited->ref++; @@ -207,6 +206,15 @@ int thread_join(thread_t thread, void** retval) } while (!IS_FINISHED(entry)); // Exit from waiting state running->status &= ~WAITING; + + if (running->last_waited) { + // Release the last waited thread if no one use it anymore + DBG("Last waited ref : %d", running->last_waited->ref); + if (--running->last_waited->ref == 0) { + free(running->last_waited); + } + running->last_waited = NULL; + } } // Save returned value if needed @@ -229,16 +237,6 @@ void thread_exit(void* retval) TRACE("Exit thread %p", running); print_entry(running); - // free the memory of the last thread struct if no one use it anymore - if(running->last_waited) { - DBG("Last waited ref : %d", running->last_waited->ref); - if(--running->last_waited->ref == 0) - free(running->last_waited); - } - if (running == NULL) { - exit(0); - } - running->status |= FINISHED; if (IS_WAITED(running)) { // If the thread was waited by another thread, we need to wake it up.