2 #define __STDC_FORMAT_MACROS
7 #include "clockvector.h"
9 #include "threads-model.h"
10 #include "nodestack.h"
12 #define ACTION_INITIAL_CLOCK 0
15 * @brief Construct a new ModelAction
17 * @param type The type of action
18 * @param order The memory order of this action. A "don't care" for non-ATOMIC
19 * actions (e.g., THREAD_* or MODEL_* actions).
20 * @param loc The location that this action acts upon
21 * @param value (optional) A value associated with the action (e.g., the value
22 * read or written). Defaults to a given macro constant, for debugging purposes.
23 * @param thread (optional) The Thread in which this action occurred. If NULL
24 * (default), then a Thread is assigned according to the scheduler.
26 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc,
27 uint64_t value, Thread *thread) :
33 reads_from_promise(NULL),
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 /* ATOMIC_UNINIT actions should never have non-zero clock */
70 ASSERT(!is_uninitialized());
71 ASSERT(seq_number == ACTION_INITIAL_CLOCK);
75 bool ModelAction::is_thread_start() const
77 return type == THREAD_START;
80 bool ModelAction::is_relseq_fixup() const
82 return type == MODEL_FIXUP_RELSEQ;
85 bool ModelAction::is_mutex_op() const
87 return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK || type == ATOMIC_WAIT || type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
90 bool ModelAction::is_lock() const
92 return type == ATOMIC_LOCK;
95 bool ModelAction::is_wait() const {
96 return type == ATOMIC_WAIT;
99 bool ModelAction::is_notify() const {
100 return type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
103 bool ModelAction::is_notify_one() const {
104 return type == ATOMIC_NOTIFY_ONE;
107 bool ModelAction::is_unlock() const
109 return type == ATOMIC_UNLOCK;
112 bool ModelAction::is_trylock() const
114 return type == ATOMIC_TRYLOCK;
117 bool ModelAction::is_success_lock() const
119 return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
122 bool ModelAction::is_failed_trylock() const
124 return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
127 /** @return True if this operation is performed on a C/C++ atomic variable */
128 bool ModelAction::is_atomic_var() const
130 return is_read() || could_be_write();
133 bool ModelAction::is_uninitialized() const
135 return type == ATOMIC_UNINIT;
138 bool ModelAction::is_read() const
140 return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMW;
143 bool ModelAction::is_write() const
145 return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT || type == ATOMIC_UNINIT;
148 bool ModelAction::could_be_write() const
150 return is_write() || is_rmwr();
153 bool ModelAction::is_yield() const
155 return type == THREAD_YIELD;
158 bool ModelAction::is_rmwr() const
160 return type == ATOMIC_RMWR;
163 bool ModelAction::is_rmw() const
165 return type == ATOMIC_RMW;
168 bool ModelAction::is_rmwc() const
170 return type == ATOMIC_RMWC;
173 bool ModelAction::is_fence() const
175 return type == ATOMIC_FENCE;
178 bool ModelAction::is_initialization() const
180 return type == ATOMIC_INIT;
183 bool ModelAction::is_relaxed() const
185 return order == std::memory_order_relaxed;
188 bool ModelAction::is_acquire() const
191 case std::memory_order_acquire:
192 case std::memory_order_acq_rel:
193 case std::memory_order_seq_cst:
200 bool ModelAction::is_release() const
203 case std::memory_order_release:
204 case std::memory_order_acq_rel:
205 case std::memory_order_seq_cst:
212 bool ModelAction::is_seqcst() const
214 return order == std::memory_order_seq_cst;
217 bool ModelAction::same_var(const ModelAction *act) const
219 if (act->is_wait() || is_wait()) {
220 if (act->is_wait() && is_wait()) {
221 if (((void *)value) == ((void *)act->value))
223 } else if (is_wait()) {
224 if (((void *)value) == act->location)
226 } else if (act->is_wait()) {
227 if (location == ((void *)act->value))
232 return location == act->location;
235 bool ModelAction::same_thread(const ModelAction *act) const
237 return tid == act->tid;
240 void ModelAction::copy_typeandorder(ModelAction * act)
242 this->type = act->type;
243 this->order = act->order;
247 * Get the Thread which is the operand of this action. This is only valid for
248 * THREAD_* operations (currently only for THREAD_CREATE and THREAD_JOIN). Note
249 * that this provides a central place for determining the conventions of Thread
250 * storage in ModelAction, where we generally aren't very type-safe (e.g., we
251 * store object references in a (void *) address.
253 * For THREAD_CREATE, this yields the Thread which is created.
254 * For THREAD_JOIN, this yields the Thread we are joining with.
256 * @return The Thread which this action acts on, if exists; otherwise NULL
258 Thread * ModelAction::get_thread_operand() const
260 if (type == THREAD_CREATE) {
261 /* THREAD_CREATE stores its (Thread *) in a thrd_t::priv */
262 thrd_t *thrd = (thrd_t *)get_location();
264 } else if (type == THREAD_JOIN)
265 /* THREAD_JOIN uses (Thread *) for location */
266 return (Thread *)get_location();
271 /** This method changes an existing read part of an RMW action into either:
272 * (1) a full RMW action in case of the completed write or
273 * (2) a READ action in case a failed action.
274 * @todo If the memory_order changes, we may potentially need to update our
277 void ModelAction::process_rmw(ModelAction *act)
279 this->order = act->order;
281 this->type = ATOMIC_READ;
282 else if (act->is_rmw()) {
283 this->type = ATOMIC_RMW;
284 this->value = act->value;
288 /** The is_synchronizing method should only explore interleavings if:
289 * (1) the operations are seq_cst and don't commute or
290 * (2) the reordering may establish or break a synchronization relation.
291 * Other memory operations will be dealt with by using the reads_from
294 * @param act is the action to consider exploring a reordering.
295 * @return tells whether we have to explore a reordering.
297 bool ModelAction::could_synchronize_with(const ModelAction *act) const
299 // Same thread can't be reordered
300 if (same_thread(act))
303 // Different locations commute
307 // Explore interleavings of seqcst writes/fences to guarantee total
308 // order of seq_cst operations that don't commute
309 if ((could_be_write() || act->could_be_write() || is_fence() || act->is_fence()) && is_seqcst() && act->is_seqcst())
312 // Explore synchronizing read/write pairs
313 if (is_acquire() && act->is_release() && is_read() && act->could_be_write())
316 // lock just released...we can grab lock
317 if ((is_lock() || is_trylock()) && (act->is_unlock() || act->is_wait()))
320 // lock just acquired...we can fail to grab lock
321 if (is_trylock() && act->is_success_lock())
324 // other thread stalling on lock...we can release lock
325 if (is_unlock() && (act->is_trylock() || act->is_lock()))
328 if (is_trylock() && (act->is_unlock() || act->is_wait()))
331 if (is_notify() && act->is_wait())
334 if (is_wait() && act->is_notify())
337 // Otherwise handle by reads_from relation
341 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
343 // Must be different threads to reorder
344 if (same_thread(act))
347 // Try to reorder a lock past a successful lock
348 if (act->is_success_lock())
351 // Try to push a successful trylock past an unlock
352 if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
355 // Try to push a successful trylock past a wait
356 if (act->is_wait() && is_trylock() && value == VALUE_TRYSUCCESS)
363 * Create a new clock vector for this action. Note that this function allows a
364 * user to clobber (and leak) a ModelAction's existing clock vector. A user
365 * should ensure that the vector has already either been rolled back
366 * (effectively "freed") or freed.
368 * @param parent A ModelAction from which to inherit a ClockVector
370 void ModelAction::create_cv(const ModelAction *parent)
373 cv = new ClockVector(parent->cv, this);
375 cv = new ClockVector(NULL, this);
378 void ModelAction::set_try_lock(bool obtainedlock) {
380 value = VALUE_TRYSUCCESS;
382 value = VALUE_TRYFAILED;
386 * @brief Get the value read by this load
388 * We differentiate this function from ModelAction::get_write_value and
389 * ModelAction::get_value for the purpose of RMW's, which may have both a
390 * 'read' and a 'write' value.
392 * Note: 'this' must be a load.
394 * @return The value read by this load
396 uint64_t ModelAction::get_reads_from_value() const
400 return reads_from->get_write_value();
401 else if (reads_from_promise)
402 return reads_from_promise->get_value();
403 return VALUE_NONE; /* Only for new actions with no reads-from */
407 * @brief Get the value written by this store
409 * We differentiate this function from ModelAction::get_reads_from_value and
410 * ModelAction::get_value for the purpose of RMW's, which may have both a
411 * 'read' and a 'write' value.
413 * Note: 'this' must be a store.
415 * @return The value written by this store
417 uint64_t ModelAction::get_write_value() const
424 * @brief Get the value returned by this action
426 * For atomic reads (including RMW), an operation returns the value it read.
427 * For atomic writes, an operation returns the value it wrote. For other
428 * operations, the return value varies (sometimes is a "don't care"), but the
429 * value is simply stored in the "value" field.
431 * @return This action's return value
433 uint64_t ModelAction::get_return_value() const
436 return get_reads_from_value();
438 return get_write_value();
443 /** @return The Node associated with this ModelAction */
444 Node * ModelAction::get_node() const
446 /* UNINIT actions do not have a Node */
447 ASSERT(!is_uninitialized());
452 * Update the model action's read_from action
453 * @param act The action to read from; should be a write
455 void ModelAction::set_read_from(const ModelAction *act)
459 reads_from_promise = NULL;
460 if (act->is_uninitialized())
461 model->assert_bug("May read from uninitialized atomic\n");
465 * Set this action's read-from promise
466 * @param promise The promise to read from
468 void ModelAction::set_read_from_promise(Promise *promise)
471 reads_from_promise = promise;
476 * Synchronize the current thread with the thread corresponding to the
477 * ModelAction parameter.
478 * @param act The ModelAction to synchronize with
479 * @return True if this is a valid synchronization; false otherwise
481 bool ModelAction::synchronize_with(const ModelAction *act)
483 if (*this < *act && type != THREAD_JOIN && type != ATOMIC_LOCK)
485 model->check_promises(act->get_tid(), cv, act->cv);
490 bool ModelAction::has_synchronized_with(const ModelAction *act) const
492 return cv->synchronized_since(act);
496 * Check whether 'this' happens before act, according to the memory-model's
497 * happens before relation. This is checked via the ClockVector constructs.
498 * @return true if this action's thread has synchronized with act's thread
499 * since the execution of act, false otherwise.
501 bool ModelAction::happens_before(const ModelAction *act) const
503 return act->cv->synchronized_since(this);
506 /** @brief Print nicely-formatted info about this ModelAction */
507 void ModelAction::print() const
509 const char *type_str, *mo_str;
510 switch (this->type) {
511 case MODEL_FIXUP_RELSEQ:
512 type_str = "relseq fixup";
515 type_str = "thread create";
518 type_str = "thread start";
521 type_str = "thread yield";
524 type_str = "thread join";
527 type_str = "thread finish";
530 type_str = "uninitialized";
533 type_str = "atomic read";
536 type_str = "atomic write";
539 type_str = "atomic rmw";
545 type_str = "atomic rmwr";
548 type_str = "atomic rmwc";
551 type_str = "init atomic";
560 type_str = "trylock";
565 case ATOMIC_NOTIFY_ONE:
566 type_str = "notify one";
568 case ATOMIC_NOTIFY_ALL:
569 type_str = "notify all";
572 type_str = "unknown type";
575 switch (this->order) {
576 case std::memory_order_relaxed:
579 case std::memory_order_acquire:
582 case std::memory_order_release:
585 case std::memory_order_acq_rel:
588 case std::memory_order_seq_cst:
596 model_print("(%4d) Thread: %-2d Action: %-13s MO: %7s Loc: %14p Value: %-#18" PRIx64,
597 seq_number, id_to_int(tid), type_str, mo_str, location, get_return_value());
600 model_print(" Rf: %-3d", reads_from->get_seq_number());
601 else if (reads_from_promise) {
602 int idx = model->get_promise_number(reads_from_promise);
604 model_print(" Rf: P%-2d", idx);
606 model_print(" RF: P? ");
608 model_print(" Rf: ? ");
620 /** @brief Get a (likely) unique hash for this ModelAction */
621 unsigned int ModelAction::hash() const
623 unsigned int hash = (unsigned int)this->type;
624 hash ^= ((unsigned int)this->order) << 3;
625 hash ^= seq_number << 5;
626 hash ^= id_to_int(tid) << 6;
628 if (is_read() && reads_from)
629 hash ^= reads_from->get_seq_number();
634 * @brief Checks the NodeStack to see if a ModelAction is in our may-read-from set
635 * @param write The ModelAction to check for
636 * @return True if the ModelAction is found; false otherwise
638 bool ModelAction::may_read_from(const ModelAction *write) const
640 for (int i = 0; i < node->get_read_from_past_size(); i++)
641 if (node->get_read_from_past(i) == write)
647 * @brief Checks the NodeStack to see if a Promise is in our may-read-from set
648 * @param promise The Promise to check for
649 * @return True if the Promise is found; false otherwise
651 bool ModelAction::may_read_from(const Promise *promise) const
653 for (int i = 0; i < node->get_read_from_promise_size(); i++)
654 if (node->get_read_from_promise(i) == promise)
660 * Only valid for LOCK, TRY_LOCK, UNLOCK, and WAIT operations.
661 * @return The mutex operated on by this action, if any; otherwise NULL
663 std::mutex * ModelAction::get_mutex() const
665 if (is_trylock() || is_lock() || is_unlock())
666 return (std::mutex *)get_location();
668 return (std::mutex *)get_value();