schedule: add scheduler, thread_yield(), etc.
[model-checker.git] / schedule.c
1 //#define CONFIG_DEBUG
2
3 #include "schedule.h"
4
5 struct thread_list_node {
6         struct thread *this;
7         struct thread_list_node *next;
8         int live;
9 };
10
11 #define NUM_LIST_NODES 32
12
13 struct thread_list_node *head, *tail;
14 struct thread_list_node nodes[NUM_LIST_NODES];
15
16 static void enqueue_thread(struct thread *t)
17 {
18         int i;
19         struct thread_list_node *node;
20
21         for (node = nodes, i = 0; node->live && i < NUM_LIST_NODES; i++, node++);
22         if (i >= NUM_LIST_NODES)
23                 printf("ran out of nodes\n");
24         node->this = t;
25         node->next = NULL;
26         node->live = 1;
27
28         if (tail)
29                 tail->next = node;
30         else
31                 head = node;
32         tail = node;
33 }
34
35 static int dequeue_thread(struct thread **t)
36 {
37         if (!head)
38                 return -1;
39         *t = head->this;
40         head->live = 0;
41         if (head == tail)
42                 tail = NULL;
43         head = head->next;
44         return 0;
45 }
46
47 void schedule_add_thread(struct thread *t)
48 {
49         DEBUG("%s: thread %d\n", __func__, t->index);
50         enqueue_thread(t);
51 }
52
53 int schedule_choose_next(struct thread **t)
54 {
55         return dequeue_thread(t);
56 }