cyclegraph: improve comments, use initializer list
[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 #include "datarace.h"
10
11 /* global "model" object */
12 #include "model.h"
13 #include "snapshot-interface.h"
14
15 /**
16  * The thread_system_next function takes the next step in the execution, if
17  * possible.
18  * @return Returns 0 (success) if there is another step and non-zero otherwise.
19  */
20 static int thread_system_next(void) {
21         Thread *curr, *next;
22
23         curr = thread_current();
24         if (curr) {
25                 if (curr->get_state() == THREAD_READY) {
26                         model->check_current_action();
27                         model->scheduler->add_thread(curr);
28                 } else if (curr->get_state() == THREAD_RUNNING) {
29                         /* Stopped while running; i.e., completed */
30                         curr->complete();
31                 } else {
32                         ASSERT(false);
33                 }
34         }
35         next = model->scheduler->next_thread();
36
37         /* Infeasible -> don't take any more steps */
38         if (!model->isfeasible())
39                 return 1;
40
41         if (next)
42                 next->set_state(THREAD_RUNNING);
43         DEBUG("(%d, %d)\n", curr ? curr->get_id() : -1, next ? next->get_id() : -1);
44
45         /* next == NULL -> don't take any more steps */
46         if (!next)
47                 return 1;
48         /* Return non-zero only if swap fails with an error */
49         return Thread::swap(model->get_system_context(), next);
50 }
51
52 /** The thread_wait_finish method runs the current execution until we
53  *  have no more steps to take.
54  */
55 static void thread_wait_finish(void) {
56         DBG();
57
58         while (!thread_system_next());
59 }
60
61 static void parse_options(struct model_params *params, int argc, char **argv) {
62 }
63
64 int main_argc;
65 char **main_argv;
66
67 /** The real_main function contains the main model checking loop. */
68 static void real_main() {
69         thrd_t user_thread;
70         ucontext_t main_context;
71         struct model_params params;
72
73         parse_options(&params, main_argc, main_argv);
74
75         //Initialize race detector
76         initRaceDetector();
77
78         //Create the singleton SnapshotStack object
79         snapshotObject = new SnapshotStack();
80
81         model = new ModelChecker(params);
82
83         if (getcontext(&main_context))
84                 return;
85
86         model->set_system_context(&main_context);
87
88         snapshotObject->snapshotStep(0);
89         do {
90                 /* Start user program */
91                 model->add_thread(new Thread(&user_thread, (void (*)(void *)) &user_main, NULL));
92
93                 /* Wait for all threads to complete */
94                 thread_wait_finish();
95         } while (model->next_execution());
96
97         delete model;
98
99         DEBUG("Exiting\n");
100 }
101
102 /**
103  * Main function.  Just initializes snapshotting library and the
104  * snapshotting library calls the real_main function.
105  */
106 int main(int argc, char ** argv) {
107         main_argc = argc;
108         main_argv = argv;
109
110         /* Let's jump in quickly and start running stuff */
111         initSnapShotLibrary(10000, 1024, 1024, 1000, &real_main);
112 }