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