main, threads: improve comments regarding thread stepping
[model-checker.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
62 /** The real_main function contains the main model checking loop. */
63 static void real_main() {
64         thrd_t user_thread;
65         ucontext_t main_context;
66
67         //Initialize race detector
68         initRaceDetector();
69
70         //Create the singleton SnapshotStack object
71         snapshotObject = new SnapshotStack();
72
73         model = new ModelChecker();
74
75         if (getcontext(&main_context))
76                 return;
77
78         model->set_system_context(&main_context);
79
80         snapshotObject->snapshotStep(0);
81         do {
82                 /* Start user program */
83                 model->add_thread(new Thread(&user_thread, (void (*)(void *)) &user_main, NULL));
84
85                 /* Wait for all threads to complete */
86                 thread_wait_finish();
87         } while (model->next_execution());
88
89         delete model;
90
91         DEBUG("Exiting\n");
92 }
93
94 int main_numargs;
95 char ** main_args;
96
97 /**
98  * Main function.  Just initializes snapshotting library and the
99  * snapshotting library calls the real_main function.
100  */
101 int main(int numargs, char ** args) {
102         /* Stash this stuff in case someone wants it eventually */
103         main_numargs=numargs;
104         main_args=args;
105
106         /* Let's jump in quickly and start running stuff */
107         initSnapShotLibrary(10000, 1024, 1024, 1000, &real_main);
108 }