model: rename print_trace() -> print_summary(), fixup summary printing
[model-checker.git] / threads.cc
index 4c2086d7ce7c74a82277aad853d845d345374a2a..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) {
@@ -73,8 +76,8 @@ Thread::Thread(thrd_t *t, void (*func)(), void *a) {
                printf("Error in create_context\n");
 
        state = THREAD_CREATED;
-       model->assign_id(this);
-       model->scheduler->add_thread(this);
+       id = model->get_next_id();
+       *user_thread = id;
 }
 
 Thread::Thread(thrd_t *t) {
@@ -83,15 +86,23 @@ Thread::Thread(thrd_t *t) {
        start_routine = NULL;
        arg = NULL;
 
-       state = THREAD_CREATED;
-       model->assign_id(this);
        create_context();
+       stack = NULL;
+       state = THREAD_CREATED;
+       id = model->get_next_id();
+       *user_thread = id;
        model->add_system_thread(this);
 }
 
+Thread::~Thread()
+{
+       complete();
+       model->remove_thread(this);
+}
+
 thread_id_t Thread::get_id()
 {
-       return thrd_to_id(*user_thread);
+       return id;
 }
 
 /*
@@ -108,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");
        }
@@ -147,8 +158,6 @@ int main()
 
                /* Wait for all threads to complete */
                thread_wait_finish();
-
-               model->print_trace();
        } while (model->next_execution());
 
        delete th;