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!= NULL ? t->get_id() : -1;
57 * @brief Construct a new ModelAction for sleep actions
59 * @param type The type of action: THREAD_SLEEP
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 The time duration a thread is scheduled to sleep.
64 * @param _time The this sleep action is constructed
66 ModelAction::ModelAction(action_type_t type, memory_order order, uint64_t value, uint64_t _time) :
70 last_fence_release(NULL),
77 original_order(order),
78 seq_number(ACTION_INITIAL_CLOCK)
80 Thread *t = thread_current();
81 this->tid = t!= NULL ? t->get_id() : -1;
85 * @brief Construct a new ModelAction
87 * @param type The type of action
88 * @param order The memory order of this action. A "don't care" for non-ATOMIC
89 * actions (e.g., THREAD_* or MODEL_* actions).
90 * @param loc The location that this action acts upon
91 * @param value (optional) A value associated with the action (e.g., the value
92 * read or written). Defaults to a given macro constant, for debugging purposes.
93 * @param size (optional) The Thread in which this action occurred. If NULL
94 * (default), then a Thread is assigned according to the scheduler.
96 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc,
97 uint64_t value, int size) :
101 last_fence_release(NULL),
108 original_order(order),
109 seq_number(ACTION_INITIAL_CLOCK)
111 /* References to NULL atomic variables can end up here */
114 Thread *t = thread_current();
115 this->tid = t->get_id();
120 * @brief Construct a new ModelAction with source line number (requires llvm support)
122 * @param type The type of action
123 * @param order The memory order of this action. A "don't care" for non-ATOMIC
124 * actions (e.g., THREAD_* or MODEL_* actions).
125 * @param loc The location that this action acts upon
126 * @param value (optional) A value associated with the action (e.g., the value
127 * read or written). Defaults to a given macro constant, for debugging purposes.
128 * @param size (optional) The Thread in which this action occurred. If NULL
129 * (default), then a Thread is assigned according to the scheduler.
131 ModelAction::ModelAction(action_type_t type, const char * position, memory_order order, void *loc,
132 uint64_t value, int size) :
136 last_fence_release(NULL),
143 original_order(order),
144 seq_number(ACTION_INITIAL_CLOCK)
146 /* References to NULL atomic variables can end up here */
149 Thread *t = thread_current();
150 this->tid = t->get_id();
155 * @brief Construct a new ModelAction with source line number (requires llvm support)
157 * @param type The type of action
158 * @param position The source line number of this atomic operation
159 * @param order The memory order of this action. A "don't care" for non-ATOMIC
160 * actions (e.g., THREAD_* or MODEL_* actions).
161 * @param loc The location that this action acts upon
162 * @param value (optional) A value associated with the action (e.g., the value
163 * read or written). Defaults to a given macro constant, for debugging purposes.
164 * @param thread (optional) The Thread in which this action occurred. If NULL
165 * (default), then a Thread is assigned according to the scheduler.
167 ModelAction::ModelAction(action_type_t type, const char * position, memory_order order,
168 void *loc, uint64_t value, Thread *thread) :
172 last_fence_release(NULL),
179 original_order(order),
180 seq_number(ACTION_INITIAL_CLOCK)
182 /* References to NULL atomic variables can end up here */
183 ASSERT(loc || type == ATOMIC_FENCE);
185 Thread *t = thread ? thread : thread_current();
186 this->tid = t->get_id();
190 /** @brief ModelAction destructor */
191 ModelAction::~ModelAction()
194 * We can't free the clock vector:
195 * Clock vectors are snapshotting state. When we delete model actions,
196 * they are at the end of the node list and have invalid old clock
197 * vectors which have already been rolled back to an unallocated state.
205 int ModelAction::getSize() const {
209 void ModelAction::copy_from_new(ModelAction *newaction)
211 seq_number = newaction->seq_number;
214 void ModelAction::set_seq_number(modelclock_t num)
216 /* ATOMIC_UNINIT actions should never have non-zero clock */
217 ASSERT(!is_uninitialized());
218 ASSERT(seq_number == ACTION_INITIAL_CLOCK);
222 void ModelAction::reset_seq_number()
227 bool ModelAction::is_thread_start() const
229 return type == THREAD_START;
232 bool ModelAction::is_thread_join() const
234 return type == THREAD_JOIN || type == PTHREAD_JOIN;
237 bool ModelAction::is_mutex_op() const
239 return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK || type == ATOMIC_WAIT || type == ATOMIC_TIMEDWAIT || type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
242 bool ModelAction::is_lock() const
244 return type == ATOMIC_LOCK;
247 bool ModelAction::is_sleep() const
249 return type == THREAD_SLEEP;
252 bool ModelAction::is_wait() const {
253 return type == ATOMIC_WAIT || type == ATOMIC_TIMEDWAIT;
256 bool ModelAction::is_notify() const {
257 return type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
260 bool ModelAction::is_notify_one() const {
261 return type == ATOMIC_NOTIFY_ONE;
264 bool ModelAction::is_unlock() const
266 return type == ATOMIC_UNLOCK;
269 bool ModelAction::is_trylock() const
271 return type == ATOMIC_TRYLOCK;
274 bool ModelAction::is_success_lock() const
276 return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
279 bool ModelAction::is_failed_trylock() const
281 return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
284 /** @return True if this operation is performed on a C/C++ atomic variable */
285 bool ModelAction::is_atomic_var() const
287 return is_read() || could_be_write();
290 bool ModelAction::is_uninitialized() const
292 return type == ATOMIC_UNINIT;
295 bool ModelAction::is_read() const
297 return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMWRCAS || type == ATOMIC_RMW;
300 bool ModelAction::is_write() const
302 return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT || type == ATOMIC_UNINIT || type == NONATOMIC_WRITE;
305 bool ModelAction::could_be_write() const
307 return is_write() || is_rmwr();
310 bool ModelAction::is_yield() const
312 return type == THREAD_YIELD;
315 bool ModelAction::is_rmwr() const
317 return type == ATOMIC_RMWR || type == ATOMIC_RMWRCAS;
320 bool ModelAction::is_rmwrcas() const
322 return type == ATOMIC_RMWRCAS;
325 bool ModelAction::is_rmw() const
327 return type == ATOMIC_RMW;
330 bool ModelAction::is_rmwc() const
332 return type == ATOMIC_RMWC;
335 bool ModelAction::is_fence() const
337 return type == ATOMIC_FENCE;
340 bool ModelAction::is_initialization() const
342 return type == ATOMIC_INIT;
345 bool ModelAction::is_annotation() const
347 return type == ATOMIC_ANNOTATION;
350 bool ModelAction::is_relaxed() const
352 return order == std::memory_order_relaxed;
355 bool ModelAction::is_acquire() const
358 case std::memory_order_acquire:
359 case std::memory_order_acq_rel:
360 case std::memory_order_seq_cst:
367 bool ModelAction::is_release() const
370 case std::memory_order_release:
371 case std::memory_order_acq_rel:
372 case std::memory_order_seq_cst:
379 bool ModelAction::is_seqcst() const
381 return order == std::memory_order_seq_cst;
384 bool ModelAction::same_var(const ModelAction *act) const
386 if (act->is_wait() || is_wait()) {
387 if (act->is_wait() && is_wait()) {
388 if (((void *)value) == ((void *)act->value))
390 } else if (is_wait()) {
391 if (((void *)value) == act->location)
393 } else if (act->is_wait()) {
394 if (location == ((void *)act->value))
399 return location == act->location;
402 bool ModelAction::same_thread(const ModelAction *act) const
404 return tid == act->tid;
407 void ModelAction::copy_typeandorder(ModelAction * act)
409 this->type = act->type;
410 this->order = act->order;
414 * Get the Thread which is the operand of this action. This is only valid for
415 * THREAD_* operations (currently only for THREAD_CREATE and THREAD_JOIN). Note
416 * that this provides a central place for determining the conventions of Thread
417 * storage in ModelAction, where we generally aren't very type-safe (e.g., we
418 * store object references in a (void *) address.
420 * For THREAD_CREATE, this yields the Thread which is created.
421 * For THREAD_JOIN, this yields the Thread we are joining with.
423 * @return The Thread which this action acts on, if exists; otherwise NULL
425 Thread * ModelAction::get_thread_operand() const
427 if (type == THREAD_CREATE) {
428 /* thread_operand is set in execution.cc */
429 return thread_operand;
430 } else if (type == PTHREAD_CREATE) {
431 return thread_operand;
432 } else if (type == THREAD_JOIN) {
433 /* THREAD_JOIN uses (Thread *) for location */
434 return (Thread *)get_location();
435 } else if (type == PTHREAD_JOIN) {
437 // thread_operand is stored in execution::pthread_map;
438 return (Thread *)get_location();
444 * @brief Convert the read portion of an RMW
446 * Changes an existing read part of an RMW action into either:
447 * -# a full RMW action in case of the completed write or
448 * -# a READ action in case a failed action.
450 * @todo If the memory_order changes, we may potentially need to update our
453 * @param act The second half of the RMW (either RMWC or RMW)
455 void ModelAction::process_rmw(ModelAction *act)
457 this->order = act->order;
459 this->type = ATOMIC_READ;
460 else if (act->is_rmw()) {
461 this->type = ATOMIC_RMW;
462 this->value = act->value;
467 * @brief Check if this action should be backtracked with another, due to
468 * potential synchronization
470 * The is_synchronizing method should only explore interleavings if:
471 * -# the operations are seq_cst and don't commute or
472 * -# the reordering may establish or break a synchronization relation.
474 * Other memory operations will be dealt with by using the reads_from relation.
476 * @param act The action to consider exploring a reordering
477 * @return True, if we have to explore a reordering; otherwise false
479 bool ModelAction::could_synchronize_with(const ModelAction *act) const
481 // Same thread can't be reordered
482 if (same_thread(act))
485 // Different locations commute
486 if (!same_var(act) && !is_fence() && !act->is_fence())
489 // Explore interleavings of seqcst writes/fences to guarantee total
490 // order of seq_cst operations that don't commute
491 if ((could_be_write() || act->could_be_write() || is_fence() || act->is_fence()) && is_seqcst() && act->is_seqcst())
494 // Explore synchronizing read/write pairs
495 if (is_acquire() && act->is_release() && is_read() && act->could_be_write())
498 // lock just released...we can grab lock
499 if ((is_lock() || is_trylock()) && (act->is_unlock() || act->is_wait()))
502 // lock just acquired...we can fail to grab lock
503 if (is_trylock() && act->is_success_lock())
506 // other thread stalling on lock...we can release lock
507 if (is_unlock() && (act->is_trylock() || act->is_lock()))
510 if (is_trylock() && (act->is_unlock() || act->is_wait()))
513 if (is_notify() && act->is_wait())
516 if (is_wait() && act->is_notify())
519 // Otherwise handle by reads_from relation
523 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
525 // Must be different threads to reorder
526 if (same_thread(act))
529 // Try to reorder a lock past a successful lock
530 if (act->is_success_lock())
533 // Try to push a successful trylock past an unlock
534 if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
537 // Try to push a successful trylock past a wait
538 if (act->is_wait() && is_trylock() && value == VALUE_TRYSUCCESS)
545 * Create a new clock vector for this action. Note that this function allows a
546 * user to clobber (and leak) a ModelAction's existing clock vector. A user
547 * should ensure that the vector has already either been rolled back
548 * (effectively "freed") or freed.
550 * @param parent A ModelAction from which to inherit a ClockVector
552 void ModelAction::create_cv(const ModelAction *parent)
555 cv = new ClockVector(parent->cv, this);
557 cv = new ClockVector(NULL, this);
560 void ModelAction::set_try_lock(bool obtainedlock)
562 value = obtainedlock ? VALUE_TRYSUCCESS : VALUE_TRYFAILED;
566 * @brief Get the value read by this load
568 * We differentiate this function from ModelAction::get_write_value and
569 * ModelAction::get_value for the purpose of RMW's, which may have both a
570 * 'read' and a 'write' value.
572 * Note: 'this' must be a load.
574 * @return The value read by this load
576 uint64_t ModelAction::get_reads_from_value() const
580 return reads_from->get_write_value();
582 return VALUE_NONE; // Only for new actions with no reads-from
586 * @brief Get the value written by this store
588 * We differentiate this function from ModelAction::get_reads_from_value and
589 * ModelAction::get_value for the purpose of RMW's, which may have both a
590 * 'read' and a 'write' value.
592 * Note: 'this' must be a store.
594 * @return The value written by this store
596 uint64_t ModelAction::get_write_value() const
603 * @brief Get the value returned by this action
605 * For atomic reads (including RMW), an operation returns the value it read.
606 * For atomic writes, an operation returns the value it wrote. For other
607 * operations, the return value varies (sometimes is a "don't care"), but the
608 * value is simply stored in the "value" field.
610 * @return This action's return value
612 uint64_t ModelAction::get_return_value() const
615 return get_reads_from_value();
617 return get_write_value();
623 * Update the model action's read_from action
624 * @param act The action to read from; should be a write
626 void ModelAction::set_read_from(ModelAction *act)
631 if (act->is_uninitialized()) { // WL
632 uint64_t val = *((uint64_t *) location);
633 ModelAction * act_uninitialized = (ModelAction *)act;
634 act_uninitialized->set_value(val);
635 reads_from = act_uninitialized;
637 // disabled by WL, because LLVM IR is unable to detect atomic init
638 /* model->assert_bug("May read from uninitialized atomic:\n"
639 " action %d, thread %d, location %p (%s, %s)",
640 seq_number, id_to_int(tid), location,
641 get_type_str(), get_mo_str());
647 * Synchronize the current thread with the thread corresponding to the
648 * ModelAction parameter.
649 * @param act The ModelAction to synchronize with
650 * @return True if this is a valid synchronization; false otherwise
652 bool ModelAction::synchronize_with(const ModelAction *act)
660 bool ModelAction::has_synchronized_with(const ModelAction *act) const
662 return cv->synchronized_since(act);
666 * Check whether 'this' happens before act, according to the memory-model's
667 * happens before relation. This is checked via the ClockVector constructs.
668 * @return true if this action's thread has synchronized with act's thread
669 * since the execution of act, false otherwise.
671 bool ModelAction::happens_before(const ModelAction *act) const
673 return act->cv->synchronized_since(this);
676 const char * ModelAction::get_type_str() const
678 switch (this->type) {
679 case THREAD_CREATE: return "thread create";
680 case THREAD_START: return "thread start";
681 case THREAD_YIELD: return "thread yield";
682 case THREAD_JOIN: return "thread join";
683 case THREAD_FINISH: return "thread finish";
684 case THREAD_SLEEP: return "thread sleep";
685 case THREADONLY_FINISH: return "pthread_exit finish";
687 case PTHREAD_CREATE: return "pthread create";
688 case PTHREAD_JOIN: return "pthread join";
690 case ATOMIC_UNINIT: return "uninitialized";
691 case NONATOMIC_WRITE: return "nonatomic write";
692 case ATOMIC_READ: return "atomic read";
693 case ATOMIC_WRITE: return "atomic write";
694 case ATOMIC_RMW: return "atomic rmw";
695 case ATOMIC_FENCE: return "fence";
696 case ATOMIC_RMWR: return "atomic rmwr";
697 case ATOMIC_RMWRCAS: return "atomic rmwrcas";
698 case ATOMIC_RMWC: return "atomic rmwc";
699 case ATOMIC_INIT: return "init atomic";
700 case ATOMIC_LOCK: return "lock";
701 case ATOMIC_UNLOCK: return "unlock";
702 case ATOMIC_TRYLOCK: return "trylock";
703 case ATOMIC_WAIT: return "wait";
704 case ATOMIC_TIMEDWAIT: return "timed wait";
705 case ATOMIC_NOTIFY_ONE: return "notify one";
706 case ATOMIC_NOTIFY_ALL: return "notify all";
707 case ATOMIC_ANNOTATION: return "annotation";
708 default: return "unknown type";
712 const char * ModelAction::get_mo_str() const
714 switch (this->order) {
715 case std::memory_order_relaxed: return "relaxed";
716 case std::memory_order_acquire: return "acquire";
717 case std::memory_order_release: return "release";
718 case std::memory_order_acq_rel: return "acq_rel";
719 case std::memory_order_seq_cst: return "seq_cst";
720 default: return "unknown";
724 /** @brief Print nicely-formatted info about this ModelAction */
725 void ModelAction::print() const
727 const char *type_str = get_type_str(), *mo_str = get_mo_str();
729 model_print("%-4d %-2d %-14s %7s %14p %-#18" PRIx64,
730 seq_number, id_to_int(tid), type_str, mo_str, location, get_return_value());
733 model_print(" %-3d", reads_from->get_seq_number());
747 /** @brief Get a (likely) unique hash for this ModelAction */
748 unsigned int ModelAction::hash() const
750 unsigned int hash = (unsigned int)this->type;
751 hash ^= ((unsigned int)this->order) << 3;
752 hash ^= seq_number << 5;
753 hash ^= id_to_int(tid) << 6;
757 hash ^= reads_from->get_seq_number();
758 hash ^= get_reads_from_value();
764 * Only valid for LOCK, TRY_LOCK, UNLOCK, and WAIT operations.
765 * @return The mutex operated on by this action, if any; otherwise NULL
767 cdsc::mutex * ModelAction::get_mutex() const
769 if (is_trylock() || is_lock() || is_unlock())
770 return (cdsc::mutex *)get_location();
772 return (cdsc::mutex *)get_value();