common.h: move common code (non-user) to header
[model-checker.git] / libthreads.c
index 05440c04be7f0459ec269eb9b222bff1688f5c57..3eae58fca24da4556c9dfb993faba81105e91f0b 100644 (file)
@@ -2,6 +2,7 @@
 #include <stdlib.h>
 
 #include "libthreads.h"
+#include "common.h"
 
 #define STACK_SIZE (1024 * 1024)
 
@@ -57,20 +58,17 @@ static int thread_yield()
        struct thread *old, *next;
 
        DBG();
-       if (current) {
-               old = current;
-               schedule_add_thread(old);
-       } else {
-               old = main_thread;
-       }
+       old = current;
+       schedule_add_thread(old);
        schedule_choose_next(&next);
        current = next;
+       DEBUG("(%d, %d)\n", old->index, next->index);
        return thread_swap(old, next);
 }
 
 static void thread_dispose(struct thread *t)
 {
-       DEBUG("completed thread %d\n", current->index);
+       DEBUG("completed thread %d\n", thread_current()->index);
        t->completed = 1;
        stack_free(t->stack);
 }
@@ -118,12 +116,17 @@ void thread_join(struct thread *t)
                thread_yield();
 }
 
+struct thread *thread_current(void)
+{
+       return current;
+}
+
 void a(int *parm)
 {
        int i;
 
        for (i = 0; i < 10; i++) {
-               printf("Thread %d, magic number %d, loop %d\n", current->index, *parm, i);
+               printf("Thread %d, magic number %d, loop %d\n", thread_current()->index, *parm, i);
                if (i % 2)
                        thread_yield();
        }