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