threads: don't call the userprogram interface for initial thread, just create it
[model-checker.git] / threads.cc
index 397c7894b70291b10016d1d3774ea74f7c840c73..e0cb277563bb057c989be29379866ac8f090094b 100644 (file)
@@ -3,24 +3,24 @@
 #include "libthreads.h"
 #include "schedule.h"
 #include "common.h"
-#include "threads_internal.h"
+#include "threads.h"
 
 /* global "model" object */
 #include "model.h"
 
 #define STACK_SIZE (1024 * 1024)
 
-static void *stack_allocate(size_t size)
+static void * stack_allocate(size_t size)
 {
-       return malloc(size);
+       return userMalloc(size);
 }
 
 static void stack_free(void *stack)
 {
-       free(stack);
+       userFree(stack);
 }
 
-Thread *thread_current(void)
+Thread * thread_current(void)
 {
        return model->scheduler->get_current_thread();
 }
@@ -33,31 +33,43 @@ int Thread::create_context()
        if (ret)
                return ret;
 
-       /* start_routine == NULL means this is our initial context */
-       if (!start_routine)
-               return 0;
-
        /* Initialize new managed context */
        stack = stack_allocate(STACK_SIZE);
        context.uc_stack.ss_sp = stack;
        context.uc_stack.ss_size = STACK_SIZE;
        context.uc_stack.ss_flags = 0;
-       context.uc_link = &model->system_thread->context;
+       context.uc_link = model->get_system_context();
        makecontext(&context, start_routine, 1, arg);
 
        return 0;
 }
 
-int Thread::swap(Thread *t)
+int Thread::swap(Thread *t, ucontext_t *ctxt)
+{
+       return swapcontext(&t->context, ctxt);
+}
+
+int Thread::swap(ucontext_t *ctxt, Thread *t)
 {
-       return swapcontext(&this->context, &t->context);
+       return swapcontext(ctxt, &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);
+       }
+}
+
+void * Thread::operator new(size_t size) {
+       return userMalloc(size);
+}
+
+void Thread::operator delete(void *ptr) {
+       userFree(ptr);
 }
 
 Thread::Thread(thrd_t *t, void (*func)(), void *a) {
@@ -73,22 +85,19 @@ 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);
-       create_context();
-       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;
 }
 
 /*
@@ -99,13 +108,13 @@ static int thread_system_next(void)
        Thread *curr, *next;
 
        curr = thread_current();
-       model->check_current_action();
        if (curr) {
-               if (curr->get_state() == THREAD_READY)
+               if (curr->get_state() == THREAD_READY) {
+                       model->check_current_action();
                        model->scheduler->add_thread(curr);
-               else if (curr->get_state() == THREAD_RUNNING)
+               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");
        }
@@ -115,7 +124,7 @@ static int thread_system_next(void)
        DEBUG("(%d, %d)\n", curr ? curr->get_id() : -1, next ? next->get_id() : -1);
        if (!next)
                return 1;
-       return model->system_thread->swap(next);
+       return Thread::swap(model->get_system_context(), next);
 }
 
 static void thread_wait_finish(void)
@@ -131,21 +140,24 @@ static void thread_wait_finish(void)
  */
 int main()
 {
-       thrd_t user_thread, main_thread;
-       Thread *th;
+       thrd_t user_thread;
+       ucontext_t main_context;
 
        model = new ModelChecker();
 
-       th = new Thread(&main_thread);
+       if (getcontext(&main_context))
+               return 1;
+
+       model->set_system_context(&main_context);
 
-       /* Start user program */
-       thrd_create(&user_thread, &user_main, NULL);
+       do {
+               /* Start user program */
+               model->add_thread(new Thread(&user_thread, &user_main, NULL));
 
-       /* Wait for all threads to complete */
-       thread_wait_finish();
+               /* Wait for all threads to complete */
+               thread_wait_finish();
+       } while (model->next_execution());
 
-       model->print_trace();
-       delete th;
        delete model;
 
        DEBUG("Exiting\n");