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_thread_start() const
75 return type == THREAD_START;
78 bool ModelAction::is_relseq_fixup() const
80 return type == MODEL_FIXUP_RELSEQ;
83 bool ModelAction::is_mutex_op() const
85 return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK || type == ATOMIC_WAIT || type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
88 bool ModelAction::is_lock() const
90 return type == ATOMIC_LOCK;
93 bool ModelAction::is_wait() const {
94 return type == ATOMIC_WAIT;
97 bool ModelAction::is_notify() const {
98 return type==ATOMIC_NOTIFY_ONE || type==ATOMIC_NOTIFY_ALL;
101 bool ModelAction::is_notify_one() const {
102 return type==ATOMIC_NOTIFY_ONE;
105 bool ModelAction::is_unlock() const
107 return type == ATOMIC_UNLOCK;
110 bool ModelAction::is_trylock() const
112 return type == ATOMIC_TRYLOCK;
115 bool ModelAction::is_success_lock() const
117 return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
120 bool ModelAction::is_failed_trylock() const
122 return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
125 bool ModelAction::is_read() const
127 return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMW;
130 bool ModelAction::is_write() const
132 return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT;
135 bool ModelAction::could_be_write() const
137 return is_write() || is_rmwr();
140 bool ModelAction::is_rmwr() const
142 return type == ATOMIC_RMWR;
145 bool ModelAction::is_rmw() const
147 return type == ATOMIC_RMW;
150 bool ModelAction::is_rmwc() const
152 return type == ATOMIC_RMWC;
155 bool ModelAction::is_fence() const
157 return type == ATOMIC_FENCE;
160 bool ModelAction::is_initialization() const
162 return type == ATOMIC_INIT;
165 bool ModelAction::is_relaxed() const
167 return order == std::memory_order_relaxed;
170 bool ModelAction::is_acquire() const
173 case std::memory_order_acquire:
174 case std::memory_order_acq_rel:
175 case std::memory_order_seq_cst:
182 bool ModelAction::is_release() const
185 case std::memory_order_release:
186 case std::memory_order_acq_rel:
187 case std::memory_order_seq_cst:
194 bool ModelAction::is_seqcst() const
196 return order==std::memory_order_seq_cst;
199 bool ModelAction::same_var(const ModelAction *act) const
201 if ( act->is_wait() || is_wait() ) {
202 if ( act->is_wait() && is_wait() ) {
203 if ( ((void *)value) == ((void *)act->value) )
205 } else if ( is_wait() ) {
206 if ( ((void *)value) == act->location )
208 } else if ( act->is_wait() ) {
209 if ( location == ((void *)act->value) )
214 return location == act->location;
217 bool ModelAction::same_thread(const ModelAction *act) const
219 return tid == act->tid;
222 void ModelAction::copy_typeandorder(ModelAction * act) {
223 this->type = act->type;
224 this->order = act->order;
227 /** This method changes an existing read part of an RMW action into either:
228 * (1) a full RMW action in case of the completed write or
229 * (2) a READ action in case a failed action.
230 * @todo If the memory_order changes, we may potentially need to update our
233 void ModelAction::process_rmw(ModelAction * act) {
234 this->order=act->order;
236 this->type=ATOMIC_READ;
237 else if (act->is_rmw()) {
238 this->type=ATOMIC_RMW;
239 this->value=act->value;
243 /** The is_synchronizing method should only explore interleavings if:
244 * (1) the operations are seq_cst and don't commute or
245 * (2) the reordering may establish or break a synchronization relation.
246 * Other memory operations will be dealt with by using the reads_from
249 * @param act is the action to consider exploring a reordering.
250 * @return tells whether we have to explore a reordering.
252 bool ModelAction::could_synchronize_with(const ModelAction *act) const
254 //Same thread can't be reordered
255 if (same_thread(act))
258 // Different locations commute
262 // Explore interleavings of seqcst writes/fences to guarantee total
263 // order of seq_cst operations that don't commute
264 if ((could_be_write() || act->could_be_write() || is_fence() || act->is_fence())
265 && is_seqcst() && act->is_seqcst())
268 // Explore synchronizing read/write/fence pairs
269 if (is_acquire() && act->is_release() && (is_read() || is_fence()) &&
270 (act->could_be_write() || act->is_fence()))
273 //lock just released...we can grab lock
274 if ((is_lock() ||is_trylock()) && (act->is_unlock()||act->is_wait()))
277 //lock just acquired...we can fail to grab lock
278 if (is_trylock() && act->is_success_lock())
281 //other thread stalling on lock...we can release lock
282 if (is_unlock() && (act->is_trylock()||act->is_lock()))
285 if (is_trylock() && (act->is_unlock()||act->is_wait()))
288 if ( is_notify() && act->is_wait() )
291 if ( is_wait() && act->is_notify() )
294 // Otherwise handle by reads_from relation
298 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
300 //Must be different threads to reorder
301 if (same_thread(act))
304 //Try to reorder a lock past a successful lock
305 if (act->is_success_lock())
308 //Try to push a successful trylock past an unlock
309 if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
312 //Try to push a successful trylock past a wait
313 if (act->is_wait() && is_trylock() && value == VALUE_TRYSUCCESS)
320 * Create a new clock vector for this action. Note that this function allows a
321 * user to clobber (and leak) a ModelAction's existing clock vector. A user
322 * should ensure that the vector has already either been rolled back
323 * (effectively "freed") or freed.
325 * @param parent A ModelAction from which to inherit a ClockVector
327 void ModelAction::create_cv(const ModelAction *parent)
330 cv = new ClockVector(parent->cv, this);
332 cv = new ClockVector(NULL, this);
335 void ModelAction::set_try_lock(bool obtainedlock) {
337 value=VALUE_TRYSUCCESS;
339 value=VALUE_TRYFAILED;
343 * Update the model action's read_from action
344 * @param act The action to read from; should be a write
346 void ModelAction::set_read_from(const ModelAction *act)
352 * Synchronize the current thread with the thread corresponding to the
353 * ModelAction parameter.
354 * @param act The ModelAction to synchronize with
355 * @return True if this is a valid synchronization; false otherwise
357 bool ModelAction::synchronize_with(const ModelAction *act) {
358 if (*this < *act && type != THREAD_JOIN && type != ATOMIC_LOCK)
360 model->check_promises(act->get_tid(), cv, act->cv);
365 bool ModelAction::has_synchronized_with(const ModelAction *act) const
367 return cv->synchronized_since(act);
371 * Check whether 'this' happens before act, according to the memory-model's
372 * happens before relation. This is checked via the ClockVector constructs.
373 * @return true if this action's thread has synchronized with act's thread
374 * since the execution of act, false otherwise.
376 bool ModelAction::happens_before(const ModelAction *act) const
378 return act->cv->synchronized_since(this);
381 /** @brief Print nicely-formatted info about this ModelAction */
382 void ModelAction::print() const
384 const char *type_str, *mo_str;
385 switch (this->type) {
386 case MODEL_FIXUP_RELSEQ:
387 type_str = "relseq fixup";
390 type_str = "thread create";
393 type_str = "thread start";
396 type_str = "thread yield";
399 type_str = "thread join";
402 type_str = "thread finish";
405 type_str = "atomic read";
408 type_str = "atomic write";
411 type_str = "atomic rmw";
417 type_str = "atomic rmwr";
420 type_str = "atomic rmwc";
423 type_str = "init atomic";
432 type_str = "trylock";
437 case ATOMIC_NOTIFY_ONE:
438 type_str = "notify one";
440 case ATOMIC_NOTIFY_ALL:
441 type_str = "notify all";
444 type_str = "unknown type";
447 uint64_t valuetoprint=type==ATOMIC_READ?(reads_from!=NULL?reads_from->value:VALUE_NONE):value;
449 switch (this->order) {
450 case std::memory_order_relaxed:
453 case std::memory_order_acquire:
456 case std::memory_order_release:
459 case std::memory_order_acq_rel:
462 case std::memory_order_seq_cst:
470 model_print("(%4d) Thread: %-2d Action: %-13s MO: %7s Loc: %14p Value: %-#18" PRIx64,
471 seq_number, id_to_int(tid), type_str, mo_str, location, valuetoprint);
474 model_print(" Rf: %-3d", reads_from->get_seq_number());
476 model_print(" Rf: ? ");
488 /** @brief Print nicely-formatted info about this ModelAction */
489 unsigned int ModelAction::hash() const
491 unsigned int hash=(unsigned int) this->type;
492 hash^=((unsigned int)this->order)<<3;
494 hash ^= id_to_int(tid) << 6;
498 hash^=reads_from->get_seq_number();