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