model: change 'struct model_checker' to 'class ModelChecker'
[c11tester.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 *t;
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                 exit(1);
29         }
30         node->t = t;
31         node->next = NULL;
32         node->live = 1;
33
34         if (tail)
35                 tail->next = node;
36         else
37                 head = node;
38         tail = node;
39 }
40
41 static struct thread *dequeue_thread(void)
42 {
43         struct thread *pop;
44
45         if (!head)
46                 return NULL;
47
48         pop = head->t;
49         head->live = 0;
50         if (head == tail)
51                 tail = NULL;
52         head = head->next;
53
54         /* Set new current thread */
55         current = pop;
56
57         return pop;
58 }
59
60 static void default_add_thread(struct thread *t)
61 {
62         DEBUG("thread %d\n", t->id);
63         enqueue_thread(t);
64 }
65
66 static struct thread *default_choose_next(void)
67 {
68         return dequeue_thread();
69 }
70
71 static struct thread *default_thread_current(void)
72 {
73         return current;
74 }
75
76 void scheduler_init(ModelChecker *mod)
77 {
78         struct scheduler *sched;
79
80         /* Initialize default scheduler */
81         sched = (struct scheduler *)malloc(sizeof(*sched));
82         sched->init = NULL;
83         sched->exit = NULL;
84         sched->add_thread = default_add_thread;
85         sched->next_thread = default_choose_next;
86         sched->get_current_thread = default_thread_current;
87         mod->scheduler = sched;
88 }