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