2 #define __STDC_FORMAT_MACROS
8 #include "clockvector.h"
10 #include "threads-model.h"
13 #define ACTION_INITIAL_CLOCK 0
15 /** @brief A special value to represent a successful trylock */
16 #define VALUE_TRYSUCCESS 1
18 /** @brief A special value to represent a failed trylock */
19 #define VALUE_TRYFAILED 0
22 * @brief Construct a new ModelAction
24 * @param type The type of action
25 * @param order The memory order of this action. A "don't care" for non-ATOMIC
26 * actions (e.g., THREAD_* or MODEL_* actions).
27 * @param loc The location that this action acts upon
28 * @param value (optional) A value associated with the action (e.g., the value
29 * read or written). Defaults to a given macro constant, for debugging purposes.
30 * @param thread (optional) The Thread in which this action occurred. If NULL
31 * (default), then a Thread is assigned according to the scheduler.
33 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc,
34 uint64_t value, Thread *thread) :
38 last_fence_release(NULL),
45 original_order(order),
46 seq_number(ACTION_INITIAL_CLOCK)
48 /* References to NULL atomic variables can end up here */
49 ASSERT(loc || type == ATOMIC_FENCE || type == NOOP);
51 Thread *t = thread ? thread : thread_current();
52 this->tid = t->get_id();
57 * @brief Construct a new ModelAction
59 * @param type The type of action
60 * @param order The memory order of this action. A "don't care" for non-ATOMIC
61 * actions (e.g., THREAD_* or MODEL_* actions).
62 * @param loc The location that this action acts upon
63 * @param value (optional) A value associated with the action (e.g., the value
64 * read or written). Defaults to a given macro constant, for debugging purposes.
65 * @param size (optional) The Thread in which this action occurred. If NULL
66 * (default), then a Thread is assigned according to the scheduler.
68 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc,
69 uint64_t value, int size) :
73 last_fence_release(NULL),
80 original_order(order),
81 seq_number(ACTION_INITIAL_CLOCK)
83 /* References to NULL atomic variables can end up here */
86 Thread *t = thread_current();
87 this->tid = t->get_id();
92 * @brief Construct a new ModelAction with source line number (requires llvm support)
94 * @param type The type of action
95 * @param order The memory order of this action. A "don't care" for non-ATOMIC
96 * actions (e.g., THREAD_* or MODEL_* actions).
97 * @param loc The location that this action acts upon
98 * @param value (optional) A value associated with the action (e.g., the value
99 * read or written). Defaults to a given macro constant, for debugging purposes.
100 * @param size (optional) The Thread in which this action occurred. If NULL
101 * (default), then a Thread is assigned according to the scheduler.
103 ModelAction::ModelAction(action_type_t type, const char * position, memory_order order, void *loc,
104 uint64_t value, int size) :
108 last_fence_release(NULL),
115 original_order(order),
116 seq_number(ACTION_INITIAL_CLOCK)
118 /* References to NULL atomic variables can end up here */
121 Thread *t = thread_current();
122 this->tid = t->get_id();
127 * @brief Construct a new ModelAction with source line number (requires llvm support)
129 * @param type The type of action
130 * @param position The source line number of this atomic operation
131 * @param order The memory order of this action. A "don't care" for non-ATOMIC
132 * actions (e.g., THREAD_* or MODEL_* actions).
133 * @param loc The location that this action acts upon
134 * @param value (optional) A value associated with the action (e.g., the value
135 * read or written). Defaults to a given macro constant, for debugging purposes.
136 * @param thread (optional) The Thread in which this action occurred. If NULL
137 * (default), then a Thread is assigned according to the scheduler.
139 ModelAction::ModelAction(action_type_t type, const char * position, memory_order order,
140 void *loc, uint64_t value, Thread *thread) :
144 last_fence_release(NULL),
151 original_order(order),
152 seq_number(ACTION_INITIAL_CLOCK)
154 /* References to NULL atomic variables can end up here */
155 ASSERT(loc || type == ATOMIC_FENCE);
157 Thread *t = thread ? thread : thread_current();
158 this->tid = t->get_id();
159 // model_print("position: %s\n", position);
163 /** @brief ModelAction destructor */
164 ModelAction::~ModelAction()
167 * We can't free the clock vector:
168 * Clock vectors are snapshotting state. When we delete model actions,
169 * they are at the end of the node list and have invalid old clock
170 * vectors which have already been rolled back to an unallocated state.
178 int ModelAction::getSize() const {
182 void ModelAction::copy_from_new(ModelAction *newaction)
184 seq_number = newaction->seq_number;
187 void ModelAction::set_seq_number(modelclock_t num)
189 /* ATOMIC_UNINIT actions should never have non-zero clock */
190 ASSERT(!is_uninitialized());
191 ASSERT(seq_number == ACTION_INITIAL_CLOCK);
195 bool ModelAction::is_thread_start() const
197 return type == THREAD_START;
200 bool ModelAction::is_thread_join() const
202 return type == THREAD_JOIN || type == PTHREAD_JOIN;
205 bool ModelAction::is_mutex_op() const
207 return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK || type == ATOMIC_WAIT || type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
210 bool ModelAction::is_lock() const
212 return type == ATOMIC_LOCK;
215 bool ModelAction::is_wait() const {
216 return type == ATOMIC_WAIT;
219 bool ModelAction::is_notify() const {
220 return type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
223 bool ModelAction::is_notify_one() const {
224 return type == ATOMIC_NOTIFY_ONE;
227 bool ModelAction::is_unlock() const
229 return type == ATOMIC_UNLOCK;
232 bool ModelAction::is_trylock() const
234 return type == ATOMIC_TRYLOCK;
237 bool ModelAction::is_success_lock() const
239 return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
242 bool ModelAction::is_failed_trylock() const
244 return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
247 /** @return True if this operation is performed on a C/C++ atomic variable */
248 bool ModelAction::is_atomic_var() const
250 return is_read() || could_be_write();
253 bool ModelAction::is_uninitialized() const
255 return type == ATOMIC_UNINIT;
258 bool ModelAction::is_read() const
260 return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMWRCAS || type == ATOMIC_RMW;
263 bool ModelAction::is_write() const
265 return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT || type == ATOMIC_UNINIT || type == NONATOMIC_WRITE;
268 bool ModelAction::could_be_write() const
270 return is_write() || is_rmwr();
273 bool ModelAction::is_yield() const
275 return type == THREAD_YIELD;
278 bool ModelAction::is_rmwr() const
280 return type == ATOMIC_RMWR || type == ATOMIC_RMWRCAS;
283 bool ModelAction::is_rmwrcas() const
285 return type == ATOMIC_RMWRCAS;
288 bool ModelAction::is_rmw() const
290 return type == ATOMIC_RMW;
293 bool ModelAction::is_rmwc() const
295 return type == ATOMIC_RMWC;
298 bool ModelAction::is_fence() const
300 return type == ATOMIC_FENCE;
303 bool ModelAction::is_initialization() const
305 return type == ATOMIC_INIT;
308 bool ModelAction::is_annotation() const
310 return type == ATOMIC_ANNOTATION;
313 bool ModelAction::is_relaxed() const
315 return order == std::memory_order_relaxed;
318 bool ModelAction::is_acquire() const
321 case std::memory_order_acquire:
322 case std::memory_order_acq_rel:
323 case std::memory_order_seq_cst:
330 bool ModelAction::is_release() const
333 case std::memory_order_release:
334 case std::memory_order_acq_rel:
335 case std::memory_order_seq_cst:
342 bool ModelAction::is_seqcst() const
344 return order == std::memory_order_seq_cst;
347 bool ModelAction::same_var(const ModelAction *act) const
349 if (act->is_wait() || is_wait()) {
350 if (act->is_wait() && is_wait()) {
351 if (((void *)value) == ((void *)act->value))
353 } else if (is_wait()) {
354 if (((void *)value) == act->location)
356 } else if (act->is_wait()) {
357 if (location == ((void *)act->value))
362 return location == act->location;
365 bool ModelAction::same_thread(const ModelAction *act) const
367 return tid == act->tid;
370 void ModelAction::copy_typeandorder(ModelAction * act)
372 this->type = act->type;
373 this->order = act->order;
377 * Get the Thread which is the operand of this action. This is only valid for
378 * THREAD_* operations (currently only for THREAD_CREATE and THREAD_JOIN). Note
379 * that this provides a central place for determining the conventions of Thread
380 * storage in ModelAction, where we generally aren't very type-safe (e.g., we
381 * store object references in a (void *) address.
383 * For THREAD_CREATE, this yields the Thread which is created.
384 * For THREAD_JOIN, this yields the Thread we are joining with.
386 * @return The Thread which this action acts on, if exists; otherwise NULL
388 Thread * ModelAction::get_thread_operand() const
390 if (type == THREAD_CREATE) {
391 /* thread_operand is set in execution.cc */
392 return thread_operand;
393 } else if (type == PTHREAD_CREATE) {
394 return thread_operand;
395 } else if (type == THREAD_JOIN) {
396 /* THREAD_JOIN uses (Thread *) for location */
397 return (Thread *)get_location();
398 } else if (type == PTHREAD_JOIN) {
400 // thread_operand is stored in execution::pthread_map;
401 return (Thread *)get_location();
407 * @brief Convert the read portion of an RMW
409 * Changes an existing read part of an RMW action into either:
410 * -# a full RMW action in case of the completed write or
411 * -# a READ action in case a failed action.
413 * @todo If the memory_order changes, we may potentially need to update our
416 * @param act The second half of the RMW (either RMWC or RMW)
418 void ModelAction::process_rmw(ModelAction *act)
420 this->order = act->order;
422 this->type = ATOMIC_READ;
423 else if (act->is_rmw()) {
424 this->type = ATOMIC_RMW;
425 this->value = act->value;
430 * @brief Check if this action should be backtracked with another, due to
431 * potential synchronization
433 * The is_synchronizing method should only explore interleavings if:
434 * -# the operations are seq_cst and don't commute or
435 * -# the reordering may establish or break a synchronization relation.
437 * Other memory operations will be dealt with by using the reads_from relation.
439 * @param act The action to consider exploring a reordering
440 * @return True, if we have to explore a reordering; otherwise false
442 bool ModelAction::could_synchronize_with(const ModelAction *act) const
444 // Same thread can't be reordered
445 if (same_thread(act))
448 // Different locations commute
449 if (!same_var(act) && !is_fence() && !act->is_fence())
452 // Explore interleavings of seqcst writes/fences to guarantee total
453 // order of seq_cst operations that don't commute
454 if ((could_be_write() || act->could_be_write() || is_fence() || act->is_fence()) && is_seqcst() && act->is_seqcst())
457 // Explore synchronizing read/write pairs
458 if (is_acquire() && act->is_release() && is_read() && act->could_be_write())
461 // lock just released...we can grab lock
462 if ((is_lock() || is_trylock()) && (act->is_unlock() || act->is_wait()))
465 // lock just acquired...we can fail to grab lock
466 if (is_trylock() && act->is_success_lock())
469 // other thread stalling on lock...we can release lock
470 if (is_unlock() && (act->is_trylock() || act->is_lock()))
473 if (is_trylock() && (act->is_unlock() || act->is_wait()))
476 if (is_notify() && act->is_wait())
479 if (is_wait() && act->is_notify())
482 // Otherwise handle by reads_from relation
486 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
488 // Must be different threads to reorder
489 if (same_thread(act))
492 // Try to reorder a lock past a successful lock
493 if (act->is_success_lock())
496 // Try to push a successful trylock past an unlock
497 if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
500 // Try to push a successful trylock past a wait
501 if (act->is_wait() && is_trylock() && value == VALUE_TRYSUCCESS)
508 * Create a new clock vector for this action. Note that this function allows a
509 * user to clobber (and leak) a ModelAction's existing clock vector. A user
510 * should ensure that the vector has already either been rolled back
511 * (effectively "freed") or freed.
513 * @param parent A ModelAction from which to inherit a ClockVector
515 void ModelAction::create_cv(const ModelAction *parent)
518 cv = new ClockVector(parent->cv, this);
520 cv = new ClockVector(NULL, this);
523 void ModelAction::set_try_lock(bool obtainedlock)
525 value = obtainedlock ? VALUE_TRYSUCCESS : VALUE_TRYFAILED;
529 * @brief Get the value read by this load
531 * We differentiate this function from ModelAction::get_write_value and
532 * ModelAction::get_value for the purpose of RMW's, which may have both a
533 * 'read' and a 'write' value.
535 * Note: 'this' must be a load.
537 * @return The value read by this load
539 uint64_t ModelAction::get_reads_from_value() const
543 return reads_from->get_write_value();
545 return VALUE_NONE; // Only for new actions with no reads-from
549 * @brief Get the value written by this store
551 * We differentiate this function from ModelAction::get_reads_from_value and
552 * ModelAction::get_value for the purpose of RMW's, which may have both a
553 * 'read' and a 'write' value.
555 * Note: 'this' must be a store.
557 * @return The value written by this store
559 uint64_t ModelAction::get_write_value() const
566 * @brief Get the value returned by this action
568 * For atomic reads (including RMW), an operation returns the value it read.
569 * For atomic writes, an operation returns the value it wrote. For other
570 * operations, the return value varies (sometimes is a "don't care"), but the
571 * value is simply stored in the "value" field.
573 * @return This action's return value
575 uint64_t ModelAction::get_return_value() const
578 return get_reads_from_value();
580 return get_write_value();
586 * Update the model action's read_from action
587 * @param act The action to read from; should be a write
589 void ModelAction::set_read_from(ModelAction *act)
595 if (act->is_uninitialized()) { // WL
596 uint64_t val = *((uint64_t *) location);
597 ModelAction * act_uninitialized = (ModelAction *)act;
598 act_uninitialized->set_value(val);
599 reads_from = act_uninitialized;
601 // disabled by WL, because LLVM IR is unable to detect atomic init
602 /* model->assert_bug("May read from uninitialized atomic:\n"
603 " action %d, thread %d, location %p (%s, %s)",
604 seq_number, id_to_int(tid), location,
605 get_type_str(), get_mo_str());
611 * Synchronize the current thread with the thread corresponding to the
612 * ModelAction parameter.
613 * @param act The ModelAction to synchronize with
614 * @return True if this is a valid synchronization; false otherwise
616 bool ModelAction::synchronize_with(const ModelAction *act)
624 bool ModelAction::has_synchronized_with(const ModelAction *act) const
626 return cv->synchronized_since(act);
630 * Check whether 'this' happens before act, according to the memory-model's
631 * happens before relation. This is checked via the ClockVector constructs.
632 * @return true if this action's thread has synchronized with act's thread
633 * since the execution of act, false otherwise.
635 bool ModelAction::happens_before(const ModelAction *act) const
637 return act->cv->synchronized_since(this);
640 const char * ModelAction::get_type_str() const
642 switch (this->type) {
643 case THREAD_CREATE: return "thread create";
644 case THREAD_START: return "thread start";
645 case THREAD_YIELD: return "thread yield";
646 case THREAD_JOIN: return "thread join";
647 case THREAD_FINISH: return "thread finish";
649 case PTHREAD_CREATE: return "pthread create";
650 case PTHREAD_JOIN: return "pthread join";
652 case ATOMIC_UNINIT: return "uninitialized";
653 case NONATOMIC_WRITE: return "nonatomic write";
654 case ATOMIC_READ: return "atomic read";
655 case ATOMIC_WRITE: return "atomic write";
656 case ATOMIC_RMW: return "atomic rmw";
657 case ATOMIC_FENCE: return "fence";
658 case ATOMIC_RMWR: return "atomic rmwr";
659 case ATOMIC_RMWRCAS: return "atomic rmwrcas";
660 case ATOMIC_RMWC: return "atomic rmwc";
661 case ATOMIC_INIT: return "init atomic";
662 case ATOMIC_LOCK: return "lock";
663 case ATOMIC_UNLOCK: return "unlock";
664 case ATOMIC_TRYLOCK: return "trylock";
665 case ATOMIC_WAIT: return "wait";
666 case ATOMIC_NOTIFY_ONE: return "notify one";
667 case ATOMIC_NOTIFY_ALL: return "notify all";
668 case ATOMIC_ANNOTATION: return "annotation";
669 default: return "unknown type";
673 const char * ModelAction::get_mo_str() const
675 switch (this->order) {
676 case std::memory_order_relaxed: return "relaxed";
677 case std::memory_order_acquire: return "acquire";
678 case std::memory_order_release: return "release";
679 case std::memory_order_acq_rel: return "acq_rel";
680 case std::memory_order_seq_cst: return "seq_cst";
681 default: return "unknown";
685 /** @brief Print nicely-formatted info about this ModelAction */
686 void ModelAction::print() const
688 const char *type_str = get_type_str(), *mo_str = get_mo_str();
690 model_print("%-4d %-2d %-14s %7s %14p %-#18" PRIx64,
691 seq_number, id_to_int(tid), type_str, mo_str, location, get_return_value());
694 model_print(" %-3d", reads_from->get_seq_number());
708 /** @brief Get a (likely) unique hash for this ModelAction */
709 unsigned int ModelAction::hash() const
711 unsigned int hash = (unsigned int)this->type;
712 hash ^= ((unsigned int)this->order) << 3;
713 hash ^= seq_number << 5;
714 hash ^= id_to_int(tid) << 6;
718 hash ^= reads_from->get_seq_number();
719 hash ^= get_reads_from_value();
725 * Only valid for LOCK, TRY_LOCK, UNLOCK, and WAIT operations.
726 * @return The mutex operated on by this action, if any; otherwise NULL
728 cdsc::mutex * ModelAction::get_mutex() const
730 if (is_trylock() || is_lock() || is_unlock())
731 return (cdsc::mutex *)get_location();
733 return (cdsc::mutex *)get_value();