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_order(order),
48 seq_number(ACTION_INITIAL_CLOCK)
50 /* References to NULL atomic variables can end up here */
51 ASSERT(loc || type == ATOMIC_FENCE);
53 Thread *t = thread ? thread : thread_current();
54 this->tid = t!= NULL ? t->get_id() : -1;
59 * @brief Construct a new ModelAction for sleep actions
61 * @param type The type of action: THREAD_SLEEP
62 * @param order The memory order of this action. A "don't care" for non-ATOMIC
63 * actions (e.g., THREAD_* or MODEL_* actions).
64 * @param loc The location that this action acts upon
65 * @param value The time duration a thread is scheduled to sleep.
66 * @param _time The this sleep action is constructed
68 ModelAction::ModelAction(action_type_t type, memory_order order, uint64_t value, uint64_t _time) :
72 last_fence_release(NULL),
81 original_order(order),
82 seq_number(ACTION_INITIAL_CLOCK)
84 Thread *t = thread_current();
85 this->tid = t!= NULL ? t->get_id() : -1;
89 * @brief Construct a new ModelAction
91 * @param type The type of action
92 * @param order The memory order of this action. A "don't care" for non-ATOMIC
93 * actions (e.g., THREAD_* or MODEL_* actions).
94 * @param loc The location that this action acts upon
95 * @param value (optional) A value associated with the action (e.g., the value
96 * read or written). Defaults to a given macro constant, for debugging purposes.
97 * @param size (optional) The Thread in which this action occurred. If NULL
98 * (default), then a Thread is assigned according to the scheduler.
100 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc,
101 uint64_t value, int size) :
105 last_fence_release(NULL),
114 original_order(order),
115 seq_number(ACTION_INITIAL_CLOCK)
117 /* References to NULL atomic variables can end up here */
120 Thread *t = thread_current();
121 this->tid = t->get_id();
126 * @brief Construct a new ModelAction with source line number (requires llvm support)
128 * @param type The type of action
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 size (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, void *loc,
138 uint64_t value, int size) :
142 last_fence_release(NULL),
151 original_order(order),
152 seq_number(ACTION_INITIAL_CLOCK)
154 /* References to NULL atomic variables can end up here */
157 Thread *t = thread_current();
158 this->tid = t->get_id();
163 * @brief Construct a new ModelAction with source line number (requires llvm support)
165 * @param type The type of action
166 * @param position The source line number of this atomic operation
167 * @param order The memory order of this action. A "don't care" for non-ATOMIC
168 * actions (e.g., THREAD_* or MODEL_* actions).
169 * @param loc The location that this action acts upon
170 * @param value (optional) A value associated with the action (e.g., the value
171 * read or written). Defaults to a given macro constant, for debugging purposes.
172 * @param thread (optional) The Thread in which this action occurred. If NULL
173 * (default), then a Thread is assigned according to the scheduler.
175 ModelAction::ModelAction(action_type_t type, const char * position, memory_order order,
176 void *loc, uint64_t value, Thread *thread) :
180 last_fence_release(NULL),
189 original_order(order),
190 seq_number(ACTION_INITIAL_CLOCK)
192 /* References to NULL atomic variables can end up here */
193 ASSERT(loc || type == ATOMIC_FENCE);
195 Thread *t = thread ? thread : thread_current();
196 this->tid = t->get_id();
200 /** @brief ModelAction destructor */
201 ModelAction::~ModelAction()
204 * We can't free the clock vector:
205 * Clock vectors are snapshotting state. When we delete model actions,
206 * they are at the end of the node list and have invalid old clock
207 * vectors which have already been rolled back to an unallocated state.
215 int ModelAction::getSize() const {
219 void ModelAction::copy_from_new(ModelAction *newaction)
221 seq_number = newaction->seq_number;
224 void ModelAction::set_seq_number(modelclock_t num)
226 ASSERT(seq_number == ACTION_INITIAL_CLOCK);
230 void ModelAction::reset_seq_number()
235 bool ModelAction::is_thread_start() const
237 return type == THREAD_START;
240 bool ModelAction::is_thread_join() const
242 return type == THREAD_JOIN || type == PTHREAD_JOIN;
245 bool ModelAction::is_mutex_op() const
247 return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK || type == ATOMIC_WAIT || type == ATOMIC_TIMEDWAIT || type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
250 bool ModelAction::is_lock() const
252 return type == ATOMIC_LOCK;
255 bool ModelAction::is_sleep() const
257 return type == THREAD_SLEEP;
260 bool ModelAction::is_wait() const {
261 return type == ATOMIC_WAIT || type == ATOMIC_TIMEDWAIT;
264 bool ModelAction::is_notify() const {
265 return type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
268 bool ModelAction::is_notify_one() const {
269 return type == ATOMIC_NOTIFY_ONE;
272 bool ModelAction::is_unlock() const
274 return type == ATOMIC_UNLOCK;
277 bool ModelAction::is_trylock() const
279 return type == ATOMIC_TRYLOCK;
282 bool ModelAction::is_success_lock() const
284 return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
287 bool ModelAction::is_failed_trylock() const
289 return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
292 /** @return True if this operation is performed on a C/C++ atomic variable */
293 bool ModelAction::is_atomic_var() const
295 return is_read() || could_be_write();
298 bool ModelAction::is_read() const
300 return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMWRCAS || type == ATOMIC_RMW;
303 bool ModelAction::is_write() const
305 return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT || type == NONATOMIC_WRITE;
308 bool ModelAction::is_free() const
310 return type == READY_FREE;
313 bool ModelAction::could_be_write() const
315 return is_write() || is_rmwr();
318 bool ModelAction::is_yield() const
320 return type == THREAD_YIELD;
323 bool ModelAction::is_rmwr() const
325 return type == ATOMIC_RMWR || type == ATOMIC_RMWRCAS;
328 bool ModelAction::is_rmwrcas() const
330 return type == ATOMIC_RMWRCAS;
333 bool ModelAction::is_rmw() const
335 return type == ATOMIC_RMW;
338 bool ModelAction::is_rmwc() const
340 return type == ATOMIC_RMWC;
343 bool ModelAction::is_fence() const
345 return type == ATOMIC_FENCE;
348 bool ModelAction::is_initialization() const
350 return type == ATOMIC_INIT;
353 bool ModelAction::is_annotation() const
355 return type == ATOMIC_ANNOTATION;
358 bool ModelAction::is_relaxed() const
360 return order == std::memory_order_relaxed;
363 bool ModelAction::is_acquire() const
366 case std::memory_order_acquire:
367 case std::memory_order_acq_rel:
368 case std::memory_order_seq_cst:
375 bool ModelAction::is_release() const
378 case std::memory_order_release:
379 case std::memory_order_acq_rel:
380 case std::memory_order_seq_cst:
387 bool ModelAction::is_seqcst() const
389 return order == std::memory_order_seq_cst;
392 bool ModelAction::same_var(const ModelAction *act) const
394 if (act->is_wait() || is_wait()) {
395 if (act->is_wait() && is_wait()) {
396 if (((void *)value) == ((void *)act->value))
398 } else if (is_wait()) {
399 if (((void *)value) == act->location)
401 } else if (act->is_wait()) {
402 if (location == ((void *)act->value))
407 return location == act->location;
410 bool ModelAction::same_thread(const ModelAction *act) const
412 return tid == act->tid;
415 void ModelAction::copy_typeandorder(ModelAction * act)
417 this->type = act->type;
418 this->order = act->order;
422 * Get the Thread which is the operand of this action. This is only valid for
423 * THREAD_* operations (currently only for THREAD_CREATE and THREAD_JOIN). Note
424 * that this provides a central place for determining the conventions of Thread
425 * storage in ModelAction, where we generally aren't very type-safe (e.g., we
426 * store object references in a (void *) address.
428 * For THREAD_CREATE, this yields the Thread which is created.
429 * For THREAD_JOIN, this yields the Thread we are joining with.
431 * @return The Thread which this action acts on, if exists; otherwise NULL
433 Thread * ModelAction::get_thread_operand() const
435 if (type == THREAD_CREATE) {
436 /* thread_operand is set in execution.cc */
437 return thread_operand;
438 } else if (type == PTHREAD_CREATE) {
439 return thread_operand;
440 } else if (type == THREAD_JOIN) {
441 /* THREAD_JOIN uses (Thread *) for location */
442 return (Thread *)get_location();
443 } else if (type == PTHREAD_JOIN) {
445 // thread_operand is stored in execution::pthread_map;
446 return (Thread *)get_location();
452 * @brief Convert the read portion of an RMW
454 * Changes an existing read part of an RMW action into either:
455 * -# a full RMW action in case of the completed write or
456 * -# a READ action in case a failed action.
458 * @todo If the memory_order changes, we may potentially need to update our
461 * @param act The second half of the RMW (either RMWC or RMW)
463 void ModelAction::process_rmw(ModelAction *act)
465 this->order = act->order;
467 this->type = ATOMIC_READ;
468 else if (act->is_rmw()) {
469 this->type = ATOMIC_RMW;
470 this->value = act->value;
475 * @brief Check if this action should be backtracked with another, due to
476 * potential synchronization
478 * The is_synchronizing method should only explore interleavings if:
479 * -# the operations are seq_cst and don't commute or
480 * -# the reordering may establish or break a synchronization relation.
482 * Other memory operations will be dealt with by using the reads_from relation.
484 * @param act The action to consider exploring a reordering
485 * @return True, if we have to explore a reordering; otherwise false
487 bool ModelAction::could_synchronize_with(const ModelAction *act) const
489 // Same thread can't be reordered
490 if (same_thread(act))
493 // Different locations commute
494 if (!same_var(act) && !is_fence() && !act->is_fence())
497 // Explore interleavings of seqcst writes/fences to guarantee total
498 // order of seq_cst operations that don't commute
499 if ((could_be_write() || act->could_be_write() || is_fence() || act->is_fence()) && is_seqcst() && act->is_seqcst())
502 // Explore synchronizing read/write pairs
503 if (is_acquire() && act->is_release() && is_read() && act->could_be_write())
506 // lock just released...we can grab lock
507 if ((is_lock() || is_trylock()) && (act->is_unlock() || act->is_wait()))
510 // lock just acquired...we can fail to grab lock
511 if (is_trylock() && act->is_success_lock())
514 // other thread stalling on lock...we can release lock
515 if (is_unlock() && (act->is_trylock() || act->is_lock()))
518 if (is_trylock() && (act->is_unlock() || act->is_wait()))
521 if (is_notify() && act->is_wait())
524 if (is_wait() && act->is_notify())
527 // Otherwise handle by reads_from relation
531 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
533 // Must be different threads to reorder
534 if (same_thread(act))
537 // Try to reorder a lock past a successful lock
538 if (act->is_success_lock())
541 // Try to push a successful trylock past an unlock
542 if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
545 // Try to push a successful trylock past a wait
546 if (act->is_wait() && is_trylock() && value == VALUE_TRYSUCCESS)
553 * Create a new clock vector for this action. Note that this function allows a
554 * user to clobber (and leak) a ModelAction's existing clock vector. A user
555 * should ensure that the vector has already either been rolled back
556 * (effectively "freed") or freed.
558 * @param parent A ModelAction from which to inherit a ClockVector
560 void ModelAction::create_cv(const ModelAction *parent)
563 cv = new ClockVector(parent->cv, this);
565 cv = new ClockVector(NULL, this);
568 void ModelAction::set_try_lock(bool obtainedlock)
570 value = obtainedlock ? VALUE_TRYSUCCESS : VALUE_TRYFAILED;
574 * @brief Get the value read by this load
576 * We differentiate this function from ModelAction::get_write_value and
577 * ModelAction::get_value for the purpose of RMW's, which may have both a
578 * 'read' and a 'write' value.
580 * Note: 'this' must be a load.
582 * @return The value read by this load
584 uint64_t ModelAction::get_reads_from_value() const
588 return reads_from->get_write_value();
590 return VALUE_NONE; // Only for new actions with no reads-from
594 * @brief Get the value written by this store
596 * We differentiate this function from ModelAction::get_reads_from_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 store.
602 * @return The value written by this store
604 uint64_t ModelAction::get_write_value() const
611 * @brief Get the value returned by this action
613 * For atomic reads (including RMW), an operation returns the value it read.
614 * For atomic writes, an operation returns the value it wrote. For other
615 * operations, the return value varies (sometimes is a "don't care"), but the
616 * value is simply stored in the "value" field.
618 * @return This action's return value
620 uint64_t ModelAction::get_return_value() const
623 return get_reads_from_value();
625 return get_write_value();
631 * Update the model action's read_from action
632 * @param act The action to read from; should be a write
634 void ModelAction::set_read_from(ModelAction *act)
642 * Synchronize the current thread with the thread corresponding to the
643 * ModelAction parameter.
644 * @param act The ModelAction to synchronize with
645 * @return True if this is a valid synchronization; false otherwise
647 bool ModelAction::synchronize_with(const ModelAction *act)
655 bool ModelAction::has_synchronized_with(const ModelAction *act) const
657 return cv->synchronized_since(act);
661 * Check whether 'this' happens before act, according to the memory-model's
662 * happens before relation. This is checked via the ClockVector constructs.
663 * @return true if this action's thread has synchronized with act's thread
664 * since the execution of act, false otherwise.
666 bool ModelAction::happens_before(const ModelAction *act) const
668 return act->cv->synchronized_since(this);
671 const char * ModelAction::get_type_str() const
673 switch (this->type) {
674 case THREAD_CREATE: return "thread create";
675 case THREAD_START: return "thread start";
676 case THREAD_YIELD: return "thread yield";
677 case THREAD_JOIN: return "thread join";
678 case THREAD_FINISH: return "thread finish";
679 case THREAD_SLEEP: return "thread sleep";
680 case THREADONLY_FINISH: return "pthread_exit finish";
682 case PTHREAD_CREATE: return "pthread create";
683 case PTHREAD_JOIN: return "pthread join";
685 case NONATOMIC_WRITE: return "nonatomic write";
686 case ATOMIC_READ: return "atomic read";
687 case ATOMIC_WRITE: return "atomic write";
688 case ATOMIC_RMW: return "atomic rmw";
689 case ATOMIC_FENCE: return "fence";
690 case ATOMIC_RMWR: return "atomic rmwr";
691 case ATOMIC_RMWRCAS: return "atomic rmwrcas";
692 case ATOMIC_RMWC: return "atomic rmwc";
693 case ATOMIC_INIT: return "init atomic";
694 case ATOMIC_LOCK: return "lock";
695 case ATOMIC_UNLOCK: return "unlock";
696 case ATOMIC_TRYLOCK: return "trylock";
697 case ATOMIC_WAIT: return "wait";
698 case ATOMIC_TIMEDWAIT: return "timed wait";
699 case ATOMIC_NOTIFY_ONE: return "notify one";
700 case ATOMIC_NOTIFY_ALL: return "notify all";
701 case ATOMIC_ANNOTATION: return "annotation";
702 default: return "unknown type";
706 const char * ModelAction::get_mo_str() const
708 switch (this->order) {
709 case std::memory_order_relaxed: return "relaxed";
710 case std::memory_order_acquire: return "acquire";
711 case std::memory_order_release: return "release";
712 case std::memory_order_acq_rel: return "acq_rel";
713 case std::memory_order_seq_cst: return "seq_cst";
714 default: return "unknown";
718 /** @brief Print nicely-formatted info about this ModelAction */
719 void ModelAction::print() const
721 const char *type_str = get_type_str(), *mo_str = get_mo_str();
723 model_print("%-4d %-2d %-14s %7s %14p %-#18" PRIx64,
724 seq_number, id_to_int(tid), type_str, mo_str, location, get_return_value());
727 model_print(" %-3d", reads_from->get_seq_number());
741 /** @brief Get a (likely) unique hash for this ModelAction */
742 unsigned int ModelAction::hash() const
744 unsigned int hash = (unsigned int)this->type;
745 hash ^= ((unsigned int)this->order) << 3;
746 hash ^= seq_number << 5;
747 hash ^= id_to_int(tid) << 6;
751 hash ^= reads_from->get_seq_number();
752 hash ^= get_reads_from_value();
758 * Only valid for LOCK, TRY_LOCK, UNLOCK, and WAIT operations.
759 * @return The mutex operated on by this action, if any; otherwise NULL
761 cdsc::mutex * ModelAction::get_mutex() const
763 if (is_trylock() || is_lock() || is_unlock())
764 return (cdsc::mutex *)get_location();
766 return (cdsc::mutex *)get_value();