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
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 thread (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, uint64_t value, uint64_t _time) :
72 last_fence_release(NULL),
79 original_order(order),
80 seq_number(ACTION_INITIAL_CLOCK)
82 Thread *t = thread_current();
83 this->tid = t!= NULL ? t->get_id() : -1;
87 * @brief Construct a new ModelAction
89 * @param type The type of action
90 * @param order The memory order of this action. A "don't care" for non-ATOMIC
91 * actions (e.g., THREAD_* or MODEL_* actions).
92 * @param loc The location that this action acts upon
93 * @param value (optional) A value associated with the action (e.g., the value
94 * read or written). Defaults to a given macro constant, for debugging purposes.
95 * @param size (optional) The Thread in which this action occurred. If NULL
96 * (default), then a Thread is assigned according to the scheduler.
98 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc,
99 uint64_t value, int size) :
103 last_fence_release(NULL),
110 original_order(order),
111 seq_number(ACTION_INITIAL_CLOCK)
113 /* References to NULL atomic variables can end up here */
116 Thread *t = thread_current();
117 this->tid = t->get_id();
122 * @brief Construct a new ModelAction with source line number (requires llvm support)
124 * @param type The type of action
125 * @param order The memory order of this action. A "don't care" for non-ATOMIC
126 * actions (e.g., THREAD_* or MODEL_* actions).
127 * @param loc The location that this action acts upon
128 * @param value (optional) A value associated with the action (e.g., the value
129 * read or written). Defaults to a given macro constant, for debugging purposes.
130 * @param size (optional) The Thread in which this action occurred. If NULL
131 * (default), then a Thread is assigned according to the scheduler.
133 ModelAction::ModelAction(action_type_t type, const char * position, memory_order order, void *loc,
134 uint64_t value, int size) :
138 last_fence_release(NULL),
145 original_order(order),
146 seq_number(ACTION_INITIAL_CLOCK)
148 /* References to NULL atomic variables can end up here */
151 Thread *t = thread_current();
152 this->tid = t->get_id();
157 * @brief Construct a new ModelAction with source line number (requires llvm support)
159 * @param type The type of action
160 * @param position The source line number of this atomic operation
161 * @param order The memory order of this action. A "don't care" for non-ATOMIC
162 * actions (e.g., THREAD_* or MODEL_* actions).
163 * @param loc The location that this action acts upon
164 * @param value (optional) A value associated with the action (e.g., the value
165 * read or written). Defaults to a given macro constant, for debugging purposes.
166 * @param thread (optional) The Thread in which this action occurred. If NULL
167 * (default), then a Thread is assigned according to the scheduler.
169 ModelAction::ModelAction(action_type_t type, const char * position, memory_order order,
170 void *loc, uint64_t value, Thread *thread) :
174 last_fence_release(NULL),
181 original_order(order),
182 seq_number(ACTION_INITIAL_CLOCK)
184 /* References to NULL atomic variables can end up here */
185 ASSERT(loc || type == ATOMIC_FENCE);
187 Thread *t = thread ? thread : thread_current();
188 this->tid = t->get_id();
189 // model_print("position: %s\n", position);
193 /** @brief ModelAction destructor */
194 ModelAction::~ModelAction()
197 * We can't free the clock vector:
198 * Clock vectors are snapshotting state. When we delete model actions,
199 * they are at the end of the node list and have invalid old clock
200 * vectors which have already been rolled back to an unallocated state.
208 int ModelAction::getSize() const {
212 void ModelAction::copy_from_new(ModelAction *newaction)
214 seq_number = newaction->seq_number;
217 void ModelAction::set_seq_number(modelclock_t num)
219 /* ATOMIC_UNINIT actions should never have non-zero clock */
220 ASSERT(!is_uninitialized());
221 ASSERT(seq_number == ACTION_INITIAL_CLOCK);
225 bool ModelAction::is_thread_start() const
227 return type == THREAD_START;
230 bool ModelAction::is_thread_join() const
232 return type == THREAD_JOIN || type == PTHREAD_JOIN;
235 bool ModelAction::is_mutex_op() const
237 return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK || type == ATOMIC_WAIT || type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
240 bool ModelAction::is_lock() const
242 return type == ATOMIC_LOCK;
245 bool ModelAction::is_sleep() const
247 return type == THREAD_SLEEP;
250 bool ModelAction::is_wait() const {
251 return type == ATOMIC_WAIT;
254 bool ModelAction::is_notify() const {
255 return type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
258 bool ModelAction::is_notify_one() const {
259 return type == ATOMIC_NOTIFY_ONE;
262 bool ModelAction::is_unlock() const
264 return type == ATOMIC_UNLOCK;
267 bool ModelAction::is_trylock() const
269 return type == ATOMIC_TRYLOCK;
272 bool ModelAction::is_success_lock() const
274 return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
277 bool ModelAction::is_failed_trylock() const
279 return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
282 /** @return True if this operation is performed on a C/C++ atomic variable */
283 bool ModelAction::is_atomic_var() const
285 return is_read() || could_be_write();
288 bool ModelAction::is_uninitialized() const
290 return type == ATOMIC_UNINIT;
293 bool ModelAction::is_read() const
295 return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMWRCAS || type == ATOMIC_RMW;
298 bool ModelAction::is_write() const
300 return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT || type == ATOMIC_UNINIT || type == NONATOMIC_WRITE;
303 bool ModelAction::could_be_write() const
305 return is_write() || is_rmwr();
308 bool ModelAction::is_yield() const
310 return type == THREAD_YIELD;
313 bool ModelAction::is_rmwr() const
315 return type == ATOMIC_RMWR || type == ATOMIC_RMWRCAS;
318 bool ModelAction::is_rmwrcas() const
320 return type == ATOMIC_RMWRCAS;
323 bool ModelAction::is_rmw() const
325 return type == ATOMIC_RMW;
328 bool ModelAction::is_rmwc() const
330 return type == ATOMIC_RMWC;
333 bool ModelAction::is_fence() const
335 return type == ATOMIC_FENCE;
338 bool ModelAction::is_initialization() const
340 return type == ATOMIC_INIT;
343 bool ModelAction::is_annotation() const
345 return type == ATOMIC_ANNOTATION;
348 bool ModelAction::is_relaxed() const
350 return order == std::memory_order_relaxed;
353 bool ModelAction::is_acquire() const
356 case std::memory_order_acquire:
357 case std::memory_order_acq_rel:
358 case std::memory_order_seq_cst:
365 bool ModelAction::is_release() const
368 case std::memory_order_release:
369 case std::memory_order_acq_rel:
370 case std::memory_order_seq_cst:
377 bool ModelAction::is_seqcst() const
379 return order == std::memory_order_seq_cst;
382 bool ModelAction::same_var(const ModelAction *act) const
384 if (act->is_wait() || is_wait()) {
385 if (act->is_wait() && is_wait()) {
386 if (((void *)value) == ((void *)act->value))
388 } else if (is_wait()) {
389 if (((void *)value) == act->location)
391 } else if (act->is_wait()) {
392 if (location == ((void *)act->value))
397 return location == act->location;
400 bool ModelAction::same_thread(const ModelAction *act) const
402 return tid == act->tid;
405 void ModelAction::copy_typeandorder(ModelAction * act)
407 this->type = act->type;
408 this->order = act->order;
412 * Get the Thread which is the operand of this action. This is only valid for
413 * THREAD_* operations (currently only for THREAD_CREATE and THREAD_JOIN). Note
414 * that this provides a central place for determining the conventions of Thread
415 * storage in ModelAction, where we generally aren't very type-safe (e.g., we
416 * store object references in a (void *) address.
418 * For THREAD_CREATE, this yields the Thread which is created.
419 * For THREAD_JOIN, this yields the Thread we are joining with.
421 * @return The Thread which this action acts on, if exists; otherwise NULL
423 Thread * ModelAction::get_thread_operand() const
425 if (type == THREAD_CREATE) {
426 /* thread_operand is set in execution.cc */
427 return thread_operand;
428 } else if (type == PTHREAD_CREATE) {
429 return thread_operand;
430 } else if (type == THREAD_JOIN) {
431 /* THREAD_JOIN uses (Thread *) for location */
432 return (Thread *)get_location();
433 } else if (type == PTHREAD_JOIN) {
435 // thread_operand is stored in execution::pthread_map;
436 return (Thread *)get_location();
442 * @brief Convert the read portion of an RMW
444 * Changes an existing read part of an RMW action into either:
445 * -# a full RMW action in case of the completed write or
446 * -# a READ action in case a failed action.
448 * @todo If the memory_order changes, we may potentially need to update our
451 * @param act The second half of the RMW (either RMWC or RMW)
453 void ModelAction::process_rmw(ModelAction *act)
455 this->order = act->order;
457 this->type = ATOMIC_READ;
458 else if (act->is_rmw()) {
459 this->type = ATOMIC_RMW;
460 this->value = act->value;
465 * @brief Check if this action should be backtracked with another, due to
466 * potential synchronization
468 * The is_synchronizing method should only explore interleavings if:
469 * -# the operations are seq_cst and don't commute or
470 * -# the reordering may establish or break a synchronization relation.
472 * Other memory operations will be dealt with by using the reads_from relation.
474 * @param act The action to consider exploring a reordering
475 * @return True, if we have to explore a reordering; otherwise false
477 bool ModelAction::could_synchronize_with(const ModelAction *act) const
479 // Same thread can't be reordered
480 if (same_thread(act))
483 // Different locations commute
484 if (!same_var(act) && !is_fence() && !act->is_fence())
487 // Explore interleavings of seqcst writes/fences to guarantee total
488 // order of seq_cst operations that don't commute
489 if ((could_be_write() || act->could_be_write() || is_fence() || act->is_fence()) && is_seqcst() && act->is_seqcst())
492 // Explore synchronizing read/write pairs
493 if (is_acquire() && act->is_release() && is_read() && act->could_be_write())
496 // lock just released...we can grab lock
497 if ((is_lock() || is_trylock()) && (act->is_unlock() || act->is_wait()))
500 // lock just acquired...we can fail to grab lock
501 if (is_trylock() && act->is_success_lock())
504 // other thread stalling on lock...we can release lock
505 if (is_unlock() && (act->is_trylock() || act->is_lock()))
508 if (is_trylock() && (act->is_unlock() || act->is_wait()))
511 if (is_notify() && act->is_wait())
514 if (is_wait() && act->is_notify())
517 // Otherwise handle by reads_from relation
521 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
523 // Must be different threads to reorder
524 if (same_thread(act))
527 // Try to reorder a lock past a successful lock
528 if (act->is_success_lock())
531 // Try to push a successful trylock past an unlock
532 if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
535 // Try to push a successful trylock past a wait
536 if (act->is_wait() && is_trylock() && value == VALUE_TRYSUCCESS)
543 * Create a new clock vector for this action. Note that this function allows a
544 * user to clobber (and leak) a ModelAction's existing clock vector. A user
545 * should ensure that the vector has already either been rolled back
546 * (effectively "freed") or freed.
548 * @param parent A ModelAction from which to inherit a ClockVector
550 void ModelAction::create_cv(const ModelAction *parent)
553 cv = new ClockVector(parent->cv, this);
555 cv = new ClockVector(NULL, this);
558 void ModelAction::set_try_lock(bool obtainedlock)
560 value = obtainedlock ? VALUE_TRYSUCCESS : VALUE_TRYFAILED;
564 * @brief Get the value read by this load
566 * We differentiate this function from ModelAction::get_write_value and
567 * ModelAction::get_value for the purpose of RMW's, which may have both a
568 * 'read' and a 'write' value.
570 * Note: 'this' must be a load.
572 * @return The value read by this load
574 uint64_t ModelAction::get_reads_from_value() const
578 return reads_from->get_write_value();
580 return VALUE_NONE; // Only for new actions with no reads-from
584 * @brief Get the value written by this store
586 * We differentiate this function from ModelAction::get_reads_from_value and
587 * ModelAction::get_value for the purpose of RMW's, which may have both a
588 * 'read' and a 'write' value.
590 * Note: 'this' must be a store.
592 * @return The value written by this store
594 uint64_t ModelAction::get_write_value() const
601 * @brief Get the value returned by this action
603 * For atomic reads (including RMW), an operation returns the value it read.
604 * For atomic writes, an operation returns the value it wrote. For other
605 * operations, the return value varies (sometimes is a "don't care"), but the
606 * value is simply stored in the "value" field.
608 * @return This action's return value
610 uint64_t ModelAction::get_return_value() const
613 return get_reads_from_value();
615 return get_write_value();
621 * Update the model action's read_from action
622 * @param act The action to read from; should be a write
624 void ModelAction::set_read_from(ModelAction *act)
630 if (act->is_uninitialized()) { // WL
631 uint64_t val = *((uint64_t *) location);
632 ModelAction * act_uninitialized = (ModelAction *)act;
633 act_uninitialized->set_value(val);
634 reads_from = act_uninitialized;
636 // disabled by WL, because LLVM IR is unable to detect atomic init
637 /* model->assert_bug("May read from uninitialized atomic:\n"
638 " action %d, thread %d, location %p (%s, %s)",
639 seq_number, id_to_int(tid), location,
640 get_type_str(), get_mo_str());
646 * Synchronize the current thread with the thread corresponding to the
647 * ModelAction parameter.
648 * @param act The ModelAction to synchronize with
649 * @return True if this is a valid synchronization; false otherwise
651 bool ModelAction::synchronize_with(const ModelAction *act)
659 bool ModelAction::has_synchronized_with(const ModelAction *act) const
661 return cv->synchronized_since(act);
665 * Check whether 'this' happens before act, according to the memory-model's
666 * happens before relation. This is checked via the ClockVector constructs.
667 * @return true if this action's thread has synchronized with act's thread
668 * since the execution of act, false otherwise.
670 bool ModelAction::happens_before(const ModelAction *act) const
672 return act->cv->synchronized_since(this);
675 const char * ModelAction::get_type_str() const
677 switch (this->type) {
678 case THREAD_CREATE: return "thread create";
679 case THREAD_START: return "thread start";
680 case THREAD_YIELD: return "thread yield";
681 case THREAD_JOIN: return "thread join";
682 case THREAD_FINISH: return "thread finish";
683 case THREAD_SLEEP: return "thread sleep";
684 case THREADONLY_FINISH: return "pthread_exit finish";
686 case PTHREAD_CREATE: return "pthread create";
687 case PTHREAD_JOIN: return "pthread join";
689 case ATOMIC_UNINIT: return "uninitialized";
690 case NONATOMIC_WRITE: return "nonatomic write";
691 case ATOMIC_READ: return "atomic read";
692 case ATOMIC_WRITE: return "atomic write";
693 case ATOMIC_RMW: return "atomic rmw";
694 case ATOMIC_FENCE: return "fence";
695 case ATOMIC_RMWR: return "atomic rmwr";
696 case ATOMIC_RMWRCAS: return "atomic rmwrcas";
697 case ATOMIC_RMWC: return "atomic rmwc";
698 case ATOMIC_INIT: return "init atomic";
699 case ATOMIC_LOCK: return "lock";
700 case ATOMIC_UNLOCK: return "unlock";
701 case ATOMIC_TRYLOCK: return "trylock";
702 case ATOMIC_WAIT: return "wait";
703 case ATOMIC_NOTIFY_ONE: return "notify one";
704 case ATOMIC_NOTIFY_ALL: return "notify all";
705 case ATOMIC_ANNOTATION: return "annotation";
706 default: return "unknown type";
710 const char * ModelAction::get_mo_str() const
712 switch (this->order) {
713 case std::memory_order_relaxed: return "relaxed";
714 case std::memory_order_acquire: return "acquire";
715 case std::memory_order_release: return "release";
716 case std::memory_order_acq_rel: return "acq_rel";
717 case std::memory_order_seq_cst: return "seq_cst";
718 default: return "unknown";
722 /** @brief Print nicely-formatted info about this ModelAction */
723 void ModelAction::print() const
725 const char *type_str = get_type_str(), *mo_str = get_mo_str();
727 model_print("%-4d %-2d %-14s %7s %14p %-#18" PRIx64,
728 seq_number, id_to_int(tid), type_str, mo_str, location, get_return_value());
731 model_print(" %-3d", reads_from->get_seq_number());
745 /** @brief Get a (likely) unique hash for this ModelAction */
746 unsigned int ModelAction::hash() const
748 unsigned int hash = (unsigned int)this->type;
749 hash ^= ((unsigned int)this->order) << 3;
750 hash ^= seq_number << 5;
751 hash ^= id_to_int(tid) << 6;
755 hash ^= reads_from->get_seq_number();
756 hash ^= get_reads_from_value();
762 * Only valid for LOCK, TRY_LOCK, UNLOCK, and WAIT operations.
763 * @return The mutex operated on by this action, if any; otherwise NULL
765 cdsc::mutex * ModelAction::get_mutex() const
767 if (is_trylock() || is_lock() || is_unlock())
768 return (cdsc::mutex *)get_location();
770 return (cdsc::mutex *)get_value();