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