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