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