common.h: move common code (non-user) to header
[c11tester.git] / schedule.c
1 #include "schedule.h"
2 #include "common.h"
3
4 struct thread_list_node {
5         struct thread *this;
6         struct thread_list_node *next;
7         int live;
8 };
9
10 #define NUM_LIST_NODES 32
11
12 struct thread_list_node *head, *tail;
13 struct thread_list_node nodes[NUM_LIST_NODES];
14
15 static void enqueue_thread(struct thread *t)
16 {
17         int i;
18         struct thread_list_node *node;
19
20         for (node = nodes, i = 0; node->live && i < NUM_LIST_NODES; i++, node++);
21         if (i >= NUM_LIST_NODES)
22                 printf("ran out of nodes\n");
23         node->this = t;
24         node->next = NULL;
25         node->live = 1;
26
27         if (tail)
28                 tail->next = node;
29         else
30                 head = node;
31         tail = node;
32 }
33
34 static int dequeue_thread(struct thread **t)
35 {
36         if (!head) {
37                 *t = NULL;
38                 return -1;
39         }
40         *t = head->this;
41         head->live = 0;
42         if (head == tail)
43                 tail = NULL;
44         head = head->next;
45         return 0;
46 }
47
48 void schedule_add_thread(struct thread *t)
49 {
50         DEBUG("thread %d\n", t->index);
51         enqueue_thread(t);
52 }
53
54 int schedule_choose_next(struct thread **t)
55 {
56         return dequeue_thread(t);
57 }