move 'current thread' details
[model-checker.git] / schedule.c
index a4613c597f95d3362888df1ba6051fbbb1d75a58..c8f54e84e493b2c591d53a3b33bafdc850d64022 100644 (file)
@@ -1,4 +1,9 @@
+#include <stdlib.h>
+
+#include "libthreads.h"
 #include "schedule.h"
+#include "common.h"
+#include "model.h"
 
 struct thread_list_node {
        struct thread *this;
@@ -10,6 +15,7 @@ struct thread_list_node {
 
 struct thread_list_node *head, *tail;
 struct thread_list_node nodes[NUM_LIST_NODES];
+struct thread *current;
 
 static void enqueue_thread(struct thread *t)
 {
@@ -30,27 +36,51 @@ static void enqueue_thread(struct thread *t)
        tail = node;
 }
 
-static int dequeue_thread(struct thread **t)
+static struct thread *dequeue_thread(void)
 {
-       if (!head) {
-               *t = NULL;
-               return -1;
-       }
-       *t = head->this;
+       struct thread *pop;
+
+       if (!head)
+               return NULL;
+
+       pop = head->this;
        head->live = 0;
        if (head == tail)
                tail = NULL;
        head = head->next;
-       return 0;
+
+       /* Set new current thread */
+       current = pop;
+
+       return pop;
 }
 
-void schedule_add_thread(struct thread *t)
+static void default_add_thread(struct thread *t)
 {
        DEBUG("thread %d\n", t->index);
        enqueue_thread(t);
 }
 
-int schedule_choose_next(struct thread **t)
+static struct thread *default_choose_next(void)
+{
+       return dequeue_thread();
+}
+
+static struct thread *default_thread_current(void)
+{
+       return current;
+}
+
+void scheduler_init(struct model_checker *mod)
 {
-       return dequeue_thread(t);
+       struct scheduler *sched;
+
+       /* Initialize default scheduler */
+       sched = malloc(sizeof(*sched));
+       sched->init = NULL;
+       sched->exit = NULL;
+       sched->add_thread = default_add_thread;
+       sched->next_thread = default_choose_next;
+       sched->get_current_thread = default_thread_current;
+       mod->scheduler = sched;
 }