main/threads: remove excess headers
[c11tester.git] / main.cc
1 #include "libthreads.h"
2 #include "common.h"
3 #include "threads.h"
4
5 /* global "model" object */
6 #include "model.h"
7
8 /*
9  * Return 1 if found next thread, 0 otherwise
10  */
11 static int thread_system_next(void)
12 {
13         Thread *curr, *next;
14
15         curr = thread_current();
16         if (curr) {
17                 if (curr->get_state() == THREAD_READY) {
18                         model->check_current_action();
19                         model->scheduler->add_thread(curr);
20                 } else if (curr->get_state() == THREAD_RUNNING)
21                         /* Stopped while running; i.e., completed */
22                         curr->complete();
23                 else
24                         ASSERT(false);
25         }
26         next = model->scheduler->next_thread();
27         if (next)
28                 next->set_state(THREAD_RUNNING);
29         DEBUG("(%d, %d)\n", curr ? curr->get_id() : -1, next ? next->get_id() : -1);
30         if (!next)
31                 return 1;
32         return Thread::swap(model->get_system_context(), next);
33 }
34
35 static void thread_wait_finish(void)
36 {
37
38         DBG();
39
40         while (!thread_system_next());
41 }
42
43 /*
44  * Main system function
45  */
46 int main()
47 {
48         thrd_t user_thread;
49         ucontext_t main_context;
50
51         model = new ModelChecker();
52
53         if (getcontext(&main_context))
54                 return 1;
55
56         model->set_system_context(&main_context);
57
58         do {
59                 /* Start user program */
60                 model->add_thread(new Thread(&user_thread, &user_main, NULL));
61
62                 /* Wait for all threads to complete */
63                 thread_wait_finish();
64         } while (model->next_execution());
65
66         delete model;
67
68         DEBUG("Exiting\n");
69         return 0;
70 }