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 == ATOMIC_NOP);
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 this->tid = thread_current_id();
119 * @brief Construct a new ModelAction with source line number (requires llvm support)
121 * @param type The type of action
122 * @param order The memory order of this action. A "don't care" for non-ATOMIC
123 * actions (e.g., THREAD_* or MODEL_* actions).
124 * @param loc The location that this action acts upon
125 * @param value (optional) A value associated with the action (e.g., the value
126 * read or written). Defaults to a given macro constant, for debugging purposes.
127 * @param size (optional) The Thread in which this action occurred. If NULL
128 * (default), then a Thread is assigned according to the scheduler.
130 ModelAction::ModelAction(action_type_t type, const char * position, memory_order order, void *loc,
131 uint64_t value, int size) :
135 last_fence_release(NULL),
142 original_order(order),
143 seq_number(ACTION_INITIAL_CLOCK)
145 /* References to NULL atomic variables can end up here */
148 this->tid = thread_current_id();
153 * @brief Construct a new ModelAction with source line number (requires llvm support)
155 * @param type The type of action
156 * @param position The source line number of this atomic operation
157 * @param order The memory order of this action. A "don't care" for non-ATOMIC
158 * actions (e.g., THREAD_* or MODEL_* actions).
159 * @param loc The location that this action acts upon
160 * @param value (optional) A value associated with the action (e.g., the value
161 * read or written). Defaults to a given macro constant, for debugging purposes.
162 * @param thread (optional) The Thread in which this action occurred. If NULL
163 * (default), then a Thread is assigned according to the scheduler.
165 ModelAction::ModelAction(action_type_t type, const char * position, memory_order order,
166 void *loc, uint64_t value, Thread *thread) :
170 last_fence_release(NULL),
177 original_order(order),
178 seq_number(ACTION_INITIAL_CLOCK)
180 /* References to NULL atomic variables can end up here */
181 ASSERT(loc || type == ATOMIC_FENCE);
183 Thread *t = thread ? thread : thread_current();
184 this->tid = t->get_id();
188 /** @brief ModelAction destructor */
189 ModelAction::~ModelAction()
192 * We can't free the clock vector:
193 * Clock vectors are snapshotting state. When we delete model actions,
194 * they are at the end of the node list and have invalid old clock
195 * 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 ASSERT(seq_number == ACTION_INITIAL_CLOCK);
220 void ModelAction::reset_seq_number()
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_TIMEDWAIT || 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 || type == ATOMIC_TIMEDWAIT;
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_read() const
290 return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMWRCAS || type == ATOMIC_RMW;
293 bool ModelAction::is_write() const
295 return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT || type == NONATOMIC_WRITE;
298 bool ModelAction::is_create() const
300 return type == THREAD_CREATE || type == PTHREAD_CREATE;
303 bool ModelAction::is_free() const
305 return type == READY_FREE;
308 bool ModelAction::could_be_write() const
310 return is_write() || is_rmwr();
313 bool ModelAction::is_yield() const
315 return type == THREAD_YIELD;
318 bool ModelAction::is_rmwr() const
320 return type == ATOMIC_RMWR || type == ATOMIC_RMWRCAS;
323 bool ModelAction::is_rmwrcas() const
325 return type == ATOMIC_RMWRCAS;
328 bool ModelAction::is_rmw() const
330 return type == ATOMIC_RMW;
333 bool ModelAction::is_rmwc() const
335 return type == ATOMIC_RMWC;
338 bool ModelAction::is_fence() const
340 return type == ATOMIC_FENCE;
343 bool ModelAction::is_initialization() const
345 return type == ATOMIC_INIT;
348 bool ModelAction::is_annotation() const
350 return type == ATOMIC_ANNOTATION;
353 bool ModelAction::is_relaxed() const
355 return order == std::memory_order_relaxed;
358 bool ModelAction::is_acquire() const
361 case std::memory_order_acquire:
362 case std::memory_order_acq_rel:
363 case std::memory_order_seq_cst:
370 bool ModelAction::is_release() const
373 case std::memory_order_release:
374 case std::memory_order_acq_rel:
375 case std::memory_order_seq_cst:
382 bool ModelAction::is_seqcst() const
384 return order == std::memory_order_seq_cst;
387 bool ModelAction::same_var(const ModelAction *act) const
389 if (act->is_wait() || is_wait()) {
390 if (act->is_wait() && is_wait()) {
391 if (((void *)value) == ((void *)act->value))
393 } else if (is_wait()) {
394 if (((void *)value) == act->location)
396 } else if (act->is_wait()) {
397 if (location == ((void *)act->value))
402 return location == act->location;
405 bool ModelAction::same_thread(const ModelAction *act) const
407 return tid == act->tid;
410 void ModelAction::copy_typeandorder(ModelAction * act)
412 this->type = act->type;
413 this->order = act->order;
417 * Get the Thread which is the operand of this action. This is only valid for
418 * THREAD_* operations (currently only for THREAD_CREATE and THREAD_JOIN). Note
419 * that this provides a central place for determining the conventions of Thread
420 * storage in ModelAction, where we generally aren't very type-safe (e.g., we
421 * store object references in a (void *) address.
423 * For THREAD_CREATE, this yields the Thread which is created.
424 * For THREAD_JOIN, this yields the Thread we are joining with.
426 * @return The Thread which this action acts on, if exists; otherwise NULL
428 Thread * ModelAction::get_thread_operand() const
430 if (type == THREAD_CREATE) {
431 /* thread_operand is set in execution.cc */
432 return thread_operand;
433 } else if (type == PTHREAD_CREATE) {
434 return thread_operand;
435 } else if (type == THREAD_JOIN) {
436 /* THREAD_JOIN uses (Thread *) for location */
437 return (Thread *)get_location();
438 } else if (type == PTHREAD_JOIN) {
440 // thread_operand is stored in execution::pthread_map;
441 return (Thread *)get_location();
447 * @brief Convert the read portion of an RMW
449 * Changes an existing read part of an RMW action into either:
450 * -# a full RMW action in case of the completed write or
451 * -# a READ action in case a failed action.
453 * @todo If the memory_order changes, we may potentially need to update our
456 * @param act The second half of the RMW (either RMWC or RMW)
458 void ModelAction::process_rmw(ModelAction *act)
460 this->order = act->order;
462 this->type = ATOMIC_READ;
463 else if (act->is_rmw()) {
464 this->type = ATOMIC_RMW;
465 this->value = act->value;
470 * @brief Check if this action should be backtracked with another, due to
471 * potential synchronization
473 * The is_synchronizing method should only explore interleavings if:
474 * -# the operations are seq_cst and don't commute or
475 * -# the reordering may establish or break a synchronization relation.
477 * Other memory operations will be dealt with by using the reads_from relation.
479 * @param act The action to consider exploring a reordering
480 * @return True, if we have to explore a reordering; otherwise false
482 bool ModelAction::could_synchronize_with(const ModelAction *act) const
484 // Same thread can't be reordered
485 if (same_thread(act))
488 // Different locations commute
489 if (!same_var(act) && !is_fence() && !act->is_fence())
492 // Explore interleavings of seqcst writes/fences to guarantee total
493 // order of seq_cst operations that don't commute
494 if ((could_be_write() || act->could_be_write() || is_fence() || act->is_fence()) && is_seqcst() && act->is_seqcst())
497 // Explore synchronizing read/write pairs
498 if (is_acquire() && act->is_release() && is_read() && act->could_be_write())
501 // lock just released...we can grab lock
502 if ((is_lock() || is_trylock()) && (act->is_unlock() || act->is_wait()))
505 // lock just acquired...we can fail to grab lock
506 if (is_trylock() && act->is_success_lock())
509 // other thread stalling on lock...we can release lock
510 if (is_unlock() && (act->is_trylock() || act->is_lock()))
513 if (is_trylock() && (act->is_unlock() || act->is_wait()))
516 if (is_notify() && act->is_wait())
519 if (is_wait() && act->is_notify())
522 // Otherwise handle by reads_from relation
526 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
528 // Must be different threads to reorder
529 if (same_thread(act))
532 // Try to reorder a lock past a successful lock
533 if (act->is_success_lock())
536 // Try to push a successful trylock past an unlock
537 if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
540 // Try to push a successful trylock past a wait
541 if (act->is_wait() && is_trylock() && value == VALUE_TRYSUCCESS)
548 * Create a new clock vector for this action. Note that this function allows a
549 * user to clobber (and leak) a ModelAction's existing clock vector. A user
550 * should ensure that the vector has already either been rolled back
551 * (effectively "freed") or freed.
553 * @param parent A ModelAction from which to inherit a ClockVector
555 void ModelAction::create_cv(const ModelAction *parent)
558 cv = new ClockVector(parent->cv, this);
560 cv = new ClockVector(NULL, this);
563 void ModelAction::set_try_lock(bool obtainedlock)
565 value = obtainedlock ? VALUE_TRYSUCCESS : VALUE_TRYFAILED;
569 * @brief Get the value read by this load
571 * We differentiate this function from ModelAction::get_write_value and
572 * ModelAction::get_value for the purpose of RMW's, which may have both a
573 * 'read' and a 'write' value.
575 * Note: 'this' must be a load.
577 * @return The value read by this load
579 uint64_t ModelAction::get_reads_from_value() const
583 return reads_from->get_write_value();
585 return VALUE_NONE; // Only for new actions with no reads-from
589 * @brief Get the value written by this store
591 * We differentiate this function from ModelAction::get_reads_from_value and
592 * ModelAction::get_value for the purpose of RMW's, which may have both a
593 * 'read' and a 'write' value.
595 * Note: 'this' must be a store.
597 * @return The value written by this store
599 uint64_t ModelAction::get_write_value() const
606 * @brief Get the value returned by this action
608 * For atomic reads (including RMW), an operation returns the value it read.
609 * For atomic writes, an operation returns the value it wrote. For other
610 * operations, the return value varies (sometimes is a "don't care"), but the
611 * value is simply stored in the "value" field.
613 * @return This action's return value
615 uint64_t ModelAction::get_return_value() const
618 return get_reads_from_value();
620 return get_write_value();
626 * Update the model action's read_from action
627 * @param act The action to read from; should be a write
629 void ModelAction::set_read_from(ModelAction *act)
637 * Synchronize the current thread with the thread corresponding to the
638 * ModelAction parameter.
639 * @param act The ModelAction to synchronize with
640 * @return True if this is a valid synchronization; false otherwise
642 bool ModelAction::synchronize_with(const ModelAction *act)
650 bool ModelAction::has_synchronized_with(const ModelAction *act) const
652 return cv->synchronized_since(act);
656 * Check whether 'this' happens before act, according to the memory-model's
657 * happens before relation. This is checked via the ClockVector constructs.
658 * @return true if this action's thread has synchronized with act's thread
659 * since the execution of act, false otherwise.
661 bool ModelAction::happens_before(const ModelAction *act) const
663 return act->cv->synchronized_since(this);
666 const char * ModelAction::get_type_str() const
668 switch (this->type) {
669 case THREAD_CREATE: return "thread create";
670 case THREAD_START: return "thread start";
671 case THREAD_YIELD: return "thread yield";
672 case THREAD_JOIN: return "thread join";
673 case THREAD_FINISH: return "thread finish";
674 case THREAD_SLEEP: return "thread sleep";
675 case THREADONLY_FINISH: return "pthread_exit finish";
677 case PTHREAD_CREATE: return "pthread create";
678 case PTHREAD_JOIN: return "pthread join";
680 case NONATOMIC_WRITE: return "nonatomic write";
681 case ATOMIC_READ: return "atomic read";
682 case ATOMIC_WRITE: return "atomic write";
683 case ATOMIC_RMW: return "atomic rmw";
684 case ATOMIC_FENCE: return "fence";
685 case ATOMIC_RMWR: return "atomic rmwr";
686 case ATOMIC_RMWRCAS: return "atomic rmwrcas";
687 case ATOMIC_RMWC: return "atomic rmwc";
688 case ATOMIC_INIT: return "init atomic";
689 case ATOMIC_LOCK: return "lock";
690 case ATOMIC_UNLOCK: return "unlock";
691 case ATOMIC_TRYLOCK: return "trylock";
692 case ATOMIC_WAIT: return "wait";
693 case ATOMIC_TIMEDWAIT: return "timed wait";
694 case ATOMIC_NOTIFY_ONE: return "notify one";
695 case ATOMIC_NOTIFY_ALL: return "notify all";
696 case ATOMIC_ANNOTATION: return "annotation";
697 default: return "unknown type";
701 const char * ModelAction::get_mo_str() const
703 switch (this->order) {
704 case std::memory_order_relaxed: return "relaxed";
705 case std::memory_order_acquire: return "acquire";
706 case std::memory_order_release: return "release";
707 case std::memory_order_acq_rel: return "acq_rel";
708 case std::memory_order_seq_cst: return "seq_cst";
709 default: return "unknown";
713 /** @brief Print nicely-formatted info about this ModelAction */
714 void ModelAction::print() const
716 const char *type_str = get_type_str(), *mo_str = get_mo_str();
718 model_print("%-4d %-2d %-14s %7s %14p %-#18" PRIx64,
719 seq_number, id_to_int(tid), type_str, mo_str, location, get_return_value());
722 model_print("(%" PRIx64 ")", get_write_value());
725 model_print(" %-3d", reads_from->get_seq_number());
739 /** @brief Get a (likely) unique hash for this ModelAction */
740 unsigned int ModelAction::hash() const
742 unsigned int hash = (unsigned int)this->type;
743 hash ^= ((unsigned int)this->order) << 3;
744 hash ^= seq_number << 5;
745 hash ^= id_to_int(tid) << 6;
749 hash ^= reads_from->get_seq_number();
750 hash ^= get_reads_from_value();
756 * Only valid for LOCK, TRY_LOCK, UNLOCK, and WAIT operations.
757 * @return The mutex operated on by this action, if any; otherwise NULL
759 cdsc::mutex * ModelAction::get_mutex() const
761 if (is_trylock() || is_lock() || is_unlock())
762 return (cdsc::mutex *)get_location();
764 return (cdsc::mutex *)get_value();