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