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