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