model: change 'struct model_checker' to 'class ModelChecker'
[model-checker.git] / libthreads.c
index 05440c04be7f0459ec269eb9b222bff1688f5c57..2ffe958ed193909cc2960fabcf28c9ee4d3eae4c 100644 (file)
@@ -2,10 +2,13 @@
 #include <stdlib.h>
 
 #include "libthreads.h"
+#include "schedule.h"
+#include "common.h"
 
-#define STACK_SIZE (1024 * 1024)
+/* global "model" object */
+#include "model.h"
 
-static struct thread *current, *main_thread;
+#define STACK_SIZE (1024 * 1024)
 
 static void *stack_allocate(size_t size)
 {
@@ -35,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;
@@ -44,61 +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);
 }
 
-static int thread_yield()
+static void thread_dispose(struct thread *t)
 {
-       struct thread *old, *next;
-
-       DBG();
-       if (current) {
-               old = current;
-               schedule_add_thread(old);
-       } else {
-               old = main_thread;
-       }
-       schedule_choose_next(&next);
-       current = next;
-       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", 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()
+static void thread_wait_finish(void)
 {
-       struct thread *next;
 
        DBG();
 
-       do {
-               if (current)
-                       thread_dispose(current);
-               schedule_choose_next(&next);
-               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;
@@ -108,47 +118,47 @@ int thread_create(struct thread *t, void (*start_routine), void *arg)
        if (ret)
                return ret;
 
-       schedule_add_thread(t);
+       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();
 }
 
-void a(int *parm)
+int thread_yield(void)
 {
-       int i;
+       struct thread *old, *next;
 
-       for (i = 0; i < 10; i++) {
-               printf("Thread %d, magic number %d, loop %d\n", current->index, *parm, i);
-               if (i % 2)
-                       thread_yield();
-       }
+       DBG();
+       old = thread_current();
+       old->state = THREAD_READY;
+       next = model->system_thread;
+       return thread_swap(old, next);
 }
 
-void user_main()
+struct thread *thread_current(void)
 {
-       struct thread t1, t2;
-       int i = 17, j = 13;
-
-       printf("%s() creating 2 threads\n", __func__);
-       thread_create(&t1, &a, &i);
-       thread_create(&t2, &a, &j);
-
-       thread_join(&t1);
-       thread_join(&t2);
-       printf("%s() is finished\n", __func__);
+       return model->scheduler->get_current_thread();
 }
 
+/*
+ * Main system function
+ */
 int main()
 {
        struct thread user_thread;
+       struct thread *main_thread;
+
+       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);