threads_internal: add 'thread_switch_to_master()' internally
[cdsspec-compiler.git] / libthreads.cc
1 #include <string.h>
2 #include <stdlib.h>
3
4 #include "libthreads.h"
5 #include "schedule.h"
6 #include "common.h"
7 #include "threads_internal.h"
8
9 /* global "model" object */
10 #include "model.h"
11
12 #define STACK_SIZE (1024 * 1024)
13
14 static void *stack_allocate(size_t size)
15 {
16         return malloc(size);
17 }
18
19 static void stack_free(void *stack)
20 {
21         free(stack);
22 }
23
24 static int create_context(struct thread *t)
25 {
26         int ret;
27
28         memset(&t->context, 0, sizeof(t->context));
29         ret = getcontext(&t->context);
30         if (ret)
31                 return ret;
32
33         /* t->start_routine == NULL means this is our initial context */
34         if (!t->start_routine)
35                 return 0;
36
37         /* Initialize new managed context */
38         t->stack = stack_allocate(STACK_SIZE);
39         t->context.uc_stack.ss_sp = t->stack;
40         t->context.uc_stack.ss_size = STACK_SIZE;
41         t->context.uc_stack.ss_flags = 0;
42         t->context.uc_link = &model->system_thread->context;
43         makecontext(&t->context, t->start_routine, 1, t->arg);
44
45         return 0;
46 }
47
48 static int create_initial_thread(struct thread *t)
49 {
50         memset(t, 0, sizeof(*t));
51         model->assign_id(t);
52         return create_context(t);
53 }
54
55 static int thread_swap(struct thread *t1, struct thread *t2)
56 {
57         return swapcontext(&t1->context, &t2->context);
58 }
59
60 static void thread_dispose(struct thread *t)
61 {
62         DEBUG("completed thread %d\n", thread_current()->id);
63         t->state = THREAD_COMPLETED;
64         stack_free(t->stack);
65 }
66
67 /*
68  * Return 1 if found next thread, 0 otherwise
69  */
70 static int thread_system_next(void)
71 {
72         struct thread *curr, *next;
73
74         curr = thread_current();
75         if (curr) {
76                 if (curr->state == THREAD_READY)
77                         model->scheduler->add_thread(curr);
78                 else if (curr->state == THREAD_RUNNING)
79                         /* Stopped while running; i.e., completed */
80                         thread_dispose(curr);
81                 else
82                         DEBUG("ERROR: current thread in unexpected state??\n");
83         }
84         next = model->scheduler->next_thread();
85         if (next)
86                 next->state = THREAD_RUNNING;
87         DEBUG("(%d, %d)\n", curr ? curr->id : -1, next ? next->id : -1);
88         if (!next)
89                 return 1;
90         return thread_swap(model->system_thread, next);
91 }
92
93 static void thread_wait_finish(void)
94 {
95
96         DBG();
97
98         while (!thread_system_next());
99 }
100
101 int thread_switch_to_master()
102 {
103         struct thread *old, *next;
104
105         DBG();
106         old = thread_current();
107         old->state = THREAD_READY;
108         next = model->system_thread;
109         return thread_swap(old, next);
110 }
111
112 /*
113  * User program API functions
114  */
115 int thread_create(struct thread *t, void (*start_routine)(), void *arg)
116 {
117         int ret = 0;
118
119         DBG();
120
121         memset(t, 0, sizeof(*t));
122         model->assign_id(t);
123         DEBUG("create thread %d\n", t->id);
124
125         t->start_routine = start_routine;
126         t->arg = arg;
127
128         /* Initialize state */
129         ret = create_context(t);
130         if (ret)
131                 return ret;
132
133         t->state = THREAD_CREATED;
134
135         model->scheduler->add_thread(t);
136         return 0;
137 }
138
139 void thread_join(struct thread *t)
140 {
141         int ret = 0;
142         while (t->state != THREAD_COMPLETED && !ret)
143                 ret = thread_switch_to_master();
144 }
145
146 int thread_yield(void)
147 {
148         return thread_switch_to_master();
149 }
150
151 struct thread *thread_current(void)
152 {
153         return model->scheduler->get_current_thread();
154 }
155
156 /*
157  * Main system function
158  */
159 int main()
160 {
161         struct thread user_thread;
162         struct thread *main_thread;
163
164         model = new ModelChecker();
165
166         main_thread = (struct thread *)myMalloc(sizeof(*main_thread));
167         create_initial_thread(main_thread);
168         model->add_system_thread(main_thread);
169
170         /* Start user program */
171         thread_create(&user_thread, &user_main, NULL);
172
173         /* Wait for all threads to complete */
174         thread_wait_finish();
175
176         delete model;
177         myFree(main_thread);
178
179         DEBUG("Exiting\n");
180         return 0;
181 }