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),
47 original_type(ATOMIC_NOP),
50 original_order(order),
51 seq_number(ACTION_INITIAL_CLOCK)
53 /* References to NULL atomic variables can end up here */
54 ASSERT(loc || type == ATOMIC_FENCE || type == ATOMIC_NOP);
56 Thread *t = thread ? thread : thread_current();
57 this->tid = t!= NULL ? t->get_id() : -1;
62 * @brief Construct a new ModelAction for sleep actions
64 * @param type The type of action: THREAD_SLEEP
65 * @param order The memory order of this action. A "don't care" for non-ATOMIC
66 * actions (e.g., THREAD_* or MODEL_* actions).
67 * @param loc The location that this action acts upon
68 * @param value The time duration a thread is scheduled to sleep.
69 * @param _time The this sleep action is constructed
71 ModelAction::ModelAction(action_type_t type, memory_order order, uint64_t value, uint64_t _time) :
75 last_fence_release(NULL),
84 original_type(ATOMIC_NOP),
87 original_order(order),
88 seq_number(ACTION_INITIAL_CLOCK)
90 Thread *t = thread_current();
91 this->tid = t!= NULL ? t->get_id() : -1;
95 * @brief Construct a new ModelAction
97 * @param type The type of action
98 * @param order The memory order of this action. A "don't care" for non-ATOMIC
99 * actions (e.g., THREAD_* or MODEL_* actions).
100 * @param loc The location that this action acts upon
101 * @param value (optional) A value associated with the action (e.g., the value
102 * read or written). Defaults to a given macro constant, for debugging purposes.
103 * @param size (optional) The Thread in which this action occurred. If NULL
104 * (default), then a Thread is assigned according to the scheduler.
106 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc,
107 uint64_t value, int size) :
111 last_fence_release(NULL),
120 original_type(ATOMIC_NOP),
123 original_order(order),
124 seq_number(ACTION_INITIAL_CLOCK)
126 /* References to NULL atomic variables can end up here */
129 Thread *t = thread_current();
130 this->tid = t->get_id();
135 * @brief Construct a new ModelAction with source line number (requires llvm support)
137 * @param type The type of action
138 * @param order The memory order of this action. A "don't care" for non-ATOMIC
139 * actions (e.g., THREAD_* or MODEL_* actions).
140 * @param loc The location that this action acts upon
141 * @param value (optional) A value associated with the action (e.g., the value
142 * read or written). Defaults to a given macro constant, for debugging purposes.
143 * @param size (optional) The Thread in which this action occurred. If NULL
144 * (default), then a Thread is assigned according to the scheduler.
146 ModelAction::ModelAction(action_type_t type, const char * position, memory_order order, void *loc,
147 uint64_t value, int size) :
151 last_fence_release(NULL),
160 original_type(ATOMIC_NOP),
163 original_order(order),
164 seq_number(ACTION_INITIAL_CLOCK)
166 /* References to NULL atomic variables can end up here */
169 Thread *t = thread_current();
170 this->tid = t->get_id();
175 * @brief Construct a new ModelAction with source line number (requires llvm support)
177 * @param type The type of action
178 * @param position The source line number of this atomic operation
179 * @param order The memory order of this action. A "don't care" for non-ATOMIC
180 * actions (e.g., THREAD_* or MODEL_* actions).
181 * @param loc The location that this action acts upon
182 * @param value (optional) A value associated with the action (e.g., the value
183 * read or written). Defaults to a given macro constant, for debugging purposes.
184 * @param thread (optional) The Thread in which this action occurred. If NULL
185 * (default), then a Thread is assigned according to the scheduler.
187 ModelAction::ModelAction(action_type_t type, const char * position, memory_order order,
188 void *loc, uint64_t value, Thread *thread) :
192 last_fence_release(NULL),
201 original_type(ATOMIC_NOP),
204 original_order(order),
205 seq_number(ACTION_INITIAL_CLOCK)
207 /* References to NULL atomic variables can end up here */
208 ASSERT(loc || type == ATOMIC_FENCE);
210 Thread *t = thread ? thread : thread_current();
211 this->tid = t->get_id();
215 /** @brief ModelAction destructor */
216 ModelAction::~ModelAction()
219 * We can't free the clock vector:
220 * Clock vectors are snapshotting state. When we delete model actions,
221 * they are at the end of the node list and have invalid old clock
222 * vectors which have already been rolled back to an unallocated state.
230 int ModelAction::getSize() const {
234 void ModelAction::copy_from_new(ModelAction *newaction)
236 seq_number = newaction->seq_number;
239 void ModelAction::set_seq_number(modelclock_t num)
241 ASSERT(seq_number == ACTION_INITIAL_CLOCK);
245 void ModelAction::reset_seq_number()
250 bool ModelAction::is_thread_start() const
252 return type == THREAD_START;
255 bool ModelAction::is_thread_join() const
257 return type == THREAD_JOIN || type == PTHREAD_JOIN;
260 bool ModelAction::is_mutex_op() const
262 return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK || type == ATOMIC_WAIT || type == ATOMIC_TIMEDWAIT || type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
265 bool ModelAction::is_lock() const
267 return type == ATOMIC_LOCK;
270 bool ModelAction::is_sleep() const
272 return type == THREAD_SLEEP;
275 bool ModelAction::is_wait() const {
276 return type == ATOMIC_WAIT || type == ATOMIC_TIMEDWAIT;
279 bool ModelAction::is_notify() const {
280 return type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
283 bool ModelAction::is_notify_one() const {
284 return type == ATOMIC_NOTIFY_ONE;
287 bool ModelAction::is_unlock() const
289 return type == ATOMIC_UNLOCK;
292 bool ModelAction::is_trylock() const
294 return type == ATOMIC_TRYLOCK;
297 bool ModelAction::is_success_lock() const
299 return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
302 bool ModelAction::is_failed_trylock() const
304 return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
307 /** @return True if this operation is performed on a C/C++ atomic variable */
308 bool ModelAction::is_atomic_var() const
310 return is_read() || could_be_write();
313 bool ModelAction::is_read() const
315 return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMWRCAS || type == ATOMIC_RMW;
318 bool ModelAction::is_write() const
320 return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT || type == NONATOMIC_WRITE;
323 bool ModelAction::is_create() const
325 return type == THREAD_CREATE || type == PTHREAD_CREATE;
328 bool ModelAction::is_free() const
330 return type == READY_FREE;
333 bool ModelAction::could_be_write() const
335 return is_write() || is_rmwr();
338 bool ModelAction::is_yield() const
340 return type == THREAD_YIELD;
343 bool ModelAction::is_rmwr() const
345 return type == ATOMIC_RMWR || type == ATOMIC_RMWRCAS;
348 bool ModelAction::is_rmwrcas() const
350 return type == ATOMIC_RMWRCAS;
353 bool ModelAction::is_rmw() const
355 return type == ATOMIC_RMW;
358 bool ModelAction::is_rmwc() const
360 return type == ATOMIC_RMWC;
363 bool ModelAction::is_fence() const
365 return type == ATOMIC_FENCE;
368 bool ModelAction::is_initialization() const
370 return type == ATOMIC_INIT;
373 bool ModelAction::is_annotation() const
375 return type == ATOMIC_ANNOTATION;
378 bool ModelAction::is_relaxed() const
380 return order == std::memory_order_relaxed;
383 bool ModelAction::is_acquire() const
386 case std::memory_order_acquire:
387 case std::memory_order_acq_rel:
388 case std::memory_order_seq_cst:
395 bool ModelAction::is_release() const
398 case std::memory_order_release:
399 case std::memory_order_acq_rel:
400 case std::memory_order_seq_cst:
407 bool ModelAction::is_seqcst() const
409 return order == std::memory_order_seq_cst;
412 bool ModelAction::same_var(const ModelAction *act) const
414 if (act->is_wait() || is_wait()) {
415 if (act->is_wait() && is_wait()) {
416 if (((void *)value) == ((void *)act->value))
418 } else if (is_wait()) {
419 if (((void *)value) == act->location)
421 } else if (act->is_wait()) {
422 if (location == ((void *)act->value))
427 return location == act->location;
430 bool ModelAction::same_thread(const ModelAction *act) const
432 return tid == act->tid;
435 void ModelAction::copy_typeandorder(ModelAction * act)
437 this->type = act->type;
438 this->order = act->order;
442 * Get the Thread which is the operand of this action. This is only valid for
443 * THREAD_* operations (currently only for THREAD_CREATE and THREAD_JOIN). Note
444 * that this provides a central place for determining the conventions of Thread
445 * storage in ModelAction, where we generally aren't very type-safe (e.g., we
446 * store object references in a (void *) address.
448 * For THREAD_CREATE, this yields the Thread which is created.
449 * For THREAD_JOIN, this yields the Thread we are joining with.
451 * @return The Thread which this action acts on, if exists; otherwise NULL
453 Thread * ModelAction::get_thread_operand() const
455 if (type == THREAD_CREATE) {
456 /* thread_operand is set in execution.cc */
457 return thread_operand;
458 } else if (type == PTHREAD_CREATE) {
459 return thread_operand;
460 } else if (type == THREAD_JOIN) {
461 /* THREAD_JOIN uses (Thread *) for location */
462 return (Thread *)get_location();
463 } else if (type == PTHREAD_JOIN) {
465 // thread_operand is stored in execution::pthread_map;
466 return (Thread *)get_location();
472 * @brief Convert the read portion of an RMW
474 * Changes an existing read part of an RMW action into either:
475 * -# a full RMW action in case of the completed write or
476 * -# a READ action in case a failed action.
478 * @todo If the memory_order changes, we may potentially need to update our
481 * @param act The second half of the RMW (either RMWC or RMW)
483 void ModelAction::process_rmw(ModelAction *act)
485 this->order = act->order;
487 this->type = ATOMIC_READ;
488 else if (act->is_rmw()) {
489 this->type = ATOMIC_RMW;
490 this->value = act->value;
495 * @brief Check if this action should be backtracked with another, due to
496 * potential synchronization
498 * The is_synchronizing method should only explore interleavings if:
499 * -# the operations are seq_cst and don't commute or
500 * -# the reordering may establish or break a synchronization relation.
502 * Other memory operations will be dealt with by using the reads_from relation.
504 * @param act The action to consider exploring a reordering
505 * @return True, if we have to explore a reordering; otherwise false
507 bool ModelAction::could_synchronize_with(const ModelAction *act) const
509 // Same thread can't be reordered
510 if (same_thread(act))
513 // Different locations commute
514 if (!same_var(act) && !is_fence() && !act->is_fence())
517 // Explore interleavings of seqcst writes/fences to guarantee total
518 // order of seq_cst operations that don't commute
519 if ((could_be_write() || act->could_be_write() || is_fence() || act->is_fence()) && is_seqcst() && act->is_seqcst())
522 // Explore synchronizing read/write pairs
523 if (is_acquire() && act->is_release() && is_read() && act->could_be_write())
526 // lock just released...we can grab lock
527 if ((is_lock() || is_trylock()) && (act->is_unlock() || act->is_wait()))
530 // lock just acquired...we can fail to grab lock
531 if (is_trylock() && act->is_success_lock())
534 // other thread stalling on lock...we can release lock
535 if (is_unlock() && (act->is_trylock() || act->is_lock()))
538 if (is_trylock() && (act->is_unlock() || act->is_wait()))
541 if (is_notify() && act->is_wait())
544 if (is_wait() && act->is_notify())
547 // Otherwise handle by reads_from relation
551 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
553 // Must be different threads to reorder
554 if (same_thread(act))
557 // Try to reorder a lock past a successful lock
558 if (act->is_success_lock())
561 // Try to push a successful trylock past an unlock
562 if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
565 // Try to push a successful trylock past a wait
566 if (act->is_wait() && is_trylock() && value == VALUE_TRYSUCCESS)
573 * Create a new clock vector for this action. Note that this function allows a
574 * user to clobber (and leak) a ModelAction's existing clock vector. A user
575 * should ensure that the vector has already either been rolled back
576 * (effectively "freed") or freed.
578 * @param parent A ModelAction from which to inherit a ClockVector
580 void ModelAction::create_cv(const ModelAction *parent)
583 cv = new ClockVector(parent->cv, this);
585 cv = new ClockVector(NULL, this);
588 void ModelAction::set_try_lock(bool obtainedlock)
590 value = obtainedlock ? VALUE_TRYSUCCESS : VALUE_TRYFAILED;
594 * @brief Get the value read by this load
596 * We differentiate this function from ModelAction::get_write_value and
597 * ModelAction::get_value for the purpose of RMW's, which may have both a
598 * 'read' and a 'write' value.
600 * Note: 'this' must be a load.
602 * @return The value read by this load
604 uint64_t ModelAction::get_reads_from_value() const
608 return reads_from->get_write_value();
610 return VALUE_NONE; // Only for new actions with no reads-from
614 * @brief Get the value written by this store
616 * We differentiate this function from ModelAction::get_reads_from_value and
617 * ModelAction::get_value for the purpose of RMW's, which may have both a
618 * 'read' and a 'write' value.
620 * Note: 'this' must be a store.
622 * @return The value written by this store
624 uint64_t ModelAction::get_write_value() const
631 * @brief Get the value returned by this action
633 * For atomic reads (including RMW), an operation returns the value it read.
634 * For atomic writes, an operation returns the value it wrote. For other
635 * operations, the return value varies (sometimes is a "don't care"), but the
636 * value is simply stored in the "value" field.
638 * @return This action's return value
640 uint64_t ModelAction::get_return_value() const
643 return get_reads_from_value();
645 return get_write_value();
651 * Update the model action's read_from action
652 * @param act The action to read from; should be a write
654 void ModelAction::set_read_from(ModelAction *act)
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 NONATOMIC_WRITE: return "nonatomic write";
706 case ATOMIC_READ: return "atomic read";
707 case ATOMIC_WRITE: return "atomic write";
708 case ATOMIC_RMW: return "atomic rmw";
709 case ATOMIC_FENCE: return "fence";
710 case ATOMIC_RMWR: return "atomic rmwr";
711 case ATOMIC_RMWRCAS: return "atomic rmwrcas";
712 case ATOMIC_RMWC: return "atomic rmwc";
713 case ATOMIC_INIT: return "init atomic";
714 case ATOMIC_LOCK: return "lock";
715 case ATOMIC_UNLOCK: return "unlock";
716 case ATOMIC_TRYLOCK: return "trylock";
717 case ATOMIC_WAIT: return "wait";
718 case ATOMIC_TIMEDWAIT: return "timed wait";
719 case ATOMIC_NOTIFY_ONE: return "notify one";
720 case ATOMIC_NOTIFY_ALL: return "notify all";
721 case ATOMIC_ANNOTATION: return "annotation";
722 default: return "unknown type";
726 const char * ModelAction::get_mo_str() const
728 switch (this->order) {
729 case std::memory_order_relaxed: return "relaxed";
730 case std::memory_order_acquire: return "acquire";
731 case std::memory_order_release: return "release";
732 case std::memory_order_acq_rel: return "acq_rel";
733 case std::memory_order_seq_cst: return "seq_cst";
734 default: return "unknown";
738 /** @brief Print nicely-formatted info about this ModelAction */
739 void ModelAction::print() const
741 const char *type_str = get_type_str(), *mo_str = get_mo_str();
743 model_print("%-4d %-2d %-14s %7s %14p %-#18" PRIx64,
744 seq_number, id_to_int(tid), type_str, mo_str, location, get_return_value());
747 model_print(" %-3d", reads_from->get_seq_number());
761 /** @brief Get a (likely) unique hash for this ModelAction */
762 unsigned int ModelAction::hash() const
764 unsigned int hash = (unsigned int)this->type;
765 hash ^= ((unsigned int)this->order) << 3;
766 hash ^= seq_number << 5;
767 hash ^= id_to_int(tid) << 6;
771 hash ^= reads_from->get_seq_number();
772 hash ^= get_reads_from_value();
778 * Only valid for LOCK, TRY_LOCK, UNLOCK, and WAIT operations.
779 * @return The mutex operated on by this action, if any; otherwise NULL
781 cdsc::mutex * ModelAction::get_mutex() const
783 if (is_trylock() || is_lock() || is_unlock())
784 return (cdsc::mutex *)get_location();
786 return (cdsc::mutex *)get_value();
791 /** @brief Swap type with original type */
792 void ModelAction::use_original_type()
794 action_type_t tmp = type;
795 type = original_type;