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