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_mutex_op() const {
31 return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK;
34 bool ModelAction::is_lock() const {
35 return type == ATOMIC_LOCK;
38 bool ModelAction::is_unlock() const {
39 return type == ATOMIC_UNLOCK;
42 bool ModelAction::is_trylock() const {
43 return type == ATOMIC_TRYLOCK;
46 bool ModelAction::is_success_lock() const {
47 return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
50 bool ModelAction::is_failed_trylock() const {
51 return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
54 bool ModelAction::is_read() const
56 return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMW;
59 bool ModelAction::is_write() const
61 return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT;
64 bool ModelAction::is_rmwr() const
66 return type == ATOMIC_RMWR;
69 bool ModelAction::is_rmw() const
71 return type == ATOMIC_RMW;
74 bool ModelAction::is_rmwc() const
76 return type == ATOMIC_RMWC;
79 bool ModelAction::is_fence() const
81 return type == ATOMIC_FENCE;
84 bool ModelAction::is_initialization() const
86 return type == ATOMIC_INIT;
89 bool ModelAction::is_acquire() const
92 case std::memory_order_acquire:
93 case std::memory_order_acq_rel:
94 case std::memory_order_seq_cst:
101 bool ModelAction::is_release() const
104 case std::memory_order_release:
105 case std::memory_order_acq_rel:
106 case std::memory_order_seq_cst:
113 bool ModelAction::is_seqcst() const
115 return order==std::memory_order_seq_cst;
118 bool ModelAction::same_var(const ModelAction *act) const
120 return location == act->location;
123 bool ModelAction::same_thread(const ModelAction *act) const
125 return tid == act->tid;
128 void ModelAction::copy_typeandorder(ModelAction * act) {
129 this->type=act->type;
130 this->order=act->order;
133 /** This method changes an existing read part of an RMW action into either:
134 * (1) a full RMW action in case of the completed write or
135 * (2) a READ action in case a failed action.
136 * @todo If the memory_order changes, we may potentially need to update our
139 void ModelAction::process_rmw(ModelAction * act) {
140 this->order=act->order;
142 this->type=ATOMIC_READ;
143 else if (act->is_rmw()) {
144 this->type=ATOMIC_RMW;
145 this->value=act->value;
149 /** The is_synchronizing method should only explore interleavings if:
150 * (1) the operations are seq_cst and don't commute or
151 * (2) the reordering may establish or break a synchronization relation.
152 * Other memory operations will be dealt with by using the reads_from
155 * @param act is the action to consider exploring a reordering.
156 * @return tells whether we have to explore a reordering.
158 bool ModelAction::is_synchronizing(const ModelAction *act) const
160 //Same thread can't be reordered
161 if (same_thread(act))
164 // Different locations commute
168 // Explore interleavings of seqcst writes to guarantee total order
169 // of seq_cst operations that don't commute
170 if (is_write() && is_seqcst() && act->is_write() && act->is_seqcst())
173 // Explore synchronizing read/write pairs
174 if (is_read() && is_acquire() && act->is_write() && act->is_release())
176 if (is_write() && is_release() && act->is_read() && act->is_acquire())
179 // Otherwise handle by reads_from relation
183 void ModelAction::create_cv(const ModelAction *parent)
189 cv = new ClockVector(parent->cv, this);
191 cv = new ClockVector(NULL, this);
194 void ModelAction::set_try_lock(bool obtainedlock) {
196 value=VALUE_TRYSUCCESS;
198 value=VALUE_TRYFAILED;
201 /** Update the model action's read_from action */
202 void ModelAction::read_from(const ModelAction *act)
206 if (act != NULL && this->is_acquire()) {
207 rel_heads_list_t release_heads;
208 model->get_release_seq_heads(this, &release_heads);
209 for (unsigned int i = 0; i < release_heads.size(); i++)
210 synchronize_with(release_heads[i]);
215 * Synchronize the current thread with the thread corresponding to the
216 * ModelAction parameter.
217 * @param act The ModelAction to synchronize with
219 void ModelAction::synchronize_with(const ModelAction *act) {
220 ASSERT(*act < *this || type == THREAD_JOIN);
221 model->check_promises(cv, act->cv);
225 bool ModelAction::has_synchronized_with(const ModelAction *act) const
227 return cv->has_synchronized_with(act->cv);
231 * Check whether 'this' happens before act, according to the memory-model's
232 * happens before relation. This is checked via the ClockVector constructs.
233 * @return true if this action's thread has synchronized with act's thread
234 * since the execution of act, false otherwise.
236 bool ModelAction::happens_before(const ModelAction *act) const
238 return act->cv->synchronized_since(this);
241 void ModelAction::print(void) const
243 const char *type_str, *mo_str;
244 switch (this->type) {
246 type_str = "thread create";
249 type_str = "thread start";
252 type_str = "thread yield";
255 type_str = "thread join";
258 type_str = "thread finish";
261 type_str = "atomic read";
264 type_str = "atomic write";
267 type_str = "atomic rmw";
273 type_str = "atomic rmwr";
276 type_str = "atomic rmwc";
279 type_str = "init atomic";
282 type_str = "unknown type";
285 uint64_t valuetoprint=type==ATOMIC_READ?(reads_from!=NULL?reads_from->value:VALUE_NONE):value;
287 switch (this->order) {
288 case std::memory_order_relaxed:
291 case std::memory_order_acquire:
294 case std::memory_order_release:
297 case std::memory_order_acq_rel:
300 case std::memory_order_seq_cst:
308 printf("(%3d) Thread: %-2d Action: %-13s MO: %7s Loc: %14p Value: %-12" PRIu64,
309 seq_number, id_to_int(tid), type_str, mo_str, location, valuetoprint);
312 printf(" Rf: %d", reads_from->get_seq_number());