remove EOL spaces, fix indentation
[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 /** The thread_system_next function takes the next step in the
16  *  execution.  @return Returns the 1 if there is another step and 0
17  *  otherwise.
18  */
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         next = model->scheduler->next_thread();
35         if (next)
36                 next->set_state(THREAD_RUNNING);
37         DEBUG("(%d, %d)\n", curr ? curr->get_id() : -1, next ? next->get_id() : -1);
38         if (!next)
39                 return 1;
40         return Thread::swap(model->get_system_context(), next);
41 }
42
43 /** The thread_wait_finish method runs the current execution until we
44  *  have no more steps to take.
45  */
46
47 static void thread_wait_finish(void) {
48         DBG();
49
50         while (!thread_system_next());
51 }
52
53
54 /** The real_main function contains the main model checking loop. */
55
56 void real_main() {
57         thrd_t user_thread;
58         ucontext_t main_context;
59
60         //Initialize race detector
61         initRaceDetector();
62
63         //Create the singleton SnapshotStack object
64         snapshotObject = new SnapshotStack();
65
66         model = new ModelChecker();
67
68         if (getcontext(&main_context))
69                 return;
70
71         model->set_system_context(&main_context);
72
73         snapshotObject->snapshotStep(0);
74
75         do {
76                 /* Start user program */
77                 model->add_thread(new Thread(&user_thread, (void (*)(void *)) &user_main, NULL));
78
79                 /* Wait for all threads to complete */
80                 thread_wait_finish();
81         } while (model->next_execution());
82
83         delete model;
84
85         DEBUG("Exiting\n");
86 }
87
88 int main_numargs;
89 char ** main_args;
90
91 /**
92  * Main function.  Just initializes snapshotting library and the
93  * snapshotting library calls the real_main function.
94  */
95 int main(int numargs, char ** args) {
96         /* Stash this stuff in case someone wants it eventually */
97         main_numargs=numargs;
98         main_args=args;
99
100         /* Let's jump in quickly and start running stuff */
101         initSnapShotLibrary(10000, 1024, 1024, 1000, &real_main);
102 }