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