model: rename print_trace() -> print_summary(), fixup summary printing
[model-checker.git] / threads.cc
index 3af74966469afe1a6897802de9e27c434adf4e76..456414d5b3200ec1ca8b7603834e78c761910b76 100644 (file)
@@ -53,11 +53,14 @@ int Thread::swap(Thread *t)
        return swapcontext(&this->context, &t->context);
 }
 
-void Thread::dispose()
+void Thread::complete()
 {
-       DEBUG("completed thread %d\n", thread_current()->get_id());
-       state = THREAD_COMPLETED;
-       stack_free(stack);
+       if (state != THREAD_COMPLETED) {
+               DEBUG("completed thread %d\n", get_id());
+               state = THREAD_COMPLETED;
+               if (stack)
+                       stack_free(stack);
+       }
 }
 
 Thread::Thread(thrd_t *t, void (*func)(), void *a) {
@@ -83,13 +86,20 @@ Thread::Thread(thrd_t *t) {
        start_routine = NULL;
        arg = NULL;
 
+       create_context();
+       stack = NULL;
        state = THREAD_CREATED;
        id = model->get_next_id();
        *user_thread = id;
-       create_context();
        model->add_system_thread(this);
 }
 
+Thread::~Thread()
+{
+       complete();
+       model->remove_thread(this);
+}
+
 thread_id_t Thread::get_id()
 {
        return id;
@@ -109,7 +119,7 @@ static int thread_system_next(void)
                        model->scheduler->add_thread(curr);
                else if (curr->get_state() == THREAD_RUNNING)
                        /* Stopped while running; i.e., completed */
-                       curr->dispose();
+                       curr->complete();
                else
                        DEBUG("ERROR: current thread in unexpected state??\n");
        }
@@ -148,8 +158,6 @@ int main()
 
                /* Wait for all threads to complete */
                thread_wait_finish();
-
-               model->print_trace();
        } while (model->next_execution());
 
        delete th;