move 'current thread' details
[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 struct thread *current;
19
20 static void enqueue_thread(struct thread *t)
21 {
22         int i;
23         struct thread_list_node *node;
24
25         for (node = nodes, i = 0; node->live && i < NUM_LIST_NODES; i++, node++);
26         if (i >= NUM_LIST_NODES)
27                 printf("ran out of nodes\n");
28         node->this = t;
29         node->next = NULL;
30         node->live = 1;
31
32         if (tail)
33                 tail->next = node;
34         else
35                 head = node;
36         tail = node;
37 }
38
39 static struct thread *dequeue_thread(void)
40 {
41         struct thread *pop;
42
43         if (!head)
44                 return NULL;
45
46         pop = head->this;
47         head->live = 0;
48         if (head == tail)
49                 tail = NULL;
50         head = head->next;
51
52         /* Set new current thread */
53         current = pop;
54
55         return pop;
56 }
57
58 static void default_add_thread(struct thread *t)
59 {
60         DEBUG("thread %d\n", t->index);
61         enqueue_thread(t);
62 }
63
64 static struct thread *default_choose_next(void)
65 {
66         return dequeue_thread();
67 }
68
69 static struct thread *default_thread_current(void)
70 {
71         return current;
72 }
73
74 void scheduler_init(struct model_checker *mod)
75 {
76         struct scheduler *sched;
77
78         /* Initialize default scheduler */
79         sched = malloc(sizeof(*sched));
80         sched->init = NULL;
81         sched->exit = NULL;
82         sched->add_thread = default_add_thread;
83         sched->next_thread = default_choose_next;
84         sched->get_current_thread = default_thread_current;
85         mod->scheduler = sched;
86 }