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;
34 bool ModelAction::is_write() const
36 return type == ATOMIC_WRITE || 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 memory_order_acquire:
53 case memory_order_acq_rel:
54 case memory_order_seq_cst:
61 bool ModelAction::is_release() const
64 case memory_order_release:
65 case memory_order_acq_rel:
66 case memory_order_seq_cst:
73 bool ModelAction::is_seqcst() const
75 return order==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 /** The is_synchronizing method should only explore interleavings if:
89 * (1) the operations are seq_cst and don't commute or
90 * (2) the reordering may establish or break a synchronization relation.
91 * Other memory operations will be dealt with by using the reads_from
94 * @param act is the action to consider exploring a reordering.
95 * @return tells whether we have to explore a reordering.
98 bool ModelAction::is_synchronizing(const ModelAction *act) const
100 //Same thread can't be reordered
101 if (same_thread(act))
104 // Different locations commute
108 // Explore interleavings of seqcst writes to guarantee total order
109 // of seq_cst operations that don't commute
110 if (is_write() && is_seqcst() && act->is_write() && act->is_seqcst())
113 // Explore synchronizing read/write pairs
114 if (is_read() && is_acquire() && act->is_write() && act->is_release())
116 if (is_write() && is_release() && act->is_read() && act->is_acquire())
119 // Otherwise handle by reads_from relation
123 void ModelAction::create_cv(const ModelAction *parent)
128 cv = new ClockVector(parent->cv, this);
130 cv = new ClockVector(NULL, this);
133 void ModelAction::read_from(const ModelAction *act)
136 if (act->is_release() && this->is_acquire())
143 * Check whether 'this' happens before act, according to the memory-model's
144 * happens before relation. This is checked via the ClockVector constructs.
145 * @return true if this action's thread has synchronized with act's thread
146 * since the execution of act, false otherwise.
148 bool ModelAction::happens_before(const ModelAction *act) const
150 return act->cv->synchronized_since(this);
153 void ModelAction::print(void) const
155 const char *type_str;
156 switch (this->type) {
158 type_str = "thread create";
161 type_str = "thread start";
164 type_str = "thread yield";
167 type_str = "thread join";
170 type_str = "atomic read";
173 type_str = "atomic write";
176 type_str = "atomic rmw";
179 type_str = "init atomic";
182 type_str = "unknown type";
185 printf("(%3d) Thread: %-2d Action: %-13s MO: %d Loc: %14p Value: %-12" PRIu64,
186 seq_number, id_to_int(tid), type_str, order, location, value);
188 printf(" Rf: %d", reads_from->get_seq_number());