add support for pthread_mutex
[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
19 #include <map>
20 #include <mutex>
21
22 /* Forward declaration */
23 class Node;
24 class NodeStack;
25 class CycleGraph;
26 class Promise;
27 class Scheduler;
28 class Thread;
29 class ClockVector;
30 class TraceAnalysis;
31 class ModelExecution;
32 class ModelAction;
33
34 typedef SnapList<ModelAction *> action_list_t;
35
36 /** @brief Model checker execution stats */
37 struct execution_stats {
38         int num_total; /**< @brief Total number of executions */
39         int num_infeasible; /**< @brief Number of infeasible executions */
40         int num_buggy_executions; /** @brief Number of buggy executions */
41         int num_complete; /**< @brief Number of feasible, non-buggy, complete executions */
42         int num_redundant; /**< @brief Number of redundant, aborted executions */
43 };
44
45 /** @brief The central structure for model-checking */
46 class ModelChecker {
47 public:
48         ModelChecker(struct model_params params);
49         ~ModelChecker();
50
51         void run();
52
53         /** Restart the model checker, intended for pluggins. */
54         void restart();
55
56         /** Exit the model checker, intended for pluggins. */
57         void exit_model_checker();
58
59         /** Check the exit_flag. */
60         bool get_exit_flag() const { return exit_flag; }
61
62         /** @returns the context for the main model-checking system thread */
63         ucontext_t * get_system_context() { return &system_context; }
64
65         ModelExecution * get_execution() const { return execution; }
66
67         int get_execution_number() const { return execution_number; }
68
69         Thread * get_thread(thread_id_t tid) const;
70         Thread * get_thread(const ModelAction *act) const;
71
72         Thread * get_current_thread() const;
73
74         void switch_from_master(Thread *thread);
75         uint64_t switch_to_master(ModelAction *act);
76
77         bool assert_bug(const char *msg, ...);
78         void assert_user_bug(const char *msg);
79
80         const model_params params;
81         void add_trace_analysis(TraceAnalysis *a) {     trace_analyses.push_back(a); }
82         void set_inspect_plugin(TraceAnalysis *a) {     inspect_plugin=a;       }
83         MEMALLOC
84         std::map<pthread_t, ModelAction*> pthread_map;
85         std::map<pthread_mutex_t *, std::mutex*> mutex_map;
86 private:
87         /** Flag indicates whether to restart the model checker. */
88         bool restart_flag;
89         /** Flag indicates whether to exit the model checker. */
90         bool exit_flag;
91
92         /** The scheduler to use: tracks the running/ready Threads */
93         Scheduler * const scheduler;
94         NodeStack * const node_stack;
95         ModelExecution *execution;
96
97         int execution_number;
98
99         unsigned int get_num_threads() const;
100
101         void execute_sleep_set();
102
103         bool next_execution();
104         bool should_terminate_execution();
105
106         Thread * get_next_thread();
107         void reset_to_initial_state();
108
109
110         ModelAction *diverge;
111         ModelAction *earliest_diverge;
112
113         ucontext_t system_context;
114
115         ModelVector<TraceAnalysis *> trace_analyses;
116
117         /** @bref Implement restart. */
118         void do_restart();
119         /** @bref Plugin that can inspect new actions. */
120         TraceAnalysis *inspect_plugin;
121         /** @brief The cumulative execution stats */
122         struct execution_stats stats;
123         void record_stats();
124         void run_trace_analyses();
125         void print_bugs() const;
126         void print_execution(bool printbugs) const;
127         void print_stats() const;
128
129         friend void user_main_wrapper();
130 };
131
132 extern ModelChecker *model;
133
134 #endif /* __MODEL_H__ */