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