Add back model_thread; it is still needed
[c11tester.git] / model.h
1 /** @file model.h
2  *  @brief Core model checker.
3  */
4
5 #ifndef __MODEL_H__
6 #define __MODEL_H__
7
8 #include <cstddef>
9 #include <inttypes.h>
10
11 #include "mymemory.h"
12 #include "hashtable.h"
13 #include "config.h"
14 #include "modeltypes.h"
15 #include "stl-model.h"
16 #include "context.h"
17 #include "params.h"
18 #include "classlist.h"
19 #include "snapshot-interface.h"
20
21 #define ENTER_MODEL_FLAG (inside_model = 1)
22 #define EXIT_MODEL_FLAG (inside_model = 0)
23 #define GET_MODEL_FLAG (inside_model)
24 #define RESTORE_MODEL_FLAG(f) (inside_model = f)
25
26 /** @brief Model checker execution stats */
27 struct execution_stats {
28         int num_total;  /**< @brief Total number of executions */
29         int num_buggy_executions;       /** @brief Number of buggy executions */
30         int num_complete;       /**< @brief Number of feasible, non-buggy, complete executions */
31 };
32
33 /** @brief The central structure for model-checking */
34 class ModelChecker {
35 public:
36         ModelChecker();
37         ~ModelChecker();
38         model_params * getParams();
39
40         /** Exit the model checker, intended for pluggins. */
41         void exit_model_checker();
42
43         ModelExecution * get_execution() const { return execution; }
44         ModelHistory * get_history() const { return history; }
45
46         int get_execution_number() const { return execution_number; }
47
48         Thread * get_thread(thread_id_t tid) const;
49         Thread * get_thread(const ModelAction *act) const;
50
51         Thread * get_current_thread() const;
52         thread_id_t get_current_thread_id() const;
53
54         uint64_t switch_thread(ModelAction *act);
55
56         void assert_bug(const char *msg, ...);
57
58         void assert_user_bug(const char *msg);
59
60         model_params params;
61         void add_trace_analysis(TraceAnalysis *a) {     trace_analyses.push_back(a); }
62         void set_inspect_plugin(TraceAnalysis *a) {     inspect_plugin=a;       }
63         void startChecker();
64         Thread * getInitThread() {return init_thread;}
65         Scheduler * getScheduler() {return scheduler;}
66         MEMALLOC
67 private:
68         /** Snapshot id we return to restart. */
69         snapshot_id snapshot;
70
71         /** The scheduler to use: tracks the running/ready Threads */
72         Scheduler * const scheduler;
73         ModelHistory * history;
74         ModelExecution *execution;
75         Thread * init_thread;
76
77         int execution_number;
78
79         unsigned int curr_thread_num;
80         Thread * chosen_thread;
81         bool break_execution;
82
83         void startRunExecution(Thread *old);
84         void finishRunExecution(Thread *old);
85         Thread * getNextThread(Thread *old);
86         bool handleChosenThread(Thread *old);
87
88         modelclock_t checkfree;
89
90         unsigned int get_num_threads() const;
91
92         void finish_execution(bool moreexecutions);
93         bool should_terminate_execution();
94
95         Thread * get_next_thread();
96         void reset_to_initial_state();
97
98         ModelVector<TraceAnalysis *> trace_analyses;
99         char random_state[256];
100
101         /** @bref Plugin that can inspect new actions. */
102         TraceAnalysis *inspect_plugin;
103         /** @brief The cumulative execution stats */
104         struct execution_stats stats;
105         void record_stats();
106         void run_trace_analyses();
107         void print_bugs() const;
108         void print_execution(bool printbugs) const;
109         void print_stats() const;
110 };
111
112 extern int inside_model;
113 extern ModelChecker *model;
114 void parse_options(struct model_params *params);
115 void install_trace_analyses(ModelExecution *execution);
116 void createModelIfNotExist();
117
118 #endif  /* __MODEL_H__ */