Bug fix for spsc-queue test
[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 typedef SnapList<ModelAction *> action_list_t;
22
23 /** @brief Model checker execution stats */
24 struct execution_stats {
25         int num_total;  /**< @brief Total number of executions */
26         int num_buggy_executions;       /** @brief Number of buggy executions */
27         int num_complete;       /**< @brief Number of feasible, non-buggy, complete executions */
28 };
29
30 /** @brief The central structure for model-checking */
31 class ModelChecker {
32 public:
33         ModelChecker();
34         ~ModelChecker();
35         model_params * getParams();
36         void run();
37
38         /** Exit the model checker, intended for pluggins. */
39         void exit_model_checker();
40
41         /** @returns the context for the main model-checking system thread */
42         ucontext_t * get_system_context() { return &system_context; }
43
44         ModelExecution * get_execution() const { return execution; }
45         ModelHistory * get_history() const { return history; }
46
47         int get_execution_number() const { return execution_number; }
48
49         Thread * get_thread(thread_id_t tid) const;
50         Thread * get_thread(const ModelAction *act) const;
51
52         Thread * get_current_thread() const;
53
54         void switch_from_master(Thread *thread);
55         uint64_t switch_to_master(ModelAction *act);
56         uint64_t switch_thread(ModelAction *act);
57
58         void continueRunExecution(Thread *old);
59         void startRunExecution(ucontext_t *old);
60         void finishRunExecution(Thread *old);
61         void finishRunExecution(ucontext_t *old);
62         void consumeAction();
63         void chooseThread(ModelAction *act, Thread *thr);
64         Thread * getNextThread();
65         void handleChosenThread(Thread *old);
66         void handleChosenThread(ucontext_t *old);
67         void handleNewValidThread(Thread *old, Thread *next);
68
69         void assert_bug(const char *msg, ...);
70
71         void assert_user_bug(const char *msg);
72
73         model_params params;
74         void add_trace_analysis(TraceAnalysis *a) {     trace_analyses.push_back(a); }
75         void set_inspect_plugin(TraceAnalysis *a) {     inspect_plugin=a;       }
76         void startChecker();
77         Thread * getInitThread() {return init_thread;}
78         Scheduler * getScheduler() {return scheduler;}
79         MEMALLOC
80 private:
81         /** Snapshot id we return to restart. */
82         snapshot_id snapshot;
83
84         /** The scheduler to use: tracks the running/ready Threads */
85         Scheduler * const scheduler;
86         ModelHistory * history;
87         ModelExecution *execution;
88         Thread * init_thread;
89
90         int execution_number;
91
92         unsigned int curr_thread_num;
93
94         Thread * chosen_thread;
95
96         bool thread_chosen;
97         bool break_execution;
98
99         modelclock_t checkfree;
100
101         unsigned int get_num_threads() const;
102
103         void finish_execution(bool moreexecutions);
104         bool should_terminate_execution();
105
106         Thread * get_next_thread();
107         void reset_to_initial_state();
108
109         ucontext_t system_context;
110
111         ModelVector<TraceAnalysis *> trace_analyses;
112
113         /** @bref Plugin that can inspect new actions. */
114         TraceAnalysis *inspect_plugin;
115         /** @brief The cumulative execution stats */
116         struct execution_stats stats;
117         void record_stats();
118         void run_trace_analyses();
119         void print_bugs() const;
120         void print_execution(bool printbugs) const;
121         void print_stats() const;
122 };
123
124 extern ModelChecker *model;
125 void parse_options(struct model_params *params);
126 void install_trace_analyses(ModelExecution *execution);
127
128 #endif  /* __MODEL_H__ */