model: rename print_trace() -> print_summary(), fixup summary printing
[model-checker.git] / threads.cc
index 830ea1a484159c9df61b641994781d30f97cb989..456414d5b3200ec1ca8b7603834e78c761910b76 100644 (file)
@@ -10,7 +10,7 @@
 
 #define STACK_SIZE (1024 * 1024)
 
-static void *stack_allocate(size_t size)
+static void * stack_allocate(size_t size)
 {
        return malloc(size);
 }
@@ -20,7 +20,7 @@ static void stack_free(void *stack)
        free(stack);
 }
 
-Thread *thread_current(void)
+Thread * thread_current(void)
 {
        return model->scheduler->get_current_thread();
 }
@@ -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,22 +76,33 @@ 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) {
        /* system thread */
        user_thread = t;
-       state = THREAD_CREATED;
-       model->assign_id(this);
+       start_routine = NULL;
+       arg = NULL;
+
        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;
 }
 
 /*
@@ -105,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");
        }
@@ -144,8 +158,6 @@ int main()
 
                /* Wait for all threads to complete */
                thread_wait_finish();
-
-               model->print_trace();
        } while (model->next_execution());
 
        delete th;