model: is_deadlocked() (sort of) had a bug
[c11tester.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         params->maxfuturevalues = 0;
24         params->expireslop = 10;
25 }
26
27 static void print_usage(struct model_params *params) {
28         /* Reset defaults before printing */
29         param_defaults(params);
30
31         printf(
32 "Usage: <program name> [MC_OPTIONS] -- [PROGRAM ARGUMENTS]\n"
33 "\n"
34 "Options:\n"
35 "-h                    Display this help message and exit\n"
36 "-m                    Maximum times a thread can read from the same write\n"
37 "                      while other writes exist. Default: %d\n"
38 "-M                    Maximum number of future values that can be sent to\n"
39 "                      the same read. Default: %d\n"
40 "-s                    Maximum actions that the model checker will wait for\n"
41 "                      a write from the future past the expected number of\n"
42 "                      actions. Default: %d\n"
43 "-S                    Future value expiration sloppiness. Default: %u\n"
44 "-f                    Specify a fairness window in which actions that are\n"
45 "                      enabled sufficiently many times should receive\n"
46 "                      priority for execution. Default: %d\n"
47 "-e                    Enabled count. Default: %d\n"
48 "-b                    Upper length bound. Default: %d\n"
49 "--                    Program arguments follow.\n\n",
50 params->maxreads, params->maxfuturevalues, params->maxfuturedelay, params->expireslop, params->fairwindow, params->enabledcount, params->bound);
51         exit(EXIT_SUCCESS);
52 }
53
54 static void parse_options(struct model_params *params, int *argc, char ***argv) {
55         const char *shortopts = "hm:M:s:S:f:e:b:";
56         int opt;
57         bool error = false;
58         while (!error && (opt = getopt(*argc, *argv, shortopts)) != -1) {
59                 switch (opt) {
60                 case 'h':
61                         print_usage(params);
62                         break;
63                 case 's':
64                         params->maxfuturedelay = atoi(optarg);
65                         break;
66                 case 'S':
67                         params->expireslop = atoi(optarg);
68                         break;
69                 case 'f':
70                         params->fairwindow = atoi(optarg);
71                         break;
72                 case 'e':
73                         params->enabledcount = atoi(optarg);
74                         break;
75                 case 'b':
76                         params->bound = atoi(optarg);
77                         break;
78                 case 'm':
79                         params->maxreads = atoi(optarg);
80                         break;
81                 case 'M':
82                         params->maxfuturevalues = atoi(optarg);
83                         break;
84                 default: /* '?' */
85                         error = true;
86                         break;
87                 }
88         }
89         (*argv)[optind - 1] = (*argv)[0];
90         (*argc) -= (optind - 1);
91         (*argv) += (optind - 1);
92         optind = 1;
93
94         if (error)
95                 print_usage(params);
96 }
97
98 int main_argc;
99 char **main_argv;
100
101 /** Wrapper to run the user's main function, with appropriate arguments */
102 void wrapper_user_main(void *)
103 {
104         user_main(main_argc, main_argv);
105 }
106
107 /** The model_main function contains the main model checking loop. */
108 static void model_main() {
109         thrd_t user_thread;
110         struct model_params params;
111
112         param_defaults(&params);
113
114         parse_options(&params, &main_argc, &main_argv);
115
116         //Initialize race detector
117         initRaceDetector();
118
119         //Create the singleton SnapshotStack object
120         snapshotObject = new SnapshotStack();
121
122         model = new ModelChecker(params);
123
124         snapshotObject->snapshotStep(0);
125         do {
126                 /* Start user program */
127                 model->add_thread(new Thread(&user_thread, &wrapper_user_main, NULL));
128
129                 /* Wait for all threads to complete */
130                 model->finish_execution();
131         } while (model->next_execution());
132
133         delete model;
134
135         DEBUG("Exiting\n");
136 }
137
138 /**
139  * Main function.  Just initializes snapshotting library and the
140  * snapshotting library calls the model_main function.
141  */
142 int main(int argc, char ** argv) {
143         main_argc = argc;
144         main_argv = argv;
145
146         /* Let's jump in quickly and start running stuff */
147         initSnapshotLibrary(10000, 1024, 1024, 4000, &model_main);
148 }