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