execution: convert HashTable to SnapVector
[c11tester.git] / execution.h
1 /** @file execution.h
2  *  @brief Model-checker core
3  */
4
5 #ifndef __EXECUTION_H__
6 #define __EXECUTION_H__
7
8 #include <cstddef>
9 #include <inttypes.h>
10
11 #include "mymemory.h"
12 #include "hashtable.h"
13 #include "workqueue.h"
14 #include "config.h"
15 #include "modeltypes.h"
16 #include "stl-model.h"
17 #include "params.h"
18
19 /* Forward declaration */
20 class Node;
21 class NodeStack;
22 class CycleGraph;
23 class Promise;
24 class Scheduler;
25 class Thread;
26 class ClockVector;
27 struct model_snapshot_members;
28 class ModelChecker;
29 struct bug_message;
30
31 /** @brief Shorthand for a list of release sequence heads */
32 typedef ModelVector<const ModelAction *> rel_heads_list_t;
33 typedef SnapList<ModelAction *> action_list_t;
34
35 struct PendingFutureValue {
36         PendingFutureValue(ModelAction *writer, ModelAction *reader) :
37                 writer(writer), reader(reader)
38         { }
39         const ModelAction *writer;
40         ModelAction *reader;
41 };
42
43 /** @brief Records information regarding a single pending release sequence */
44 struct release_seq {
45         /** @brief The acquire operation */
46         ModelAction *acquire;
47         /** @brief The read operation that may read from a release sequence;
48          *  may be the same as acquire, or else an earlier action in the same
49          *  thread (i.e., when 'acquire' is a fence-acquire) */
50         const ModelAction *read;
51         /** @brief The head of the RMW chain from which 'read' reads; may be
52          *  equal to 'release' */
53         const ModelAction *rf;
54         /** @brief The head of the potential longest release sequence chain */
55         const ModelAction *release;
56         /** @brief The write(s) that may break the release sequence */
57         SnapVector<const ModelAction *> writes;
58 };
59
60 /** @brief The central structure for model-checking */
61 class ModelExecution {
62 public:
63         ModelExecution(ModelChecker *m,
64                         struct model_params *params,
65                         Scheduler *scheduler,
66                         NodeStack *node_stack);
67         ~ModelExecution();
68
69         Thread * take_step(ModelAction *curr);
70         void fixup_release_sequences();
71
72         void print_summary() const;
73 #if SUPPORT_MOD_ORDER_DUMP
74         void dumpGraph(char *filename) const;
75 #endif
76
77         void add_thread(Thread *t);
78         Thread * get_thread(thread_id_t tid) const;
79         Thread * get_thread(const ModelAction *act) const;
80         int get_promise_number(const Promise *promise) const;
81
82         bool is_enabled(Thread *t) const;
83         bool is_enabled(thread_id_t tid) const;
84
85         thread_id_t get_next_id();
86         unsigned int get_num_threads() const;
87
88         ClockVector * get_cv(thread_id_t tid) const;
89         ModelAction * get_parent_action(thread_id_t tid) const;
90         void check_promises_thread_disabled();
91         bool isfeasibleprefix() const;
92
93         action_list_t * get_actions_on_obj(void * obj, thread_id_t tid) const;
94         ModelAction * get_last_action(thread_id_t tid) const;
95
96         bool check_action_enabled(ModelAction *curr);
97
98         bool assert_bug(const char *msg);
99         bool have_bug_reports() const;
100         SnapVector<bug_message *> * get_bugs() const;
101
102         bool has_asserted() const;
103         void set_assert();
104         bool is_complete_execution() const;
105
106         void print_infeasibility(const char *prefix) const;
107         bool is_feasible_prefix_ignore_relseq() const;
108         bool is_infeasible() const;
109         bool is_deadlocked() const;
110         bool too_many_steps() const;
111
112         ModelAction * get_next_backtrack();
113
114         action_list_t * get_action_trace() { return &action_trace; }
115
116         SNAPSHOTALLOC
117 private:
118         int get_execution_number() const;
119
120         ModelChecker *model;
121
122         const model_params * const params;
123
124         /** The scheduler to use: tracks the running/ready Threads */
125         Scheduler * const scheduler;
126
127         bool sleep_can_read_from(ModelAction *curr, const ModelAction *write);
128         bool thin_air_constraint_may_allow(const ModelAction *writer, const ModelAction *reader) const;
129         bool mo_may_allow(const ModelAction *writer, const ModelAction *reader);
130         bool promises_may_allow(const ModelAction *writer, const ModelAction *reader) const;
131         void set_bad_synchronization();
132         bool promises_expired() const;
133         bool should_wake_up(const ModelAction *curr, const Thread *thread) const;
134         void wake_up_sleeping_actions(ModelAction *curr);
135         modelclock_t get_next_seq_num();
136
137         bool next_execution();
138         ModelAction * check_current_action(ModelAction *curr);
139         bool initialize_curr_action(ModelAction **curr);
140         bool process_read(ModelAction *curr);
141         bool process_write(ModelAction *curr);
142         bool process_fence(ModelAction *curr);
143         bool process_mutex(ModelAction *curr);
144         bool process_thread_action(ModelAction *curr);
145         void process_relseq_fixup(ModelAction *curr, work_queue_t *work_queue);
146         bool read_from(ModelAction *act, const ModelAction *rf);
147         bool synchronize(const ModelAction *first, ModelAction *second);
148
149         template <typename T>
150         bool check_recency(ModelAction *curr, const T *rf) const;
151
152         template <typename T, typename U>
153         bool should_read_instead(const ModelAction *curr, const T *rf, const U *other_rf) const;
154
155         ModelAction * get_last_fence_conflict(ModelAction *act) const;
156         ModelAction * get_last_conflict(ModelAction *act) const;
157         void set_backtracking(ModelAction *act);
158         bool set_latest_backtrack(ModelAction *act);
159         Promise * pop_promise_to_resolve(const ModelAction *curr);
160         bool resolve_promise(ModelAction *curr, Promise *promise);
161         void compute_promises(ModelAction *curr);
162         void compute_relseq_breakwrites(ModelAction *curr);
163
164         void check_promises(thread_id_t tid, ClockVector *old_cv, ClockVector *merge_cv);
165         void mo_check_promises(const ModelAction *act, bool is_read_check);
166         void thread_blocking_check_promises(Thread *blocker, Thread *waiting);
167
168         void check_curr_backtracking(ModelAction *curr);
169         void add_action_to_lists(ModelAction *act);
170         ModelAction * get_last_fence_release(thread_id_t tid) const;
171         ModelAction * get_last_seq_cst_write(ModelAction *curr) const;
172         ModelAction * get_last_seq_cst_fence(thread_id_t tid, const ModelAction *before_fence) const;
173         ModelAction * get_last_unlock(ModelAction *curr) const;
174         void build_may_read_from(ModelAction *curr);
175         ModelAction * process_rmw(ModelAction *curr);
176
177         template <typename rf_type>
178         bool r_modification_order(ModelAction *curr, const rf_type *rf);
179
180         bool w_modification_order(ModelAction *curr, ModelVector<ModelAction *> *send_fv);
181         void get_release_seq_heads(ModelAction *acquire, ModelAction *read, rel_heads_list_t *release_heads);
182         bool release_seq_heads(const ModelAction *rf, rel_heads_list_t *release_heads, struct release_seq *pending) const;
183         bool resolve_release_sequences(void *location, work_queue_t *work_queue);
184         void add_future_value(const ModelAction *writer, ModelAction *reader);
185
186         ModelAction * get_uninitialized_action(const ModelAction *curr) const;
187
188         action_list_t action_trace;
189         SnapVector<Thread *> thread_map;
190
191         /** Per-object list of actions. Maps an object (i.e., memory location)
192          * to a trace of all actions performed on the object. */
193         HashTable<const void *, action_list_t *, uintptr_t, 4> * const obj_map;
194
195         /** Per-object list of actions. Maps an object (i.e., memory location)
196          * to a trace of all actions performed on the object. */
197         HashTable<const void *, action_list_t *, uintptr_t, 4> condvar_waiters_map;
198
199         HashTable<void *, SnapVector<action_list_t> *, uintptr_t, 4> obj_thrd_map;
200         SnapVector<Promise *> promises;
201         SnapVector<struct PendingFutureValue> futurevalues;
202
203         /**
204          * List of pending release sequences. Release sequences might be
205          * determined lazily as promises are fulfilled and modification orders
206          * are established. Each entry in the list may only be partially
207          * filled, depending on its pending status.
208          */
209         SnapVector<struct release_seq *> pending_rel_seqs;
210
211         SnapVector<ModelAction *> thrd_last_action;
212         SnapVector<ModelAction *> thrd_last_fence_release;
213         NodeStack * const node_stack;
214
215         /** A special model-checker Thread; used for associating with
216          *  model-checker-related ModelAcitons */
217         Thread *model_thread;
218
219         /** Private data members that should be snapshotted. They are grouped
220          * together for efficiency and maintainability. */
221         struct model_snapshot_members * const priv;
222
223         /**
224          * @brief The modification order graph
225          *
226          * A directed acyclic graph recording observations of the modification
227          * order on all the atomic objects in the system. This graph should
228          * never contain any cycles, as that represents a violation of the
229          * memory model (total ordering). This graph really consists of many
230          * disjoint (unconnected) subgraphs, each graph corresponding to a
231          * separate ordering on a distinct object.
232          *
233          * The edges in this graph represent the "ordered before" relation,
234          * such that <tt>a --> b</tt> means <tt>a</tt> was ordered before
235          * <tt>b</tt>.
236          */
237         CycleGraph * const mo_graph;
238
239         Thread * action_select_next_thread(const ModelAction *curr) const;
240 };
241
242 #endif /* __EXECUTION_H__ */