nodestack: add release sequence breakage backtracking
[c11tester.git] / action.cc
1 #include <stdio.h>
2 #define __STDC_FORMAT_MACROS
3 #include <inttypes.h>
4 #include <vector>
5
6 #include "model.h"
7 #include "action.h"
8 #include "clockvector.h"
9 #include "common.h"
10 #include "threads.h"
11
12 #define ACTION_INITIAL_CLOCK 0
13
14 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, uint64_t value) :
15         type(type),
16         order(order),
17         location(loc),
18         value(value),
19         reads_from(NULL),
20         seq_number(ACTION_INITIAL_CLOCK),
21         cv(NULL)
22 {
23         Thread *t = thread_current();
24         this->tid = t->get_id();
25 }
26
27 /** @brief ModelAction destructor */
28 ModelAction::~ModelAction()
29 {
30         /**
31          * We can't free the clock vector:
32          * Clock vectors are snapshotting state. When we delete model actions,
33          * they are at the end of the node list and have invalid old clock
34          * vectors which have already been rolled back to an unallocated state.
35          */
36
37         /*
38          if (cv)
39                 delete cv; */
40 }
41
42 void ModelAction::copy_from_new(ModelAction *newaction)
43 {
44         seq_number = newaction->seq_number;
45 }
46
47 void ModelAction::set_seq_number(modelclock_t num)
48 {
49         ASSERT(seq_number == ACTION_INITIAL_CLOCK);
50         seq_number = num;
51 }
52
53 bool ModelAction::is_relseq_fixup() const
54 {
55         return type == MODEL_FIXUP_RELSEQ;
56 }
57
58 bool ModelAction::is_mutex_op() const
59 {
60         return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK;
61 }
62
63 bool ModelAction::is_lock() const
64 {
65         return type == ATOMIC_LOCK;
66 }
67
68 bool ModelAction::is_unlock() const
69 {
70         return type == ATOMIC_UNLOCK;
71 }
72
73 bool ModelAction::is_trylock() const
74 {
75         return type == ATOMIC_TRYLOCK;
76 }
77
78 bool ModelAction::is_success_lock() const
79 {
80         return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
81 }
82
83 bool ModelAction::is_failed_trylock() const
84 {
85         return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
86 }
87
88 bool ModelAction::is_read() const
89 {
90         return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMW;
91 }
92
93 bool ModelAction::is_write() const
94 {
95         return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT;
96 }
97
98 bool ModelAction::is_rmwr() const
99 {
100         return type == ATOMIC_RMWR;
101 }
102
103 bool ModelAction::is_rmw() const
104 {
105         return type == ATOMIC_RMW;
106 }
107
108 bool ModelAction::is_rmwc() const
109 {
110         return type == ATOMIC_RMWC;
111 }
112
113 bool ModelAction::is_fence() const 
114 {
115         return type == ATOMIC_FENCE;
116 }
117
118 bool ModelAction::is_initialization() const
119 {
120         return type == ATOMIC_INIT;
121 }
122
123 bool ModelAction::is_acquire() const
124 {
125         switch (order) {
126         case std::memory_order_acquire:
127         case std::memory_order_acq_rel:
128         case std::memory_order_seq_cst:
129                 return true;
130         default:
131                 return false;
132         }
133 }
134
135 bool ModelAction::is_release() const
136 {
137         switch (order) {
138         case std::memory_order_release:
139         case std::memory_order_acq_rel:
140         case std::memory_order_seq_cst:
141                 return true;
142         default:
143                 return false;
144         }
145 }
146
147 bool ModelAction::is_seqcst() const
148 {
149         return order==std::memory_order_seq_cst;
150 }
151
152 bool ModelAction::same_var(const ModelAction *act) const
153 {
154         return location == act->location;
155 }
156
157 bool ModelAction::same_thread(const ModelAction *act) const
158 {
159         return tid == act->tid;
160 }
161
162 void ModelAction::copy_typeandorder(ModelAction * act) {
163         this->type = act->type;
164         this->order = act->order;
165 }
166
167 /** This method changes an existing read part of an RMW action into either:
168  *  (1) a full RMW action in case of the completed write or
169  *  (2) a READ action in case a failed action.
170  * @todo  If the memory_order changes, we may potentially need to update our
171  * clock vector.
172  */
173 void ModelAction::process_rmw(ModelAction * act) {
174         this->order=act->order;
175         if (act->is_rmwc())
176                 this->type=ATOMIC_READ;
177         else if (act->is_rmw()) {
178                 this->type=ATOMIC_RMW;
179                 this->value=act->value;
180         }
181 }
182
183 /** The is_synchronizing method should only explore interleavings if:
184  *  (1) the operations are seq_cst and don't commute or
185  *  (2) the reordering may establish or break a synchronization relation.
186  *  Other memory operations will be dealt with by using the reads_from
187  *  relation.
188  *
189  *  @param act is the action to consider exploring a reordering.
190  *  @return tells whether we have to explore a reordering.
191  */
192 bool ModelAction::could_synchronize_with(const ModelAction *act) const
193 {
194         //Same thread can't be reordered
195         if (same_thread(act))
196                 return false;
197
198         // Different locations commute
199         if (!same_var(act))
200                 return false;
201
202         // Explore interleavings of seqcst writes to guarantee total order
203         // of seq_cst operations that don't commute
204         if ((is_write() || act->is_write()) && is_seqcst() && act->is_seqcst())
205                 return true;
206
207         // Explore synchronizing read/write pairs
208         if (is_read() && is_acquire() && act->is_write() && act->is_release())
209                 return true;
210
211         // Otherwise handle by reads_from relation
212         return false;
213 }
214
215 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
216 {
217         //Must be different threads to reorder
218         if (same_thread(act))
219                 return false;
220         
221         //Try to reorder a lock past a successful lock
222         if (act->is_success_lock())
223                 return true;
224         
225         //Try to push a successful trylock past an unlock
226         if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
227                 return true;
228
229         return false;
230 }
231
232 /**
233  * Create a new clock vector for this action. Note that this function allows a
234  * user to clobber (and leak) a ModelAction's existing clock vector. A user
235  * should ensure that the vector has already either been rolled back
236  * (effectively "freed") or freed.
237  *
238  * @param parent A ModelAction from which to inherit a ClockVector
239  */
240 void ModelAction::create_cv(const ModelAction *parent)
241 {
242         if (parent)
243                 cv = new ClockVector(parent->cv, this);
244         else
245                 cv = new ClockVector(NULL, this);
246 }
247
248 void ModelAction::set_try_lock(bool obtainedlock) {
249         if (obtainedlock)
250                 value=VALUE_TRYSUCCESS;
251         else
252                 value=VALUE_TRYFAILED;
253 }
254
255 /**
256  * Update the model action's read_from action
257  * @param act The action to read from; should be a write
258  * @return True if this read established synchronization
259  */
260 bool ModelAction::read_from(const ModelAction *act)
261 {
262         ASSERT(cv);
263         reads_from = act;
264         if (act != NULL && this->is_acquire()) {
265                 rel_heads_list_t release_heads;
266                 model->get_release_seq_heads(this, &release_heads);
267                 int num_heads = release_heads.size();
268                 for (unsigned int i = 0; i < release_heads.size(); i++)
269                         if (!synchronize_with(release_heads[i])) {
270                                 model->set_bad_synchronization();
271                                 num_heads--;
272                         }
273                 return num_heads > 0;
274         }
275         return false;
276 }
277
278 /**
279  * Synchronize the current thread with the thread corresponding to the
280  * ModelAction parameter.
281  * @param act The ModelAction to synchronize with
282  * @return True if this is a valid synchronization; false otherwise
283  */
284 bool ModelAction::synchronize_with(const ModelAction *act) {
285         if (*this < *act && type != THREAD_JOIN && type != ATOMIC_LOCK)
286                 return false;
287         model->check_promises(act->get_tid(), cv, act->cv);
288         cv->merge(act->cv);
289         return true;
290 }
291
292 bool ModelAction::has_synchronized_with(const ModelAction *act) const
293 {
294         return cv->has_synchronized_with(act->cv);
295 }
296
297 /**
298  * Check whether 'this' happens before act, according to the memory-model's
299  * happens before relation. This is checked via the ClockVector constructs.
300  * @return true if this action's thread has synchronized with act's thread
301  * since the execution of act, false otherwise.
302  */
303 bool ModelAction::happens_before(const ModelAction *act) const
304 {
305         return act->cv->synchronized_since(this);
306 }
307
308 /** @brief Print nicely-formatted info about this ModelAction */
309 void ModelAction::print() const
310 {
311         const char *type_str, *mo_str;
312         switch (this->type) {
313         case MODEL_FIXUP_RELSEQ:
314                 type_str = "relseq fixup";
315                 break;
316         case THREAD_CREATE:
317                 type_str = "thread create";
318                 break;
319         case THREAD_START:
320                 type_str = "thread start";
321                 break;
322         case THREAD_YIELD:
323                 type_str = "thread yield";
324                 break;
325         case THREAD_JOIN:
326                 type_str = "thread join";
327                 break;
328         case THREAD_FINISH:
329                 type_str = "thread finish";
330                 break;
331         case ATOMIC_READ:
332                 type_str = "atomic read";
333                 break;
334         case ATOMIC_WRITE:
335                 type_str = "atomic write";
336                 break;
337         case ATOMIC_RMW:
338                 type_str = "atomic rmw";
339                 break;
340         case ATOMIC_FENCE:
341                 type_str = "fence";
342                 break;
343         case ATOMIC_RMWR:
344                 type_str = "atomic rmwr";
345                 break;
346         case ATOMIC_RMWC:
347                 type_str = "atomic rmwc";
348                 break;
349         case ATOMIC_INIT:
350                 type_str = "init atomic";
351                 break;
352         case ATOMIC_LOCK:
353                 type_str = "lock";
354                 break;
355         case ATOMIC_UNLOCK:
356                 type_str = "unlock";
357                 break;
358         case ATOMIC_TRYLOCK:
359                 type_str = "trylock";
360                 break;
361         default:
362                 type_str = "unknown type";
363         }
364
365         uint64_t valuetoprint=type==ATOMIC_READ?(reads_from!=NULL?reads_from->value:VALUE_NONE):value;
366
367         switch (this->order) {
368         case std::memory_order_relaxed:
369                 mo_str = "relaxed";
370                 break;
371         case std::memory_order_acquire:
372                 mo_str = "acquire";
373                 break;
374         case std::memory_order_release:
375                 mo_str = "release";
376                 break;
377         case std::memory_order_acq_rel:
378                 mo_str = "acq_rel";
379                 break;
380         case std::memory_order_seq_cst:
381                 mo_str = "seq_cst";
382                 break;
383         default:
384                 mo_str = "unknown";
385                 break;
386         }
387
388         printf("(%3d) Thread: %-2d   Action: %-13s   MO: %7s  Loc: %14p  Value: %-12" PRIu64,
389                         seq_number, id_to_int(tid), type_str, mo_str, location, valuetoprint);
390         if (is_read()) {
391                 if (reads_from)
392                         printf(" Rf: %d", reads_from->get_seq_number());
393                 else
394                         printf(" Rf: ?");
395         }
396         if (cv) {
397                 printf("\t");
398                 cv->print();
399         } else
400                 printf("\n");
401 }