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