model/action: move complicated read_from logic into model.cc
[model-checker.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  */
341 void ModelAction::set_read_from(const ModelAction *act)
342 {
343         reads_from = act;
344 }
345
346 /**
347  * Synchronize the current thread with the thread corresponding to the
348  * ModelAction parameter.
349  * @param act The ModelAction to synchronize with
350  * @return True if this is a valid synchronization; false otherwise
351  */
352 bool ModelAction::synchronize_with(const ModelAction *act) {
353         if (*this < *act && type != THREAD_JOIN && type != ATOMIC_LOCK)
354                 return false;
355         model->check_promises(act->get_tid(), cv, act->cv);
356         cv->merge(act->cv);
357         return true;
358 }
359
360 bool ModelAction::has_synchronized_with(const ModelAction *act) const
361 {
362         return cv->synchronized_since(act);
363 }
364
365 /**
366  * Check whether 'this' happens before act, according to the memory-model's
367  * happens before relation. This is checked via the ClockVector constructs.
368  * @return true if this action's thread has synchronized with act's thread
369  * since the execution of act, false otherwise.
370  */
371 bool ModelAction::happens_before(const ModelAction *act) const
372 {
373         return act->cv->synchronized_since(this);
374 }
375
376 /** @brief Print nicely-formatted info about this ModelAction */
377 void ModelAction::print() const
378 {
379         const char *type_str, *mo_str;
380         switch (this->type) {
381         case MODEL_FIXUP_RELSEQ:
382                 type_str = "relseq fixup";
383                 break;
384         case THREAD_CREATE:
385                 type_str = "thread create";
386                 break;
387         case THREAD_START:
388                 type_str = "thread start";
389                 break;
390         case THREAD_YIELD:
391                 type_str = "thread yield";
392                 break;
393         case THREAD_JOIN:
394                 type_str = "thread join";
395                 break;
396         case THREAD_FINISH:
397                 type_str = "thread finish";
398                 break;
399         case ATOMIC_READ:
400                 type_str = "atomic read";
401                 break;
402         case ATOMIC_WRITE:
403                 type_str = "atomic write";
404                 break;
405         case ATOMIC_RMW:
406                 type_str = "atomic rmw";
407                 break;
408         case ATOMIC_FENCE:
409                 type_str = "fence";
410                 break;
411         case ATOMIC_RMWR:
412                 type_str = "atomic rmwr";
413                 break;
414         case ATOMIC_RMWC:
415                 type_str = "atomic rmwc";
416                 break;
417         case ATOMIC_INIT:
418                 type_str = "init atomic";
419                 break;
420         case ATOMIC_LOCK:
421                 type_str = "lock";
422                 break;
423         case ATOMIC_UNLOCK:
424                 type_str = "unlock";
425                 break;
426         case ATOMIC_TRYLOCK:
427                 type_str = "trylock";
428                 break;
429         case ATOMIC_WAIT:
430                 type_str = "wait";
431                 break;
432         case ATOMIC_NOTIFY_ONE:
433                 type_str = "notify one";
434                 break;
435         case ATOMIC_NOTIFY_ALL:
436                 type_str = "notify all";
437                 break;
438         default:
439                 type_str = "unknown type";
440         }
441
442         uint64_t valuetoprint=type==ATOMIC_READ?(reads_from!=NULL?reads_from->value:VALUE_NONE):value;
443
444         switch (this->order) {
445         case std::memory_order_relaxed:
446                 mo_str = "relaxed";
447                 break;
448         case std::memory_order_acquire:
449                 mo_str = "acquire";
450                 break;
451         case std::memory_order_release:
452                 mo_str = "release";
453                 break;
454         case std::memory_order_acq_rel:
455                 mo_str = "acq_rel";
456                 break;
457         case std::memory_order_seq_cst:
458                 mo_str = "seq_cst";
459                 break;
460         default:
461                 mo_str = "unknown";
462                 break;
463         }
464
465         model_print("(%4d) Thread: %-2d   Action: %-13s   MO: %7s  Loc: %14p   Value: %-#18" PRIx64,
466                         seq_number, id_to_int(tid), type_str, mo_str, location, valuetoprint);
467         if (is_read()) {
468                 if (reads_from)
469                         model_print("  Rf: %-3d", reads_from->get_seq_number());
470                 else
471                         model_print("  Rf: ?  ");
472         }
473         if (cv) {
474                 if (is_read())
475                         model_print(" ");
476                 else
477                         model_print("          ");
478                 cv->print();
479         } else
480                 model_print("\n");
481 }
482
483 /** @brief Print nicely-formatted info about this ModelAction */
484 unsigned int ModelAction::hash() const
485 {
486         unsigned int hash=(unsigned int) this->type;
487         hash^=((unsigned int)this->order)<<3;
488         hash^=seq_number<<5;
489         hash ^= id_to_int(tid) << 6;
490
491         if (is_read()) {
492                 if (reads_from)
493                         hash^=reads_from->get_seq_number();
494         }
495         return hash;
496 }