d567dd7fd5799f3723acf3d2ff678fa3471e6cea
[model-checker.git] / schedule.c
1 #include "libthreads.h"
2 #include "schedule.h"
3 #include "common.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 struct thread *dequeue_thread(void)
36 {
37         struct thread *pop;
38
39         if (!head)
40                 return NULL;
41
42         pop = head->this;
43         head->live = 0;
44         if (head == tail)
45                 tail = NULL;
46         head = head->next;
47
48         return pop;
49 }
50
51 void schedule_add_thread(struct thread *t)
52 {
53         DEBUG("thread %d\n", t->index);
54         enqueue_thread(t);
55 }
56
57 struct thread *schedule_choose_next(void)
58 {
59         return dequeue_thread();
60 }