libthreads: thread_join: return 'int' as status
[model-checker.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(ModelAction *act)
102 {
103         struct thread *old, *next;
104
105         DBG();
106         model->set_current_action(act);
107         old = thread_current();
108         old->state = THREAD_READY;
109         next = model->system_thread;
110         return thread_swap(old, next);
111 }
112
113 /*
114  * User program API functions
115  */
116 int thread_create(struct thread *t, void (*start_routine)(), void *arg)
117 {
118         int ret = 0;
119
120         DBG();
121
122         memset(t, 0, sizeof(*t));
123         model->assign_id(t);
124         DEBUG("create thread %d\n", t->id);
125
126         t->start_routine = start_routine;
127         t->arg = arg;
128
129         /* Initialize state */
130         ret = create_context(t);
131         if (ret)
132                 return ret;
133
134         t->state = THREAD_CREATED;
135
136         model->scheduler->add_thread(t);
137         return 0;
138 }
139
140 int thread_join(struct thread *t)
141 {
142         int ret = 0;
143         while (t->state != THREAD_COMPLETED && !ret)
144                 /* seq_cst is just a 'don't care' parameter */
145                 ret = thread_switch_to_master(new ModelAction(THREAD_JOIN, memory_order_seq_cst, NULL, VALUE_NONE));
146         return ret;
147 }
148
149 int thread_yield(void)
150 {
151         /* seq_cst is just a 'don't care' parameter */
152         return thread_switch_to_master(new ModelAction(THREAD_YIELD, memory_order_seq_cst, NULL, VALUE_NONE));
153 }
154
155 struct thread *thread_current(void)
156 {
157         return model->scheduler->get_current_thread();
158 }
159
160 /*
161  * Main system function
162  */
163 int main()
164 {
165         struct thread user_thread;
166         struct thread *main_thread;
167
168         model = new ModelChecker();
169
170         main_thread = (struct thread *)myMalloc(sizeof(*main_thread));
171         create_initial_thread(main_thread);
172         model->add_system_thread(main_thread);
173
174         /* Start user program */
175         thread_create(&user_thread, &user_main, NULL);
176
177         /* Wait for all threads to complete */
178         thread_wait_finish();
179
180         delete model;
181         myFree(main_thread);
182
183         DEBUG("Exiting\n");
184         return 0;
185 }