model: add ModelChecker::get_last_seq_cst()
[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.
17  * @return Returns the 1 if there is another step and 0 otherwise.
18  */
19 static int thread_system_next(void) {
20         Thread *curr, *next;
21
22         curr = thread_current();
23         if (curr) {
24                 if (curr->get_state() == THREAD_READY) {
25                         model->check_current_action();
26                         model->scheduler->add_thread(curr);
27                 } else if (curr->get_state() == THREAD_RUNNING)
28                         /* Stopped while running; i.e., completed */
29                         curr->complete();
30                 else
31                         ASSERT(false);
32         }
33         next = model->scheduler->next_thread();
34         if (next)
35                 next->set_state(THREAD_RUNNING);
36         DEBUG("(%d, %d)\n", curr ? curr->get_id() : -1, next ? next->get_id() : -1);
37         if (!next)
38                 return 1;
39         return Thread::swap(model->get_system_context(), next);
40 }
41
42 /** The thread_wait_finish method runs the current execution until we
43  *  have no more steps to take.
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 static void real_main() {
54         thrd_t user_thread;
55         ucontext_t main_context;
56
57         //Initialize race detector
58         initRaceDetector();
59
60         //Create the singleton SnapshotStack object
61         snapshotObject = new SnapshotStack();
62
63         model = new ModelChecker();
64
65         if (getcontext(&main_context))
66                 return;
67
68         model->set_system_context(&main_context);
69
70         snapshotObject->snapshotStep(0);
71
72         do {
73                 /* Start user program */
74                 model->add_thread(new Thread(&user_thread, (void (*)(void *)) &user_main, NULL));
75
76                 /* Wait for all threads to complete */
77                 thread_wait_finish();
78         } while (model->next_execution());
79
80         delete model;
81
82         DEBUG("Exiting\n");
83 }
84
85 int main_numargs;
86 char ** main_args;
87
88 /**
89  * Main function.  Just initializes snapshotting library and the
90  * snapshotting library calls the real_main function.
91  */
92 int main(int numargs, char ** args) {
93         /* Stash this stuff in case someone wants it eventually */
94         main_numargs=numargs;
95         main_args=args;
96
97         /* Let's jump in quickly and start running stuff */
98         initSnapShotLibrary(10000, 1024, 1024, 1000, &real_main);
99 }