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