model: build up 'may_read_from' set
[c11tester.git] / main.cc
1 /** @file main.cc
2  *  @brief Entry point for the model checker.
3  */
4
5 #include "libthreads.h"
6 #include "common.h"
7 #include "threads.h"
8
9 /* global "model" object */
10 #include "model.h"
11 #include "snapshot-interface.h"
12
13 /** The thread_system_next function takes the next step in the
14  *  execution.  @return Returns the 1 if there is another step and 0
15  *  otherwise.
16  */
17
18 static int thread_system_next(void) {
19         Thread *curr, *next;
20
21         curr = thread_current();
22         if (curr) {
23                 if (curr->get_state() == THREAD_READY) {
24                         model->check_current_action();
25                         model->scheduler->add_thread(curr);
26                 } else if (curr->get_state() == THREAD_RUNNING)
27                         /* Stopped while running; i.e., completed */
28                         curr->complete();
29                 else
30                         ASSERT(false);
31         }
32         next = model->scheduler->next_thread();
33         if (next)
34                 next->set_state(THREAD_RUNNING);
35         DEBUG("(%d, %d)\n", curr ? curr->get_id() : -1, next ? next->get_id() : -1);
36         if (!next)
37                 return 1;
38         return Thread::swap(model->get_system_context(), next);
39 }
40
41 /** The thread_wait_finish method runs the current execution until we
42  *  have no more steps to take.
43  */
44
45 static void thread_wait_finish(void) {
46         DBG();
47
48         while (!thread_system_next());
49 }
50
51
52 /** The real_main function contains the main model checking loop. */
53
54 void real_main() {
55         thrd_t user_thread;
56         ucontext_t main_context;
57
58         //Create the singleton SnapshotStack object
59         snapshotObject = new SnapshotStack();
60
61         model = new ModelChecker();
62
63         if (getcontext(&main_context))
64                 return;
65
66         model->set_system_context(&main_context);
67
68         snapshotObject->snapshotStep(0);
69
70         do {
71                 /* Start user program */
72                 model->add_thread(new Thread(&user_thread, (void (*)(void *)) &user_main, NULL));
73
74                 /* Wait for all threads to complete */
75                 thread_wait_finish();
76         } while (model->next_execution());
77
78         delete model;
79
80         DEBUG("Exiting\n");
81         finalize();
82 }
83
84 int main_numargs;
85 char ** main_args;
86
87 /**
88  * Main function.  Just initializes snapshotting library and the
89  * snapshotting library calls the real_main function.
90  */
91 int main(int numargs, char ** args) {
92         /* Stash this stuff in case someone wants it eventually */
93         main_numargs=numargs;
94         main_args=args;
95
96         /* Let's jump in quickly and start running stuff */
97         initSnapShotLibrary(10000, 1024, 1024, 1000, &real_main);
98 }