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) :
35 seq_number(ACTION_INITIAL_CLOCK),
39 /* References to NULL atomic variables can end up here */
40 ASSERT(loc || type == MODEL_FIXUP_RELSEQ);
42 Thread *t = thread ? thread : thread_current();
43 this->tid = t->get_id();
46 /** @brief ModelAction destructor */
47 ModelAction::~ModelAction()
50 * We can't free the clock vector:
51 * Clock vectors are snapshotting state. When we delete model actions,
52 * they are at the end of the node list and have invalid old clock
53 * vectors which have already been rolled back to an unallocated state.
61 void ModelAction::copy_from_new(ModelAction *newaction)
63 seq_number = newaction->seq_number;
66 void ModelAction::set_seq_number(modelclock_t num)
68 ASSERT(seq_number == ACTION_INITIAL_CLOCK);
72 bool ModelAction::is_relseq_fixup() const
74 return type == MODEL_FIXUP_RELSEQ;
77 bool ModelAction::is_mutex_op() const
79 return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK || type == ATOMIC_WAIT || type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
82 bool ModelAction::is_lock() const
84 return type == ATOMIC_LOCK;
87 bool ModelAction::is_wait() const {
88 return type == ATOMIC_WAIT;
91 bool ModelAction::is_notify() const {
92 return type==ATOMIC_NOTIFY_ONE || type==ATOMIC_NOTIFY_ALL;
95 bool ModelAction::is_notify_one() const {
96 return type==ATOMIC_NOTIFY_ONE;
99 bool ModelAction::is_unlock() const
101 return type == ATOMIC_UNLOCK;
104 bool ModelAction::is_trylock() const
106 return type == ATOMIC_TRYLOCK;
109 bool ModelAction::is_success_lock() const
111 return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
114 bool ModelAction::is_failed_trylock() const
116 return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
119 bool ModelAction::is_read() const
121 return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMW;
124 bool ModelAction::is_write() const
126 return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT;
129 bool ModelAction::could_be_write() const
131 return is_write() || is_rmwr();
134 bool ModelAction::is_rmwr() const
136 return type == ATOMIC_RMWR;
139 bool ModelAction::is_rmw() const
141 return type == ATOMIC_RMW;
144 bool ModelAction::is_rmwc() const
146 return type == ATOMIC_RMWC;
149 bool ModelAction::is_fence() const
151 return type == ATOMIC_FENCE;
154 bool ModelAction::is_initialization() const
156 return type == ATOMIC_INIT;
159 bool ModelAction::is_relaxed() const
161 return order == std::memory_order_relaxed;
164 bool ModelAction::is_acquire() const
167 case std::memory_order_acquire:
168 case std::memory_order_acq_rel:
169 case std::memory_order_seq_cst:
176 bool ModelAction::is_release() const
179 case std::memory_order_release:
180 case std::memory_order_acq_rel:
181 case std::memory_order_seq_cst:
188 bool ModelAction::is_seqcst() const
190 return order==std::memory_order_seq_cst;
193 bool ModelAction::same_var(const ModelAction *act) const
195 if ( act->is_wait() || is_wait() ) {
196 if ( act->is_wait() && is_wait() ) {
197 if ( ((void *)value) == ((void *)act->value) )
199 } else if ( is_wait() ) {
200 if ( ((void *)value) == act->location )
202 } else if ( act->is_wait() ) {
203 if ( location == ((void *)act->value) )
208 return location == act->location;
211 bool ModelAction::same_thread(const ModelAction *act) const
213 return tid == act->tid;
216 void ModelAction::copy_typeandorder(ModelAction * act) {
217 this->type = act->type;
218 this->order = act->order;
221 /** This method changes an existing read part of an RMW action into either:
222 * (1) a full RMW action in case of the completed write or
223 * (2) a READ action in case a failed action.
224 * @todo If the memory_order changes, we may potentially need to update our
227 void ModelAction::process_rmw(ModelAction * act) {
228 this->order=act->order;
230 this->type=ATOMIC_READ;
231 else if (act->is_rmw()) {
232 this->type=ATOMIC_RMW;
233 this->value=act->value;
237 /** The is_synchronizing method should only explore interleavings if:
238 * (1) the operations are seq_cst and don't commute or
239 * (2) the reordering may establish or break a synchronization relation.
240 * Other memory operations will be dealt with by using the reads_from
243 * @param act is the action to consider exploring a reordering.
244 * @return tells whether we have to explore a reordering.
246 bool ModelAction::could_synchronize_with(const ModelAction *act) const
248 //Same thread can't be reordered
249 if (same_thread(act))
252 // Different locations commute
256 // Explore interleavings of seqcst writes to guarantee total order
257 // of seq_cst operations that don't commute
258 if ((could_be_write() || act->could_be_write()) && is_seqcst() && act->is_seqcst())
261 // Explore synchronizing read/write pairs
262 if (is_read() && is_acquire() && act->could_be_write() && act->is_release())
265 //lock just released...we can grab lock
266 if ((is_lock() ||is_trylock()) && (act->is_unlock()||act->is_wait()))
269 //lock just acquired...we can fail to grab lock
270 if (is_trylock() && act->is_success_lock())
273 //other thread stalling on lock...we can release lock
274 if (is_unlock() && (act->is_trylock()||act->is_lock()))
277 if (is_trylock() && (act->is_unlock()||act->is_wait()))
280 if ( is_notify() && act->is_wait() )
283 if ( is_wait() && act->is_notify() )
286 // Otherwise handle by reads_from relation
290 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
292 //Must be different threads to reorder
293 if (same_thread(act))
296 //Try to reorder a lock past a successful lock
297 if (act->is_success_lock())
300 //Try to push a successful trylock past an unlock
301 if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
304 //Try to push a successful trylock past a wait
305 if (act->is_wait() && is_trylock() && value == VALUE_TRYSUCCESS)
312 * Create a new clock vector for this action. Note that this function allows a
313 * user to clobber (and leak) a ModelAction's existing clock vector. A user
314 * should ensure that the vector has already either been rolled back
315 * (effectively "freed") or freed.
317 * @param parent A ModelAction from which to inherit a ClockVector
319 void ModelAction::create_cv(const ModelAction *parent)
322 cv = new ClockVector(parent->cv, this);
324 cv = new ClockVector(NULL, this);
327 void ModelAction::set_try_lock(bool obtainedlock) {
329 value=VALUE_TRYSUCCESS;
331 value=VALUE_TRYFAILED;
335 * Update the model action's read_from action
336 * @param act The action to read from; should be a write
337 * @return True if this read established synchronization
339 bool ModelAction::read_from(const ModelAction *act)
343 if (act != NULL && this->is_acquire()) {
344 rel_heads_list_t release_heads;
345 model->get_release_seq_heads(this, &release_heads);
346 int num_heads = release_heads.size();
347 for (unsigned int i = 0; i < release_heads.size(); i++)
348 if (!synchronize_with(release_heads[i])) {
349 model->set_bad_synchronization();
352 return num_heads > 0;
358 * Synchronize the current thread with the thread corresponding to the
359 * ModelAction parameter.
360 * @param act The ModelAction to synchronize with
361 * @return True if this is a valid synchronization; false otherwise
363 bool ModelAction::synchronize_with(const ModelAction *act) {
364 if (*this < *act && type != THREAD_JOIN && type != ATOMIC_LOCK)
366 model->check_promises(act->get_tid(), cv, act->cv);
371 bool ModelAction::has_synchronized_with(const ModelAction *act) const
373 return cv->synchronized_since(act);
377 * Check whether 'this' happens before act, according to the memory-model's
378 * happens before relation. This is checked via the ClockVector constructs.
379 * @return true if this action's thread has synchronized with act's thread
380 * since the execution of act, false otherwise.
382 bool ModelAction::happens_before(const ModelAction *act) const
384 return act->cv->synchronized_since(this);
387 /** @brief Print nicely-formatted info about this ModelAction */
388 void ModelAction::print() const
390 const char *type_str, *mo_str;
391 switch (this->type) {
392 case MODEL_FIXUP_RELSEQ:
393 type_str = "relseq fixup";
396 type_str = "thread create";
399 type_str = "thread start";
402 type_str = "thread yield";
405 type_str = "thread join";
408 type_str = "thread finish";
411 type_str = "atomic read";
414 type_str = "atomic write";
417 type_str = "atomic rmw";
423 type_str = "atomic rmwr";
426 type_str = "atomic rmwc";
429 type_str = "init atomic";
438 type_str = "trylock";
443 case ATOMIC_NOTIFY_ONE:
444 type_str = "notify one";
446 case ATOMIC_NOTIFY_ALL:
447 type_str = "notify all";
450 type_str = "unknown type";
453 uint64_t valuetoprint=type==ATOMIC_READ?(reads_from!=NULL?reads_from->value:VALUE_NONE):value;
455 switch (this->order) {
456 case std::memory_order_relaxed:
459 case std::memory_order_acquire:
462 case std::memory_order_release:
465 case std::memory_order_acq_rel:
468 case std::memory_order_seq_cst:
476 model_print("(%4d) Thread: %-2d Action: %-13s MO: %7s Loc: %14p Value: %-#18" PRIx64,
477 seq_number, id_to_int(tid), type_str, mo_str, location, valuetoprint);
480 model_print(" Rf: %-3d", reads_from->get_seq_number());
482 model_print(" Rf: ? ");
494 /** @brief Print nicely-formatted info about this ModelAction */
495 unsigned int ModelAction::hash() const
497 unsigned int hash=(unsigned int) this->type;
498 hash^=((unsigned int)this->order)<<3;
500 hash ^= id_to_int(tid) << 6;
504 hash^=reads_from->get_seq_number();