diff --git a/src/backend/threads.c b/src/backend/threads.c index 4aeebbf23..2ec076978 100644 --- a/src/backend/threads.c +++ b/src/backend/threads.c @@ -92,25 +92,30 @@ thread_loop (void* thread) t->rh = &lumiera_lock_section_.rh; do { + lumiera_threadpool_release_thread(t); + LUMIERA_CONDITION_WAIT (t->state != LUMIERA_THREADSTATE_IDLE); + INFO (threads, "Thread awaken with state %d", t->state); + // NULL function means: no work to do INFO (threads, "function %p", t->function); if (t->function) t->function (t->arguments); - lumiera_threadpool_release_thread(t); - LUMIERA_CONDITION_WAIT (t->state != LUMIERA_THREADSTATE_IDLE); - INFO (threads, "Thread awaken with state %d", t->state); + TRACE (threads, "function done"); + + if (t->kind & LUMIERA_THREAD_JOINABLE) + { + INFO (threads, "Thread zombified"); + /* move error state to data the other thread will it pick up from there */ + t->arguments = (void*)lumiera_error (); + t->state = LUMIERA_THREADSTATE_ZOMBIE; + LUMIERA_CONDITION_SIGNAL; + LUMIERA_CONDITION_WAIT (t->state == LUMIERA_THREADSTATE_JOINED); + INFO (threads, "Thread joined"); + } + } while (t->state != LUMIERA_THREADSTATE_SHUTDOWN); // SHUTDOWN state - if (t->kind & LUMIERA_THREAD_JOINABLE) - { - INFO (threads, "Thread zombified"); - /* move error state to data the other thread will it pick up from there */ - t->arguments = (void*)lumiera_error (); - t->state = LUMIERA_THREADSTATE_ZOMBIE; - LUMIERA_CONDITION_WAIT (t->state == LUMIERA_THREADSTATE_JOINED); - INFO (threads, "Thread joined"); - } INFO (threads, "Thread Shutdown"); }