model: change 'struct model_checker' to 'class ModelChecker'
[model-checker.git] / libthreads.c
index 6da8fbf6a6604b89ddaa7286289a88c9105cd1a7..2ffe958ed193909cc2960fabcf28c9ee4d3eae4c 100644 (file)
@@ -1,20 +1,25 @@
 #include <string.h>
 #include <stdlib.h>
-#include <ucontext.h>
-
-//#define CONFIG_DEBUG
 
 #include "libthreads.h"
+#include "schedule.h"
+#include "common.h"
 
-#define STACK_SIZE (1024 * 1024)
+/* global "model" object */
+#include "model.h"
 
-static struct thread *current;
+#define STACK_SIZE (1024 * 1024)
 
 static void *stack_allocate(size_t size)
 {
        return malloc(size);
 }
 
+static void stack_free(void *stack)
+{
+       free(stack);
+}
+
 static int create_context(struct thread *t)
 {
        int ret;
@@ -33,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 = &current->context;
+       t->context.uc_link = &model->system_thread->context;
        makecontext(&t->context, t->start_routine, 1, t->arg);
 
        return 0;
@@ -42,71 +47,124 @@ 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);
 }
 
-int thread_create(struct thread *t, void (*start_routine), void *arg)
+static int thread_swap(struct thread *t1, struct thread *t2)
 {
-       static int created = 1;
-       ucontext_t local;
+       return swapcontext(&t1->context, &t2->context);
+}
 
-       DBG();
+static void thread_dispose(struct thread *t)
+{
+       DEBUG("completed thread %d\n", thread_current()->id);
+       t->state = THREAD_COMPLETED;
+       stack_free(t->stack);
+}
 
-       t->index = created++;
-       DEBUG("create thread %d\n", t->index);
+/*
+ * Return 1 if found next thread, 0 otherwise
+ */
+static int thread_system_next(void)
+{
+       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);
+}
 
-       t->start_routine = start_routine;
-       t->arg = arg;
+static void thread_wait_finish(void)
+{
 
-       /* Initialize state */
-       return create_context(t);
+       DBG();
+
+       while (!thread_system_next());
 }
 
-void thread_start(struct thread *t)
+/*
+ * User program API functions
+ */
+int thread_create(struct thread *t, void (*start_routine)(), void *arg)
 {
-       struct thread *old = current;
+       int ret = 0;
+
        DBG();
 
-       current = t;
-       swapcontext(&old->context, &current->context);
+       memset(t, 0, sizeof(*t));
+       model->assign_id(t);
+       DEBUG("create thread %d\n", t->id);
 
-       DBG();
+       t->start_routine = start_routine;
+       t->arg = arg;
+
+       /* Initialize state */
+       ret = create_context(t);
+       if (ret)
+               return ret;
+
+       t->state = THREAD_CREATED;
+
+       model->scheduler->add_thread(t);
+       return 0;
 }
 
-void a(int *idx)
+void thread_join(struct thread *t)
 {
-       int i;
-
-       for (i = 0; i < 10; i++)
-               printf("Thread %d, loop %d\n", *idx, i);
+       while (t->state != THREAD_COMPLETED)
+               thread_yield();
 }
 
-void user_main()
+int thread_yield(void)
 {
-       struct thread t1, t2;
-       int i = 1, j = 2;
+       struct thread *old, *next;
 
-       thread_create(&t1, &a, &i);
-       thread_create(&t2, &a, &j);
+       DBG();
+       old = thread_current();
+       old->state = THREAD_READY;
+       next = model->system_thread;
+       return thread_swap(old, next);
+}
 
-       printf("user_main() is going to start 2 threads\n");
-       thread_start(&t1);
-       thread_start(&t2);
-       printf("user_main() is finished\n");
+struct thread *thread_current(void)
+{
+       return model->scheduler->get_current_thread();
 }
 
+/*
+ * Main system function
+ */
 int main()
 {
-       struct thread main_thread, user_thread;
+       struct thread user_thread;
+       struct thread *main_thread;
 
-       create_initial_thread(&main_thread);
-       current = &main_thread;
+       model = new ModelChecker();
 
-       thread_create(&user_thread, &user_main, NULL);
+       main_thread = (struct thread *)malloc(sizeof(*main_thread));
+       create_initial_thread(main_thread);
+       model->add_system_thread(main_thread);
 
-       thread_start(&user_thread);
+       /* Start user program */
+       thread_create(&user_thread, &user_main, NULL);
 
-       DBG();
+       /* Wait for all threads to complete */
+       thread_wait_finish();
 
        DEBUG("Exiting\n");
        return 0;