model: document behavior of ModelChecker::initialize_curr_action
[model-checker.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 /**
28  * Model checker parameter structure. Holds run-time configuration options for
29  * the model checker.
30  */
31 struct model_params {
32         int maxreads;
33         int maxfuturedelay;
34 };
35
36 struct PendingFutureValue {
37         uint64_t value;
38         modelclock_t expiration;
39         ModelAction * act;
40 };
41
42 /**
43  * Structure for holding small ModelChecker members that should be snapshotted
44  */
45 struct model_snapshot_members {
46         ModelAction *current_action;
47         int next_thread_id;
48         modelclock_t used_sequence_numbers;
49         Thread *nextThread;
50         ModelAction *next_backtrack;
51
52         /** @see ModelChecker::lazy_sync_size */
53         unsigned int lazy_sync_size;
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,
89                         std::vector< const ModelAction *, MyAlloc<const ModelAction *> > *release_heads);
90         void finish_execution();
91         bool isfeasibleprefix();
92         void set_assert() {asserted=true;}
93
94         MEMALLOC
95 private:
96         /** The scheduler to use: tracks the running/ready Threads */
97         Scheduler *scheduler;
98
99         bool thin_air_constraint_may_allow(const ModelAction * writer, const ModelAction *reader);
100         bool has_asserted() {return asserted;}
101         void reset_asserted() {asserted=false;}
102         int num_executions;
103         int num_feasible_executions;
104         bool promises_expired();
105         const model_params params;
106
107         /**
108          * Stores the ModelAction for the current thread action.  Call this
109          * immediately before switching from user- to system-context to pass
110          * data between them.
111          * @param act The ModelAction created by the user-thread action
112          */
113         void set_current_action(ModelAction *act) { priv->current_action = act; }
114         Thread * check_current_action(ModelAction *curr);
115         ModelAction * initialize_curr_action(ModelAction *curr);
116         bool process_read(ModelAction *curr, bool second_part_of_rmw);
117         bool process_write(ModelAction *curr);
118
119         bool take_step();
120
121         void check_recency(ModelAction *curr);
122         ModelAction * get_last_conflict(ModelAction *act);
123         void set_backtracking(ModelAction *act);
124         Thread * get_next_thread(ModelAction *curr);
125         ModelAction * get_next_backtrack();
126         void reset_to_initial_state();
127         bool resolve_promises(ModelAction *curr);
128         void compute_promises(ModelAction *curr);
129
130         void check_curr_backtracking(ModelAction * curr);
131         void add_action_to_lists(ModelAction *act);
132         ModelAction * get_last_action(thread_id_t tid);
133         ModelAction * get_last_seq_cst(ModelAction *curr);
134         void build_reads_from_past(ModelAction *curr);
135         ModelAction * process_rmw(ModelAction *curr);
136         void post_r_modification_order(ModelAction *curr, const ModelAction *rf);
137         bool r_modification_order(ModelAction *curr, const ModelAction *rf);
138         bool w_modification_order(ModelAction *curr);
139         bool release_seq_head(const ModelAction *rf,
140                         std::vector< const ModelAction *, MyAlloc<const ModelAction *> > *release_heads) const;
141         bool resolve_release_sequences(void *location, work_queue_t *work_queue);
142
143         ModelAction *diverge;
144
145         ucontext_t system_context;
146         action_list_t *action_trace;
147         HashTable<int, Thread *, int> *thread_map;
148
149         /** Per-object list of actions. Maps an object (i.e., memory location)
150          * to a trace of all actions performed on the object. */
151         HashTable<const void *, action_list_t, uintptr_t, 4> *obj_map;
152
153         HashTable<void *, std::vector<action_list_t>, uintptr_t, 4 > *obj_thrd_map;
154         std::vector<Promise *> *promises;
155         std::vector<struct PendingFutureValue> *futurevalues;
156
157         /**
158          * Collection of lists of objects that might synchronize with one or
159          * more release sequence. Release sequences might be determined lazily
160          * as promises are fulfilled and modification orders are established.
161          * This structure maps its lists by object location. Each ModelAction
162          * in the lists should be an acquire operation.
163          */
164         HashTable<void *, std::list<ModelAction *>, uintptr_t, 4> *lazy_sync_with_release;
165
166         /**
167          * Represents the total size of the
168          * ModelChecker::lazy_sync_with_release lists. This count should be
169          * snapshotted, so it is actually a pointer to a location within
170          * ModelChecker::priv
171          */
172         unsigned int *lazy_sync_size;
173
174         std::vector<ModelAction *> *thrd_last_action;
175         NodeStack *node_stack;
176
177         /** Private data members that should be snapshotted. They are grouped
178          * together for efficiency and maintainability. */
179         struct model_snapshot_members *priv;
180
181         /**
182          * @brief The modification order graph
183          *
184          * A directed acyclic graph recording observations of the modification
185          * order on all the atomic objects in the system. This graph should
186          * never contain any cycles, as that represents a violation of the
187          * memory model (total ordering). This graph really consists of many
188          * disjoint (unconnected) subgraphs, each graph corresponding to a
189          * separate ordering on a distinct object.
190          *
191          * The edges in this graph represent the "ordered before" relation,
192          * such that <tt>a --> b</tt> means <tt>a</tt> was ordered before
193          * <tt>b</tt>.
194          */
195         CycleGraph *mo_graph;
196         bool failed_promise;
197         bool too_many_reads;
198         bool asserted;
199 };
200
201 extern ModelChecker *model;
202
203 #endif /* __MODEL_H__ */