4ac8b4991b37a70993a10485923c367bc847d4d2
[model-checker.git] / schedule.c
1 #include <stdlib.h>
2
3 #include "libthreads.h"
4 #include "schedule.h"
5 #include "common.h"
6 #include "model.h"
7
8 struct thread_list_node {
9         struct thread *this;
10         struct thread_list_node *next;
11         int live;
12 };
13
14 #define NUM_LIST_NODES 32
15
16 struct thread_list_node *head, *tail;
17 struct thread_list_node nodes[NUM_LIST_NODES];
18
19 static void enqueue_thread(struct thread *t)
20 {
21         int i;
22         struct thread_list_node *node;
23
24         for (node = nodes, i = 0; node->live && i < NUM_LIST_NODES; i++, node++);
25         if (i >= NUM_LIST_NODES)
26                 printf("ran out of nodes\n");
27         node->this = t;
28         node->next = NULL;
29         node->live = 1;
30
31         if (tail)
32                 tail->next = node;
33         else
34                 head = node;
35         tail = node;
36 }
37
38 struct thread *dequeue_thread(void)
39 {
40         struct thread *pop;
41
42         if (!head)
43                 return NULL;
44
45         pop = head->this;
46         head->live = 0;
47         if (head == tail)
48                 tail = NULL;
49         head = head->next;
50
51         return pop;
52 }
53
54 void schedule_add_thread(struct thread *t)
55 {
56         DEBUG("thread %d\n", t->index);
57         enqueue_thread(t);
58 }
59
60 struct thread *schedule_choose_next(void)
61 {
62         return dequeue_thread();
63 }
64
65 void scheduler_init(struct model_checker *mod)
66 {
67         struct scheduler *sched;
68
69         /* Initialize FCFS scheduler */
70         sched = malloc(sizeof(*sched));
71         sched->init = NULL;
72         sched->exit = NULL;
73         sched->add_thread = schedule_add_thread;
74         sched->next_thread = schedule_choose_next;
75         sched->get_current_thread = thread_current;
76         mod->scheduler = sched;
77 }