2 #define __STDC_FORMAT_MACROS
8 #include "clockvector.h"
10 #include "threads-model.h"
11 #include "nodestack.h"
13 #define ACTION_INITIAL_CLOCK 0
16 * @brief Construct a new ModelAction
18 * @param type The type of action
19 * @param order The memory order of this action. A "don't care" for non-ATOMIC
20 * actions (e.g., THREAD_* or MODEL_* actions).
21 * @param loc The location that this action acts upon
22 * @param value (optional) A value associated with the action (e.g., the value
23 * read or written). Defaults to a given macro constant, for debugging purposes.
24 * @param thread (optional) The Thread in which this action occurred. If NULL
25 * (default), then a Thread is assigned according to the scheduler.
27 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc,
28 uint64_t value, Thread *thread) :
34 last_fence_release(NULL),
36 seq_number(ACTION_INITIAL_CLOCK),
40 /* References to NULL atomic variables can end up here */
41 ASSERT(loc || type == ATOMIC_FENCE || type == MODEL_FIXUP_RELSEQ);
43 Thread *t = thread ? thread : thread_current();
44 this->tid = t->get_id();
47 /** @brief ModelAction destructor */
48 ModelAction::~ModelAction()
51 * We can't free the clock vector:
52 * Clock vectors are snapshotting state. When we delete model actions,
53 * they are at the end of the node list and have invalid old clock
54 * vectors which have already been rolled back to an unallocated state.
62 void ModelAction::copy_from_new(ModelAction *newaction)
64 seq_number = newaction->seq_number;
67 void ModelAction::set_seq_number(modelclock_t num)
69 ASSERT(seq_number == ACTION_INITIAL_CLOCK);
73 bool ModelAction::is_relseq_fixup() const
75 return type == MODEL_FIXUP_RELSEQ;
78 bool ModelAction::is_mutex_op() const
80 return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK || type == ATOMIC_WAIT || type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
83 bool ModelAction::is_lock() const
85 return type == ATOMIC_LOCK;
88 bool ModelAction::is_wait() const {
89 return type == ATOMIC_WAIT;
92 bool ModelAction::is_notify() const {
93 return type==ATOMIC_NOTIFY_ONE || type==ATOMIC_NOTIFY_ALL;
96 bool ModelAction::is_notify_one() const {
97 return type==ATOMIC_NOTIFY_ONE;
100 bool ModelAction::is_unlock() const
102 return type == ATOMIC_UNLOCK;
105 bool ModelAction::is_trylock() const
107 return type == ATOMIC_TRYLOCK;
110 bool ModelAction::is_success_lock() const
112 return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
115 bool ModelAction::is_failed_trylock() const
117 return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
120 bool ModelAction::is_read() const
122 return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMW;
125 bool ModelAction::is_write() const
127 return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT;
130 bool ModelAction::could_be_write() const
132 return is_write() || is_rmwr();
135 bool ModelAction::is_rmwr() const
137 return type == ATOMIC_RMWR;
140 bool ModelAction::is_rmw() const
142 return type == ATOMIC_RMW;
145 bool ModelAction::is_rmwc() const
147 return type == ATOMIC_RMWC;
150 bool ModelAction::is_fence() const
152 return type == ATOMIC_FENCE;
155 bool ModelAction::is_initialization() const
157 return type == ATOMIC_INIT;
160 bool ModelAction::is_relaxed() const
162 return order == std::memory_order_relaxed;
165 bool ModelAction::is_acquire() const
168 case std::memory_order_acquire:
169 case std::memory_order_acq_rel:
170 case std::memory_order_seq_cst:
177 bool ModelAction::is_release() const
180 case std::memory_order_release:
181 case std::memory_order_acq_rel:
182 case std::memory_order_seq_cst:
189 bool ModelAction::is_seqcst() const
191 return order==std::memory_order_seq_cst;
194 bool ModelAction::same_var(const ModelAction *act) const
196 if ( act->is_wait() || is_wait() ) {
197 if ( act->is_wait() && is_wait() ) {
198 if ( ((void *)value) == ((void *)act->value) )
200 } else if ( is_wait() ) {
201 if ( ((void *)value) == act->location )
203 } else if ( act->is_wait() ) {
204 if ( location == ((void *)act->value) )
209 return location == act->location;
212 bool ModelAction::same_thread(const ModelAction *act) const
214 return tid == act->tid;
217 void ModelAction::copy_typeandorder(ModelAction * act) {
218 this->type = act->type;
219 this->order = act->order;
222 /** This method changes an existing read part of an RMW action into either:
223 * (1) a full RMW action in case of the completed write or
224 * (2) a READ action in case a failed action.
225 * @todo If the memory_order changes, we may potentially need to update our
228 void ModelAction::process_rmw(ModelAction * act) {
229 this->order=act->order;
231 this->type=ATOMIC_READ;
232 else if (act->is_rmw()) {
233 this->type=ATOMIC_RMW;
234 this->value=act->value;
238 /** The is_synchronizing method should only explore interleavings if:
239 * (1) the operations are seq_cst and don't commute or
240 * (2) the reordering may establish or break a synchronization relation.
241 * Other memory operations will be dealt with by using the reads_from
244 * @param act is the action to consider exploring a reordering.
245 * @return tells whether we have to explore a reordering.
247 bool ModelAction::could_synchronize_with(const ModelAction *act) const
249 //Same thread can't be reordered
250 if (same_thread(act))
253 // Different locations commute
257 // Explore interleavings of seqcst writes/fences to guarantee total
258 // order of seq_cst operations that don't commute
259 if ((could_be_write() || act->could_be_write() || is_fence() || act->is_fence())
260 && is_seqcst() && act->is_seqcst())
263 // Explore synchronizing read/write/fence pairs
264 if (is_acquire() && act->is_release() && (is_read() || is_fence()) &&
265 (act->could_be_write() || act->is_fence()))
268 //lock just released...we can grab lock
269 if ((is_lock() ||is_trylock()) && (act->is_unlock()||act->is_wait()))
272 //lock just acquired...we can fail to grab lock
273 if (is_trylock() && act->is_success_lock())
276 //other thread stalling on lock...we can release lock
277 if (is_unlock() && (act->is_trylock()||act->is_lock()))
280 if (is_trylock() && (act->is_unlock()||act->is_wait()))
283 if ( is_notify() && act->is_wait() )
286 if ( is_wait() && act->is_notify() )
289 // Otherwise handle by reads_from relation
293 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
295 //Must be different threads to reorder
296 if (same_thread(act))
299 //Try to reorder a lock past a successful lock
300 if (act->is_success_lock())
303 //Try to push a successful trylock past an unlock
304 if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
307 //Try to push a successful trylock past a wait
308 if (act->is_wait() && is_trylock() && value == VALUE_TRYSUCCESS)
315 * Create a new clock vector for this action. Note that this function allows a
316 * user to clobber (and leak) a ModelAction's existing clock vector. A user
317 * should ensure that the vector has already either been rolled back
318 * (effectively "freed") or freed.
320 * @param parent A ModelAction from which to inherit a ClockVector
322 void ModelAction::create_cv(const ModelAction *parent)
325 cv = new ClockVector(parent->cv, this);
327 cv = new ClockVector(NULL, this);
330 void ModelAction::set_try_lock(bool obtainedlock) {
332 value=VALUE_TRYSUCCESS;
334 value=VALUE_TRYFAILED;
338 * Update the model action's read_from action
339 * @param act The action to read from; should be a write
340 * @return True if this read established synchronization
342 bool ModelAction::read_from(const ModelAction *act)
346 if (act != NULL && this->is_acquire()) {
347 rel_heads_list_t release_heads;
348 model->get_release_seq_heads(this, &release_heads);
349 int num_heads = release_heads.size();
350 for (unsigned int i = 0; i < release_heads.size(); i++)
351 if (!synchronize_with(release_heads[i])) {
352 model->set_bad_synchronization();
355 return num_heads > 0;
361 * Synchronize the current thread with the thread corresponding to the
362 * ModelAction parameter.
363 * @param act The ModelAction to synchronize with
364 * @return True if this is a valid synchronization; false otherwise
366 bool ModelAction::synchronize_with(const ModelAction *act) {
367 if (*this < *act && type != THREAD_JOIN && type != ATOMIC_LOCK)
369 model->check_promises(act->get_tid(), cv, act->cv);
374 bool ModelAction::has_synchronized_with(const ModelAction *act) const
376 return cv->synchronized_since(act);
380 * Check whether 'this' happens before act, according to the memory-model's
381 * happens before relation. This is checked via the ClockVector constructs.
382 * @return true if this action's thread has synchronized with act's thread
383 * since the execution of act, false otherwise.
385 bool ModelAction::happens_before(const ModelAction *act) const
387 return act->cv->synchronized_since(this);
390 /** @brief Print nicely-formatted info about this ModelAction */
391 void ModelAction::print() const
393 const char *type_str, *mo_str;
394 switch (this->type) {
395 case MODEL_FIXUP_RELSEQ:
396 type_str = "relseq fixup";
399 type_str = "thread create";
402 type_str = "thread start";
405 type_str = "thread yield";
408 type_str = "thread join";
411 type_str = "thread finish";
414 type_str = "atomic read";
417 type_str = "atomic write";
420 type_str = "atomic rmw";
426 type_str = "atomic rmwr";
429 type_str = "atomic rmwc";
432 type_str = "init atomic";
441 type_str = "trylock";
446 case ATOMIC_NOTIFY_ONE:
447 type_str = "notify one";
449 case ATOMIC_NOTIFY_ALL:
450 type_str = "notify all";
453 type_str = "unknown type";
456 uint64_t valuetoprint=type==ATOMIC_READ?(reads_from!=NULL?reads_from->value:VALUE_NONE):value;
458 switch (this->order) {
459 case std::memory_order_relaxed:
462 case std::memory_order_acquire:
465 case std::memory_order_release:
468 case std::memory_order_acq_rel:
471 case std::memory_order_seq_cst:
479 model_print("(%4d) Thread: %-2d Action: %-13s MO: %7s Loc: %14p Value: %-#18" PRIx64,
480 seq_number, id_to_int(tid), type_str, mo_str, location, valuetoprint);
483 model_print(" Rf: %-3d", reads_from->get_seq_number());
485 model_print(" Rf: ? ");
497 /** @brief Print nicely-formatted info about this ModelAction */
498 unsigned int ModelAction::hash() const
500 unsigned int hash=(unsigned int) this->type;
501 hash^=((unsigned int)this->order)<<3;
503 hash ^= id_to_int(tid) << 6;
507 hash^=reads_from->get_seq_number();