more doc changes
[model-checker.git] / main.cc
1 #include "libthreads.h"
2 #include "common.h"
3 #include "threads.h"
4
5 /* global "model" object */
6 #include "model.h"
7 #include "snapshot-interface.h"
8
9 /*
10  * Return 1 if found next thread, 0 otherwise
11  */
12 static int thread_system_next(void) {
13         Thread *curr, *next;
14
15         curr = thread_current();
16         if (curr) {
17                 if (curr->get_state() == THREAD_READY) {
18                         model->check_current_action();
19                         model->scheduler->add_thread(curr);
20                 } else if (curr->get_state() == THREAD_RUNNING)
21                         /* Stopped while running; i.e., completed */
22                         curr->complete();
23                 else
24                         ASSERT(false);
25         }
26         next = model->scheduler->next_thread();
27         if (next)
28                 next->set_state(THREAD_RUNNING);
29         DEBUG("(%d, %d)\n", curr ? curr->get_id() : -1, next ? next->get_id() : -1);
30         if (!next)
31                 return 1;
32         return Thread::swap(model->get_system_context(), next);
33 }
34
35 static void thread_wait_finish(void) {
36         DBG();
37
38         while (!thread_system_next());
39 }
40
41 void real_main() {
42         thrd_t user_thread;
43         ucontext_t main_context;
44
45         //Create the singleton SnapshotStack object
46         snapshotObject = new SnapshotStack();
47
48         model = new ModelChecker();
49
50         if (getcontext(&main_context))
51                 return;
52
53         model->set_system_context(&main_context);
54
55         snapshotObject->snapshotStep(0);
56
57         do {
58                 /* Start user program */
59                 model->add_thread(new Thread(&user_thread, (void (*)(void *)) &user_main, NULL));
60
61                 /* Wait for all threads to complete */
62                 thread_wait_finish();
63         } while (model->next_execution());
64
65         delete model;
66
67         DEBUG("Exiting\n");
68         finalize();
69 }
70
71 int main_numargs;
72 char ** main_args;
73
74 /*
75  * Main system function
76  */
77 int main(int numargs, char ** args) {
78         /* Stash this stuff in case someone wants it eventually */
79         main_numargs=numargs;
80         main_args=args;
81
82         /* Let's jump in quickly and start running stuff */
83         initSnapShotLibrary(10000, 1024, 1024, 1000, &real_main);
84 }