2 #define __STDC_FORMAT_MACROS
8 #include "clockvector.h"
10 #include "threads-model.h"
11 #include "nodestack.h"
14 #define ACTION_INITIAL_CLOCK 0
16 /** @brief A special value to represent a successful trylock */
17 #define VALUE_TRYSUCCESS 1
19 /** @brief A special value to represent a failed trylock */
20 #define VALUE_TRYFAILED 0
23 * @brief Construct a new ModelAction
25 * @param type The type of action
26 * @param order The memory order of this action. A "don't care" for non-ATOMIC
27 * actions (e.g., THREAD_* or MODEL_* actions).
28 * @param loc The location that this action acts upon
29 * @param value (optional) A value associated with the action (e.g., the value
30 * read or written). Defaults to a given macro constant, for debugging purposes.
31 * @param thread (optional) The Thread in which this action occurred. If NULL
32 * (default), then a Thread is assigned according to the scheduler.
34 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc,
35 uint64_t value, Thread *thread) :
39 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),
79 original_order(order),
80 seq_number(ACTION_INITIAL_CLOCK)
82 /* References to NULL atomic variables can end up here */
85 Thread *t = thread_current();
86 this->tid = t->get_id();
91 * @brief Construct a new ModelAction with source line number (requires llvm support)
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, const char * position, memory_order order, void *loc,
103 uint64_t value, int size) :
107 last_fence_release(NULL),
113 original_order(order),
114 seq_number(ACTION_INITIAL_CLOCK)
116 /* References to NULL atomic variables can end up here */
119 Thread *t = thread_current();
120 this->tid = t->get_id();
125 * @brief Construct a new ModelAction with source line number (requires llvm support)
127 * @param type The type of action
128 * @param position The source line number of this atomic operation
129 * @param order The memory order of this action. A "don't care" for non-ATOMIC
130 * actions (e.g., THREAD_* or MODEL_* actions).
131 * @param loc The location that this action acts upon
132 * @param value (optional) A value associated with the action (e.g., the value
133 * read or written). Defaults to a given macro constant, for debugging purposes.
134 * @param thread (optional) The Thread in which this action occurred. If NULL
135 * (default), then a Thread is assigned according to the scheduler.
137 ModelAction::ModelAction(action_type_t type, const char * position, memory_order order,
138 void *loc, uint64_t value, Thread *thread) :
142 last_fence_release(NULL),
148 original_order(order),
149 seq_number(ACTION_INITIAL_CLOCK)
151 /* References to NULL atomic variables can end up here */
152 ASSERT(loc || type == ATOMIC_FENCE);
154 Thread *t = thread ? thread : thread_current();
155 this->tid = t->get_id();
156 // model_print("position: %s\n", position);
160 /** @brief ModelAction destructor */
161 ModelAction::~ModelAction()
164 * We can't free the clock vector:
165 * Clock vectors are snapshotting state. When we delete model actions,
166 * they are at the end of the node list and have invalid old clock
167 * vectors which have already been rolled back to an unallocated state.
175 int ModelAction::getSize() const {
179 void ModelAction::copy_from_new(ModelAction *newaction)
181 seq_number = newaction->seq_number;
184 void ModelAction::set_seq_number(modelclock_t num)
186 /* ATOMIC_UNINIT actions should never have non-zero clock */
187 ASSERT(!is_uninitialized());
188 ASSERT(seq_number == ACTION_INITIAL_CLOCK);
192 bool ModelAction::is_thread_start() const
194 return type == THREAD_START;
197 bool ModelAction::is_thread_join() const
199 return type == THREAD_JOIN || type == PTHREAD_JOIN;
202 bool ModelAction::is_mutex_op() const
204 return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK || type == ATOMIC_WAIT || type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
207 bool ModelAction::is_lock() const
209 return type == ATOMIC_LOCK;
212 bool ModelAction::is_wait() const {
213 return type == ATOMIC_WAIT;
216 bool ModelAction::is_notify() const {
217 return type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
220 bool ModelAction::is_notify_one() const {
221 return type == ATOMIC_NOTIFY_ONE;
224 bool ModelAction::is_unlock() const
226 return type == ATOMIC_UNLOCK;
229 bool ModelAction::is_trylock() const
231 return type == ATOMIC_TRYLOCK;
234 bool ModelAction::is_success_lock() const
236 return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
239 bool ModelAction::is_failed_trylock() const
241 return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
244 /** @return True if this operation is performed on a C/C++ atomic variable */
245 bool ModelAction::is_atomic_var() const
247 return is_read() || could_be_write();
250 bool ModelAction::is_uninitialized() const
252 return type == ATOMIC_UNINIT;
255 bool ModelAction::is_read() const
257 return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMWRCAS || type == ATOMIC_RMW;
260 bool ModelAction::is_write() const
262 return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT || type == ATOMIC_UNINIT;
265 bool ModelAction::could_be_write() const
267 return is_write() || is_rmwr();
270 bool ModelAction::is_yield() const
272 return type == THREAD_YIELD;
275 bool ModelAction::is_rmwr() const
277 return type == ATOMIC_RMWR || type == ATOMIC_RMWRCAS;
280 bool ModelAction::is_rmwrcas() const
282 return type == ATOMIC_RMWRCAS;
285 bool ModelAction::is_rmw() const
287 return type == ATOMIC_RMW;
290 bool ModelAction::is_rmwc() const
292 return type == ATOMIC_RMWC;
295 bool ModelAction::is_fence() const
297 return type == ATOMIC_FENCE;
300 bool ModelAction::is_initialization() const
302 return type == ATOMIC_INIT;
305 bool ModelAction::is_annotation() const
307 return type == ATOMIC_ANNOTATION;
310 bool ModelAction::is_relaxed() const
312 return order == std::memory_order_relaxed;
315 bool ModelAction::is_acquire() const
318 case std::memory_order_acquire:
319 case std::memory_order_acq_rel:
320 case std::memory_order_seq_cst:
327 bool ModelAction::is_release() const
330 case std::memory_order_release:
331 case std::memory_order_acq_rel:
332 case std::memory_order_seq_cst:
339 bool ModelAction::is_seqcst() const
341 return order == std::memory_order_seq_cst;
344 bool ModelAction::same_var(const ModelAction *act) const
346 if (act->is_wait() || is_wait()) {
347 if (act->is_wait() && is_wait()) {
348 if (((void *)value) == ((void *)act->value))
350 } else if (is_wait()) {
351 if (((void *)value) == act->location)
353 } else if (act->is_wait()) {
354 if (location == ((void *)act->value))
359 return location == act->location;
362 bool ModelAction::same_thread(const ModelAction *act) const
364 return tid == act->tid;
367 void ModelAction::copy_typeandorder(ModelAction * act)
369 this->type = act->type;
370 this->order = act->order;
374 * Get the Thread which is the operand of this action. This is only valid for
375 * THREAD_* operations (currently only for THREAD_CREATE and THREAD_JOIN). Note
376 * that this provides a central place for determining the conventions of Thread
377 * storage in ModelAction, where we generally aren't very type-safe (e.g., we
378 * store object references in a (void *) address.
380 * For THREAD_CREATE, this yields the Thread which is created.
381 * For THREAD_JOIN, this yields the Thread we are joining with.
383 * @return The Thread which this action acts on, if exists; otherwise NULL
385 Thread * ModelAction::get_thread_operand() const
387 if (type == THREAD_CREATE) {
388 /* thread_operand is set in execution.cc */
389 return thread_operand;
390 } else if (type == PTHREAD_CREATE) {
391 return thread_operand;
392 } else if (type == THREAD_JOIN) {
393 /* THREAD_JOIN uses (Thread *) for location */
394 return (Thread *)get_location();
395 } else if (type == PTHREAD_JOIN) {
397 // thread_operand is stored in execution::pthread_map;
398 return (Thread *)get_location();
404 * @brief Convert the read portion of an RMW
406 * Changes an existing read part of an RMW action into either:
407 * -# a full RMW action in case of the completed write or
408 * -# a READ action in case a failed action.
410 * @todo If the memory_order changes, we may potentially need to update our
413 * @param act The second half of the RMW (either RMWC or RMW)
415 void ModelAction::process_rmw(ModelAction *act)
417 this->order = act->order;
419 this->type = ATOMIC_READ;
420 else if (act->is_rmw()) {
421 this->type = ATOMIC_RMW;
422 this->value = act->value;
427 * @brief Check if this action should be backtracked with another, due to
428 * potential synchronization
430 * The is_synchronizing method should only explore interleavings if:
431 * -# the operations are seq_cst and don't commute or
432 * -# the reordering may establish or break a synchronization relation.
434 * Other memory operations will be dealt with by using the reads_from relation.
436 * @param act The action to consider exploring a reordering
437 * @return True, if we have to explore a reordering; otherwise false
439 bool ModelAction::could_synchronize_with(const ModelAction *act) const
441 // Same thread can't be reordered
442 if (same_thread(act))
445 // Different locations commute
446 if (!same_var(act) && !is_fence() && !act->is_fence())
449 // Explore interleavings of seqcst writes/fences to guarantee total
450 // order of seq_cst operations that don't commute
451 if ((could_be_write() || act->could_be_write() || is_fence() || act->is_fence()) && is_seqcst() && act->is_seqcst())
454 // Explore synchronizing read/write pairs
455 if (is_acquire() && act->is_release() && is_read() && act->could_be_write())
458 // lock just released...we can grab lock
459 if ((is_lock() || is_trylock()) && (act->is_unlock() || act->is_wait()))
462 // lock just acquired...we can fail to grab lock
463 if (is_trylock() && act->is_success_lock())
466 // other thread stalling on lock...we can release lock
467 if (is_unlock() && (act->is_trylock() || act->is_lock()))
470 if (is_trylock() && (act->is_unlock() || act->is_wait()))
473 if (is_notify() && act->is_wait())
476 if (is_wait() && act->is_notify())
479 // Otherwise handle by reads_from relation
483 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
485 // Must be different threads to reorder
486 if (same_thread(act))
489 // Try to reorder a lock past a successful lock
490 if (act->is_success_lock())
493 // Try to push a successful trylock past an unlock
494 if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
497 // Try to push a successful trylock past a wait
498 if (act->is_wait() && is_trylock() && value == VALUE_TRYSUCCESS)
505 * Create a new clock vector for this action. Note that this function allows a
506 * user to clobber (and leak) a ModelAction's existing clock vector. A user
507 * should ensure that the vector has already either been rolled back
508 * (effectively "freed") or freed.
510 * @param parent A ModelAction from which to inherit a ClockVector
512 void ModelAction::create_cv(const ModelAction *parent)
515 cv = new ClockVector(parent->cv, this);
517 cv = new ClockVector(NULL, this);
520 void ModelAction::set_try_lock(bool obtainedlock)
522 value = obtainedlock ? VALUE_TRYSUCCESS : VALUE_TRYFAILED;
526 * @brief Get the value read by this load
528 * We differentiate this function from ModelAction::get_write_value and
529 * ModelAction::get_value for the purpose of RMW's, which may have both a
530 * 'read' and a 'write' value.
532 * Note: 'this' must be a load.
534 * @return The value read by this load
536 uint64_t ModelAction::get_reads_from_value() const
540 return reads_from->get_write_value();
542 return VALUE_NONE; // Only for new actions with no reads-from
546 * @brief Get the value written by this store
548 * We differentiate this function from ModelAction::get_reads_from_value and
549 * ModelAction::get_value for the purpose of RMW's, which may have both a
550 * 'read' and a 'write' value.
552 * Note: 'this' must be a store.
554 * @return The value written by this store
556 uint64_t ModelAction::get_write_value() const
563 * @brief Get the value returned by this action
565 * For atomic reads (including RMW), an operation returns the value it read.
566 * For atomic writes, an operation returns the value it wrote. For other
567 * operations, the return value varies (sometimes is a "don't care"), but the
568 * value is simply stored in the "value" field.
570 * @return This action's return value
572 uint64_t ModelAction::get_return_value() const
575 return get_reads_from_value();
577 return get_write_value();
582 /** @return The Node associated with this ModelAction */
583 Node * ModelAction::get_node() const
585 /* UNINIT actions do not have a Node */
586 ASSERT(!is_uninitialized());
591 * Update the model action's read_from action
592 * @param act The action to read from; should be a write
594 void ModelAction::set_read_from(const ModelAction *act)
600 if (act->is_uninitialized()) { // WL
601 uint64_t val = *((uint64_t *) location);
602 ModelAction * act_initialized = (ModelAction *)act;
603 act_initialized->set_value(val);
604 reads_from = (const ModelAction *)act_initialized;
606 // disabled by WL, because LLVM IR is unable to detect atomic init
607 /* model->assert_bug("May read from uninitialized atomic:\n"
608 " action %d, thread %d, location %p (%s, %s)",
609 seq_number, id_to_int(tid), location,
610 get_type_str(), get_mo_str());
616 * Synchronize the current thread with the thread corresponding to the
617 * ModelAction parameter.
618 * @param act The ModelAction to synchronize with
619 * @return True if this is a valid synchronization; false otherwise
621 bool ModelAction::synchronize_with(const ModelAction *act)
629 bool ModelAction::has_synchronized_with(const ModelAction *act) const
631 return cv->synchronized_since(act);
635 * Check whether 'this' happens before act, according to the memory-model's
636 * happens before relation. This is checked via the ClockVector constructs.
637 * @return true if this action's thread has synchronized with act's thread
638 * since the execution of act, false otherwise.
640 bool ModelAction::happens_before(const ModelAction *act) const
642 return act->cv->synchronized_since(this);
645 const char * ModelAction::get_type_str() const
647 switch (this->type) {
648 case THREAD_CREATE: return "thread create";
649 case THREAD_START: return "thread start";
650 case THREAD_YIELD: return "thread yield";
651 case THREAD_JOIN: return "thread join";
652 case THREAD_FINISH: return "thread finish";
654 case PTHREAD_CREATE: return "pthread create";
655 case PTHREAD_JOIN: return "pthread join";
657 case ATOMIC_UNINIT: return "uninitialized";
658 case ATOMIC_READ: return "atomic read";
659 case ATOMIC_WRITE: return "atomic write";
660 case ATOMIC_RMW: return "atomic rmw";
661 case ATOMIC_FENCE: return "fence";
662 case ATOMIC_RMWR: return "atomic rmwr";
663 case ATOMIC_RMWRCAS: return "atomic rmwrcas";
664 case ATOMIC_RMWC: return "atomic rmwc";
665 case ATOMIC_INIT: return "init atomic";
666 case ATOMIC_LOCK: return "lock";
667 case ATOMIC_UNLOCK: return "unlock";
668 case ATOMIC_TRYLOCK: return "trylock";
669 case ATOMIC_WAIT: return "wait";
670 case ATOMIC_NOTIFY_ONE: return "notify one";
671 case ATOMIC_NOTIFY_ALL: return "notify all";
672 case ATOMIC_ANNOTATION: return "annotation";
673 default: return "unknown type";
677 const char * ModelAction::get_mo_str() const
679 switch (this->order) {
680 case std::memory_order_relaxed: return "relaxed";
681 case std::memory_order_acquire: return "acquire";
682 case std::memory_order_release: return "release";
683 case std::memory_order_acq_rel: return "acq_rel";
684 case std::memory_order_seq_cst: return "seq_cst";
685 default: return "unknown";
689 /** @brief Print nicely-formatted info about this ModelAction */
690 void ModelAction::print() const
692 const char *type_str = get_type_str(), *mo_str = get_mo_str();
694 model_print("%-4d %-2d %-14s %7s %14p %-#18" PRIx64,
695 seq_number, id_to_int(tid), type_str, mo_str, location, get_return_value());
698 model_print(" %-3d", reads_from->get_seq_number());
712 /** @brief Get a (likely) unique hash for this ModelAction */
713 unsigned int ModelAction::hash() const
715 unsigned int hash = (unsigned int)this->type;
716 hash ^= ((unsigned int)this->order) << 3;
717 hash ^= seq_number << 5;
718 hash ^= id_to_int(tid) << 6;
722 hash ^= reads_from->get_seq_number();
723 hash ^= get_reads_from_value();
729 * Only valid for LOCK, TRY_LOCK, UNLOCK, and WAIT operations.
730 * @return The mutex operated on by this action, if any; otherwise NULL
732 cdsc::mutex * ModelAction::get_mutex() const
734 if (is_trylock() || is_lock() || is_unlock())
735 return (cdsc::mutex *)get_location();
737 return (cdsc::mutex *)get_value();