f0ad48feaf731d808f69212b5926170e4ce96905
[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  */
107
108 //TODO:  If the memory_order changes, we may potentially need to update our
109 //clock vector.
110
111 void ModelAction::process_rmw(ModelAction * act) {
112         this->order=act->order;
113         if (act->is_rmwc())
114                 this->type=ATOMIC_READ;
115         else if (act->is_rmw()) {
116                 this->type=ATOMIC_RMW;
117                 this->value=act->value;
118         }
119 }
120
121 /** The is_synchronizing method should only explore interleavings if:
122  *  (1) the operations are seq_cst and don't commute or
123  *  (2) the reordering may establish or break a synchronization relation.
124  *  Other memory operations will be dealt with by using the reads_from
125  *  relation.
126  *
127  *  @param act is the action to consider exploring a reordering.
128  *  @return tells whether we have to explore a reordering.
129  */
130
131 bool ModelAction::is_synchronizing(const ModelAction *act) const
132 {
133         //Same thread can't be reordered
134         if (same_thread(act))
135                 return false;
136
137         // Different locations commute
138         if (!same_var(act))
139                 return false;
140
141         // Explore interleavings of seqcst writes to guarantee total order
142         // of seq_cst operations that don't commute
143         if (is_write() && is_seqcst() && act->is_write() && act->is_seqcst())
144                 return true;
145
146         // Explore synchronizing read/write pairs
147         if (is_read() && is_acquire() && act->is_write() && act->is_release())
148                 return true;
149         if (is_write() && is_release() && act->is_read() && act->is_acquire())
150                 return true;
151
152         // Otherwise handle by reads_from relation
153         return false;
154 }
155
156 void ModelAction::create_cv(const ModelAction *parent)
157 {
158         ASSERT(cv == NULL);
159
160         if (parent)
161                 cv = new ClockVector(parent->cv, this);
162         else
163                 cv = new ClockVector(NULL, this);
164 }
165
166
167 /** Update the model action's read_from action */
168 void ModelAction::read_from(const ModelAction *act)
169 {
170         ASSERT(cv);
171         if (act->is_release() && this->is_acquire())
172                 cv->merge(act->cv);
173         reads_from = act;
174 }
175
176 /**
177  * Check whether 'this' happens before act, according to the memory-model's
178  * happens before relation. This is checked via the ClockVector constructs.
179  * @return true if this action's thread has synchronized with act's thread
180  * since the execution of act, false otherwise.
181  */
182 bool ModelAction::happens_before(const ModelAction *act) const
183 {
184         return act->cv->synchronized_since(this);
185 }
186
187 void ModelAction::print(void) const
188 {
189         const char *type_str;
190         switch (this->type) {
191         case THREAD_CREATE:
192                 type_str = "thread create";
193                 break;
194         case THREAD_START:
195                 type_str = "thread start";
196                 break;
197         case THREAD_YIELD:
198                 type_str = "thread yield";
199                 break;
200         case THREAD_JOIN:
201                 type_str = "thread join";
202                 break;
203         case ATOMIC_READ:
204                 type_str = "atomic read";
205                 break;
206         case ATOMIC_WRITE:
207                 type_str = "atomic write";
208                 break;
209         case ATOMIC_RMW:
210                 type_str = "atomic rmw";
211                 break;
212         case ATOMIC_RMWR:
213                 type_str = "atomic rmwr";
214                 break;
215         case ATOMIC_RMWC:
216                 type_str = "atomic rmwc";
217                 break;
218         case ATOMIC_INIT:
219                 type_str = "init atomic";
220                 break;
221         default:
222                 type_str = "unknown type";
223         }
224
225         uint64_t valuetoprint=type==ATOMIC_READ?(reads_from!=NULL?reads_from->value:VALUE_NONE):value;
226
227         printf("(%3d) Thread: %-2d    Action: %-13s    MO: %d    Loc: %14p    Value: %-12" PRIu64,
228                         seq_number, id_to_int(tid), type_str, order, location, valuetoprint);
229         if (reads_from)
230                 printf(" Rf: %d", reads_from->get_seq_number());
231         if (cv) {
232                 printf("\t");
233                 cv->print();
234         } else
235                 printf("\n");
236 }