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