model: don't include schedule.h
[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 <list>
9 #include <vector>
10 #include <cstddef>
11 #include <ucontext.h>
12
13 #include "mymemory.h"
14 #include "libthreads.h"
15 #include "threads.h"
16 #include "action.h"
17 #include "clockvector.h"
18 #include "hashtable.h"
19 #include "workqueue.h"
20 #include "config.h"
21
22 /* Forward declaration */
23 class NodeStack;
24 class CycleGraph;
25 class Promise;
26 class Scheduler;
27
28 /** @brief Shorthand for a list of release sequence heads */
29 typedef std::vector< const ModelAction *, ModelAlloc<const ModelAction *> > rel_heads_list_t;
30
31 /**
32  * Model checker parameter structure. Holds run-time configuration options for
33  * the model checker.
34  */
35 struct model_params {
36         int maxreads;
37         int maxfuturedelay;
38         unsigned int fairwindow;
39         unsigned int enabledcount;
40 };
41
42 struct PendingFutureValue {
43         uint64_t value;
44         modelclock_t expiration;
45         ModelAction * act;
46 };
47
48 /**
49  * Structure for holding small ModelChecker members that should be snapshotted
50  */
51 struct model_snapshot_members {
52         ModelAction *current_action;
53         int next_thread_id;
54         modelclock_t used_sequence_numbers;
55         Thread *nextThread;
56         ModelAction *next_backtrack;
57 };
58
59 /** @brief The central structure for model-checking */
60 class ModelChecker {
61 public:
62         ModelChecker(struct model_params params);
63         ~ModelChecker();
64
65         /** @returns the context for the main model-checking system thread */
66         ucontext_t * get_system_context() { return &system_context; }
67
68         /** Prints an execution summary with trace information. */
69         void print_summary();
70 #if SUPPORT_MOD_ORDER_DUMP
71         void dumpGraph(char *filename);
72 #endif
73
74         void add_thread(Thread *t);
75         void remove_thread(Thread *t);
76         Thread * get_thread(thread_id_t tid);
77         Thread * get_thread(ModelAction *act);
78
79         thread_id_t get_next_id();
80         int get_num_threads();
81         Thread * get_current_thread();
82
83         int switch_to_master(ModelAction *act);
84         ClockVector * get_cv(thread_id_t tid);
85         ModelAction * get_parent_action(thread_id_t tid);
86         bool next_execution();
87         bool isfeasible();
88         bool isfeasibleotherthanRMW();
89         bool isfinalfeasible();
90         void check_promises(ClockVector *old_cv, ClockVector * merge_cv);
91         void get_release_seq_heads(ModelAction *act, rel_heads_list_t *release_heads);
92         void finish_execution();
93         bool isfeasibleprefix();
94         void set_assert() {asserted=true;}
95
96         /** @brief Alert the model-checker that an incorrectly-ordered
97          * synchronization was made */
98         void set_bad_synchronization() { bad_synchronization = true; }
99
100         const model_params params;
101
102         MEMALLOC
103 private:
104         /** The scheduler to use: tracks the running/ready Threads */
105         Scheduler *scheduler;
106
107         bool thin_air_constraint_may_allow(const ModelAction * writer, const ModelAction *reader);
108         bool has_asserted() {return asserted;}
109         void reset_asserted() {asserted=false;}
110         int num_executions;
111         int num_feasible_executions;
112         bool promises_expired();
113
114         modelclock_t get_next_seq_num();
115
116         /**
117          * Stores the ModelAction for the current thread action.  Call this
118          * immediately before switching from user- to system-context to pass
119          * data between them.
120          * @param act The ModelAction created by the user-thread action
121          */
122         void set_current_action(ModelAction *act) { priv->current_action = act; }
123         Thread * check_current_action(ModelAction *curr);
124         ModelAction * initialize_curr_action(ModelAction *curr);
125         bool process_read(ModelAction *curr, bool second_part_of_rmw);
126         bool process_write(ModelAction *curr);
127         bool process_mutex(ModelAction *curr);
128         bool process_thread_action(ModelAction *curr);
129         bool check_action_enabled(ModelAction *curr);
130
131         bool take_step();
132
133         void check_recency(ModelAction *curr, const ModelAction *rf);
134         ModelAction * get_last_conflict(ModelAction *act);
135         void set_backtracking(ModelAction *act);
136         Thread * get_next_thread(ModelAction *curr);
137         ModelAction * get_next_backtrack();
138         void reset_to_initial_state();
139         bool resolve_promises(ModelAction *curr);
140         void compute_promises(ModelAction *curr);
141
142         void check_curr_backtracking(ModelAction * curr);
143         void add_action_to_lists(ModelAction *act);
144         ModelAction * get_last_action(thread_id_t tid) const;
145         ModelAction * get_last_seq_cst(ModelAction *curr) const;
146         ModelAction * get_last_unlock(ModelAction *curr) const;
147         void build_reads_from_past(ModelAction *curr);
148         ModelAction * process_rmw(ModelAction *curr);
149         void post_r_modification_order(ModelAction *curr, const ModelAction *rf);
150         bool r_modification_order(ModelAction *curr, const ModelAction *rf);
151         bool w_modification_order(ModelAction *curr);
152         bool release_seq_head(const ModelAction *rf, rel_heads_list_t *release_heads) const;
153         bool resolve_release_sequences(void *location, work_queue_t *work_queue);
154         void do_complete_join(ModelAction *join);
155
156         ModelAction *diverge;
157         ModelAction *earliest_diverge;
158
159         ucontext_t system_context;
160         action_list_t *action_trace;
161         HashTable<int, Thread *, int> *thread_map;
162
163         /** Per-object list of actions. Maps an object (i.e., memory location)
164          * to a trace of all actions performed on the object. */
165         HashTable<const void *, action_list_t, uintptr_t, 4> *obj_map;
166
167         /** Per-object list of actions. Maps an object (i.e., memory location)
168          * to a trace of all actions performed on the object. */
169         HashTable<const void *, action_list_t, uintptr_t, 4> *lock_waiters_map;
170
171         HashTable<void *, std::vector<action_list_t>, uintptr_t, 4 > *obj_thrd_map;
172         std::vector<Promise *> *promises;
173         std::vector<struct PendingFutureValue> *futurevalues;
174
175         /**
176          * List of acquire actions that might synchronize with one or more
177          * release sequence. Release sequences might be determined lazily as
178          * promises are fulfilled and modification orders are established. Each
179          * ModelAction in this list must be an acquire operation.
180          */
181         std::vector<ModelAction *> *pending_acq_rel_seq;
182
183         std::vector<ModelAction *> *thrd_last_action;
184         NodeStack *node_stack;
185
186         /** Private data members that should be snapshotted. They are grouped
187          * together for efficiency and maintainability. */
188         struct model_snapshot_members *priv;
189
190         /**
191          * @brief The modification order graph
192          *
193          * A directed acyclic graph recording observations of the modification
194          * order on all the atomic objects in the system. This graph should
195          * never contain any cycles, as that represents a violation of the
196          * memory model (total ordering). This graph really consists of many
197          * disjoint (unconnected) subgraphs, each graph corresponding to a
198          * separate ordering on a distinct object.
199          *
200          * The edges in this graph represent the "ordered before" relation,
201          * such that <tt>a --> b</tt> means <tt>a</tt> was ordered before
202          * <tt>b</tt>.
203          */
204         CycleGraph *mo_graph;
205         bool failed_promise;
206         bool too_many_reads;
207         bool asserted;
208         /** @brief Incorrectly-ordered synchronization was made */
209         bool bad_synchronization;
210 };
211
212 extern ModelChecker *model;
213
214 #endif /* __MODEL_H__ */