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