common.h: move common code (non-user) to header
[model-checker.git] / libthreads.c
index bd7f501df3cc4c76a1d7bb30c982f66df802cc1e..3eae58fca24da4556c9dfb993faba81105e91f0b 100644 (file)
@@ -1,9 +1,8 @@
 #include <string.h>
 #include <stdlib.h>
 
-//#define CONFIG_DEBUG
-
 #include "libthreads.h"
+#include "common.h"
 
 #define STACK_SIZE (1024 * 1024)
 
@@ -14,6 +13,11 @@ 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;
@@ -54,33 +58,33 @@ 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 int master_thread_yield()
+static void thread_dispose(struct thread *t)
+{
+       DEBUG("completed thread %d\n", thread_current()->index);
+       t->completed = 1;
+       stack_free(t->stack);
+}
+
+static void thread_wait_finish()
 {
        struct thread *next;
 
        DBG();
 
-       if (current) {
-               DEBUG("completed thread %d\n", current->index);
-               current->completed = 1;
-       }
-       schedule_choose_next(&next);
-       if (next && !next->completed) {
+       do {
+               if (current)
+                       thread_dispose(current);
+               schedule_choose_next(&next);
                current = next;
-               return thread_swap(main_thread, next);
-       }
-       return 1;
+       } while (next && !thread_swap(main_thread, next));
 }
 
 int thread_create(struct thread *t, void (*start_routine), void *arg)
@@ -112,12 +116,17 @@ void thread_join(struct thread *t)
                thread_yield();
 }
 
-void a(int *idx)
+struct thread *thread_current(void)
+{
+       return current;
+}
+
+void a(int *parm)
 {
        int i;
 
        for (i = 0; i < 10; i++) {
-               printf("Thread %d, loop %d\n", *idx, i);
+               printf("Thread %d, magic number %d, loop %d\n", thread_current()->index, *parm, i);
                if (i % 2)
                        thread_yield();
        }
@@ -126,7 +135,7 @@ void a(int *idx)
 void user_main()
 {
        struct thread t1, t2;
-       int i = 2, j = 3;
+       int i = 17, j = 13;
 
        printf("%s() creating 2 threads\n", __func__);
        thread_create(&t1, &a, &i);
@@ -144,10 +153,11 @@ int main()
        main_thread = malloc(sizeof(struct thread));
        create_initial_thread(main_thread);
 
+       /* Start user program */
        thread_create(&user_thread, &user_main, NULL);
 
        /* Wait for all threads to complete */
-       while (master_thread_yield() == 0);
+       thread_wait_finish();
 
        DEBUG("Exiting\n");
        return 0;