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 void ModelAction::copy_from_new(ModelAction *newaction) {
31 seq_number=newaction->seq_number;
34 bool ModelAction::is_mutex_op() const {
35 return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK;
38 bool ModelAction::is_lock() const {
39 return type == ATOMIC_LOCK;
42 bool ModelAction::is_unlock() const {
43 return type == ATOMIC_UNLOCK;
46 bool ModelAction::is_trylock() const {
47 return type == ATOMIC_TRYLOCK;
50 bool ModelAction::is_success_lock() const {
51 return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
54 bool ModelAction::is_failed_trylock() const {
55 return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
58 bool ModelAction::is_read() const
60 return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMW;
63 bool ModelAction::is_write() const
65 return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT;
68 bool ModelAction::is_rmwr() const
70 return type == ATOMIC_RMWR;
73 bool ModelAction::is_rmw() const
75 return type == ATOMIC_RMW;
78 bool ModelAction::is_rmwc() const
80 return type == ATOMIC_RMWC;
83 bool ModelAction::is_fence() const
85 return type == ATOMIC_FENCE;
88 bool ModelAction::is_initialization() const
90 return type == ATOMIC_INIT;
93 bool ModelAction::is_acquire() const
96 case std::memory_order_acquire:
97 case std::memory_order_acq_rel:
98 case std::memory_order_seq_cst:
105 bool ModelAction::is_release() const
108 case std::memory_order_release:
109 case std::memory_order_acq_rel:
110 case std::memory_order_seq_cst:
117 bool ModelAction::is_seqcst() const
119 return order==std::memory_order_seq_cst;
122 bool ModelAction::same_var(const ModelAction *act) const
124 return location == act->location;
127 bool ModelAction::same_thread(const ModelAction *act) const
129 return tid == act->tid;
132 void ModelAction::copy_typeandorder(ModelAction * act) {
133 this->type=act->type;
134 this->order=act->order;
137 /** This method changes an existing read part of an RMW action into either:
138 * (1) a full RMW action in case of the completed write or
139 * (2) a READ action in case a failed action.
140 * @todo If the memory_order changes, we may potentially need to update our
143 void ModelAction::process_rmw(ModelAction * act) {
144 this->order=act->order;
146 this->type=ATOMIC_READ;
147 else if (act->is_rmw()) {
148 this->type=ATOMIC_RMW;
149 this->value=act->value;
153 /** The is_synchronizing method should only explore interleavings if:
154 * (1) the operations are seq_cst and don't commute or
155 * (2) the reordering may establish or break a synchronization relation.
156 * Other memory operations will be dealt with by using the reads_from
159 * @param act is the action to consider exploring a reordering.
160 * @return tells whether we have to explore a reordering.
162 bool ModelAction::is_synchronizing(const ModelAction *act) const
164 //Same thread can't be reordered
165 if (same_thread(act))
168 // Different locations commute
172 // Explore interleavings of seqcst writes to guarantee total order
173 // of seq_cst operations that don't commute
174 if (is_write() && is_seqcst() && act->is_write() && act->is_seqcst())
177 // Explore synchronizing read/write pairs
178 if (is_read() && is_acquire() && act->is_write() && act->is_release())
180 if (is_write() && is_release() && act->is_read() && act->is_acquire())
183 // Otherwise handle by reads_from relation
187 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
189 //Must be different threads to reorder
190 if (same_thread(act))
193 //Try to reorder a lock past a successful lock
194 if (act->is_success_lock())
197 //Try to push a successful trylock past an unlock
198 if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
205 * Create a new clock vector for this action. Note that this function allows a
206 * user to clobber (and leak) a ModelAction's existing clock vector. A user
207 * should ensure that the vector has already either been rolled back
208 * (effectively "freed") or freed.
210 * @param parent A ModelAction from which to inherit a ClockVector
212 void ModelAction::create_cv(const ModelAction *parent)
215 cv = new ClockVector(parent->cv, this);
217 cv = new ClockVector(NULL, this);
220 void ModelAction::set_try_lock(bool obtainedlock) {
222 value=VALUE_TRYSUCCESS;
224 value=VALUE_TRYFAILED;
227 /** Update the model action's read_from action */
228 void ModelAction::read_from(const ModelAction *act)
232 if (act != NULL && this->is_acquire()) {
233 rel_heads_list_t release_heads;
234 model->get_release_seq_heads(this, &release_heads);
235 for (unsigned int i = 0; i < release_heads.size(); i++)
236 if (!synchronize_with(release_heads[i]))
237 model->set_bad_synchronization();
242 * Synchronize the current thread with the thread corresponding to the
243 * ModelAction parameter.
244 * @param act The ModelAction to synchronize with
245 * @return True if this is a valid synchronization; false otherwise
247 bool ModelAction::synchronize_with(const ModelAction *act) {
248 if (*this < *act && type != THREAD_JOIN && type != ATOMIC_LOCK)
250 model->check_promises(cv, act->cv);
255 bool ModelAction::has_synchronized_with(const ModelAction *act) const
257 return cv->has_synchronized_with(act->cv);
261 * Check whether 'this' happens before act, according to the memory-model's
262 * happens before relation. This is checked via the ClockVector constructs.
263 * @return true if this action's thread has synchronized with act's thread
264 * since the execution of act, false otherwise.
266 bool ModelAction::happens_before(const ModelAction *act) const
268 return act->cv->synchronized_since(this);
271 void ModelAction::print(void) const
273 const char *type_str, *mo_str;
274 switch (this->type) {
276 type_str = "thread create";
279 type_str = "thread start";
282 type_str = "thread yield";
285 type_str = "thread join";
288 type_str = "thread finish";
291 type_str = "atomic read";
294 type_str = "atomic write";
297 type_str = "atomic rmw";
303 type_str = "atomic rmwr";
306 type_str = "atomic rmwc";
309 type_str = "init atomic";
318 type_str = "trylock";
321 type_str = "unknown type";
324 uint64_t valuetoprint=type==ATOMIC_READ?(reads_from!=NULL?reads_from->value:VALUE_NONE):value;
326 switch (this->order) {
327 case std::memory_order_relaxed:
330 case std::memory_order_acquire:
333 case std::memory_order_release:
336 case std::memory_order_acq_rel:
339 case std::memory_order_seq_cst:
347 printf("(%3d) Thread: %-2d Action: %-13s MO: %7s Loc: %14p Value: %-12" PRIu64,
348 seq_number, id_to_int(tid), type_str, mo_str, location, valuetoprint);
351 printf(" Rf: %d", reads_from->get_seq_number());