2 #define __STDC_FORMAT_MACROS
7 #include "clockvector.h"
10 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, uint64_t value) :
18 Thread *t = thread_current();
19 this->tid = t->get_id();
20 this->seq_number = model->get_next_seq_num();
23 ModelAction::~ModelAction()
29 bool ModelAction::is_read() const
31 return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMW;
34 bool ModelAction::is_write() const
36 return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT;
39 bool ModelAction::is_rmwr() const
41 return type == ATOMIC_RMWR;
44 bool ModelAction::is_rmw() const
46 return type == ATOMIC_RMW;
49 bool ModelAction::is_rmwc() const
51 return type == ATOMIC_RMWC;
54 bool ModelAction::is_initialization() const
56 return type == ATOMIC_INIT;
59 bool ModelAction::is_acquire() const
62 case std::memory_order_acquire:
63 case std::memory_order_acq_rel:
64 case std::memory_order_seq_cst:
71 bool ModelAction::is_release() const
74 case std::memory_order_release:
75 case std::memory_order_acq_rel:
76 case std::memory_order_seq_cst:
83 bool ModelAction::is_seqcst() const
85 return order==std::memory_order_seq_cst;
88 bool ModelAction::same_var(const ModelAction *act) const
90 return location == act->location;
93 bool ModelAction::same_thread(const ModelAction *act) const
95 return tid == act->tid;
98 void ModelAction::copy_typeandorder(ModelAction * act) {
100 this->order=act->order;
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.
108 //TODO: If the memory_order changes, we may potentially need to update our
111 void ModelAction::process_rmw(ModelAction * act) {
112 this->order=act->order;
114 this->type=ATOMIC_READ;
115 else if (act->is_rmw()) {
116 this->type=ATOMIC_RMW;
117 this->value=act->value;
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
127 * @param act is the action to consider exploring a reordering.
128 * @return tells whether we have to explore a reordering.
131 bool ModelAction::is_synchronizing(const ModelAction *act) const
133 //Same thread can't be reordered
134 if (same_thread(act))
137 // Different locations commute
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())
146 // Explore synchronizing read/write pairs
147 if (is_read() && is_acquire() && act->is_write() && act->is_release())
149 if (is_write() && is_release() && act->is_read() && act->is_acquire())
152 // Otherwise handle by reads_from relation
156 void ModelAction::create_cv(const ModelAction *parent)
161 cv = new ClockVector(parent->cv, this);
163 cv = new ClockVector(NULL, this);
167 /** Update the model action's read_from action */
168 void ModelAction::read_from(const ModelAction *act)
171 if (act->is_release() && this->is_acquire())
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.
182 bool ModelAction::happens_before(const ModelAction *act) const
184 return act->cv->synchronized_since(this);
187 void ModelAction::print(void) const
189 const char *type_str;
190 switch (this->type) {
192 type_str = "thread create";
195 type_str = "thread start";
198 type_str = "thread yield";
201 type_str = "thread join";
204 type_str = "atomic read";
207 type_str = "atomic write";
210 type_str = "atomic rmw";
213 type_str = "atomic rmwr";
216 type_str = "atomic rmwc";
219 type_str = "init atomic";
222 type_str = "unknown type";
225 uint64_t valuetoprint=type==ATOMIC_READ?(reads_from!=NULL?reads_from->value:VALUE_NONE):value;
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);
230 printf(" Rf: %d", reads_from->get_seq_number());