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);
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();
87 /** @brief ModelAction destructor */
88 ModelAction::~ModelAction()
91 * We can't free the clock vector:
92 * Clock vectors are snapshotting state. When we delete model actions,
93 * they are at the end of the node list and have invalid old clock
94 * vectors which have already been rolled back to an unallocated state.
102 int ModelAction::getSize() const {
106 void ModelAction::copy_from_new(ModelAction *newaction)
108 seq_number = newaction->seq_number;
111 void ModelAction::set_seq_number(modelclock_t num)
113 /* ATOMIC_UNINIT actions should never have non-zero clock */
114 ASSERT(!is_uninitialized());
115 ASSERT(seq_number == ACTION_INITIAL_CLOCK);
119 bool ModelAction::is_thread_start() const
121 return type == THREAD_START;
124 bool ModelAction::is_thread_join() const
126 return type == THREAD_JOIN || type == PTHREAD_JOIN;
129 bool ModelAction::is_mutex_op() const
131 return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK || type == ATOMIC_WAIT || type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
134 bool ModelAction::is_lock() const
136 return type == ATOMIC_LOCK;
139 bool ModelAction::is_wait() const {
140 return type == ATOMIC_WAIT;
143 bool ModelAction::is_notify() const {
144 return type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
147 bool ModelAction::is_notify_one() const {
148 return type == ATOMIC_NOTIFY_ONE;
151 bool ModelAction::is_unlock() const
153 return type == ATOMIC_UNLOCK;
156 bool ModelAction::is_trylock() const
158 return type == ATOMIC_TRYLOCK;
161 bool ModelAction::is_success_lock() const
163 return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
166 bool ModelAction::is_failed_trylock() const
168 return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
171 /** @return True if this operation is performed on a C/C++ atomic variable */
172 bool ModelAction::is_atomic_var() const
174 return is_read() || could_be_write();
177 bool ModelAction::is_uninitialized() const
179 return type == ATOMIC_UNINIT;
182 bool ModelAction::is_read() const
184 return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMWRCAS || type == ATOMIC_RMW;
187 bool ModelAction::is_write() const
189 return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT || type == ATOMIC_UNINIT;
192 bool ModelAction::could_be_write() const
194 return is_write() || is_rmwr();
197 bool ModelAction::is_yield() const
199 return type == THREAD_YIELD;
202 bool ModelAction::is_rmwr() const
204 return type == ATOMIC_RMWR || type == ATOMIC_RMWRCAS;
207 bool ModelAction::is_rmwrcas() const
209 return type == ATOMIC_RMWRCAS;
212 bool ModelAction::is_rmw() const
214 return type == ATOMIC_RMW;
217 bool ModelAction::is_rmwc() const
219 return type == ATOMIC_RMWC;
222 bool ModelAction::is_fence() const
224 return type == ATOMIC_FENCE;
227 bool ModelAction::is_initialization() const
229 return type == ATOMIC_INIT;
232 bool ModelAction::is_annotation() const
234 return type == ATOMIC_ANNOTATION;
237 bool ModelAction::is_relaxed() const
239 return order == std::memory_order_relaxed;
242 bool ModelAction::is_acquire() const
245 case std::memory_order_acquire:
246 case std::memory_order_acq_rel:
247 case std::memory_order_seq_cst:
254 bool ModelAction::is_release() const
257 case std::memory_order_release:
258 case std::memory_order_acq_rel:
259 case std::memory_order_seq_cst:
266 bool ModelAction::is_seqcst() const
268 return order == std::memory_order_seq_cst;
271 bool ModelAction::same_var(const ModelAction *act) const
273 if (act->is_wait() || is_wait()) {
274 if (act->is_wait() && is_wait()) {
275 if (((void *)value) == ((void *)act->value))
277 } else if (is_wait()) {
278 if (((void *)value) == act->location)
280 } else if (act->is_wait()) {
281 if (location == ((void *)act->value))
286 return location == act->location;
289 bool ModelAction::same_thread(const ModelAction *act) const
291 return tid == act->tid;
294 void ModelAction::copy_typeandorder(ModelAction * act)
296 this->type = act->type;
297 this->order = act->order;
301 * Get the Thread which is the operand of this action. This is only valid for
302 * THREAD_* operations (currently only for THREAD_CREATE and THREAD_JOIN). Note
303 * that this provides a central place for determining the conventions of Thread
304 * storage in ModelAction, where we generally aren't very type-safe (e.g., we
305 * store object references in a (void *) address.
307 * For THREAD_CREATE, this yields the Thread which is created.
308 * For THREAD_JOIN, this yields the Thread we are joining with.
310 * @return The Thread which this action acts on, if exists; otherwise NULL
312 Thread * ModelAction::get_thread_operand() const
314 if (type == THREAD_CREATE) {
315 /* thread_operand is set in execution.cc */
316 return thread_operand;
317 } else if (type == PTHREAD_CREATE) {
318 return thread_operand;
319 } else if (type == THREAD_JOIN) {
320 /* THREAD_JOIN uses (Thread *) for location */
321 return (Thread *)get_location();
322 } else if (type == PTHREAD_JOIN) {
324 // thread_operand is stored in execution::pthread_map;
325 return (Thread *)get_location();
331 * @brief Convert the read portion of an RMW
333 * Changes an existing read part of an RMW action into either:
334 * -# a full RMW action in case of the completed write or
335 * -# a READ action in case a failed action.
337 * @todo If the memory_order changes, we may potentially need to update our
340 * @param act The second half of the RMW (either RMWC or RMW)
342 void ModelAction::process_rmw(ModelAction *act)
344 this->order = act->order;
346 this->type = ATOMIC_READ;
347 else if (act->is_rmw()) {
348 this->type = ATOMIC_RMW;
349 this->value = act->value;
354 * @brief Check if this action should be backtracked with another, due to
355 * potential synchronization
357 * The is_synchronizing method should only explore interleavings if:
358 * -# the operations are seq_cst and don't commute or
359 * -# the reordering may establish or break a synchronization relation.
361 * Other memory operations will be dealt with by using the reads_from relation.
363 * @param act The action to consider exploring a reordering
364 * @return True, if we have to explore a reordering; otherwise false
366 bool ModelAction::could_synchronize_with(const ModelAction *act) const
368 // Same thread can't be reordered
369 if (same_thread(act))
372 // Different locations commute
373 if (!same_var(act) && !is_fence() && !act->is_fence())
376 // Explore interleavings of seqcst writes/fences to guarantee total
377 // order of seq_cst operations that don't commute
378 if ((could_be_write() || act->could_be_write() || is_fence() || act->is_fence()) && is_seqcst() && act->is_seqcst())
381 // Explore synchronizing read/write pairs
382 if (is_acquire() && act->is_release() && is_read() && act->could_be_write())
385 // lock just released...we can grab lock
386 if ((is_lock() || is_trylock()) && (act->is_unlock() || act->is_wait()))
389 // lock just acquired...we can fail to grab lock
390 if (is_trylock() && act->is_success_lock())
393 // other thread stalling on lock...we can release lock
394 if (is_unlock() && (act->is_trylock() || act->is_lock()))
397 if (is_trylock() && (act->is_unlock() || act->is_wait()))
400 if (is_notify() && act->is_wait())
403 if (is_wait() && act->is_notify())
406 // Otherwise handle by reads_from relation
410 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
412 // Must be different threads to reorder
413 if (same_thread(act))
416 // Try to reorder a lock past a successful lock
417 if (act->is_success_lock())
420 // Try to push a successful trylock past an unlock
421 if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
424 // Try to push a successful trylock past a wait
425 if (act->is_wait() && is_trylock() && value == VALUE_TRYSUCCESS)
432 * Create a new clock vector for this action. Note that this function allows a
433 * user to clobber (and leak) a ModelAction's existing clock vector. A user
434 * should ensure that the vector has already either been rolled back
435 * (effectively "freed") or freed.
437 * @param parent A ModelAction from which to inherit a ClockVector
439 void ModelAction::create_cv(const ModelAction *parent)
442 cv = new ClockVector(parent->cv, this);
444 cv = new ClockVector(NULL, this);
447 void ModelAction::set_try_lock(bool obtainedlock)
449 value = obtainedlock ? VALUE_TRYSUCCESS : VALUE_TRYFAILED;
453 * @brief Get the value read by this load
455 * We differentiate this function from ModelAction::get_write_value and
456 * ModelAction::get_value for the purpose of RMW's, which may have both a
457 * 'read' and a 'write' value.
459 * Note: 'this' must be a load.
461 * @return The value read by this load
463 uint64_t ModelAction::get_reads_from_value() const
467 return reads_from->get_write_value();
469 return VALUE_NONE; // Only for new actions with no reads-from
473 * @brief Get the value written by this store
475 * We differentiate this function from ModelAction::get_reads_from_value and
476 * ModelAction::get_value for the purpose of RMW's, which may have both a
477 * 'read' and a 'write' value.
479 * Note: 'this' must be a store.
481 * @return The value written by this store
483 uint64_t ModelAction::get_write_value() const
490 * @brief Get the value returned by this action
492 * For atomic reads (including RMW), an operation returns the value it read.
493 * For atomic writes, an operation returns the value it wrote. For other
494 * operations, the return value varies (sometimes is a "don't care"), but the
495 * value is simply stored in the "value" field.
497 * @return This action's return value
499 uint64_t ModelAction::get_return_value() const
502 return get_reads_from_value();
504 return get_write_value();
509 /** @return The Node associated with this ModelAction */
510 Node * ModelAction::get_node() const
512 /* UNINIT actions do not have a Node */
513 ASSERT(!is_uninitialized());
518 * Update the model action's read_from action
519 * @param act The action to read from; should be a write
521 void ModelAction::set_read_from(const ModelAction *act)
527 if (act->is_uninitialized()) { // WL
528 uint64_t val = *((uint64_t *) location);
529 ModelAction * act_initialized = (ModelAction *)act;
530 act_initialized->set_value(val);
531 reads_from = (const ModelAction *)act_initialized;
533 // disabled by WL, because LLVM IR is unable to detect atomic init
534 /* model->assert_bug("May read from uninitialized atomic:\n"
535 " action %d, thread %d, location %p (%s, %s)",
536 seq_number, id_to_int(tid), location,
537 get_type_str(), get_mo_str());
543 * Synchronize the current thread with the thread corresponding to the
544 * ModelAction parameter.
545 * @param act The ModelAction to synchronize with
546 * @return True if this is a valid synchronization; false otherwise
548 bool ModelAction::synchronize_with(const ModelAction *act)
556 bool ModelAction::has_synchronized_with(const ModelAction *act) const
558 return cv->synchronized_since(act);
562 * Check whether 'this' happens before act, according to the memory-model's
563 * happens before relation. This is checked via the ClockVector constructs.
564 * @return true if this action's thread has synchronized with act's thread
565 * since the execution of act, false otherwise.
567 bool ModelAction::happens_before(const ModelAction *act) const
569 return act->cv->synchronized_since(this);
572 const char * ModelAction::get_type_str() const
574 switch (this->type) {
575 case THREAD_CREATE: return "thread create";
576 case THREAD_START: return "thread start";
577 case THREAD_YIELD: return "thread yield";
578 case THREAD_JOIN: return "thread join";
579 case THREAD_FINISH: return "thread finish";
581 case PTHREAD_CREATE: return "pthread create";
582 case PTHREAD_JOIN: return "pthread join";
584 case ATOMIC_UNINIT: return "uninitialized";
585 case ATOMIC_READ: return "atomic read";
586 case ATOMIC_WRITE: return "atomic write";
587 case ATOMIC_RMW: return "atomic rmw";
588 case ATOMIC_FENCE: return "fence";
589 case ATOMIC_RMWR: return "atomic rmwr";
590 case ATOMIC_RMWRCAS: return "atomic rmwrcas";
591 case ATOMIC_RMWC: return "atomic rmwc";
592 case ATOMIC_INIT: return "init atomic";
593 case ATOMIC_LOCK: return "lock";
594 case ATOMIC_UNLOCK: return "unlock";
595 case ATOMIC_TRYLOCK: return "trylock";
596 case ATOMIC_WAIT: return "wait";
597 case ATOMIC_NOTIFY_ONE: return "notify one";
598 case ATOMIC_NOTIFY_ALL: return "notify all";
599 case ATOMIC_ANNOTATION: return "annotation";
600 default: return "unknown type";
604 const char * ModelAction::get_mo_str() const
606 switch (this->order) {
607 case std::memory_order_relaxed: return "relaxed";
608 case std::memory_order_acquire: return "acquire";
609 case std::memory_order_release: return "release";
610 case std::memory_order_acq_rel: return "acq_rel";
611 case std::memory_order_seq_cst: return "seq_cst";
612 default: return "unknown";
616 /** @brief Print nicely-formatted info about this ModelAction */
617 void ModelAction::print() const
619 const char *type_str = get_type_str(), *mo_str = get_mo_str();
621 model_print("%-4d %-2d %-14s %7s %14p %-#18" PRIx64,
622 seq_number, id_to_int(tid), type_str, mo_str, location, get_return_value());
625 model_print(" %-3d", reads_from->get_seq_number());
639 /** @brief Get a (likely) unique hash for this ModelAction */
640 unsigned int ModelAction::hash() const
642 unsigned int hash = (unsigned int)this->type;
643 hash ^= ((unsigned int)this->order) << 3;
644 hash ^= seq_number << 5;
645 hash ^= id_to_int(tid) << 6;
649 hash ^= reads_from->get_seq_number();
650 hash ^= get_reads_from_value();
656 * Only valid for LOCK, TRY_LOCK, UNLOCK, and WAIT operations.
657 * @return The mutex operated on by this action, if any; otherwise NULL
659 cdsc::mutex * ModelAction::get_mutex() const
661 if (is_trylock() || is_lock() || is_unlock())
662 return (cdsc::mutex *)get_location();
664 return (cdsc::mutex *)get_value();