a06a5c402f2f0ae13be90f263d87dce79a17c063
[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 static int dequeue_thread(struct thread **t)
36 {
37         if (!head) {
38                 *t = NULL;
39                 return -1;
40         }
41         *t = head->this;
42         head->live = 0;
43         if (head == tail)
44                 tail = NULL;
45         head = head->next;
46         return 0;
47 }
48
49 void schedule_add_thread(struct thread *t)
50 {
51         DEBUG("thread %d\n", t->index);
52         enqueue_thread(t);
53 }
54
55 int schedule_choose_next(struct thread **t)
56 {
57         return dequeue_thread(t);
58 }