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