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