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