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