model: rename PendingFutureValue 'act' to 'reader'
[c11tester.git] / action.cc
1 #include <stdio.h>
2 #define __STDC_FORMAT_MACROS
3 #include <inttypes.h>
4
5 #include "model.h"
6 #include "action.h"
7 #include "clockvector.h"
8 #include "common.h"
9 #include "threads-model.h"
10 #include "nodestack.h"
11
12 #define ACTION_INITIAL_CLOCK 0
13
14 /**
15  * @brief Construct a new ModelAction
16  *
17  * @param type The type of action
18  * @param order The memory order of this action. A "don't care" for non-ATOMIC
19  * actions (e.g., THREAD_* or MODEL_* actions).
20  * @param loc The location that this action acts upon
21  * @param value (optional) A value associated with the action (e.g., the value
22  * read or written). Defaults to a given macro constant, for debugging purposes.
23  * @param thread (optional) The Thread in which this action occurred. If NULL
24  * (default), then a Thread is assigned according to the scheduler.
25  */
26 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc,
27                 uint64_t value, Thread *thread) :
28         type(type),
29         order(order),
30         location(loc),
31         value(value),
32         reads_from(NULL),
33         reads_from_promise(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_yield() const
154 {
155         return type == THREAD_YIELD;
156 }
157
158 bool ModelAction::is_rmwr() const
159 {
160         return type == ATOMIC_RMWR;
161 }
162
163 bool ModelAction::is_rmw() const
164 {
165         return type == ATOMIC_RMW;
166 }
167
168 bool ModelAction::is_rmwc() const
169 {
170         return type == ATOMIC_RMWC;
171 }
172
173 bool ModelAction::is_fence() const
174 {
175         return type == ATOMIC_FENCE;
176 }
177
178 bool ModelAction::is_initialization() const
179 {
180         return type == ATOMIC_INIT;
181 }
182
183 bool ModelAction::is_relaxed() const
184 {
185         return order == std::memory_order_relaxed;
186 }
187
188 bool ModelAction::is_acquire() const
189 {
190         switch (order) {
191         case std::memory_order_acquire:
192         case std::memory_order_acq_rel:
193         case std::memory_order_seq_cst:
194                 return true;
195         default:
196                 return false;
197         }
198 }
199
200 bool ModelAction::is_release() const
201 {
202         switch (order) {
203         case std::memory_order_release:
204         case std::memory_order_acq_rel:
205         case std::memory_order_seq_cst:
206                 return true;
207         default:
208                 return false;
209         }
210 }
211
212 bool ModelAction::is_seqcst() const
213 {
214         return order == std::memory_order_seq_cst;
215 }
216
217 bool ModelAction::same_var(const ModelAction *act) const
218 {
219         if (act->is_wait() || is_wait()) {
220                 if (act->is_wait() && is_wait()) {
221                         if (((void *)value) == ((void *)act->value))
222                                 return true;
223                 } else if (is_wait()) {
224                         if (((void *)value) == act->location)
225                                 return true;
226                 } else if (act->is_wait()) {
227                         if (location == ((void *)act->value))
228                                 return true;
229                 }
230         }
231
232         return location == act->location;
233 }
234
235 bool ModelAction::same_thread(const ModelAction *act) const
236 {
237         return tid == act->tid;
238 }
239
240 void ModelAction::copy_typeandorder(ModelAction * act)
241 {
242         this->type = act->type;
243         this->order = act->order;
244 }
245
246 /**
247  * Get the Thread which is the operand of this action. This is only valid for
248  * THREAD_* operations (currently only for THREAD_CREATE and THREAD_JOIN). Note
249  * that this provides a central place for determining the conventions of Thread
250  * storage in ModelAction, where we generally aren't very type-safe (e.g., we
251  * store object references in a (void *) address.
252  *
253  * For THREAD_CREATE, this yields the Thread which is created.
254  * For THREAD_JOIN, this yields the Thread we are joining with.
255  *
256  * @return The Thread which this action acts on, if exists; otherwise NULL
257  */
258 Thread * ModelAction::get_thread_operand() const
259 {
260         if (type == THREAD_CREATE) {
261                 /* THREAD_CREATE stores its (Thread *) in a thrd_t::priv */
262                 thrd_t *thrd = (thrd_t *)get_location();
263                 return thrd->priv;
264         } else if (type == THREAD_JOIN)
265                 /* THREAD_JOIN uses (Thread *) for location */
266                 return (Thread *)get_location();
267         else
268                 return NULL;
269 }
270
271 /** This method changes an existing read part of an RMW action into either:
272  *  (1) a full RMW action in case of the completed write or
273  *  (2) a READ action in case a failed action.
274  * @todo  If the memory_order changes, we may potentially need to update our
275  * clock vector.
276  */
277 void ModelAction::process_rmw(ModelAction *act)
278 {
279         this->order = act->order;
280         if (act->is_rmwc())
281                 this->type = ATOMIC_READ;
282         else if (act->is_rmw()) {
283                 this->type = ATOMIC_RMW;
284                 this->value = act->value;
285         }
286 }
287
288 /** The is_synchronizing method should only explore interleavings if:
289  *  (1) the operations are seq_cst and don't commute or
290  *  (2) the reordering may establish or break a synchronization relation.
291  *  Other memory operations will be dealt with by using the reads_from
292  *  relation.
293  *
294  *  @param act is the action to consider exploring a reordering.
295  *  @return tells whether we have to explore a reordering.
296  */
297 bool ModelAction::could_synchronize_with(const ModelAction *act) const
298 {
299         // Same thread can't be reordered
300         if (same_thread(act))
301                 return false;
302
303         // Different locations commute
304         if (!same_var(act))
305                 return false;
306
307         // Explore interleavings of seqcst writes/fences to guarantee total
308         // order of seq_cst operations that don't commute
309         if ((could_be_write() || act->could_be_write() || is_fence() || act->is_fence()) && is_seqcst() && act->is_seqcst())
310                 return true;
311
312         // Explore synchronizing read/write pairs
313         if (is_acquire() && act->is_release() && is_read() && act->could_be_write())
314                 return true;
315
316         // lock just released...we can grab lock
317         if ((is_lock() || is_trylock()) && (act->is_unlock() || act->is_wait()))
318                 return true;
319
320         // lock just acquired...we can fail to grab lock
321         if (is_trylock() && act->is_success_lock())
322                 return true;
323
324         // other thread stalling on lock...we can release lock
325         if (is_unlock() && (act->is_trylock() || act->is_lock()))
326                 return true;
327
328         if (is_trylock() && (act->is_unlock() || act->is_wait()))
329                 return true;
330
331         if (is_notify() && act->is_wait())
332                 return true;
333
334         if (is_wait() && act->is_notify())
335                 return true;
336
337         // Otherwise handle by reads_from relation
338         return false;
339 }
340
341 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
342 {
343         // Must be different threads to reorder
344         if (same_thread(act))
345                 return false;
346
347         // Try to reorder a lock past a successful lock
348         if (act->is_success_lock())
349                 return true;
350
351         // Try to push a successful trylock past an unlock
352         if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
353                 return true;
354
355         // Try to push a successful trylock past a wait
356         if (act->is_wait() && is_trylock() && value == VALUE_TRYSUCCESS)
357                 return true;
358
359         return false;
360 }
361
362 /**
363  * Create a new clock vector for this action. Note that this function allows a
364  * user to clobber (and leak) a ModelAction's existing clock vector. A user
365  * should ensure that the vector has already either been rolled back
366  * (effectively "freed") or freed.
367  *
368  * @param parent A ModelAction from which to inherit a ClockVector
369  */
370 void ModelAction::create_cv(const ModelAction *parent)
371 {
372         if (parent)
373                 cv = new ClockVector(parent->cv, this);
374         else
375                 cv = new ClockVector(NULL, this);
376 }
377
378 void ModelAction::set_try_lock(bool obtainedlock) {
379         if (obtainedlock)
380                 value = VALUE_TRYSUCCESS;
381         else
382                 value = VALUE_TRYFAILED;
383 }
384
385 /**
386  * @brief Get the value read by this load
387  *
388  * We differentiate this function from ModelAction::get_write_value and
389  * ModelAction::get_value for the purpose of RMW's, which may have both a
390  * 'read' and a 'write' value.
391  *
392  * Note: 'this' must be a load.
393  *
394  * @return The value read by this load
395  */
396 uint64_t ModelAction::get_reads_from_value() const
397 {
398         ASSERT(is_read());
399         if (reads_from)
400                 return reads_from->get_write_value();
401         else if (reads_from_promise)
402                 return reads_from_promise->get_value();
403         return VALUE_NONE; /* Only for new actions with no reads-from */
404 }
405
406 /**
407  * @brief Get the value written by this store
408  *
409  * We differentiate this function from ModelAction::get_reads_from_value and
410  * ModelAction::get_value for the purpose of RMW's, which may have both a
411  * 'read' and a 'write' value.
412  *
413  * Note: 'this' must be a store.
414  *
415  * @return The value written by this store
416  */
417 uint64_t ModelAction::get_write_value() const
418 {
419         ASSERT(is_write());
420         return value;
421 }
422
423 /**
424  * @brief Get the value returned by this action
425  *
426  * For atomic reads (including RMW), an operation returns the value it read.
427  * For atomic writes, an operation returns the value it wrote. For other
428  * operations, the return value varies (sometimes is a "don't care"), but the
429  * value is simply stored in the "value" field.
430  *
431  * @return This action's return value
432  */
433 uint64_t ModelAction::get_return_value() const
434 {
435         if (is_read())
436                 return get_reads_from_value();
437         else if (is_write())
438                 return get_write_value();
439         else
440                 return value;
441 }
442
443 /** @return The Node associated with this ModelAction */
444 Node * ModelAction::get_node() const
445 {
446         /* UNINIT actions do not have a Node */
447         ASSERT(!is_uninitialized());
448         return node;
449 }
450
451 /**
452  * Update the model action's read_from action
453  * @param act The action to read from; should be a write
454  */
455 void ModelAction::set_read_from(const ModelAction *act)
456 {
457         ASSERT(act);
458         reads_from = act;
459         reads_from_promise = NULL;
460         if (act->is_uninitialized())
461                 model->assert_bug("May read from uninitialized atomic\n");
462 }
463
464 /**
465  * Set this action's read-from promise
466  * @param promise The promise to read from
467  */
468 void ModelAction::set_read_from_promise(Promise *promise)
469 {
470         ASSERT(is_read());
471         reads_from_promise = promise;
472         reads_from = NULL;
473 }
474
475 /**
476  * Synchronize the current thread with the thread corresponding to the
477  * ModelAction parameter.
478  * @param act The ModelAction to synchronize with
479  * @return True if this is a valid synchronization; false otherwise
480  */
481 bool ModelAction::synchronize_with(const ModelAction *act)
482 {
483         if (*this < *act && type != THREAD_JOIN && type != ATOMIC_LOCK)
484                 return false;
485         model->check_promises(act->get_tid(), cv, act->cv);
486         cv->merge(act->cv);
487         return true;
488 }
489
490 bool ModelAction::has_synchronized_with(const ModelAction *act) const
491 {
492         return cv->synchronized_since(act);
493 }
494
495 /**
496  * Check whether 'this' happens before act, according to the memory-model's
497  * happens before relation. This is checked via the ClockVector constructs.
498  * @return true if this action's thread has synchronized with act's thread
499  * since the execution of act, false otherwise.
500  */
501 bool ModelAction::happens_before(const ModelAction *act) const
502 {
503         return act->cv->synchronized_since(this);
504 }
505
506 /** @brief Print nicely-formatted info about this ModelAction */
507 void ModelAction::print() const
508 {
509         const char *type_str, *mo_str;
510         switch (this->type) {
511         case MODEL_FIXUP_RELSEQ:
512                 type_str = "relseq fixup";
513                 break;
514         case THREAD_CREATE:
515                 type_str = "thread create";
516                 break;
517         case THREAD_START:
518                 type_str = "thread start";
519                 break;
520         case THREAD_YIELD:
521                 type_str = "thread yield";
522                 break;
523         case THREAD_JOIN:
524                 type_str = "thread join";
525                 break;
526         case THREAD_FINISH:
527                 type_str = "thread finish";
528                 break;
529         case ATOMIC_UNINIT:
530                 type_str = "uninitialized";
531                 break;
532         case ATOMIC_READ:
533                 type_str = "atomic read";
534                 break;
535         case ATOMIC_WRITE:
536                 type_str = "atomic write";
537                 break;
538         case ATOMIC_RMW:
539                 type_str = "atomic rmw";
540                 break;
541         case ATOMIC_FENCE:
542                 type_str = "fence";
543                 break;
544         case ATOMIC_RMWR:
545                 type_str = "atomic rmwr";
546                 break;
547         case ATOMIC_RMWC:
548                 type_str = "atomic rmwc";
549                 break;
550         case ATOMIC_INIT:
551                 type_str = "init atomic";
552                 break;
553         case ATOMIC_LOCK:
554                 type_str = "lock";
555                 break;
556         case ATOMIC_UNLOCK:
557                 type_str = "unlock";
558                 break;
559         case ATOMIC_TRYLOCK:
560                 type_str = "trylock";
561                 break;
562         case ATOMIC_WAIT:
563                 type_str = "wait";
564                 break;
565         case ATOMIC_NOTIFY_ONE:
566                 type_str = "notify one";
567                 break;
568         case ATOMIC_NOTIFY_ALL:
569                 type_str = "notify all";
570                 break;
571         default:
572                 type_str = "unknown type";
573         }
574
575         switch (this->order) {
576         case std::memory_order_relaxed:
577                 mo_str = "relaxed";
578                 break;
579         case std::memory_order_acquire:
580                 mo_str = "acquire";
581                 break;
582         case std::memory_order_release:
583                 mo_str = "release";
584                 break;
585         case std::memory_order_acq_rel:
586                 mo_str = "acq_rel";
587                 break;
588         case std::memory_order_seq_cst:
589                 mo_str = "seq_cst";
590                 break;
591         default:
592                 mo_str = "unknown";
593                 break;
594         }
595
596         model_print("(%4d) Thread: %-2d   Action: %-13s   MO: %7s  Loc: %14p   Value: %-#18" PRIx64,
597                         seq_number, id_to_int(tid), type_str, mo_str, location, get_return_value());
598         if (is_read()) {
599                 if (reads_from)
600                         model_print("  Rf: %-3d", reads_from->get_seq_number());
601                 else if (reads_from_promise) {
602                         int idx = model->get_promise_number(reads_from_promise);
603                         if (idx >= 0)
604                                 model_print("  Rf: P%-2d", idx);
605                         else
606                                 model_print("  RF: P? ");
607                 } else
608                         model_print("  Rf: ?  ");
609         }
610         if (cv) {
611                 if (is_read())
612                         model_print(" ");
613                 else
614                         model_print("          ");
615                 cv->print();
616         } else
617                 model_print("\n");
618 }
619
620 /** @brief Get a (likely) unique hash for this ModelAction */
621 unsigned int ModelAction::hash() const
622 {
623         unsigned int hash = (unsigned int)this->type;
624         hash ^= ((unsigned int)this->order) << 3;
625         hash ^= seq_number << 5;
626         hash ^= id_to_int(tid) << 6;
627
628         if (is_read() && reads_from)
629                 hash ^= reads_from->get_seq_number();
630         return hash;
631 }
632
633 /**
634  * @brief Checks the NodeStack to see if a ModelAction is in our may-read-from set
635  * @param write The ModelAction to check for
636  * @return True if the ModelAction is found; false otherwise
637  */
638 bool ModelAction::may_read_from(const ModelAction *write) const
639 {
640         for (int i = 0; i < node->get_read_from_past_size(); i++)
641                 if (node->get_read_from_past(i) == write)
642                         return true;
643         return false;
644 }
645
646 /**
647  * @brief Checks the NodeStack to see if a Promise is in our may-read-from set
648  * @param promise The Promise to check for
649  * @return True if the Promise is found; false otherwise
650  */
651 bool ModelAction::may_read_from(const Promise *promise) const
652 {
653         for (int i = 0; i < node->get_read_from_promise_size(); i++)
654                 if (node->get_read_from_promise(i) == promise)
655                         return true;
656         return false;
657 }
658
659 /**
660  * Only valid for LOCK, TRY_LOCK, UNLOCK, and WAIT operations.
661  * @return The mutex operated on by this action, if any; otherwise NULL
662  */
663 std::mutex * ModelAction::get_mutex() const
664 {
665         if (is_trylock() || is_lock() || is_unlock())
666                 return (std::mutex *)get_location();
667         else if (is_wait())
668                 return (std::mutex *)get_value();
669         else
670                 return NULL;
671 }