2 #define __STDC_FORMAT_MACROS
8 #include "clockvector.h"
11 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, uint64_t value) :
19 Thread *t = thread_current();
20 this->tid = t->get_id();
21 this->seq_number = model->get_next_seq_num();
24 ModelAction::~ModelAction()
30 bool ModelAction::is_read() const
32 return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMW;
35 bool ModelAction::is_write() const
37 return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT;
40 bool ModelAction::is_rmwr() const
42 return type == ATOMIC_RMWR;
45 bool ModelAction::is_rmw() const
47 return type == ATOMIC_RMW;
50 bool ModelAction::is_rmwc() const
52 return type == ATOMIC_RMWC;
55 bool ModelAction::is_fence() const
57 return type == ATOMIC_FENCE;
60 bool ModelAction::is_initialization() const
62 return type == ATOMIC_INIT;
65 bool ModelAction::is_acquire() const
68 case std::memory_order_acquire:
69 case std::memory_order_acq_rel:
70 case std::memory_order_seq_cst:
77 bool ModelAction::is_release() const
80 case std::memory_order_release:
81 case std::memory_order_acq_rel:
82 case std::memory_order_seq_cst:
89 bool ModelAction::is_seqcst() const
91 return order==std::memory_order_seq_cst;
94 bool ModelAction::same_var(const ModelAction *act) const
96 return location == act->location;
99 bool ModelAction::same_thread(const ModelAction *act) const
101 return tid == act->tid;
104 void ModelAction::copy_typeandorder(ModelAction * act) {
105 this->type=act->type;
106 this->order=act->order;
109 /** This method changes an existing read part of an RMW action into either:
110 * (1) a full RMW action in case of the completed write or
111 * (2) a READ action in case a failed action.
112 * @todo If the memory_order changes, we may potentially need to update our
115 void ModelAction::process_rmw(ModelAction * act) {
116 this->order=act->order;
118 this->type=ATOMIC_READ;
119 else if (act->is_rmw()) {
120 this->type=ATOMIC_RMW;
121 this->value=act->value;
125 /** The is_synchronizing method should only explore interleavings if:
126 * (1) the operations are seq_cst and don't commute or
127 * (2) the reordering may establish or break a synchronization relation.
128 * Other memory operations will be dealt with by using the reads_from
131 * @param act is the action to consider exploring a reordering.
132 * @return tells whether we have to explore a reordering.
134 bool ModelAction::is_synchronizing(const ModelAction *act) const
136 //Same thread can't be reordered
137 if (same_thread(act))
140 // Different locations commute
144 // Explore interleavings of seqcst writes to guarantee total order
145 // of seq_cst operations that don't commute
146 if (is_write() && is_seqcst() && act->is_write() && act->is_seqcst())
149 // Explore synchronizing read/write pairs
150 if (is_read() && is_acquire() && act->is_write() && act->is_release())
152 if (is_write() && is_release() && act->is_read() && act->is_acquire())
155 // Otherwise handle by reads_from relation
159 void ModelAction::create_cv(const ModelAction *parent)
165 cv = new ClockVector(parent->cv, this);
167 cv = new ClockVector(NULL, this);
170 /** Update the model action's read_from action */
171 void ModelAction::read_from(const ModelAction *act)
175 if (act != NULL && this->is_acquire()) {
176 std::vector< const ModelAction *, MyAlloc<const ModelAction *> > release_heads;
177 model->get_release_seq_heads(this, &release_heads);
178 for (unsigned int i = 0; i < release_heads.size(); i++)
179 synchronize_with(release_heads[i]);
184 * Synchronize the current thread with the thread corresponding to the
185 * ModelAction parameter.
186 * @param act The ModelAction to synchronize with
188 void ModelAction::synchronize_with(const ModelAction *act) {
189 ASSERT(*act < *this || type == THREAD_JOIN);
190 model->check_promises(cv, act->cv);
194 bool ModelAction::has_synchronized_with(const ModelAction *act) const
196 return cv->has_synchronized_with(act->cv);
200 * Check whether 'this' happens before act, according to the memory-model's
201 * happens before relation. This is checked via the ClockVector constructs.
202 * @return true if this action's thread has synchronized with act's thread
203 * since the execution of act, false otherwise.
205 bool ModelAction::happens_before(const ModelAction *act) const
207 return act->cv->synchronized_since(this);
210 void ModelAction::print(void) const
212 const char *type_str, *mo_str;
213 switch (this->type) {
215 type_str = "thread create";
218 type_str = "thread start";
221 type_str = "thread yield";
224 type_str = "thread join";
227 type_str = "thread finish";
230 type_str = "atomic read";
233 type_str = "atomic write";
236 type_str = "atomic rmw";
242 type_str = "atomic rmwr";
245 type_str = "atomic rmwc";
248 type_str = "init atomic";
251 type_str = "unknown type";
254 uint64_t valuetoprint=type==ATOMIC_READ?(reads_from!=NULL?reads_from->value:VALUE_NONE):value;
256 switch (this->order) {
257 case std::memory_order_relaxed:
260 case std::memory_order_acquire:
263 case std::memory_order_release:
266 case std::memory_order_acq_rel:
269 case std::memory_order_seq_cst:
277 printf("(%3d) Thread: %-2d Action: %-13s MO: %7s Loc: %14p Value: %-12" PRIu64,
278 seq_number, id_to_int(tid), type_str, mo_str, location, valuetoprint);
281 printf(" Rf: %d", reads_from->get_seq_number());