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) :
38 last_fence_release(NULL),
44 original_order(order),
45 seq_number(ACTION_INITIAL_CLOCK)
47 /* References to NULL atomic variables can end up here */
48 ASSERT(loc || type == ATOMIC_FENCE || type == NOOP);
50 Thread *t = thread ? thread : thread_current();
51 this->tid = t->get_id();
56 * @brief Construct a new ModelAction
58 * @param type The type of action
59 * @param order The memory order of this action. A "don't care" for non-ATOMIC
60 * actions (e.g., THREAD_* or MODEL_* actions).
61 * @param loc The location that this action acts upon
62 * @param value (optional) A value associated with the action (e.g., the value
63 * read or written). Defaults to a given macro constant, for debugging purposes.
64 * @param size (optional) The Thread in which this action occurred. If NULL
65 * (default), then a Thread is assigned according to the scheduler.
67 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc,
68 uint64_t value, int size) :
71 last_fence_release(NULL),
77 original_order(order),
78 seq_number(ACTION_INITIAL_CLOCK)
80 /* References to NULL atomic variables can end up here */
83 Thread *t = thread_current();
84 this->tid = t->get_id();
89 * @brief Construct a new ModelAction with source line number (requires llvm support)
91 * @param type The type of action
92 * @param position The source line number of this atomic operation
93 * @param order The memory order of this action. A "don't care" for non-ATOMIC
94 * actions (e.g., THREAD_* or MODEL_* actions).
95 * @param loc The location that this action acts upon
96 * @param value (optional) A value associated with the action (e.g., the value
97 * read or written). Defaults to a given macro constant, for debugging purposes.
98 * @param thread (optional) The Thread in which this action occurred. If NULL
99 * (default), then a Thread is assigned according to the scheduler.
101 ModelAction::ModelAction(action_type_t type, const char * position, memory_order order,
102 void *loc, uint64_t value, Thread *thread) :
106 last_fence_release(NULL),
112 original_order(order),
113 seq_number(ACTION_INITIAL_CLOCK)
115 /* References to NULL atomic variables can end up here */
116 ASSERT(loc || type == ATOMIC_FENCE);
118 Thread *t = thread ? thread : thread_current();
119 this->tid = t->get_id();
120 // model_print("position: %s\n", position);
124 /** @brief ModelAction destructor */
125 ModelAction::~ModelAction()
128 * We can't free the clock vector:
129 * Clock vectors are snapshotting state. When we delete model actions,
130 * they are at the end of the node list and have invalid old clock
131 * vectors which have already been rolled back to an unallocated state.
139 int ModelAction::getSize() const {
143 void ModelAction::copy_from_new(ModelAction *newaction)
145 seq_number = newaction->seq_number;
148 void ModelAction::set_seq_number(modelclock_t num)
150 /* ATOMIC_UNINIT actions should never have non-zero clock */
151 ASSERT(!is_uninitialized());
152 ASSERT(seq_number == ACTION_INITIAL_CLOCK);
156 bool ModelAction::is_thread_start() const
158 return type == THREAD_START;
161 bool ModelAction::is_thread_join() const
163 return type == THREAD_JOIN || type == PTHREAD_JOIN;
166 bool ModelAction::is_mutex_op() const
168 return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK || type == ATOMIC_WAIT || type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
171 bool ModelAction::is_lock() const
173 return type == ATOMIC_LOCK;
176 bool ModelAction::is_wait() const {
177 return type == ATOMIC_WAIT;
180 bool ModelAction::is_notify() const {
181 return type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
184 bool ModelAction::is_notify_one() const {
185 return type == ATOMIC_NOTIFY_ONE;
188 bool ModelAction::is_unlock() const
190 return type == ATOMIC_UNLOCK;
193 bool ModelAction::is_trylock() const
195 return type == ATOMIC_TRYLOCK;
198 bool ModelAction::is_success_lock() const
200 return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
203 bool ModelAction::is_failed_trylock() const
205 return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
208 /** @return True if this operation is performed on a C/C++ atomic variable */
209 bool ModelAction::is_atomic_var() const
211 return is_read() || could_be_write();
214 bool ModelAction::is_uninitialized() const
216 return type == ATOMIC_UNINIT;
219 bool ModelAction::is_read() const
221 return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMWRCAS || type == ATOMIC_RMW;
224 bool ModelAction::is_write() const
226 return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT || type == ATOMIC_UNINIT;
229 bool ModelAction::could_be_write() const
231 return is_write() || is_rmwr();
234 bool ModelAction::is_yield() const
236 return type == THREAD_YIELD;
239 bool ModelAction::is_rmwr() const
241 return type == ATOMIC_RMWR || type == ATOMIC_RMWRCAS;
244 bool ModelAction::is_rmwrcas() const
246 return type == ATOMIC_RMWRCAS;
249 bool ModelAction::is_rmw() const
251 return type == ATOMIC_RMW;
254 bool ModelAction::is_rmwc() const
256 return type == ATOMIC_RMWC;
259 bool ModelAction::is_fence() const
261 return type == ATOMIC_FENCE;
264 bool ModelAction::is_initialization() const
266 return type == ATOMIC_INIT;
269 bool ModelAction::is_annotation() const
271 return type == ATOMIC_ANNOTATION;
274 bool ModelAction::is_relaxed() const
276 return order == std::memory_order_relaxed;
279 bool ModelAction::is_acquire() const
282 case std::memory_order_acquire:
283 case std::memory_order_acq_rel:
284 case std::memory_order_seq_cst:
291 bool ModelAction::is_release() const
294 case std::memory_order_release:
295 case std::memory_order_acq_rel:
296 case std::memory_order_seq_cst:
303 bool ModelAction::is_seqcst() const
305 return order == std::memory_order_seq_cst;
308 bool ModelAction::same_var(const ModelAction *act) const
310 if (act->is_wait() || is_wait()) {
311 if (act->is_wait() && is_wait()) {
312 if (((void *)value) == ((void *)act->value))
314 } else if (is_wait()) {
315 if (((void *)value) == act->location)
317 } else if (act->is_wait()) {
318 if (location == ((void *)act->value))
323 return location == act->location;
326 bool ModelAction::same_thread(const ModelAction *act) const
328 return tid == act->tid;
331 void ModelAction::copy_typeandorder(ModelAction * act)
333 this->type = act->type;
334 this->order = act->order;
338 * Get the Thread which is the operand of this action. This is only valid for
339 * THREAD_* operations (currently only for THREAD_CREATE and THREAD_JOIN). Note
340 * that this provides a central place for determining the conventions of Thread
341 * storage in ModelAction, where we generally aren't very type-safe (e.g., we
342 * store object references in a (void *) address.
344 * For THREAD_CREATE, this yields the Thread which is created.
345 * For THREAD_JOIN, this yields the Thread we are joining with.
347 * @return The Thread which this action acts on, if exists; otherwise NULL
349 Thread * ModelAction::get_thread_operand() const
351 if (type == THREAD_CREATE) {
352 /* thread_operand is set in execution.cc */
353 return thread_operand;
354 } else if (type == PTHREAD_CREATE) {
355 return thread_operand;
356 } else if (type == THREAD_JOIN) {
357 /* THREAD_JOIN uses (Thread *) for location */
358 return (Thread *)get_location();
359 } else if (type == PTHREAD_JOIN) {
361 // thread_operand is stored in execution::pthread_map;
362 return (Thread *)get_location();
368 * @brief Convert the read portion of an RMW
370 * Changes an existing read part of an RMW action into either:
371 * -# a full RMW action in case of the completed write or
372 * -# a READ action in case a failed action.
374 * @todo If the memory_order changes, we may potentially need to update our
377 * @param act The second half of the RMW (either RMWC or RMW)
379 void ModelAction::process_rmw(ModelAction *act)
381 this->order = act->order;
383 this->type = ATOMIC_READ;
384 else if (act->is_rmw()) {
385 this->type = ATOMIC_RMW;
386 this->value = act->value;
391 * @brief Check if this action should be backtracked with another, due to
392 * potential synchronization
394 * The is_synchronizing method should only explore interleavings if:
395 * -# the operations are seq_cst and don't commute or
396 * -# the reordering may establish or break a synchronization relation.
398 * Other memory operations will be dealt with by using the reads_from relation.
400 * @param act The action to consider exploring a reordering
401 * @return True, if we have to explore a reordering; otherwise false
403 bool ModelAction::could_synchronize_with(const ModelAction *act) const
405 // Same thread can't be reordered
406 if (same_thread(act))
409 // Different locations commute
410 if (!same_var(act) && !is_fence() && !act->is_fence())
413 // Explore interleavings of seqcst writes/fences to guarantee total
414 // order of seq_cst operations that don't commute
415 if ((could_be_write() || act->could_be_write() || is_fence() || act->is_fence()) && is_seqcst() && act->is_seqcst())
418 // Explore synchronizing read/write pairs
419 if (is_acquire() && act->is_release() && is_read() && act->could_be_write())
422 // lock just released...we can grab lock
423 if ((is_lock() || is_trylock()) && (act->is_unlock() || act->is_wait()))
426 // lock just acquired...we can fail to grab lock
427 if (is_trylock() && act->is_success_lock())
430 // other thread stalling on lock...we can release lock
431 if (is_unlock() && (act->is_trylock() || act->is_lock()))
434 if (is_trylock() && (act->is_unlock() || act->is_wait()))
437 if (is_notify() && act->is_wait())
440 if (is_wait() && act->is_notify())
443 // Otherwise handle by reads_from relation
447 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
449 // Must be different threads to reorder
450 if (same_thread(act))
453 // Try to reorder a lock past a successful lock
454 if (act->is_success_lock())
457 // Try to push a successful trylock past an unlock
458 if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
461 // Try to push a successful trylock past a wait
462 if (act->is_wait() && is_trylock() && value == VALUE_TRYSUCCESS)
469 * Create a new clock vector for this action. Note that this function allows a
470 * user to clobber (and leak) a ModelAction's existing clock vector. A user
471 * should ensure that the vector has already either been rolled back
472 * (effectively "freed") or freed.
474 * @param parent A ModelAction from which to inherit a ClockVector
476 void ModelAction::create_cv(const ModelAction *parent)
479 cv = new ClockVector(parent->cv, this);
481 cv = new ClockVector(NULL, this);
484 void ModelAction::set_try_lock(bool obtainedlock)
486 value = obtainedlock ? VALUE_TRYSUCCESS : VALUE_TRYFAILED;
490 * @brief Get the value read by this load
492 * We differentiate this function from ModelAction::get_write_value and
493 * ModelAction::get_value for the purpose of RMW's, which may have both a
494 * 'read' and a 'write' value.
496 * Note: 'this' must be a load.
498 * @return The value read by this load
500 uint64_t ModelAction::get_reads_from_value() const
504 return reads_from->get_write_value();
506 return VALUE_NONE; // Only for new actions with no reads-from
510 * @brief Get the value written by this store
512 * We differentiate this function from ModelAction::get_reads_from_value and
513 * ModelAction::get_value for the purpose of RMW's, which may have both a
514 * 'read' and a 'write' value.
516 * Note: 'this' must be a store.
518 * @return The value written by this store
520 uint64_t ModelAction::get_write_value() const
527 * @brief Get the value returned by this action
529 * For atomic reads (including RMW), an operation returns the value it read.
530 * For atomic writes, an operation returns the value it wrote. For other
531 * operations, the return value varies (sometimes is a "don't care"), but the
532 * value is simply stored in the "value" field.
534 * @return This action's return value
536 uint64_t ModelAction::get_return_value() const
539 return get_reads_from_value();
541 return get_write_value();
546 /** @return The Node associated with this ModelAction */
547 Node * ModelAction::get_node() const
549 /* UNINIT actions do not have a Node */
550 ASSERT(!is_uninitialized());
555 * Update the model action's read_from action
556 * @param act The action to read from; should be a write
558 void ModelAction::set_read_from(const ModelAction *act)
564 if (act->is_uninitialized()) { // WL
565 uint64_t val = *((uint64_t *) location);
566 ModelAction * act_initialized = (ModelAction *)act;
567 act_initialized->set_value(val);
568 reads_from = (const ModelAction *)act_initialized;
570 // disabled by WL, because LLVM IR is unable to detect atomic init
571 /* model->assert_bug("May read from uninitialized atomic:\n"
572 " action %d, thread %d, location %p (%s, %s)",
573 seq_number, id_to_int(tid), location,
574 get_type_str(), get_mo_str());
580 * Synchronize the current thread with the thread corresponding to the
581 * ModelAction parameter.
582 * @param act The ModelAction to synchronize with
583 * @return True if this is a valid synchronization; false otherwise
585 bool ModelAction::synchronize_with(const ModelAction *act)
593 bool ModelAction::has_synchronized_with(const ModelAction *act) const
595 return cv->synchronized_since(act);
599 * Check whether 'this' happens before act, according to the memory-model's
600 * happens before relation. This is checked via the ClockVector constructs.
601 * @return true if this action's thread has synchronized with act's thread
602 * since the execution of act, false otherwise.
604 bool ModelAction::happens_before(const ModelAction *act) const
606 return act->cv->synchronized_since(this);
609 const char * ModelAction::get_type_str() const
611 switch (this->type) {
612 case THREAD_CREATE: return "thread create";
613 case THREAD_START: return "thread start";
614 case THREAD_YIELD: return "thread yield";
615 case THREAD_JOIN: return "thread join";
616 case THREAD_FINISH: return "thread finish";
618 case PTHREAD_CREATE: return "pthread create";
619 case PTHREAD_JOIN: return "pthread join";
621 case ATOMIC_UNINIT: return "uninitialized";
622 case ATOMIC_READ: return "atomic read";
623 case ATOMIC_WRITE: return "atomic write";
624 case ATOMIC_RMW: return "atomic rmw";
625 case ATOMIC_FENCE: return "fence";
626 case ATOMIC_RMWR: return "atomic rmwr";
627 case ATOMIC_RMWRCAS: return "atomic rmwrcas";
628 case ATOMIC_RMWC: return "atomic rmwc";
629 case ATOMIC_INIT: return "init atomic";
630 case ATOMIC_LOCK: return "lock";
631 case ATOMIC_UNLOCK: return "unlock";
632 case ATOMIC_TRYLOCK: return "trylock";
633 case ATOMIC_WAIT: return "wait";
634 case ATOMIC_NOTIFY_ONE: return "notify one";
635 case ATOMIC_NOTIFY_ALL: return "notify all";
636 case ATOMIC_ANNOTATION: return "annotation";
637 default: return "unknown type";
641 const char * ModelAction::get_mo_str() const
643 switch (this->order) {
644 case std::memory_order_relaxed: return "relaxed";
645 case std::memory_order_acquire: return "acquire";
646 case std::memory_order_release: return "release";
647 case std::memory_order_acq_rel: return "acq_rel";
648 case std::memory_order_seq_cst: return "seq_cst";
649 default: return "unknown";
653 /** @brief Print nicely-formatted info about this ModelAction */
654 void ModelAction::print() const
656 const char *type_str = get_type_str(), *mo_str = get_mo_str();
658 model_print("%-4d %-2d %-14s %7s %14p %-#18" PRIx64,
659 seq_number, id_to_int(tid), type_str, mo_str, location, get_return_value());
662 model_print(" %-3d", reads_from->get_seq_number());
676 /** @brief Get a (likely) unique hash for this ModelAction */
677 unsigned int ModelAction::hash() const
679 unsigned int hash = (unsigned int)this->type;
680 hash ^= ((unsigned int)this->order) << 3;
681 hash ^= seq_number << 5;
682 hash ^= id_to_int(tid) << 6;
686 hash ^= reads_from->get_seq_number();
687 hash ^= get_reads_from_value();
693 * Only valid for LOCK, TRY_LOCK, UNLOCK, and WAIT operations.
694 * @return The mutex operated on by this action, if any; otherwise NULL
696 cdsc::mutex * ModelAction::get_mutex() const
698 if (is_trylock() || is_lock() || is_unlock())
699 return (cdsc::mutex *)get_location();
701 return (cdsc::mutex *)get_value();