schedule: create 'class Scheduler' with implementation 'class DefaultScheduler'
[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 void DefaultScheduler::add_thread(struct thread *t)
61 {
62         DEBUG("thread %d\n", t->id);
63         enqueue_thread(t);
64 }
65
66 struct thread *DefaultScheduler::next_thread(void)
67 {
68         return dequeue_thread();
69 }
70
71 struct thread *DefaultScheduler::get_current_thread(void)
72 {
73         return current;
74 }