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