Makefile: add PHONY targets
[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
21 /* Forward declaration */
22 class NodeStack;
23 class CycleGraph;
24 class Promise;
25
26 /**
27  * Model checker parameter structure. Holds run-time configuration options for
28  * the model checker.
29  */
30 struct model_params {
31         int maxreads;
32         int maxfuturedelay;
33 };
34
35 struct PendingFutureValue {
36         uint64_t value;
37         modelclock_t expiration;
38         ModelAction * act;
39 };
40
41 /**
42  * Structure for holding small ModelChecker members that should be snapshotted
43  */
44 struct model_snapshot_members {
45         ModelAction *current_action;
46         int next_thread_id;
47         modelclock_t used_sequence_numbers;
48         Thread *nextThread;
49         ModelAction *next_backtrack;
50
51         /** @see ModelChecker::lazy_sync_size */
52         unsigned int lazy_sync_size;
53 };
54
55 /** @brief The central structure for model-checking */
56 class ModelChecker {
57 public:
58         ModelChecker(struct model_params params);
59         ~ModelChecker();
60
61         /** @returns the context for the main model-checking system thread */
62         ucontext_t * get_system_context() { return &system_context; }
63
64         /** Prints an execution summary with trace information. */
65         void print_summary();
66
67         void add_thread(Thread *t);
68         void remove_thread(Thread *t);
69         Thread * get_thread(thread_id_t tid) { return thread_map->get(id_to_int(tid)); }
70         Thread * get_thread(ModelAction *act) { return get_thread(act->get_tid()); }
71
72         thread_id_t get_next_id();
73         int get_num_threads();
74         modelclock_t get_next_seq_num();
75
76         /** @return The currently executing Thread. */
77         Thread * get_current_thread() { return scheduler->get_current_thread(); }
78
79         int switch_to_master(ModelAction *act);
80         ClockVector * get_cv(thread_id_t tid);
81         ModelAction * get_parent_action(thread_id_t tid);
82         bool next_execution();
83         bool isfeasible();
84         bool isfeasibleotherthanRMW();
85         bool isfinalfeasible();
86         void check_promises(ClockVector *old_cv, ClockVector * merge_cv);
87         void get_release_seq_heads(ModelAction *act,
88                         std::vector< const ModelAction *, MyAlloc<const ModelAction *> > *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         bool promises_expired();
103         const model_params params;
104
105         /**
106          * Stores the ModelAction for the current thread action.  Call this
107          * immediately before switching from user- to system-context to pass
108          * data between them.
109          * @param act The ModelAction created by the user-thread action
110          */
111         void set_current_action(ModelAction *act) { priv->current_action = act; }
112         Thread * check_current_action(ModelAction *curr);
113         bool process_read(ModelAction *curr, Thread * th, bool second_part_of_rmw);
114
115         bool take_step();
116
117         void check_recency(ModelAction *curr, bool already_added);
118         ModelAction * get_last_conflict(ModelAction *act);
119         void set_backtracking(ModelAction *act);
120         Thread * get_next_thread(ModelAction *curr);
121         ModelAction * get_next_backtrack();
122         void reset_to_initial_state();
123         bool resolve_promises(ModelAction *curr);
124         void compute_promises(ModelAction *curr);
125
126         void check_curr_backtracking(ModelAction * curr);
127         void add_action_to_lists(ModelAction *act);
128         ModelAction * get_last_action(thread_id_t tid);
129         ModelAction * get_last_seq_cst(const void *location);
130         void build_reads_from_past(ModelAction *curr);
131         ModelAction * process_rmw(ModelAction *curr);
132         void post_r_modification_order(ModelAction *curr, const ModelAction *rf);
133         bool r_modification_order(ModelAction *curr, const ModelAction *rf);
134         bool w_modification_order(ModelAction *curr);
135         bool release_seq_head(const ModelAction *rf,
136                         std::vector< const ModelAction *, MyAlloc<const ModelAction *> > *release_heads) const;
137         bool resolve_release_sequences(void *location);
138
139         ModelAction *diverge;
140
141         ucontext_t system_context;
142         action_list_t *action_trace;
143         HashTable<int, Thread *, int> *thread_map;
144
145         /** Per-object list of actions. Maps an object (i.e., memory location)
146          * to a trace of all actions performed on the object. */
147         HashTable<const void *, action_list_t, uintptr_t, 4> *obj_map;
148
149         HashTable<void *, std::vector<action_list_t>, uintptr_t, 4 > *obj_thrd_map;
150         std::vector<Promise *> *promises;
151         std::vector<struct PendingFutureValue> *futurevalues;
152
153         /**
154          * Collection of lists of objects that might synchronize with one or
155          * more release sequence. Release sequences might be determined lazily
156          * as promises are fulfilled and modification orders are established.
157          * This structure maps its lists by object location. Each ModelAction
158          * in the lists should be an acquire operation.
159          */
160         HashTable<void *, std::list<ModelAction *>, uintptr_t, 4> *lazy_sync_with_release;
161
162         /**
163          * Represents the total size of the
164          * ModelChecker::lazy_sync_with_release lists. This count should be
165          * snapshotted, so it is actually a pointer to a location within
166          * ModelChecker::priv
167          */
168         unsigned int *lazy_sync_size;
169
170         std::vector<ModelAction *> *thrd_last_action;
171         NodeStack *node_stack;
172
173         /** Private data members that should be snapshotted. They are grouped
174          * together for efficiency and maintainability. */
175         struct model_snapshot_members *priv;
176
177         /**
178          * @brief The modification order graph
179          *
180          * A directed acyclic graph recording observations of the modification
181          * order on all the atomic objects in the system. This graph should
182          * never contain any cycles, as that represents a violation of the
183          * memory model (total ordering). This graph really consists of many
184          * disjoint (unconnected) subgraphs, each graph corresponding to a
185          * separate ordering on a distinct object.
186          *
187          * The edges in this graph represent the "ordered before" relation,
188          * such that <tt>a --> b</tt> means <tt>a</tt> was ordered before
189          * <tt>b</tt>.
190          */
191         CycleGraph *mo_graph;
192         bool failed_promise;
193         bool too_many_reads;
194         bool asserted;
195 };
196
197 extern ModelChecker *model;
198
199 #endif /* __MODEL_H__ */