e456ac6b326469a85383af6a97a422e9a2c65f92
[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 int num;
12 int num1;
13 int num2;
14
15 static int thread_system_next(void)
16 {
17         Thread *curr, *next;
18
19         curr = thread_current();
20         if (curr) {
21                 if (curr->get_state() == THREAD_READY) {
22                         model->check_current_action();
23                         model->scheduler->add_thread(curr);
24                 } else if (curr->get_state() == THREAD_RUNNING)
25                         /* Stopped while running; i.e., completed */
26                         curr->complete();
27                 else
28                         ASSERT(false);
29         }
30         next = model->scheduler->next_thread();
31         if (next)
32                 next->set_state(THREAD_RUNNING);
33         DEBUG("(%d, %d)\n", curr ? curr->get_id() : -1, next ? next->get_id() : -1);
34         if (!next)
35                 return 1;
36         return Thread::swap(model->get_system_context(), next);
37 }
38
39 static void thread_wait_finish(void)
40 {
41
42         DBG();
43
44         while (!thread_system_next());
45 }
46
47 /*
48  * Main system function
49  */
50 int main()
51 {
52         thrd_t user_thread;
53         ucontext_t main_context;
54
55         model = new ModelChecker();
56
57         if (getcontext(&main_context))
58                 return 1;
59
60         model->set_system_context(&main_context);
61
62         do {
63                 /* Start user program */
64                 model->add_thread(new Thread(&user_thread, &user_main, NULL));
65
66                 /* Wait for all threads to complete */
67                 thread_wait_finish();
68         } while (model->next_execution());
69
70         delete model;
71
72         DEBUG("Exiting\n");
73         return 0;
74 }