fairness changes...
[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         unsigned int fairwindow;
38         unsigned int enabledcount;
39 };
40
41 struct PendingFutureValue {
42         uint64_t value;
43         modelclock_t expiration;
44         ModelAction * act;
45 };
46
47 /**
48  * Structure for holding small ModelChecker members that should be snapshotted
49  */
50 struct model_snapshot_members {
51         ModelAction *current_action;
52         int next_thread_id;
53         modelclock_t used_sequence_numbers;
54         Thread *nextThread;
55         ModelAction *next_backtrack;
56
57         /** @see ModelChecker::lazy_sync_size */
58         unsigned int lazy_sync_size;
59 };
60
61 /** @brief The central structure for model-checking */
62 class ModelChecker {
63 public:
64         ModelChecker(struct model_params params);
65         ~ModelChecker();
66
67         /** @returns the context for the main model-checking system thread */
68         ucontext_t * get_system_context() { return &system_context; }
69
70         /** Prints an execution summary with trace information. */
71         void print_summary();
72
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()); }
77
78         thread_id_t get_next_id();
79         int get_num_threads();
80         modelclock_t get_next_seq_num();
81
82         /** @return The currently executing Thread. */
83         Thread * get_current_thread() { return scheduler->get_current_thread(); }
84
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();
89         bool isfeasible();
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;
98
99         MEMALLOC
100 private:
101         /** The scheduler to use: tracks the running/ready Threads */
102         Scheduler *scheduler;
103
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;}
107         int num_executions;
108         int num_feasible_executions;
109         bool promises_expired();
110
111         /**
112          * Stores the ModelAction for the current thread action.  Call this
113          * immediately before switching from user- to system-context to pass
114          * data between them.
115          * @param act The ModelAction created by the user-thread action
116          */
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);
124
125         bool take_step();
126
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);
135
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);
149
150         ModelAction *diverge;
151
152         ucontext_t system_context;
153         action_list_t *action_trace;
154         HashTable<int, Thread *, int> *thread_map;
155
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;
159
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;
163
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;
167
168         /**
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.
174          */
175         HashTable<void *, action_list_t, uintptr_t, 4> *lazy_sync_with_release;
176
177         /**
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
181          * ModelChecker::priv
182          */
183         unsigned int *lazy_sync_size;
184
185         std::vector<ModelAction *> *thrd_last_action;
186         NodeStack *node_stack;
187
188         /** Private data members that should be snapshotted. They are grouped
189          * together for efficiency and maintainability. */
190         struct model_snapshot_members *priv;
191
192         /**
193          * @brief The modification order graph
194          *
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.
201          *
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
204          * <tt>b</tt>.
205          */
206         CycleGraph *mo_graph;
207         bool failed_promise;
208         bool too_many_reads;
209         bool asserted;
210 };
211
212 extern ModelChecker *model;
213
214 #endif /* __MODEL_H__ */