2 * @brief Model-checker core
5 #ifndef __EXECUTION_H__
6 #define __EXECUTION_H__
12 #include "hashtable.h"
13 #include "workqueue.h"
15 #include "modeltypes.h"
16 #include "stl-model.h"
20 #include <condition_variable>
22 /* Forward declaration */
29 struct model_snapshot_members;
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;
37 struct PendingFutureValue {
38 PendingFutureValue(ModelAction *writer, ModelAction *reader) :
39 writer(writer), reader(reader)
41 const ModelAction *writer;
45 /** @brief Records information regarding a single pending release sequence */
47 /** @brief The acquire operation */
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;
62 /** @brief The central structure for model-checking */
63 class ModelExecution {
65 ModelExecution(ModelChecker *m,
66 const struct model_params *params,
68 NodeStack *node_stack);
71 const struct model_params * get_params() const { return params; }
73 Thread * take_step(ModelAction *curr);
74 void fixup_release_sequences();
76 void print_summary() const;
77 #if SUPPORT_MOD_ORDER_DUMP
78 void dumpGraph(char *filename) const;
81 void add_thread(Thread *t);
82 Thread * get_thread(thread_id_t tid) const;
83 Thread * get_thread(const ModelAction *act) const;
85 pthread_t get_pthread_counter() { return pthread_counter; }
86 void incr_pthread_counter() { pthread_counter++; }
87 Thread * get_pthread(pthread_t pid);
89 bool is_enabled(Thread *t) const;
90 bool is_enabled(thread_id_t tid) const;
92 thread_id_t get_next_id();
93 unsigned int get_num_threads() const;
95 ClockVector * get_cv(thread_id_t tid) const;
96 ModelAction * get_parent_action(thread_id_t tid) const;
97 bool isfeasibleprefix() const;
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;
102 bool check_action_enabled(ModelAction *curr);
104 bool assert_bug(const char *msg);
105 bool have_bug_reports() const;
106 SnapVector<bug_message *> * get_bugs() const;
108 bool has_asserted() const;
110 bool is_complete_execution() const;
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;
119 ModelAction * get_next_backtrack();
121 action_list_t * get_action_trace() { return &action_trace; }
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;}
129 int get_execution_number() const;
130 pthread_t pthread_counter;
134 const model_params * const params;
136 /** The scheduler to use: tracks the running/ready Threads */
137 Scheduler * const scheduler;
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();
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);
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);
161 template <typename T>
162 bool check_recency(ModelAction *curr, const T *rf) const;
164 template <typename T, typename U>
165 bool should_read_instead(const ModelAction *curr, const T *rf, const U *other_rf) const;
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);
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);
182 template <typename rf_type>
183 bool r_modification_order(ModelAction *curr, const rf_type *rf);
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;
193 action_list_t action_trace;
194 SnapVector<Thread *> thread_map;
195 SnapVector<Thread *> pthread_map;
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;
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;
205 HashTable<void *, SnapVector<action_list_t> *, uintptr_t, 4> obj_thrd_map;
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;
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.
216 SnapVector<struct release_seq *> pending_rel_seqs;
218 SnapVector<ModelAction *> thrd_last_action;
219 SnapVector<ModelAction *> thrd_last_fence_release;
220 NodeStack * const node_stack;
222 /** A special model-checker Thread; used for associating with
223 * model-checker-related ModelAcitons */
224 Thread *model_thread;
226 /** Private data members that should be snapshotted. They are grouped
227 * together for efficiency and maintainability. */
228 struct model_snapshot_members * const priv;
231 * @brief The modification order graph
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.
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
244 CycleGraph * const mo_graph;
246 Thread * action_select_next_thread(const ModelAction *curr) const;
249 #endif /* __EXECUTION_H__ */