Add back model_thread; it is still needed
[c11tester.git] / execution.h
1 /** @file execution.h
2  *  @brief Model-checker core
3  */
4
5 #ifndef __EXECUTION_H__
6 #define __EXECUTION_H__
7
8 #include <cstddef>
9 #include <inttypes.h>
10
11 #include "mymemory.h"
12 #include "hashtable.h"
13 #include "config.h"
14 #include "modeltypes.h"
15 #include "stl-model.h"
16 #include "params.h"
17 #include "mypthread.h"
18 #include "mutex.h"
19 #include <condition_variable>
20 #include "classlist.h"
21
22 #define INITIAL_THREAD_ID       0
23 #define MAIN_THREAD_ID          1
24
25 struct PendingFutureValue {
26         PendingFutureValue(ModelAction *writer, ModelAction *reader) :
27                 writer(writer), reader(reader)
28         { }
29         const ModelAction *writer;
30         ModelAction *reader;
31 };
32
33 #ifdef COLLECT_STAT
34 void print_atomic_accesses();
35 #endif
36
37 /** @brief The central structure for model-checking */
38 class ModelExecution {
39 public:
40         ModelExecution(ModelChecker *m, Scheduler *scheduler);
41         ~ModelExecution();
42
43         struct model_params * get_params() const { return params; }
44         void setParams(struct model_params * _params) {params = _params;}
45
46         Thread * take_step(ModelAction *curr);
47
48         void print_summary();
49         void print_tail();
50 #if SUPPORT_MOD_ORDER_DUMP
51         void dumpGraph(char *filename);
52 #endif
53
54         void add_thread(Thread *t);
55         Thread * get_thread(thread_id_t tid) const;
56         Thread * get_thread(const ModelAction *act) const;
57
58         uint32_t get_pthread_counter() { return pthread_counter; }
59         void incr_pthread_counter() { pthread_counter++; }
60         Thread * get_pthread(pthread_t pid);
61
62         bool is_enabled(Thread *t) const;
63         bool is_enabled(thread_id_t tid) const;
64
65         thread_id_t get_next_id();
66         unsigned int get_num_threads() const;
67
68         ClockVector * get_cv(thread_id_t tid) const;
69         ModelAction * get_parent_action(thread_id_t tid) const;
70
71         ModelAction * get_last_action(thread_id_t tid) const;
72
73         bool check_action_enabled(ModelAction *curr);
74
75         void assert_bug(const char *msg);
76
77         bool have_bug_reports() const;
78
79         SnapVector<bug_message *> * get_bugs() const;
80
81         bool has_asserted() const;
82         void set_assert();
83         bool is_complete_execution() const;
84
85         bool is_deadlocked() const;
86
87         action_list_t * get_action_trace() { return &action_trace; }
88         Fuzzer * getFuzzer();
89         CycleGraph * const get_mo_graph() { return mo_graph; }
90         HashTable<pthread_cond_t *, cdsc::snapcondition_variable *, uintptr_t, 4> * getCondMap() {return &cond_map;}
91         HashTable<pthread_mutex_t *, cdsc::snapmutex *, uintptr_t, 4> * getMutexMap() {return &mutex_map;}
92         ModelAction * check_current_action(ModelAction *curr);
93
94         bool isFinished() {return isfinished;}
95         void setFinished() {isfinished = true;}
96         void restore_last_seq_num();
97         void collectActions();
98         modelclock_t get_curr_seq_num();
99 #ifdef TLS
100         pthread_key_t getPthreadKey() {return pthreadkey;}
101 #endif
102         SNAPSHOTALLOC
103 private:
104         int get_execution_number() const;
105         bool should_wake_up(const ModelAction * asleep) const;
106         void wake_up_sleeping_actions();
107         modelclock_t get_next_seq_num();
108         bool next_execution();
109         bool initialize_curr_action(ModelAction **curr);
110         bool process_read(ModelAction *curr, SnapVector<ModelAction *> * rf_set);
111         void process_write(ModelAction *curr);
112         void process_fence(ModelAction *curr);
113         bool process_mutex(ModelAction *curr);
114         void process_thread_action(ModelAction *curr);
115         void read_from(ModelAction *act, ModelAction *rf);
116         bool synchronize(const ModelAction *first, ModelAction *second);
117         void add_action_to_lists(ModelAction *act, bool canprune);
118         void add_normal_write_to_lists(ModelAction *act);
119         void add_write_to_lists(ModelAction *act);
120         ModelAction * get_last_fence_release(thread_id_t tid) const;
121         ModelAction * get_last_seq_cst_write(ModelAction *curr) const;
122         ModelAction * get_last_seq_cst_fence(thread_id_t tid, const ModelAction *before_fence) const;
123         ModelAction * get_last_unlock(ModelAction *curr) const;
124         SnapVector<ModelAction *> * build_may_read_from(ModelAction *curr);
125         ModelAction * process_rmw(ModelAction *curr);
126         bool r_modification_order(ModelAction *curr, const ModelAction *rf, SnapVector<ModelAction *> *priorset, bool *canprune);
127         void w_modification_order(ModelAction *curr);
128         ClockVector * get_hb_from_write(ModelAction *rf) const;
129         ModelAction * convertNonAtomicStore(void*);
130         ClockVector * computeMinimalCV();
131         void removeAction(ModelAction *act);
132         void fixupLastAct(ModelAction *act);
133
134 #ifdef TLS
135         pthread_key_t pthreadkey;
136 #endif
137         ModelChecker *model;
138         struct model_params * params;
139
140         /** The scheduler to use: tracks the running/ready Threads */
141         Scheduler * const scheduler;
142
143
144         SnapVector<Thread *> thread_map;
145         SnapVector<Thread *> pthread_map;
146         uint32_t pthread_counter;
147
148         action_list_t action_trace;
149
150
151         /** Per-object list of actions. Maps an object (i.e., memory location)
152          * to a trace of all actions performed on the object.
153          * Used only for SC fences, unlocks, & wait.
154          */
155         HashTable<const void *, simple_action_list_t *, uintptr_t, 2> obj_map;
156
157         /** Per-object list of actions. Maps an object (i.e., memory location)
158          * to a trace of all actions performed on the object. */
159         HashTable<const void *, simple_action_list_t *, uintptr_t, 2> condvar_waiters_map;
160
161         /** Per-object list of actions that each thread performed. */
162         HashTable<const void *, SnapVector<action_list_t> *, uintptr_t, 2> obj_thrd_map;
163
164         /** Per-object list of writes that each thread performed. */
165         HashTable<const void *, SnapVector<simple_action_list_t> *, uintptr_t, 2> obj_wr_thrd_map;
166
167         HashTable<const void *, ModelAction *, uintptr_t, 4> obj_last_sc_map;
168
169
170         HashTable<pthread_mutex_t *, cdsc::snapmutex *, uintptr_t, 4> mutex_map;
171         HashTable<pthread_cond_t *, cdsc::snapcondition_variable *, uintptr_t, 4> cond_map;
172
173         /**
174          * List of pending release sequences. Release sequences might be
175          * determined lazily as promises are fulfilled and modification orders
176          * are established. Each entry in the list may only be partially
177          * filled, depending on its pending status.
178          */
179
180         SnapVector<ModelAction *> thrd_last_action;
181         SnapVector<ModelAction *> thrd_last_fence_release;
182
183         /** A special model-checker Thread; used for associating with
184          *  model-checker-related ModelAcitons */
185         Thread *model_thread;
186
187         /** Private data members that should be snapshotted. They are grouped
188          * together for efficiency and maintainability. */
189         struct model_snapshot_members * const priv;
190
191         /**
192          * @brief The modification order graph
193          *
194          * A directed acyclic graph recording observations of the modification
195          * order on all the atomic objects in the system. This graph should
196          * never contain any cycles, as that represents a violation of the
197          * memory model (total ordering). This graph really consists of many
198          * disjoint (unconnected) subgraphs, each graph corresponding to a
199          * separate ordering on a distinct object.
200          *
201          * The edges in this graph represent the "ordered before" relation,
202          * such that <tt>a --> b</tt> means <tt>a</tt> was ordered before
203          * <tt>b</tt>.
204          */
205         CycleGraph * const mo_graph;
206
207         Fuzzer * fuzzer;
208
209         Thread * action_select_next_thread(const ModelAction *curr) const;
210
211         bool isfinished;
212 };
213
214 #endif  /* __EXECUTION_H__ */