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