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_RMW;
34 bool ModelAction::is_write() const
36 return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT;
39 bool ModelAction::is_rmw() const
41 return type == ATOMIC_RMW;
44 bool ModelAction::is_initialization() const
46 return type == ATOMIC_INIT;
49 bool ModelAction::is_acquire() const
52 case std::memory_order_acquire:
53 case std::memory_order_acq_rel:
54 case std::memory_order_seq_cst:
61 bool ModelAction::is_release() const
64 case std::memory_order_release:
65 case std::memory_order_acq_rel:
66 case std::memory_order_seq_cst:
73 bool ModelAction::is_seqcst() const
75 return order==std::memory_order_seq_cst;
78 bool ModelAction::same_var(const ModelAction *act) const
80 return location == act->location;
83 bool ModelAction::same_thread(const ModelAction *act) const
85 return tid == act->tid;
88 void ModelAction::upgrade_rmw(ModelAction * act) {
90 ASSERT(act->is_rmw());
91 //Upgrade our type to the act's type
93 this->order=act->order;
94 this->value=act->value;
97 /** The is_synchronizing method should only explore interleavings if:
98 * (1) the operations are seq_cst and don't commute or
99 * (2) the reordering may establish or break a synchronization relation.
100 * Other memory operations will be dealt with by using the reads_from
103 * @param act is the action to consider exploring a reordering.
104 * @return tells whether we have to explore a reordering.
107 bool ModelAction::is_synchronizing(const ModelAction *act) const
109 //Same thread can't be reordered
110 if (same_thread(act))
113 // Different locations commute
117 // Explore interleavings of seqcst writes to guarantee total order
118 // of seq_cst operations that don't commute
119 if (is_write() && is_seqcst() && act->is_write() && act->is_seqcst())
122 // Explore synchronizing read/write pairs
123 if (is_read() && is_acquire() && act->is_write() && act->is_release())
125 if (is_write() && is_release() && act->is_read() && act->is_acquire())
128 // Otherwise handle by reads_from relation
132 void ModelAction::create_cv(const ModelAction *parent)
137 cv = new ClockVector(parent->cv, this);
139 cv = new ClockVector(NULL, this);
142 void ModelAction::read_from(const ModelAction *act)
145 if (act->is_release() && this->is_acquire())
151 * Check whether 'this' happens before act, according to the memory-model's
152 * happens before relation. This is checked via the ClockVector constructs.
153 * @return true if this action's thread has synchronized with act's thread
154 * since the execution of act, false otherwise.
156 bool ModelAction::happens_before(const ModelAction *act) const
158 return act->cv->synchronized_since(this);
161 void ModelAction::print(void) const
163 const char *type_str;
164 switch (this->type) {
166 type_str = "thread create";
169 type_str = "thread start";
172 type_str = "thread yield";
175 type_str = "thread join";
178 type_str = "atomic read";
181 type_str = "atomic write";
184 type_str = "atomic rmw";
187 type_str = "init atomic";
190 type_str = "unknown type";
193 uint64_t valuetoprint=type==ATOMIC_READ?reads_from->value:value;
195 printf("(%3d) Thread: %-2d Action: %-13s MO: %d Loc: %14p Value: %-12" PRIu64,
196 seq_number, id_to_int(tid), type_str, order, location, valuetoprint);
198 printf(" Rf: %d", reads_from->get_seq_number());