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