model: change 'struct model_checker' to 'class ModelChecker'
[model-checker.git] / libthreads.c
index 3697c0851e035109ddd49329ea640f8a154b08ca..2ffe958ed193909cc2960fabcf28c9ee4d3eae4c 100644 (file)
@@ -5,13 +5,11 @@
 #include "schedule.h"
 #include "common.h"
 
-/* global "model" struct */
+/* global "model" object */
 #include "model.h"
 
 #define STACK_SIZE (1024 * 1024)
 
-static struct thread *current, *main_thread;
-
 static void *stack_allocate(size_t size)
 {
        return malloc(size);
@@ -40,7 +38,7 @@ static int create_context(struct thread *t)
        t->context.uc_stack.ss_sp = t->stack;
        t->context.uc_stack.ss_size = STACK_SIZE;
        t->context.uc_stack.ss_flags = 0;
-       t->context.uc_link = &main_thread->context;
+       t->context.uc_link = &model->system_thread->context;
        makecontext(&t->context, t->start_routine, 1, t->arg);
 
        return 0;
@@ -49,58 +47,68 @@ static int create_context(struct thread *t)
 static int create_initial_thread(struct thread *t)
 {
        memset(t, 0, sizeof(*t));
+       model->assign_id(t);
        return create_context(t);
 }
 
-static int thread_swap(struct thread *old, struct thread *new)
+static int thread_swap(struct thread *t1, struct thread *t2)
 {
-       return swapcontext(&old->context, &new->context);
+       return swapcontext(&t1->context, &t2->context);
 }
 
-int thread_yield(void)
+static void thread_dispose(struct thread *t)
 {
-       struct thread *old, *next;
-
-       DBG();
-       old = current;
-       model->scheduler->add_thread(old);
-       next = model->scheduler->next_thread();
-       current = next;
-       DEBUG("(%d, %d)\n", old->index, next->index);
-       return thread_swap(old, next);
+       DEBUG("completed thread %d\n", thread_current()->id);
+       t->state = THREAD_COMPLETED;
+       stack_free(t->stack);
 }
 
-static void thread_dispose(struct thread *t)
+/*
+ * Return 1 if found next thread, 0 otherwise
+ */
+static int thread_system_next(void)
 {
-       DEBUG("completed thread %d\n", thread_current()->index);
-       t->completed = 1;
-       stack_free(t->stack);
+       struct thread *curr, *next;
+
+       curr = thread_current();
+       if (curr) {
+               if (curr->state == THREAD_READY)
+                       model->scheduler->add_thread(curr);
+               else if (curr->state == THREAD_RUNNING)
+                       /* Stopped while running; i.e., completed */
+                       thread_dispose(curr);
+               else
+                       DEBUG("ERROR: current thread in unexpected state??\n");
+       }
+       next = model->scheduler->next_thread();
+       if (next)
+               next->state = THREAD_RUNNING;
+       DEBUG("(%d, %d)\n", curr ? curr->id : -1, next ? next->id : -1);
+       if (!next)
+               return 1;
+       return thread_swap(model->system_thread, next);
 }
 
 static void thread_wait_finish(void)
 {
-       struct thread *next;
 
        DBG();
 
-       do {
-               if (current)
-                       thread_dispose(current);
-               next = model->scheduler->next_thread();
-               current = next;
-       } while (next && !thread_swap(main_thread, next));
+       while (!thread_system_next());
 }
 
-int thread_create(struct thread *t, void (*start_routine), void *arg)
+/*
+ * User program API functions
+ */
+int thread_create(struct thread *t, void (*start_routine)(), void *arg)
 {
-       static int created = 1;
        int ret = 0;
 
        DBG();
 
        memset(t, 0, sizeof(*t));
-       t->index = created++;
-       DEBUG("create thread %d\n", t->index);
+       model->assign_id(t);
+       DEBUG("create thread %d\n", t->id);
 
        t->start_routine = start_routine;
        t->arg = arg;
@@ -110,29 +118,47 @@ int thread_create(struct thread *t, void (*start_routine), void *arg)
        if (ret)
                return ret;
 
+       t->state = THREAD_CREATED;
+
        model->scheduler->add_thread(t);
        return 0;
 }
 
 void thread_join(struct thread *t)
 {
-       while (!t->completed)
+       while (t->state != THREAD_COMPLETED)
                thread_yield();
 }
 
+int thread_yield(void)
+{
+       struct thread *old, *next;
+
+       DBG();
+       old = thread_current();
+       old->state = THREAD_READY;
+       next = model->system_thread;
+       return thread_swap(old, next);
+}
+
 struct thread *thread_current(void)
 {
-       return current;
+       return model->scheduler->get_current_thread();
 }
 
+/*
+ * Main system function
+ */
 int main()
 {
        struct thread user_thread;
+       struct thread *main_thread;
 
-       model_checker_init();
+       model = new ModelChecker();
 
-       main_thread = malloc(sizeof(struct thread));
+       main_thread = (struct thread *)malloc(sizeof(*main_thread));
        create_initial_thread(main_thread);
+       model->add_system_thread(main_thread);
 
        /* Start user program */
        thread_create(&user_thread, &user_main, NULL);