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 original_order(order),
42 reads_from_promise(NULL),
43 last_fence_release(NULL),
45 seq_number(ACTION_INITIAL_CLOCK),
49 /* References to NULL atomic variables can end up here */
50 ASSERT(loc || type == ATOMIC_FENCE || type == MODEL_FIXUP_RELSEQ);
52 Thread *t = thread ? thread : thread_current();
53 this->tid = t->get_id();
56 /** @brief ModelAction destructor */
57 ModelAction::~ModelAction()
60 * We can't free the clock vector:
61 * Clock vectors are snapshotting state. When we delete model actions,
62 * they are at the end of the node list and have invalid old clock
63 * vectors which have already been rolled back to an unallocated state.
71 void ModelAction::copy_from_new(ModelAction *newaction)
73 seq_number = newaction->seq_number;
76 void ModelAction::set_seq_number(modelclock_t num)
78 /* ATOMIC_UNINIT actions should never have non-zero clock */
79 ASSERT(!is_uninitialized());
80 ASSERT(seq_number == ACTION_INITIAL_CLOCK);
84 bool ModelAction::is_thread_start() const
86 return type == THREAD_START;
89 bool ModelAction::is_thread_join() const
91 return type == THREAD_JOIN || type == PTHREAD_JOIN;
94 bool ModelAction::is_relseq_fixup() const
96 return type == MODEL_FIXUP_RELSEQ;
99 bool ModelAction::is_mutex_op() const
101 return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK || type == ATOMIC_WAIT || type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
104 bool ModelAction::is_lock() const
106 return type == ATOMIC_LOCK;
109 bool ModelAction::is_wait() const {
110 return type == ATOMIC_WAIT;
113 bool ModelAction::is_notify() const {
114 return type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
117 bool ModelAction::is_notify_one() const {
118 return type == ATOMIC_NOTIFY_ONE;
121 bool ModelAction::is_unlock() const
123 return type == ATOMIC_UNLOCK;
126 bool ModelAction::is_trylock() const
128 return type == ATOMIC_TRYLOCK;
131 bool ModelAction::is_success_lock() const
133 return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
136 bool ModelAction::is_failed_trylock() const
138 return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
141 /** @return True if this operation is performed on a C/C++ atomic variable */
142 bool ModelAction::is_atomic_var() const
144 return is_read() || could_be_write();
147 bool ModelAction::is_uninitialized() const
149 return type == ATOMIC_UNINIT;
152 bool ModelAction::is_read() const
154 return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMW;
157 bool ModelAction::is_write() const
159 return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT || type == ATOMIC_UNINIT;
162 bool ModelAction::could_be_write() const
164 return is_write() || is_rmwr();
167 bool ModelAction::is_yield() const
169 return type == THREAD_YIELD;
172 bool ModelAction::is_rmwr() const
174 return type == ATOMIC_RMWR;
177 bool ModelAction::is_rmw() const
179 return type == ATOMIC_RMW;
182 bool ModelAction::is_rmwc() const
184 return type == ATOMIC_RMWC;
187 bool ModelAction::is_fence() const
189 return type == ATOMIC_FENCE;
192 bool ModelAction::is_initialization() const
194 return type == ATOMIC_INIT;
197 bool ModelAction::is_annotation() const
199 return type == ATOMIC_ANNOTATION;
202 bool ModelAction::is_relaxed() const
204 return order == std::memory_order_relaxed;
207 bool ModelAction::is_acquire() const
210 case std::memory_order_acquire:
211 case std::memory_order_acq_rel:
212 case std::memory_order_seq_cst:
219 bool ModelAction::is_release() const
222 case std::memory_order_release:
223 case std::memory_order_acq_rel:
224 case std::memory_order_seq_cst:
231 bool ModelAction::is_seqcst() const
233 return order == std::memory_order_seq_cst;
236 bool ModelAction::same_var(const ModelAction *act) const
238 if (act->is_wait() || is_wait()) {
239 if (act->is_wait() && is_wait()) {
240 if (((void *)value) == ((void *)act->value))
242 } else if (is_wait()) {
243 if (((void *)value) == act->location)
245 } else if (act->is_wait()) {
246 if (location == ((void *)act->value))
251 return location == act->location;
254 bool ModelAction::same_thread(const ModelAction *act) const
256 return tid == act->tid;
259 void ModelAction::copy_typeandorder(ModelAction * act)
261 this->type = act->type;
262 this->order = act->order;
266 * Get the Thread which is the operand of this action. This is only valid for
267 * THREAD_* operations (currently only for THREAD_CREATE and THREAD_JOIN). Note
268 * that this provides a central place for determining the conventions of Thread
269 * storage in ModelAction, where we generally aren't very type-safe (e.g., we
270 * store object references in a (void *) address.
272 * For THREAD_CREATE, this yields the Thread which is created.
273 * For THREAD_JOIN, this yields the Thread we are joining with.
275 * @return The Thread which this action acts on, if exists; otherwise NULL
277 Thread * ModelAction::get_thread_operand() const
279 if (type == THREAD_CREATE) {
280 /* thread_operand is set in execution.cc */
281 return thread_operand;
282 } else if (type == PTHREAD_CREATE) {
283 return thread_operand;
284 } else if (type == THREAD_JOIN) {
285 /* THREAD_JOIN uses (Thread *) for location */
286 return (Thread *)get_location();
287 } else if (type == PTHREAD_JOIN) {
289 // thread_operand is stored in execution::pthread_map;
290 return (Thread *)get_location();
296 * @brief Convert the read portion of an RMW
298 * Changes an existing read part of an RMW action into either:
299 * -# a full RMW action in case of the completed write or
300 * -# a READ action in case a failed action.
302 * @todo If the memory_order changes, we may potentially need to update our
305 * @param act The second half of the RMW (either RMWC or RMW)
307 void ModelAction::process_rmw(ModelAction *act)
309 this->order = act->order;
311 this->type = ATOMIC_READ;
312 else if (act->is_rmw()) {
313 this->type = ATOMIC_RMW;
314 this->value = act->value;
319 * @brief Check if this action should be backtracked with another, due to
320 * potential synchronization
322 * The is_synchronizing method should only explore interleavings if:
323 * -# the operations are seq_cst and don't commute or
324 * -# the reordering may establish or break a synchronization relation.
326 * Other memory operations will be dealt with by using the reads_from relation.
328 * @param act The action to consider exploring a reordering
329 * @return True, if we have to explore a reordering; otherwise false
331 bool ModelAction::could_synchronize_with(const ModelAction *act) const
333 // Same thread can't be reordered
334 if (same_thread(act))
337 // Different locations commute
338 if (!same_var(act) && !is_fence() && !act->is_fence())
341 // Explore interleavings of seqcst writes/fences to guarantee total
342 // order of seq_cst operations that don't commute
343 if ((could_be_write() || act->could_be_write() || is_fence() || act->is_fence()) && is_seqcst() && act->is_seqcst())
346 // Explore synchronizing read/write pairs
347 if (is_acquire() && act->is_release() && is_read() && act->could_be_write())
350 // lock just released...we can grab lock
351 if ((is_lock() || is_trylock()) && (act->is_unlock() || act->is_wait()))
354 // lock just acquired...we can fail to grab lock
355 if (is_trylock() && act->is_success_lock())
358 // other thread stalling on lock...we can release lock
359 if (is_unlock() && (act->is_trylock() || act->is_lock()))
362 if (is_trylock() && (act->is_unlock() || act->is_wait()))
365 if (is_notify() && act->is_wait())
368 if (is_wait() && act->is_notify())
371 // Otherwise handle by reads_from relation
375 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
377 // Must be different threads to reorder
378 if (same_thread(act))
381 // Try to reorder a lock past a successful lock
382 if (act->is_success_lock())
385 // Try to push a successful trylock past an unlock
386 if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
389 // Try to push a successful trylock past a wait
390 if (act->is_wait() && is_trylock() && value == VALUE_TRYSUCCESS)
397 * Create a new clock vector for this action. Note that this function allows a
398 * user to clobber (and leak) a ModelAction's existing clock vector. A user
399 * should ensure that the vector has already either been rolled back
400 * (effectively "freed") or freed.
402 * @param parent A ModelAction from which to inherit a ClockVector
404 void ModelAction::create_cv(const ModelAction *parent)
407 cv = new ClockVector(parent->cv, this);
409 cv = new ClockVector(NULL, this);
412 void ModelAction::set_try_lock(bool obtainedlock)
414 value = obtainedlock ? VALUE_TRYSUCCESS : VALUE_TRYFAILED;
418 * @brief Get the value read by this load
420 * We differentiate this function from ModelAction::get_write_value and
421 * ModelAction::get_value for the purpose of RMW's, which may have both a
422 * 'read' and a 'write' value.
424 * Note: 'this' must be a load.
426 * @return The value read by this load
428 uint64_t ModelAction::get_reads_from_value() const
432 return reads_from->get_write_value();
433 else if (reads_from_promise)
434 return reads_from_promise->get_value();
435 return VALUE_NONE; /* Only for new actions with no reads-from */
439 * @brief Get the value written by this store
441 * We differentiate this function from ModelAction::get_reads_from_value and
442 * ModelAction::get_value for the purpose of RMW's, which may have both a
443 * 'read' and a 'write' value.
445 * Note: 'this' must be a store.
447 * @return The value written by this store
449 uint64_t ModelAction::get_write_value() const
456 * @brief Get the value returned by this action
458 * For atomic reads (including RMW), an operation returns the value it read.
459 * For atomic writes, an operation returns the value it wrote. For other
460 * operations, the return value varies (sometimes is a "don't care"), but the
461 * value is simply stored in the "value" field.
463 * @return This action's return value
465 uint64_t ModelAction::get_return_value() const
468 return get_reads_from_value();
470 return get_write_value();
475 /** @return The Node associated with this ModelAction */
476 Node * ModelAction::get_node() const
478 /* UNINIT actions do not have a Node */
479 ASSERT(!is_uninitialized());
484 * Update the model action's read_from action
485 * @param act The action to read from; should be a write
487 void ModelAction::set_read_from(const ModelAction *act)
492 reads_from_promise = NULL;
494 if (act->is_uninitialized()) { // WL
495 uint64_t val = *((uint64_t *) location);
496 ModelAction * act_initialized = (ModelAction *)act;
497 act_initialized->set_value(val);
498 reads_from = (const ModelAction *)act_initialized;
500 // disabled by WL, because LLVM IR is unable to detect atomic init
501 /* model->assert_bug("May read from uninitialized atomic:\n"
502 " action %d, thread %d, location %p (%s, %s)",
503 seq_number, id_to_int(tid), location,
504 get_type_str(), get_mo_str());
510 * Set this action's read-from promise
511 * @param promise The promise to read from
513 void ModelAction::set_read_from_promise(Promise *promise)
516 reads_from_promise = promise;
521 * Synchronize the current thread with the thread corresponding to the
522 * ModelAction parameter.
523 * @param act The ModelAction to synchronize with
524 * @return True if this is a valid synchronization; false otherwise
526 bool ModelAction::synchronize_with(const ModelAction *act)
534 bool ModelAction::has_synchronized_with(const ModelAction *act) const
536 return cv->synchronized_since(act);
540 * Check whether 'this' happens before act, according to the memory-model's
541 * happens before relation. This is checked via the ClockVector constructs.
542 * @return true if this action's thread has synchronized with act's thread
543 * since the execution of act, false otherwise.
545 bool ModelAction::happens_before(const ModelAction *act) const
547 return act->cv->synchronized_since(this);
550 const char * ModelAction::get_type_str() const
552 switch (this->type) {
553 case MODEL_FIXUP_RELSEQ: return "relseq fixup";
554 case THREAD_CREATE: return "thread create";
555 case THREAD_START: return "thread start";
556 case THREAD_YIELD: return "thread yield";
557 case THREAD_JOIN: return "thread join";
558 case THREAD_FINISH: return "thread finish";
560 case PTHREAD_CREATE: return "pthread create";
561 case PTHREAD_JOIN: return "pthread join";
563 case ATOMIC_UNINIT: return "uninitialized";
564 case ATOMIC_READ: return "atomic read";
565 case ATOMIC_WRITE: return "atomic write";
566 case ATOMIC_RMW: return "atomic rmw";
567 case ATOMIC_FENCE: return "fence";
568 case ATOMIC_RMWR: return "atomic rmwr";
569 case ATOMIC_RMWC: return "atomic rmwc";
570 case ATOMIC_INIT: return "init atomic";
571 case ATOMIC_LOCK: return "lock";
572 case ATOMIC_UNLOCK: return "unlock";
573 case ATOMIC_TRYLOCK: return "trylock";
574 case ATOMIC_WAIT: return "wait";
575 case ATOMIC_NOTIFY_ONE: return "notify one";
576 case ATOMIC_NOTIFY_ALL: return "notify all";
577 case ATOMIC_ANNOTATION: return "annotation";
578 default: return "unknown type";
582 const char * ModelAction::get_mo_str() const
584 switch (this->order) {
585 case std::memory_order_relaxed: return "relaxed";
586 case std::memory_order_acquire: return "acquire";
587 case std::memory_order_release: return "release";
588 case std::memory_order_acq_rel: return "acq_rel";
589 case std::memory_order_seq_cst: return "seq_cst";
590 default: return "unknown";
594 /** @brief Print nicely-formatted info about this ModelAction */
595 void ModelAction::print() const
597 const char *type_str = get_type_str(), *mo_str = get_mo_str();
599 model_print("%-4d %-2d %-14s %7s %14p %-#18" PRIx64,
600 seq_number, id_to_int(tid), type_str, mo_str, location, get_return_value());
603 model_print(" %-3d", reads_from->get_seq_number());
604 else if (reads_from_promise) {
605 int idx = reads_from_promise->get_index();
607 model_print(" P%-2d", idx);
623 /** @brief Get a (likely) unique hash for this ModelAction */
624 unsigned int ModelAction::hash() const
626 unsigned int hash = (unsigned int)this->type;
627 hash ^= ((unsigned int)this->order) << 3;
628 hash ^= seq_number << 5;
629 hash ^= id_to_int(tid) << 6;
633 hash ^= reads_from->get_seq_number();
634 else if (reads_from_promise)
635 hash ^= reads_from_promise->get_index();
636 hash ^= get_reads_from_value();
642 * @brief Checks the NodeStack to see if a ModelAction is in our may-read-from set
643 * @param write The ModelAction to check for
644 * @return True if the ModelAction is found; false otherwise
646 bool ModelAction::may_read_from(const ModelAction *write) const
648 for (int i = 0; i < node->get_read_from_past_size(); i++)
649 if (node->get_read_from_past(i) == write)
655 * @brief Checks the NodeStack to see if a Promise is in our may-read-from set
656 * @param promise The Promise to check for
657 * @return True if the Promise is found; false otherwise
659 bool ModelAction::may_read_from(const Promise *promise) const
661 for (int i = 0; i < node->get_read_from_promise_size(); i++)
662 if (node->get_read_from_promise(i) == promise)
668 * Only valid for LOCK, TRY_LOCK, UNLOCK, and WAIT operations.
669 * @return The mutex operated on by this action, if any; otherwise NULL
671 std::mutex * ModelAction::get_mutex() const
673 if (is_trylock() || is_lock() || is_unlock())
674 return (std::mutex *)get_location();
676 return (std::mutex *)get_value();