model: flatten "pending acquire/release sequence" structure
[c11tester.git] / model.h
1 /** @file model.h
2  *  @brief Core model checker.
3  */
4
5 #ifndef __MODEL_H__
6 #define __MODEL_H__
7
8 #include <list>
9 #include <vector>
10 #include <cstddef>
11 #include <ucontext.h>
12
13 #include "schedule.h"
14 #include "mymemory.h"
15 #include "libthreads.h"
16 #include "threads.h"
17 #include "action.h"
18 #include "clockvector.h"
19 #include "hashtable.h"
20 #include "workqueue.h"
21
22 /* Forward declaration */
23 class NodeStack;
24 class CycleGraph;
25 class Promise;
26
27 /** @brief Shorthand for a list of release sequence heads */
28 typedef std::vector< const ModelAction *, MyAlloc<const ModelAction *> > rel_heads_list_t;
29
30 /**
31  * Model checker parameter structure. Holds run-time configuration options for
32  * the model checker.
33  */
34 struct model_params {
35         int maxreads;
36         int maxfuturedelay;
37 };
38
39 struct PendingFutureValue {
40         uint64_t value;
41         modelclock_t expiration;
42         ModelAction * act;
43 };
44
45 /**
46  * Structure for holding small ModelChecker members that should be snapshotted
47  */
48 struct model_snapshot_members {
49         ModelAction *current_action;
50         int next_thread_id;
51         modelclock_t used_sequence_numbers;
52         Thread *nextThread;
53         ModelAction *next_backtrack;
54 };
55
56 /** @brief The central structure for model-checking */
57 class ModelChecker {
58 public:
59         ModelChecker(struct model_params params);
60         ~ModelChecker();
61
62         /** @returns the context for the main model-checking system thread */
63         ucontext_t * get_system_context() { return &system_context; }
64
65         /** Prints an execution summary with trace information. */
66         void print_summary();
67
68         void add_thread(Thread *t);
69         void remove_thread(Thread *t);
70         Thread * get_thread(thread_id_t tid) { return thread_map->get(id_to_int(tid)); }
71         Thread * get_thread(ModelAction *act) { return get_thread(act->get_tid()); }
72
73         thread_id_t get_next_id();
74         int get_num_threads();
75         modelclock_t get_next_seq_num();
76
77         /** @return The currently executing Thread. */
78         Thread * get_current_thread() { return scheduler->get_current_thread(); }
79
80         int switch_to_master(ModelAction *act);
81         ClockVector * get_cv(thread_id_t tid);
82         ModelAction * get_parent_action(thread_id_t tid);
83         bool next_execution();
84         bool isfeasible();
85         bool isfeasibleotherthanRMW();
86         bool isfinalfeasible();
87         void check_promises(ClockVector *old_cv, ClockVector * merge_cv);
88         void get_release_seq_heads(ModelAction *act, rel_heads_list_t *release_heads);
89         void finish_execution();
90         bool isfeasibleprefix();
91         void set_assert() {asserted=true;}
92
93         MEMALLOC
94 private:
95         /** The scheduler to use: tracks the running/ready Threads */
96         Scheduler *scheduler;
97
98         bool thin_air_constraint_may_allow(const ModelAction * writer, const ModelAction *reader);
99         bool has_asserted() {return asserted;}
100         void reset_asserted() {asserted=false;}
101         int num_executions;
102         int num_feasible_executions;
103         bool promises_expired();
104         const model_params params;
105
106         /**
107          * Stores the ModelAction for the current thread action.  Call this
108          * immediately before switching from user- to system-context to pass
109          * data between them.
110          * @param act The ModelAction created by the user-thread action
111          */
112         void set_current_action(ModelAction *act) { priv->current_action = act; }
113         Thread * check_current_action(ModelAction *curr);
114         ModelAction * initialize_curr_action(ModelAction *curr);
115         bool process_read(ModelAction *curr, bool second_part_of_rmw);
116         bool process_write(ModelAction *curr);
117         void process_mutex(ModelAction *curr);
118         bool check_action_enabled(ModelAction *curr);
119
120         bool take_step();
121
122         void check_recency(ModelAction *curr);
123         ModelAction * get_last_conflict(ModelAction *act);
124         void set_backtracking(ModelAction *act);
125         Thread * get_next_thread(ModelAction *curr);
126         ModelAction * get_next_backtrack();
127         void reset_to_initial_state();
128         bool resolve_promises(ModelAction *curr);
129         void compute_promises(ModelAction *curr);
130
131         void check_curr_backtracking(ModelAction * curr);
132         void add_action_to_lists(ModelAction *act);
133         ModelAction * get_last_action(thread_id_t tid) const;
134         ModelAction * get_last_seq_cst(ModelAction *curr) const;
135         ModelAction * get_last_unlock(ModelAction *curr) const;
136         void build_reads_from_past(ModelAction *curr);
137         ModelAction * process_rmw(ModelAction *curr);
138         void post_r_modification_order(ModelAction *curr, const ModelAction *rf);
139         bool r_modification_order(ModelAction *curr, const ModelAction *rf);
140         bool w_modification_order(ModelAction *curr);
141         bool release_seq_head(const ModelAction *rf, rel_heads_list_t *release_heads) const;
142         bool resolve_release_sequences(void *location, work_queue_t *work_queue);
143         void do_complete_join(ModelAction *join);
144
145         ModelAction *diverge;
146
147         ucontext_t system_context;
148         action_list_t *action_trace;
149         HashTable<int, Thread *, int> *thread_map;
150
151         /** Per-object list of actions. Maps an object (i.e., memory location)
152          * to a trace of all actions performed on the object. */
153         HashTable<const void *, action_list_t, uintptr_t, 4> *obj_map;
154
155         /** Per-object list of actions. Maps an object (i.e., memory location)
156          * to a trace of all actions performed on the object. */
157         HashTable<const void *, action_list_t, uintptr_t, 4> *lock_waiters_map;
158
159         HashTable<void *, std::vector<action_list_t>, uintptr_t, 4 > *obj_thrd_map;
160         std::vector<Promise *> *promises;
161         std::vector<struct PendingFutureValue> *futurevalues;
162
163         /**
164          * List of acquire actions that might synchronize with one or more
165          * release sequence. Release sequences might be determined lazily as
166          * promises are fulfilled and modification orders are established. Each
167          * ModelAction in this list must be an acquire operation.
168          */
169         std::vector<ModelAction *> *pending_acq_rel_seq;
170
171         std::vector<ModelAction *> *thrd_last_action;
172         NodeStack *node_stack;
173
174         /** Private data members that should be snapshotted. They are grouped
175          * together for efficiency and maintainability. */
176         struct model_snapshot_members *priv;
177
178         /**
179          * @brief The modification order graph
180          *
181          * A directed acyclic graph recording observations of the modification
182          * order on all the atomic objects in the system. This graph should
183          * never contain any cycles, as that represents a violation of the
184          * memory model (total ordering). This graph really consists of many
185          * disjoint (unconnected) subgraphs, each graph corresponding to a
186          * separate ordering on a distinct object.
187          *
188          * The edges in this graph represent the "ordered before" relation,
189          * such that <tt>a --> b</tt> means <tt>a</tt> was ordered before
190          * <tt>b</tt>.
191          */
192         CycleGraph *mo_graph;
193         bool failed_promise;
194         bool too_many_reads;
195         bool asserted;
196 };
197
198 extern ModelChecker *model;
199
200 #endif /* __MODEL_H__ */