2 * @brief Core model checker.
15 #include "libthreads.h"
18 #include "clockvector.h"
19 #include "hashtable.h"
20 #include "workqueue.h"
22 /* Forward declaration */
27 /** @brief Shorthand for a list of release sequence heads */
28 typedef std::vector< const ModelAction *, MyAlloc<const ModelAction *> > rel_heads_list_t;
31 * Model checker parameter structure. Holds run-time configuration options for
37 unsigned int fairwindow;
38 unsigned int enabledcount;
41 struct PendingFutureValue {
43 modelclock_t expiration;
48 * Structure for holding small ModelChecker members that should be snapshotted
50 struct model_snapshot_members {
51 ModelAction *current_action;
53 modelclock_t used_sequence_numbers;
55 ModelAction *next_backtrack;
57 /** @see ModelChecker::lazy_sync_size */
58 unsigned int lazy_sync_size;
61 /** @brief The central structure for model-checking */
64 ModelChecker(struct model_params params);
67 /** @returns the context for the main model-checking system thread */
68 ucontext_t * get_system_context() { return &system_context; }
70 /** Prints an execution summary with trace information. */
73 void add_thread(Thread *t);
74 void remove_thread(Thread *t);
75 Thread * get_thread(thread_id_t tid) { return thread_map->get(id_to_int(tid)); }
76 Thread * get_thread(ModelAction *act) { return get_thread(act->get_tid()); }
78 thread_id_t get_next_id();
79 int get_num_threads();
80 modelclock_t get_next_seq_num();
82 /** @return The currently executing Thread. */
83 Thread * get_current_thread() { return scheduler->get_current_thread(); }
85 int switch_to_master(ModelAction *act);
86 ClockVector * get_cv(thread_id_t tid);
87 ModelAction * get_parent_action(thread_id_t tid);
88 bool next_execution();
90 bool isfeasibleotherthanRMW();
91 bool isfinalfeasible();
92 void check_promises(ClockVector *old_cv, ClockVector * merge_cv);
93 void get_release_seq_heads(ModelAction *act, rel_heads_list_t *release_heads);
94 void finish_execution();
95 bool isfeasibleprefix();
96 void set_assert() {asserted=true;}
97 const model_params params;
101 /** The scheduler to use: tracks the running/ready Threads */
102 Scheduler *scheduler;
104 bool thin_air_constraint_may_allow(const ModelAction * writer, const ModelAction *reader);
105 bool has_asserted() {return asserted;}
106 void reset_asserted() {asserted=false;}
108 int num_feasible_executions;
109 bool promises_expired();
112 * Stores the ModelAction for the current thread action. Call this
113 * immediately before switching from user- to system-context to pass
115 * @param act The ModelAction created by the user-thread action
117 void set_current_action(ModelAction *act) { priv->current_action = act; }
118 Thread * check_current_action(ModelAction *curr);
119 ModelAction * initialize_curr_action(ModelAction *curr);
120 bool process_read(ModelAction *curr, bool second_part_of_rmw);
121 bool process_write(ModelAction *curr);
122 void process_mutex(ModelAction *curr);
123 bool check_action_enabled(ModelAction *curr);
127 void check_recency(ModelAction *curr);
128 ModelAction * get_last_conflict(ModelAction *act);
129 void set_backtracking(ModelAction *act);
130 Thread * get_next_thread(ModelAction *curr);
131 ModelAction * get_next_backtrack();
132 void reset_to_initial_state();
133 bool resolve_promises(ModelAction *curr);
134 void compute_promises(ModelAction *curr);
136 void check_curr_backtracking(ModelAction * curr);
137 void add_action_to_lists(ModelAction *act);
138 ModelAction * get_last_action(thread_id_t tid) const;
139 ModelAction * get_last_seq_cst(ModelAction *curr) const;
140 ModelAction * get_last_unlock(ModelAction *curr) const;
141 void build_reads_from_past(ModelAction *curr);
142 ModelAction * process_rmw(ModelAction *curr);
143 void post_r_modification_order(ModelAction *curr, const ModelAction *rf);
144 bool r_modification_order(ModelAction *curr, const ModelAction *rf);
145 bool w_modification_order(ModelAction *curr);
146 bool release_seq_head(const ModelAction *rf, rel_heads_list_t *release_heads) const;
147 bool resolve_release_sequences(void *location, work_queue_t *work_queue);
148 void do_complete_join(ModelAction *join);
150 ModelAction *diverge;
152 ucontext_t system_context;
153 action_list_t *action_trace;
154 HashTable<int, Thread *, int> *thread_map;
156 /** Per-object list of actions. Maps an object (i.e., memory location)
157 * to a trace of all actions performed on the object. */
158 HashTable<const void *, action_list_t, uintptr_t, 4> *obj_map;
160 /** Per-object list of actions. Maps an object (i.e., memory location)
161 * to a trace of all actions performed on the object. */
162 HashTable<const void *, action_list_t, uintptr_t, 4> *lock_waiters_map;
164 HashTable<void *, std::vector<action_list_t>, uintptr_t, 4 > *obj_thrd_map;
165 std::vector<Promise *> *promises;
166 std::vector<struct PendingFutureValue> *futurevalues;
169 * Collection of lists of objects that might synchronize with one or
170 * more release sequence. Release sequences might be determined lazily
171 * as promises are fulfilled and modification orders are established.
172 * This structure maps its lists by object location. Each ModelAction
173 * in the lists should be an acquire operation.
175 HashTable<void *, action_list_t, uintptr_t, 4> *lazy_sync_with_release;
178 * Represents the total size of the
179 * ModelChecker::lazy_sync_with_release lists. This count should be
180 * snapshotted, so it is actually a pointer to a location within
183 unsigned int *lazy_sync_size;
185 std::vector<ModelAction *> *thrd_last_action;
186 NodeStack *node_stack;
188 /** Private data members that should be snapshotted. They are grouped
189 * together for efficiency and maintainability. */
190 struct model_snapshot_members *priv;
193 * @brief The modification order graph
195 * A directed acyclic graph recording observations of the modification
196 * order on all the atomic objects in the system. This graph should
197 * never contain any cycles, as that represents a violation of the
198 * memory model (total ordering). This graph really consists of many
199 * disjoint (unconnected) subgraphs, each graph corresponding to a
200 * separate ordering on a distinct object.
202 * The edges in this graph represent the "ordered before" relation,
203 * such that <tt>a --> b</tt> means <tt>a</tt> was ordered before
206 CycleGraph *mo_graph;
212 extern ModelChecker *model;
214 #endif /* __MODEL_H__ */