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::could_synchronize_with(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() || act->is_write()) && is_seqcst() && 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
206         // Otherwise handle by reads_from relation
207         return false;
208 }
209
210 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
211 {
212         //Must be different threads to reorder
213         if (same_thread(act))
214                 return false;
215         
216         //Try to reorder a lock past a successful lock
217         if (act->is_success_lock())
218                 return true;
219         
220         //Try to push a successful trylock past an unlock
221         if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
222                 return true;
223
224         return false;
225 }
226
227 /**
228  * Create a new clock vector for this action. Note that this function allows a
229  * user to clobber (and leak) a ModelAction's existing clock vector. A user
230  * should ensure that the vector has already either been rolled back
231  * (effectively "freed") or freed.
232  *
233  * @param parent A ModelAction from which to inherit a ClockVector
234  */
235 void ModelAction::create_cv(const ModelAction *parent)
236 {
237         if (parent)
238                 cv = new ClockVector(parent->cv, this);
239         else
240                 cv = new ClockVector(NULL, this);
241 }
242
243 void ModelAction::set_try_lock(bool obtainedlock) {
244         if (obtainedlock)
245                 value=VALUE_TRYSUCCESS;
246         else
247                 value=VALUE_TRYFAILED;
248 }
249
250 /**
251  * Update the model action's read_from action
252  * @param act The action to read from; should be a write
253  * @return True if this read established synchronization
254  */
255 bool ModelAction::read_from(const ModelAction *act)
256 {
257         ASSERT(cv);
258         reads_from = act;
259         if (act != NULL && this->is_acquire()) {
260                 rel_heads_list_t release_heads;
261                 model->get_release_seq_heads(this, &release_heads);
262                 int num_heads = release_heads.size();
263                 for (unsigned int i = 0; i < release_heads.size(); i++)
264                         if (!synchronize_with(release_heads[i])) {
265                                 model->set_bad_synchronization();
266                                 num_heads--;
267                         }
268                 return num_heads > 0;
269         }
270         return false;
271 }
272
273 /**
274  * Synchronize the current thread with the thread corresponding to the
275  * ModelAction parameter.
276  * @param act The ModelAction to synchronize with
277  * @return True if this is a valid synchronization; false otherwise
278  */
279 bool ModelAction::synchronize_with(const ModelAction *act) {
280         if (*this < *act && type != THREAD_JOIN && type != ATOMIC_LOCK)
281                 return false;
282         model->check_promises(act->get_tid(), cv, act->cv);
283         cv->merge(act->cv);
284         return true;
285 }
286
287 bool ModelAction::has_synchronized_with(const ModelAction *act) const
288 {
289         return cv->has_synchronized_with(act->cv);
290 }
291
292 /**
293  * Check whether 'this' happens before act, according to the memory-model's
294  * happens before relation. This is checked via the ClockVector constructs.
295  * @return true if this action's thread has synchronized with act's thread
296  * since the execution of act, false otherwise.
297  */
298 bool ModelAction::happens_before(const ModelAction *act) const
299 {
300         return act->cv->synchronized_since(this);
301 }
302
303 /** @brief Print nicely-formatted info about this ModelAction */
304 void ModelAction::print() const
305 {
306         const char *type_str, *mo_str;
307         switch (this->type) {
308         case THREAD_CREATE:
309                 type_str = "thread create";
310                 break;
311         case THREAD_START:
312                 type_str = "thread start";
313                 break;
314         case THREAD_YIELD:
315                 type_str = "thread yield";
316                 break;
317         case THREAD_JOIN:
318                 type_str = "thread join";
319                 break;
320         case THREAD_FINISH:
321                 type_str = "thread finish";
322                 break;
323         case ATOMIC_READ:
324                 type_str = "atomic read";
325                 break;
326         case ATOMIC_WRITE:
327                 type_str = "atomic write";
328                 break;
329         case ATOMIC_RMW:
330                 type_str = "atomic rmw";
331                 break;
332         case ATOMIC_FENCE:
333                 type_str = "fence";
334                 break;
335         case ATOMIC_RMWR:
336                 type_str = "atomic rmwr";
337                 break;
338         case ATOMIC_RMWC:
339                 type_str = "atomic rmwc";
340                 break;
341         case ATOMIC_INIT:
342                 type_str = "init atomic";
343                 break;
344         case ATOMIC_LOCK:
345                 type_str = "lock";
346                 break;
347         case ATOMIC_UNLOCK:
348                 type_str = "unlock";
349                 break;
350         case ATOMIC_TRYLOCK:
351                 type_str = "trylock";
352                 break;
353         default:
354                 type_str = "unknown type";
355         }
356
357         uint64_t valuetoprint=type==ATOMIC_READ?(reads_from!=NULL?reads_from->value:VALUE_NONE):value;
358
359         switch (this->order) {
360         case std::memory_order_relaxed:
361                 mo_str = "relaxed";
362                 break;
363         case std::memory_order_acquire:
364                 mo_str = "acquire";
365                 break;
366         case std::memory_order_release:
367                 mo_str = "release";
368                 break;
369         case std::memory_order_acq_rel:
370                 mo_str = "acq_rel";
371                 break;
372         case std::memory_order_seq_cst:
373                 mo_str = "seq_cst";
374                 break;
375         default:
376                 mo_str = "unknown";
377                 break;
378         }
379
380         printf("(%3d) Thread: %-2d   Action: %-13s   MO: %7s  Loc: %14p  Value: %-12" PRIu64,
381                         seq_number, id_to_int(tid), type_str, mo_str, location, valuetoprint);
382         if (is_read()) {
383                 if (reads_from)
384                         printf(" Rf: %d", reads_from->get_seq_number());
385                 else
386                         printf(" Rf: ?");
387         }
388         if (cv) {
389                 printf("\t");
390                 cv->print();
391         } else
392                 printf("\n");
393 }