schedule: create 'class Scheduler' with implementation 'class DefaultScheduler'
[cdsspec-compiler.git] / schedule.c
index f5ce644810856b06b3002996a1d95c1e11f17b9d..4f5f3de7d70ddf881e15ac6bd39cb27c795c728e 100644 (file)
@@ -1,7 +1,12 @@
+#include <stdlib.h>
+
+#include "libthreads.h"
 #include "schedule.h"
+#include "common.h"
+#include "model.h"
 
 struct thread_list_node {
-       struct thread *this;
+       struct thread *t;
        struct thread_list_node *next;
        int live;
 };
@@ -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)
 {
@@ -17,9 +23,11 @@ static void enqueue_thread(struct thread *t)
        struct thread_list_node *node;
 
        for (node = nodes, i = 0; node->live && i < NUM_LIST_NODES; i++, node++);
-       if (i >= NUM_LIST_NODES)
+       if (i >= NUM_LIST_NODES) {
                printf("ran out of nodes\n");
-       node->this = t;
+               exit(1);
+       }
+       node->t = t;
        node->next = NULL;
        node->live = 1;
 
@@ -30,25 +38,37 @@ static void enqueue_thread(struct thread *t)
        tail = node;
 }
 
-static int dequeue_thread(struct thread **t)
+static struct thread *dequeue_thread(void)
 {
+       struct thread *pop;
+
        if (!head)
-               return -1;
-       *t = head->this;
+               return NULL;
+
+       pop = head->t;
        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)
+void DefaultScheduler::add_thread(struct thread *t)
 {
-       DEBUG("thread %d\n", t->index);
+       DEBUG("thread %d\n", t->id);
        enqueue_thread(t);
 }
 
-int schedule_choose_next(struct thread **t)
+struct thread *DefaultScheduler::next_thread(void)
+{
+       return dequeue_thread();
+}
+
+struct thread *DefaultScheduler::get_current_thread(void)
 {
-       return dequeue_thread(t);
+       return current;
 }