action/model: backtrack for seq-cst and release/acquire fences
[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-model.h"
11 #include "nodestack.h"
12
13 #define ACTION_INITIAL_CLOCK 0
14
15 /**
16  * @brief Construct a new ModelAction
17  *
18  * @param type The type of action
19  * @param order The memory order of this action. A "don't care" for non-ATOMIC
20  * actions (e.g., THREAD_* or MODEL_* actions).
21  * @param loc The location that this action acts upon
22  * @param value (optional) A value associated with the action (e.g., the value
23  * read or written). Defaults to a given macro constant, for debugging purposes.
24  * @param thread (optional) The Thread in which this action occurred. If NULL
25  * (default), then a Thread is assigned according to the scheduler.
26  */
27 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc,
28                 uint64_t value, Thread *thread) :
29         type(type),
30         order(order),
31         location(loc),
32         value(value),
33         reads_from(NULL),
34         node(NULL),
35         seq_number(ACTION_INITIAL_CLOCK),
36         cv(NULL),
37         sleep_flag(false)
38 {
39         /* References to NULL atomic variables can end up here */
40         ASSERT(loc || type == ATOMIC_FENCE || type == MODEL_FIXUP_RELSEQ);
41
42         Thread *t = thread ? thread : thread_current();
43         this->tid = t->get_id();
44 }
45
46 /** @brief ModelAction destructor */
47 ModelAction::~ModelAction()
48 {
49         /**
50          * We can't free the clock vector:
51          * Clock vectors are snapshotting state. When we delete model actions,
52          * they are at the end of the node list and have invalid old clock
53          * vectors which have already been rolled back to an unallocated state.
54          */
55
56         /*
57          if (cv)
58                 delete cv; */
59 }
60
61 void ModelAction::copy_from_new(ModelAction *newaction)
62 {
63         seq_number = newaction->seq_number;
64 }
65
66 void ModelAction::set_seq_number(modelclock_t num)
67 {
68         ASSERT(seq_number == ACTION_INITIAL_CLOCK);
69         seq_number = num;
70 }
71
72 bool ModelAction::is_relseq_fixup() const
73 {
74         return type == MODEL_FIXUP_RELSEQ;
75 }
76
77 bool ModelAction::is_mutex_op() const
78 {
79         return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK || type == ATOMIC_WAIT || type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
80 }
81
82 bool ModelAction::is_lock() const
83 {
84         return type == ATOMIC_LOCK;
85 }
86
87 bool ModelAction::is_wait() const {
88         return type == ATOMIC_WAIT;
89 }
90
91 bool ModelAction::is_notify() const {
92         return type==ATOMIC_NOTIFY_ONE || type==ATOMIC_NOTIFY_ALL;
93 }
94
95 bool ModelAction::is_notify_one() const {
96         return type==ATOMIC_NOTIFY_ONE;
97 }
98
99 bool ModelAction::is_unlock() const
100 {
101         return type == ATOMIC_UNLOCK;
102 }
103
104 bool ModelAction::is_trylock() const
105 {
106         return type == ATOMIC_TRYLOCK;
107 }
108
109 bool ModelAction::is_success_lock() const
110 {
111         return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
112 }
113
114 bool ModelAction::is_failed_trylock() const
115 {
116         return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
117 }
118
119 bool ModelAction::is_read() const
120 {
121         return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMW;
122 }
123
124 bool ModelAction::is_write() const
125 {
126         return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT;
127 }
128
129 bool ModelAction::could_be_write() const
130 {
131         return is_write() || is_rmwr();
132 }
133
134 bool ModelAction::is_rmwr() const
135 {
136         return type == ATOMIC_RMWR;
137 }
138
139 bool ModelAction::is_rmw() const
140 {
141         return type == ATOMIC_RMW;
142 }
143
144 bool ModelAction::is_rmwc() const
145 {
146         return type == ATOMIC_RMWC;
147 }
148
149 bool ModelAction::is_fence() const 
150 {
151         return type == ATOMIC_FENCE;
152 }
153
154 bool ModelAction::is_initialization() const
155 {
156         return type == ATOMIC_INIT;
157 }
158
159 bool ModelAction::is_relaxed() const
160 {
161         return order == std::memory_order_relaxed;
162 }
163
164 bool ModelAction::is_acquire() const
165 {
166         switch (order) {
167         case std::memory_order_acquire:
168         case std::memory_order_acq_rel:
169         case std::memory_order_seq_cst:
170                 return true;
171         default:
172                 return false;
173         }
174 }
175
176 bool ModelAction::is_release() const
177 {
178         switch (order) {
179         case std::memory_order_release:
180         case std::memory_order_acq_rel:
181         case std::memory_order_seq_cst:
182                 return true;
183         default:
184                 return false;
185         }
186 }
187
188 bool ModelAction::is_seqcst() const
189 {
190         return order==std::memory_order_seq_cst;
191 }
192
193 bool ModelAction::same_var(const ModelAction *act) const
194 {
195         if ( act->is_wait() || is_wait() ) {
196                 if ( act->is_wait() && is_wait() ) {
197                         if ( ((void *)value) == ((void *)act->value) )
198                                 return true;
199                 } else if ( is_wait() ) {
200                         if ( ((void *)value) == act->location )
201                                 return true;
202                 } else if ( act->is_wait() ) {
203                         if ( location == ((void *)act->value) )
204                                 return true;
205                 }
206         }
207
208         return location == act->location;
209 }
210
211 bool ModelAction::same_thread(const ModelAction *act) const
212 {
213         return tid == act->tid;
214 }
215
216 void ModelAction::copy_typeandorder(ModelAction * act) {
217         this->type = act->type;
218         this->order = act->order;
219 }
220
221 /** This method changes an existing read part of an RMW action into either:
222  *  (1) a full RMW action in case of the completed write or
223  *  (2) a READ action in case a failed action.
224  * @todo  If the memory_order changes, we may potentially need to update our
225  * clock vector.
226  */
227 void ModelAction::process_rmw(ModelAction * act) {
228         this->order=act->order;
229         if (act->is_rmwc())
230                 this->type=ATOMIC_READ;
231         else if (act->is_rmw()) {
232                 this->type=ATOMIC_RMW;
233                 this->value=act->value;
234         }
235 }
236
237 /** The is_synchronizing method should only explore interleavings if:
238  *  (1) the operations are seq_cst and don't commute or
239  *  (2) the reordering may establish or break a synchronization relation.
240  *  Other memory operations will be dealt with by using the reads_from
241  *  relation.
242  *
243  *  @param act is the action to consider exploring a reordering.
244  *  @return tells whether we have to explore a reordering.
245  */
246 bool ModelAction::could_synchronize_with(const ModelAction *act) const
247 {
248         //Same thread can't be reordered
249         if (same_thread(act))
250                 return false;
251
252         // Different locations commute
253         if (!same_var(act))
254                 return false;
255
256         // Explore interleavings of seqcst writes/fences to guarantee total
257         // order of seq_cst operations that don't commute
258         if ((could_be_write() || act->could_be_write() || is_fence() || act->is_fence())
259                         && is_seqcst() && act->is_seqcst())
260                 return true;
261
262         // Explore synchronizing read/write/fence pairs
263         if (is_acquire() && act->is_release() && (is_read() || is_fence()) &&
264                         (act->could_be_write() || act->is_fence()))
265                 return true;
266
267         //lock just released...we can grab lock
268         if ((is_lock() ||is_trylock()) && (act->is_unlock()||act->is_wait()))
269                 return true;
270
271         //lock just acquired...we can fail to grab lock
272         if (is_trylock() && act->is_success_lock())
273                 return true;
274
275         //other thread stalling on lock...we can release lock
276         if (is_unlock() && (act->is_trylock()||act->is_lock()))
277                 return true;
278
279         if (is_trylock() && (act->is_unlock()||act->is_wait()))
280                 return true;
281
282         if ( is_notify() && act->is_wait() )
283                 return true;
284
285         if ( is_wait() && act->is_notify() )
286                 return true;
287
288         // Otherwise handle by reads_from relation
289         return false;
290 }
291
292 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
293 {
294         //Must be different threads to reorder
295         if (same_thread(act))
296                 return false;
297         
298         //Try to reorder a lock past a successful lock
299         if (act->is_success_lock())
300                 return true;
301         
302         //Try to push a successful trylock past an unlock
303         if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
304                 return true;
305
306         //Try to push a successful trylock past a wait
307         if (act->is_wait() && is_trylock() && value == VALUE_TRYSUCCESS)
308                 return true;
309
310         return false;
311 }
312
313 /**
314  * Create a new clock vector for this action. Note that this function allows a
315  * user to clobber (and leak) a ModelAction's existing clock vector. A user
316  * should ensure that the vector has already either been rolled back
317  * (effectively "freed") or freed.
318  *
319  * @param parent A ModelAction from which to inherit a ClockVector
320  */
321 void ModelAction::create_cv(const ModelAction *parent)
322 {
323         if (parent)
324                 cv = new ClockVector(parent->cv, this);
325         else
326                 cv = new ClockVector(NULL, this);
327 }
328
329 void ModelAction::set_try_lock(bool obtainedlock) {
330         if (obtainedlock)
331                 value=VALUE_TRYSUCCESS;
332         else
333                 value=VALUE_TRYFAILED;
334 }
335
336 /**
337  * Update the model action's read_from action
338  * @param act The action to read from; should be a write
339  * @return True if this read established synchronization
340  */
341 bool ModelAction::read_from(const ModelAction *act)
342 {
343         ASSERT(cv);
344         reads_from = act;
345         if (act != NULL && this->is_acquire()) {
346                 rel_heads_list_t release_heads;
347                 model->get_release_seq_heads(this, &release_heads);
348                 int num_heads = release_heads.size();
349                 for (unsigned int i = 0; i < release_heads.size(); i++)
350                         if (!synchronize_with(release_heads[i])) {
351                                 model->set_bad_synchronization();
352                                 num_heads--;
353                         }
354                 return num_heads > 0;
355         }
356         return false;
357 }
358
359 /**
360  * Synchronize the current thread with the thread corresponding to the
361  * ModelAction parameter.
362  * @param act The ModelAction to synchronize with
363  * @return True if this is a valid synchronization; false otherwise
364  */
365 bool ModelAction::synchronize_with(const ModelAction *act) {
366         if (*this < *act && type != THREAD_JOIN && type != ATOMIC_LOCK)
367                 return false;
368         model->check_promises(act->get_tid(), cv, act->cv);
369         cv->merge(act->cv);
370         return true;
371 }
372
373 bool ModelAction::has_synchronized_with(const ModelAction *act) const
374 {
375         return cv->synchronized_since(act);
376 }
377
378 /**
379  * Check whether 'this' happens before act, according to the memory-model's
380  * happens before relation. This is checked via the ClockVector constructs.
381  * @return true if this action's thread has synchronized with act's thread
382  * since the execution of act, false otherwise.
383  */
384 bool ModelAction::happens_before(const ModelAction *act) const
385 {
386         return act->cv->synchronized_since(this);
387 }
388
389 /** @brief Print nicely-formatted info about this ModelAction */
390 void ModelAction::print() const
391 {
392         const char *type_str, *mo_str;
393         switch (this->type) {
394         case MODEL_FIXUP_RELSEQ:
395                 type_str = "relseq fixup";
396                 break;
397         case THREAD_CREATE:
398                 type_str = "thread create";
399                 break;
400         case THREAD_START:
401                 type_str = "thread start";
402                 break;
403         case THREAD_YIELD:
404                 type_str = "thread yield";
405                 break;
406         case THREAD_JOIN:
407                 type_str = "thread join";
408                 break;
409         case THREAD_FINISH:
410                 type_str = "thread finish";
411                 break;
412         case ATOMIC_READ:
413                 type_str = "atomic read";
414                 break;
415         case ATOMIC_WRITE:
416                 type_str = "atomic write";
417                 break;
418         case ATOMIC_RMW:
419                 type_str = "atomic rmw";
420                 break;
421         case ATOMIC_FENCE:
422                 type_str = "fence";
423                 break;
424         case ATOMIC_RMWR:
425                 type_str = "atomic rmwr";
426                 break;
427         case ATOMIC_RMWC:
428                 type_str = "atomic rmwc";
429                 break;
430         case ATOMIC_INIT:
431                 type_str = "init atomic";
432                 break;
433         case ATOMIC_LOCK:
434                 type_str = "lock";
435                 break;
436         case ATOMIC_UNLOCK:
437                 type_str = "unlock";
438                 break;
439         case ATOMIC_TRYLOCK:
440                 type_str = "trylock";
441                 break;
442         case ATOMIC_WAIT:
443                 type_str = "wait";
444                 break;
445         case ATOMIC_NOTIFY_ONE:
446                 type_str = "notify one";
447                 break;
448         case ATOMIC_NOTIFY_ALL:
449                 type_str = "notify all";
450                 break;
451         default:
452                 type_str = "unknown type";
453         }
454
455         uint64_t valuetoprint=type==ATOMIC_READ?(reads_from!=NULL?reads_from->value:VALUE_NONE):value;
456
457         switch (this->order) {
458         case std::memory_order_relaxed:
459                 mo_str = "relaxed";
460                 break;
461         case std::memory_order_acquire:
462                 mo_str = "acquire";
463                 break;
464         case std::memory_order_release:
465                 mo_str = "release";
466                 break;
467         case std::memory_order_acq_rel:
468                 mo_str = "acq_rel";
469                 break;
470         case std::memory_order_seq_cst:
471                 mo_str = "seq_cst";
472                 break;
473         default:
474                 mo_str = "unknown";
475                 break;
476         }
477
478         model_print("(%4d) Thread: %-2d   Action: %-13s   MO: %7s  Loc: %14p   Value: %-#18" PRIx64,
479                         seq_number, id_to_int(tid), type_str, mo_str, location, valuetoprint);
480         if (is_read()) {
481                 if (reads_from)
482                         model_print("  Rf: %-3d", reads_from->get_seq_number());
483                 else
484                         model_print("  Rf: ?  ");
485         }
486         if (cv) {
487                 if (is_read())
488                         model_print(" ");
489                 else
490                         model_print("          ");
491                 cv->print();
492         } else
493                 model_print("\n");
494 }
495
496 /** @brief Print nicely-formatted info about this ModelAction */
497 unsigned int ModelAction::hash() const
498 {
499         unsigned int hash=(unsigned int) this->type;
500         hash^=((unsigned int)this->order)<<3;
501         hash^=seq_number<<5;
502         hash ^= id_to_int(tid) << 6;
503
504         if (is_read()) {
505                 if (reads_from)
506                         hash^=reads_from->get_seq_number();
507         }
508         return hash;
509 }