model: silence "uninitialized" warning
[model-checker.git] / main.cc
1 /** @file main.cc
2  *  @brief Entry point for the model checker.
3  */
4
5 #include <unistd.h>
6
7 #include <threads.h>
8 #include "common.h"
9 #include "threads-model.h"
10
11 #include "datarace.h"
12
13 /* global "model" object */
14 #include "model.h"
15 #include "snapshot-interface.h"
16
17 static void param_defaults(struct model_params * params) {
18         params->maxreads = 0;
19         params->maxfuturedelay = 100;
20         params->fairwindow = 0;
21         params->enabledcount = 1;
22         params->bound = 0;
23 }
24
25 static void print_usage(struct model_params *params) {
26         printf(
27 "Usage: <program name> [MC_OPTIONS] -- [PROGRAM ARGUMENTS]\n"
28 "\n"
29 "Options:\n"
30 "-h                    Display this help message and exit\n"
31 "-m                    Maximum times a thread can read from the same write\n"
32 "                      while other writes exist. Default: %d\n"
33 "-s                    Maximum actions that the model checker will wait for\n"
34 "                      a write from the future past the expected number of\n"
35 "                      actions. Default: %d\n"
36 "-f                    Specify a fairness window in which actions that are\n"
37 "                      enabled sufficiently many times should receive\n"
38 "                      priority for execution. Default: %d\n"
39 "-e                    Enabled count. Default: %d\n"
40 "-b                    Upper length bound. Default: %d\n"
41 "--                    Program arguments follow.\n\n",
42 params->maxreads, params->maxfuturedelay, params->fairwindow, params->enabledcount, params->bound);
43         exit(EXIT_SUCCESS);
44 }
45
46 static void parse_options(struct model_params *params, int *argc, char ***argv) {
47         const char *shortopts = "hm:s:f:e:b:";
48         int opt;
49         bool error = false;
50         while (!error && (opt = getopt(*argc, *argv, shortopts)) != -1) {
51                 switch (opt) {
52                 case 'h':
53                         print_usage(params);
54                         break;
55                 case 's':
56                         params->maxfuturedelay = atoi(optarg);
57                         break;
58                 case 'f':
59                         params->fairwindow = atoi(optarg);
60                         break;
61                 case 'e':
62                         params->enabledcount = atoi(optarg);
63                         break;
64                 case 'b':
65                         params->bound = atoi(optarg);
66                         break;
67                 case 'm':
68                         params->maxreads = atoi(optarg);
69                         break;
70                 default: /* '?' */
71                         error = true;
72                         break;
73                 }
74         }
75         (*argc) -= optind;
76         (*argv) += optind;
77
78         if (error)
79                 print_usage(params);
80 }
81
82 int main_argc;
83 char **main_argv;
84
85 /** Wrapper to run the user's main function, with appropriate arguments */
86 void wrapper_user_main(void *)
87 {
88         user_main(main_argc, main_argv);
89 }
90
91 /** The model_main function contains the main model checking loop. */
92 static void model_main() {
93         thrd_t user_thread;
94         struct model_params params;
95
96         param_defaults(&params);
97
98         parse_options(&params, &main_argc, &main_argv);
99
100         //Initialize race detector
101         initRaceDetector();
102
103         //Create the singleton SnapshotStack object
104         snapshotObject = new SnapshotStack();
105
106         model = new ModelChecker(params);
107
108         snapshotObject->snapshotStep(0);
109         do {
110                 /* Start user program */
111                 model->add_thread(new Thread(&user_thread, &wrapper_user_main, NULL));
112
113                 /* Wait for all threads to complete */
114                 model->finish_execution();
115         } while (model->next_execution());
116
117         delete model;
118
119         DEBUG("Exiting\n");
120 }
121
122 /**
123  * Main function.  Just initializes snapshotting library and the
124  * snapshotting library calls the model_main function.
125  */
126 int main(int argc, char ** argv) {
127         main_argc = argc;
128         main_argv = argv;
129
130         /* Let's jump in quickly and start running stuff */
131         initSnapshotLibrary(10000, 1024, 1024, 4000, &model_main);
132 }