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