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),
48 original_order(order),
49 seq_number(ACTION_INITIAL_CLOCK)
51 /* References to NULL atomic variables can end up here */
52 ASSERT(loc || type == ATOMIC_FENCE);
54 Thread *t = thread ? thread : thread_current();
55 this->tid = t!= NULL ? t->get_id() : -1;
60 * @brief Construct a new ModelAction for sleep actions
62 * @param type The type of action: THREAD_SLEEP
63 * @param order The memory order of this action. A "don't care" for non-ATOMIC
64 * actions (e.g., THREAD_* or MODEL_* actions).
65 * @param loc The location that this action acts upon
66 * @param value The time duration a thread is scheduled to sleep.
67 * @param _time The this sleep action is constructed
69 ModelAction::ModelAction(action_type_t type, memory_order order, uint64_t value, uint64_t _time) :
73 last_fence_release(NULL),
83 original_order(order),
84 seq_number(ACTION_INITIAL_CLOCK)
86 Thread *t = thread_current();
87 this->tid = t!= NULL ? t->get_id() : -1;
91 * @brief Construct a new ModelAction
93 * @param type The type of action
94 * @param order The memory order of this action. A "don't care" for non-ATOMIC
95 * actions (e.g., THREAD_* or MODEL_* actions).
96 * @param loc The location that this action acts upon
97 * @param value (optional) A value associated with the action (e.g., the value
98 * read or written). Defaults to a given macro constant, for debugging purposes.
99 * @param size (optional) The Thread in which this action occurred. If NULL
100 * (default), then a Thread is assigned according to the scheduler.
102 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc,
103 uint64_t value, int size) :
107 last_fence_release(NULL),
117 original_order(order),
118 seq_number(ACTION_INITIAL_CLOCK)
120 /* References to NULL atomic variables can end up here */
123 Thread *t = thread_current();
124 this->tid = t->get_id();
129 * @brief Construct a new ModelAction with source line number (requires llvm support)
131 * @param type The type of action
132 * @param order The memory order of this action. A "don't care" for non-ATOMIC
133 * actions (e.g., THREAD_* or MODEL_* actions).
134 * @param loc The location that this action acts upon
135 * @param value (optional) A value associated with the action (e.g., the value
136 * read or written). Defaults to a given macro constant, for debugging purposes.
137 * @param size (optional) The Thread in which this action occurred. If NULL
138 * (default), then a Thread is assigned according to the scheduler.
140 ModelAction::ModelAction(action_type_t type, const char * position, memory_order order, void *loc,
141 uint64_t value, int size) :
145 last_fence_release(NULL),
155 original_order(order),
156 seq_number(ACTION_INITIAL_CLOCK)
158 /* References to NULL atomic variables can end up here */
161 Thread *t = thread_current();
162 this->tid = t->get_id();
167 * @brief Construct a new ModelAction with source line number (requires llvm support)
169 * @param type The type of action
170 * @param position The source line number of this atomic operation
171 * @param order The memory order of this action. A "don't care" for non-ATOMIC
172 * actions (e.g., THREAD_* or MODEL_* actions).
173 * @param loc The location that this action acts upon
174 * @param value (optional) A value associated with the action (e.g., the value
175 * read or written). Defaults to a given macro constant, for debugging purposes.
176 * @param thread (optional) The Thread in which this action occurred. If NULL
177 * (default), then a Thread is assigned according to the scheduler.
179 ModelAction::ModelAction(action_type_t type, const char * position, memory_order order,
180 void *loc, uint64_t value, Thread *thread) :
184 last_fence_release(NULL),
194 original_order(order),
195 seq_number(ACTION_INITIAL_CLOCK)
197 /* References to NULL atomic variables can end up here */
198 ASSERT(loc || type == ATOMIC_FENCE);
200 Thread *t = thread ? thread : thread_current();
201 this->tid = t->get_id();
205 /** @brief ModelAction destructor */
206 ModelAction::~ModelAction()
209 * We can't free the clock vector:
210 * Clock vectors are snapshotting state. When we delete model actions,
211 * they are at the end of the node list and have invalid old clock
212 * vectors which have already been rolled back to an unallocated state.
220 int ModelAction::getSize() const {
224 void ModelAction::copy_from_new(ModelAction *newaction)
226 seq_number = newaction->seq_number;
229 void ModelAction::set_seq_number(modelclock_t num)
231 /* ATOMIC_UNINIT actions should never have non-zero clock */
232 ASSERT(!is_uninitialized());
233 ASSERT(seq_number == ACTION_INITIAL_CLOCK);
237 void ModelAction::reset_seq_number()
242 bool ModelAction::is_thread_start() const
244 return type == THREAD_START;
247 bool ModelAction::is_thread_join() const
249 return type == THREAD_JOIN || type == PTHREAD_JOIN;
252 bool ModelAction::is_mutex_op() const
254 return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK || type == ATOMIC_WAIT || type == ATOMIC_TIMEDWAIT || type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
257 bool ModelAction::is_lock() const
259 return type == ATOMIC_LOCK;
262 bool ModelAction::is_sleep() const
264 return type == THREAD_SLEEP;
267 bool ModelAction::is_wait() const {
268 return type == ATOMIC_WAIT || type == ATOMIC_TIMEDWAIT;
271 bool ModelAction::is_notify() const {
272 return type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
275 bool ModelAction::is_notify_one() const {
276 return type == ATOMIC_NOTIFY_ONE;
279 bool ModelAction::is_unlock() const
281 return type == ATOMIC_UNLOCK;
284 bool ModelAction::is_trylock() const
286 return type == ATOMIC_TRYLOCK;
289 bool ModelAction::is_success_lock() const
291 return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
294 bool ModelAction::is_failed_trylock() const
296 return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
299 /** @return True if this operation is performed on a C/C++ atomic variable */
300 bool ModelAction::is_atomic_var() const
302 return is_read() || could_be_write();
305 bool ModelAction::is_uninitialized() const
307 return type == ATOMIC_UNINIT;
310 bool ModelAction::is_read() const
312 return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMWRCAS || type == ATOMIC_RMW;
315 bool ModelAction::is_write() const
317 return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT || type == ATOMIC_UNINIT || type == NONATOMIC_WRITE;
320 bool ModelAction::could_be_write() const
322 return is_write() || is_rmwr();
325 bool ModelAction::is_yield() const
327 return type == THREAD_YIELD;
330 bool ModelAction::is_rmwr() const
332 return type == ATOMIC_RMWR || type == ATOMIC_RMWRCAS;
335 bool ModelAction::is_rmwrcas() const
337 return type == ATOMIC_RMWRCAS;
340 bool ModelAction::is_rmw() const
342 return type == ATOMIC_RMW;
345 bool ModelAction::is_rmwc() const
347 return type == ATOMIC_RMWC;
350 bool ModelAction::is_fence() const
352 return type == ATOMIC_FENCE;
355 bool ModelAction::is_initialization() const
357 return type == ATOMIC_INIT;
360 bool ModelAction::is_annotation() const
362 return type == ATOMIC_ANNOTATION;
365 bool ModelAction::is_relaxed() const
367 return order == std::memory_order_relaxed;
370 bool ModelAction::is_acquire() const
373 case std::memory_order_acquire:
374 case std::memory_order_acq_rel:
375 case std::memory_order_seq_cst:
382 bool ModelAction::is_release() const
385 case std::memory_order_release:
386 case std::memory_order_acq_rel:
387 case std::memory_order_seq_cst:
394 bool ModelAction::is_seqcst() const
396 return order == std::memory_order_seq_cst;
399 bool ModelAction::same_var(const ModelAction *act) const
401 if (act->is_wait() || is_wait()) {
402 if (act->is_wait() && is_wait()) {
403 if (((void *)value) == ((void *)act->value))
405 } else if (is_wait()) {
406 if (((void *)value) == act->location)
408 } else if (act->is_wait()) {
409 if (location == ((void *)act->value))
414 return location == act->location;
417 bool ModelAction::same_thread(const ModelAction *act) const
419 return tid == act->tid;
422 void ModelAction::copy_typeandorder(ModelAction * act)
424 this->type = act->type;
425 this->order = act->order;
429 * Get the Thread which is the operand of this action. This is only valid for
430 * THREAD_* operations (currently only for THREAD_CREATE and THREAD_JOIN). Note
431 * that this provides a central place for determining the conventions of Thread
432 * storage in ModelAction, where we generally aren't very type-safe (e.g., we
433 * store object references in a (void *) address.
435 * For THREAD_CREATE, this yields the Thread which is created.
436 * For THREAD_JOIN, this yields the Thread we are joining with.
438 * @return The Thread which this action acts on, if exists; otherwise NULL
440 Thread * ModelAction::get_thread_operand() const
442 if (type == THREAD_CREATE) {
443 /* thread_operand is set in execution.cc */
444 return thread_operand;
445 } else if (type == PTHREAD_CREATE) {
446 return thread_operand;
447 } else if (type == THREAD_JOIN) {
448 /* THREAD_JOIN uses (Thread *) for location */
449 return (Thread *)get_location();
450 } else if (type == PTHREAD_JOIN) {
452 // thread_operand is stored in execution::pthread_map;
453 return (Thread *)get_location();
459 * @brief Convert the read portion of an RMW
461 * Changes an existing read part of an RMW action into either:
462 * -# a full RMW action in case of the completed write or
463 * -# a READ action in case a failed action.
465 * @todo If the memory_order changes, we may potentially need to update our
468 * @param act The second half of the RMW (either RMWC or RMW)
470 void ModelAction::process_rmw(ModelAction *act)
472 this->order = act->order;
474 this->type = ATOMIC_READ;
475 else if (act->is_rmw()) {
476 this->type = ATOMIC_RMW;
477 this->value = act->value;
482 * @brief Check if this action should be backtracked with another, due to
483 * potential synchronization
485 * The is_synchronizing method should only explore interleavings if:
486 * -# the operations are seq_cst and don't commute or
487 * -# the reordering may establish or break a synchronization relation.
489 * Other memory operations will be dealt with by using the reads_from relation.
491 * @param act The action to consider exploring a reordering
492 * @return True, if we have to explore a reordering; otherwise false
494 bool ModelAction::could_synchronize_with(const ModelAction *act) const
496 // Same thread can't be reordered
497 if (same_thread(act))
500 // Different locations commute
501 if (!same_var(act) && !is_fence() && !act->is_fence())
504 // Explore interleavings of seqcst writes/fences to guarantee total
505 // order of seq_cst operations that don't commute
506 if ((could_be_write() || act->could_be_write() || is_fence() || act->is_fence()) && is_seqcst() && act->is_seqcst())
509 // Explore synchronizing read/write pairs
510 if (is_acquire() && act->is_release() && is_read() && act->could_be_write())
513 // lock just released...we can grab lock
514 if ((is_lock() || is_trylock()) && (act->is_unlock() || act->is_wait()))
517 // lock just acquired...we can fail to grab lock
518 if (is_trylock() && act->is_success_lock())
521 // other thread stalling on lock...we can release lock
522 if (is_unlock() && (act->is_trylock() || act->is_lock()))
525 if (is_trylock() && (act->is_unlock() || act->is_wait()))
528 if (is_notify() && act->is_wait())
531 if (is_wait() && act->is_notify())
534 // Otherwise handle by reads_from relation
538 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
540 // Must be different threads to reorder
541 if (same_thread(act))
544 // Try to reorder a lock past a successful lock
545 if (act->is_success_lock())
548 // Try to push a successful trylock past an unlock
549 if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
552 // Try to push a successful trylock past a wait
553 if (act->is_wait() && is_trylock() && value == VALUE_TRYSUCCESS)
560 * Create a new clock vector for this action. Note that this function allows a
561 * user to clobber (and leak) a ModelAction's existing clock vector. A user
562 * should ensure that the vector has already either been rolled back
563 * (effectively "freed") or freed.
565 * @param parent A ModelAction from which to inherit a ClockVector
567 void ModelAction::create_cv(const ModelAction *parent)
570 cv = new ClockVector(parent->cv, this);
572 cv = new ClockVector(NULL, this);
575 void ModelAction::set_try_lock(bool obtainedlock)
577 value = obtainedlock ? VALUE_TRYSUCCESS : VALUE_TRYFAILED;
581 * @brief Get the value read by this load
583 * We differentiate this function from ModelAction::get_write_value and
584 * ModelAction::get_value for the purpose of RMW's, which may have both a
585 * 'read' and a 'write' value.
587 * Note: 'this' must be a load.
589 * @return The value read by this load
591 uint64_t ModelAction::get_reads_from_value() const
595 return reads_from->get_write_value();
597 return VALUE_NONE; // Only for new actions with no reads-from
601 * @brief Get the value written by this store
603 * We differentiate this function from ModelAction::get_reads_from_value and
604 * ModelAction::get_value for the purpose of RMW's, which may have both a
605 * 'read' and a 'write' value.
607 * Note: 'this' must be a store.
609 * @return The value written by this store
611 uint64_t ModelAction::get_write_value() const
618 * @brief Get the value returned by this action
620 * For atomic reads (including RMW), an operation returns the value it read.
621 * For atomic writes, an operation returns the value it wrote. For other
622 * operations, the return value varies (sometimes is a "don't care"), but the
623 * value is simply stored in the "value" field.
625 * @return This action's return value
627 uint64_t ModelAction::get_return_value() const
630 return get_reads_from_value();
632 return get_write_value();
638 * Update the model action's read_from action
639 * @param act The action to read from; should be a write
641 void ModelAction::set_read_from(ModelAction *act)
646 if (act->is_uninitialized()) { // WL
647 uint64_t val = *((uint64_t *) location);
648 ModelAction * act_uninitialized = (ModelAction *)act;
649 act_uninitialized->set_value(val);
650 reads_from = act_uninitialized;
652 // disabled by WL, because LLVM IR is unable to detect atomic init
653 /* model->assert_bug("May read from uninitialized atomic:\n"
654 " action %d, thread %d, location %p (%s, %s)",
655 seq_number, id_to_int(tid), location,
656 get_type_str(), get_mo_str());
662 * Synchronize the current thread with the thread corresponding to the
663 * ModelAction parameter.
664 * @param act The ModelAction to synchronize with
665 * @return True if this is a valid synchronization; false otherwise
667 bool ModelAction::synchronize_with(const ModelAction *act)
675 bool ModelAction::has_synchronized_with(const ModelAction *act) const
677 return cv->synchronized_since(act);
681 * Check whether 'this' happens before act, according to the memory-model's
682 * happens before relation. This is checked via the ClockVector constructs.
683 * @return true if this action's thread has synchronized with act's thread
684 * since the execution of act, false otherwise.
686 bool ModelAction::happens_before(const ModelAction *act) const
688 return act->cv->synchronized_since(this);
691 const char * ModelAction::get_type_str() const
693 switch (this->type) {
694 case THREAD_CREATE: return "thread create";
695 case THREAD_START: return "thread start";
696 case THREAD_YIELD: return "thread yield";
697 case THREAD_JOIN: return "thread join";
698 case THREAD_FINISH: return "thread finish";
699 case THREAD_SLEEP: return "thread sleep";
700 case THREADONLY_FINISH: return "pthread_exit finish";
702 case PTHREAD_CREATE: return "pthread create";
703 case PTHREAD_JOIN: return "pthread join";
705 case ATOMIC_UNINIT: return "uninitialized";
706 case NONATOMIC_WRITE: return "nonatomic write";
707 case ATOMIC_READ: return "atomic read";
708 case ATOMIC_WRITE: return "atomic write";
709 case ATOMIC_RMW: return "atomic rmw";
710 case ATOMIC_FENCE: return "fence";
711 case ATOMIC_RMWR: return "atomic rmwr";
712 case ATOMIC_RMWRCAS: return "atomic rmwrcas";
713 case ATOMIC_RMWC: return "atomic rmwc";
714 case ATOMIC_INIT: return "init atomic";
715 case ATOMIC_LOCK: return "lock";
716 case ATOMIC_UNLOCK: return "unlock";
717 case ATOMIC_TRYLOCK: return "trylock";
718 case ATOMIC_WAIT: return "wait";
719 case ATOMIC_TIMEDWAIT: return "timed wait";
720 case ATOMIC_NOTIFY_ONE: return "notify one";
721 case ATOMIC_NOTIFY_ALL: return "notify all";
722 case ATOMIC_ANNOTATION: return "annotation";
723 default: return "unknown type";
727 const char * ModelAction::get_mo_str() const
729 switch (this->order) {
730 case std::memory_order_relaxed: return "relaxed";
731 case std::memory_order_acquire: return "acquire";
732 case std::memory_order_release: return "release";
733 case std::memory_order_acq_rel: return "acq_rel";
734 case std::memory_order_seq_cst: return "seq_cst";
735 default: return "unknown";
739 /** @brief Print nicely-formatted info about this ModelAction */
740 void ModelAction::print() const
742 const char *type_str = get_type_str(), *mo_str = get_mo_str();
744 model_print("%-4d %-2d %-14s %7s %14p %-#18" PRIx64,
745 seq_number, id_to_int(tid), type_str, mo_str, location, get_return_value());
748 model_print(" %-3d", reads_from->get_seq_number());
762 /** @brief Get a (likely) unique hash for this ModelAction */
763 unsigned int ModelAction::hash() const
765 unsigned int hash = (unsigned int)this->type;
766 hash ^= ((unsigned int)this->order) << 3;
767 hash ^= seq_number << 5;
768 hash ^= id_to_int(tid) << 6;
772 hash ^= reads_from->get_seq_number();
773 hash ^= get_reads_from_value();
779 * Only valid for LOCK, TRY_LOCK, UNLOCK, and WAIT operations.
780 * @return The mutex operated on by this action, if any; otherwise NULL
782 cdsc::mutex * ModelAction::get_mutex() const
784 if (is_trylock() || is_lock() || is_unlock())
785 return (cdsc::mutex *)get_location();
787 return (cdsc::mutex *)get_value();