switch back to norris style spacing in changed files
[model-checker.git] / main.cc
1 /* -*- Mode: C; indent-tabs-mode: t -*- */
2
3 #include "libthreads.h"
4 #include "common.h"
5 #include "threads.h"
6
7 /* global "model" object */
8 #include "model.h"
9 #include "snapshot.h"
10 #include "snapshot-interface.h"
11
12 /*
13  * Return 1 if found next thread, 0 otherwise
14  */
15 static int thread_system_next(void) {
16         Thread *curr, *next;
17   
18         curr = thread_current();
19         if (curr) {
20                 if (curr->get_state() == THREAD_READY) {
21                         model->check_current_action();
22                         model->scheduler->add_thread(curr);
23                 } else if (curr->get_state() == THREAD_RUNNING)
24                         /* Stopped while running; i.e., completed */
25                         curr->complete();
26                 else
27                         ASSERT(false);
28         }
29         next = model->scheduler->next_thread();
30         if (next)
31                 next->set_state(THREAD_RUNNING);
32         DEBUG("(%d, %d)\n", curr ? curr->get_id() : -1, next ? next->get_id() : -1);
33         if (!next)
34                 return 1;
35         return Thread::swap(model->get_system_context(), next);
36 }
37
38 static void thread_wait_finish(void) {
39         DBG();
40
41         while (!thread_system_next());
42 }
43
44 void real_main() {
45         thrd_t user_thread;
46         ucontext_t main_context;
47
48         //Create the singleton snapshotStack object
49         snapshotObject = new snapshotStack();
50   
51         model = new ModelChecker();
52   
53         if (getcontext(&main_context))
54                 return;
55   
56         model->set_system_context(&main_context);
57
58         do {
59                 /* Start user program */
60                 model->add_thread(new Thread(&user_thread, &user_main, NULL));
61     
62                 /* Wait for all threads to complete */
63                 thread_wait_finish();
64         } while (model->next_execution());
65   
66         delete model;
67   
68         DEBUG("Exiting\n");
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 /*int numbackingpages*/, 1024 /*unsigned int numsnapshots*/, 1024 /*unsigned int nummemoryregions*/ , 1000 /*int numheappages*/, &real_main /*MyFuncPtr entryPoint*/);
84 }