schedule: exit if we run out of linked-list nodes
[c11tester.git] / libthreads.c
index 5f442b20b1c6ac642f09f7888f055b74f076d7d7..8d76bf12dbf7459cd83d97c18948fa08a39725fd 100644 (file)
 #include <string.h>
 #include <stdlib.h>
-#include <ucontext.h>
-#include <stdio.h>
 
-//#define CONFIG_DEBUG
+#include "libthreads.h"
+#include "schedule.h"
+#include "common.h"
 
-#ifdef CONFIG_DEBUG
-#define DBG() do { printf("Here: %s, L%d\n", __func__, __LINE__); } while (0)
-#define DEBUG(fmt, ...) printf(fmt, ##__VA_ARGS__)
-#else
-#define DBG()
-#define DEBUG(fmt, ...)
-#endif
+/* global "model" struct */
+#include "model.h"
 
 #define STACK_SIZE (1024 * 1024)
 
-struct thread {
-       void (*start_routine);
-       void *arg;
-       ucontext_t context;
-       void *stack;
-       int started;
-       int index;
-};
-
-static struct thread *current;
-static ucontext_t *cleanup;
-
 static void *stack_allocate(size_t size)
 {
        return malloc(size);
 }
 
-int thread_create(struct thread *t, void (*start_routine), void *arg)
+static void stack_free(void *stack)
 {
-       static int created;
-       ucontext_t local;
+       free(stack);
+}
 
-       DBG();
+static int create_context(struct thread *t)
+{
+       int ret;
 
-       t->index = created++;
-       DEBUG("create thread %d\n", t->index);
+       memset(&t->context, 0, sizeof(t->context));
+       ret = getcontext(&t->context);
+       if (ret)
+               return ret;
 
-       t->start_routine = start_routine;
-       t->arg = arg;
-       t->started = 0;
+       /* t->start_routine == NULL means this is our initial context */
+       if (!t->start_routine)
+               return 0;
 
-       /* Initialize state */
-       getcontext(&t->context);
+       /* Initialize new managed context */
        t->stack = stack_allocate(STACK_SIZE);
        t->context.uc_stack.ss_sp = t->stack;
        t->context.uc_stack.ss_size = STACK_SIZE;
        t->context.uc_stack.ss_flags = 0;
-       if (current)
-               t->context.uc_link = &current->context;
-       else
-               t->context.uc_link = cleanup;
+       t->context.uc_link = &model->system_thread->context;
        makecontext(&t->context, t->start_routine, 1, t->arg);
 
        return 0;
 }
 
-void thread_start(struct thread *t)
+static int create_initial_thread(struct thread *t)
 {
-       DBG();
+       memset(t, 0, sizeof(*t));
+       return create_context(t);
+}
+
+static int thread_swap(struct thread *old, struct thread *new)
+{
+       return swapcontext(&old->context, &new->context);
+}
+
+int thread_yield(void)
+{
+       struct thread *old, *next;
 
-       t->started = 1;
-       
-       if (current) {
-               struct thread *old = current;
-               current = t;
-               swapcontext(&old->context, &current->context);
-       } else {
-               current = t;
-               swapcontext(cleanup, &current->context);
-       }
        DBG();
+       old = thread_current();
+       model->scheduler->add_thread(old);
+       next = model->scheduler->next_thread();
+       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", thread_current()->index);
+       t->completed = 1;
+       stack_free(t->stack);
 }
 
-void a(int *idx)
+static void thread_wait_finish(void)
 {
-       int i;
+       struct thread *curr, *next;
 
-       for (i = 0; i < 10; i++)
-               printf("Thread %d, loop %d\n", *idx, i);
+       DBG();
+
+       do {
+               if ((curr = thread_current()))
+                       thread_dispose(curr);
+               next = model->scheduler->next_thread();
+       } while (next && !thread_swap(model->system_thread, next));
 }
 
-void user_main()
+int thread_create(struct thread *t, void (*start_routine), void *arg)
 {
-       struct thread t1, t2;
-       int i = 1, j = 2;
+       static int created = 1;
+       int ret = 0;
+
+       DBG();
+
+       memset(t, 0, sizeof(*t));
+       t->index = created++;
+       DEBUG("create thread %d\n", t->index);
+
+       t->start_routine = start_routine;
+       t->arg = arg;
+
+       /* Initialize state */
+       ret = create_context(t);
+       if (ret)
+               return ret;
+
+       model->scheduler->add_thread(t);
+       return 0;
+}
 
-       thread_create(&t1, &a, &i);
-       thread_create(&t2, &a, &j);
+void thread_join(struct thread *t)
+{
+       while (!t->completed)
+               thread_yield();
+}
 
-       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();
 }
 
 int main()
 {
-       struct thread t;
-       ucontext_t main_context;
-       int pass = 0;
+       struct thread user_thread;
+       struct thread *main_thread;
 
-       cleanup = &main_context;
+       model_checker_init();
 
-       thread_create(&t, &user_main, NULL);
+       main_thread = malloc(sizeof(struct thread));
+       create_initial_thread(main_thread);
+       model_checker_add_system_thread(main_thread);
 
-       getcontext(&main_context);
-       if (!pass++)
-               thread_start(&t);
+       /* Start user program */
+       thread_create(&user_thread, &user_main, NULL);
 
-       DBG();
+       /* Wait for all threads to complete */
+       thread_wait_finish();
 
-       DEBUG("Exiting?\n");
+       DEBUG("Exiting\n");
        return 0;
 }