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 last_fence_release(NULL),
44 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 original_order(order),
75 last_fence_release(NULL),
77 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();
468 return VALUE_NONE; /* Only for new actions with no reads-from */
472 * @brief Get the value written by this store
474 * We differentiate this function from ModelAction::get_reads_from_value and
475 * ModelAction::get_value for the purpose of RMW's, which may have both a
476 * 'read' and a 'write' value.
478 * Note: 'this' must be a store.
480 * @return The value written by this store
482 uint64_t ModelAction::get_write_value() const
489 * @brief Get the value returned by this action
491 * For atomic reads (including RMW), an operation returns the value it read.
492 * For atomic writes, an operation returns the value it wrote. For other
493 * operations, the return value varies (sometimes is a "don't care"), but the
494 * value is simply stored in the "value" field.
496 * @return This action's return value
498 uint64_t ModelAction::get_return_value() const
501 return get_reads_from_value();
503 return get_write_value();
508 /** @return The Node associated with this ModelAction */
509 Node * ModelAction::get_node() const
511 /* UNINIT actions do not have a Node */
512 ASSERT(!is_uninitialized());
517 * Update the model action's read_from action
518 * @param act The action to read from; should be a write
520 void ModelAction::set_read_from(const ModelAction *act)
526 if (act->is_uninitialized()) { // WL
527 uint64_t val = *((uint64_t *) location);
528 ModelAction * act_initialized = (ModelAction *)act;
529 act_initialized->set_value(val);
530 reads_from = (const ModelAction *)act_initialized;
532 // disabled by WL, because LLVM IR is unable to detect atomic init
533 /* model->assert_bug("May read from uninitialized atomic:\n"
534 " action %d, thread %d, location %p (%s, %s)",
535 seq_number, id_to_int(tid), location,
536 get_type_str(), get_mo_str());
542 * Synchronize the current thread with the thread corresponding to the
543 * ModelAction parameter.
544 * @param act The ModelAction to synchronize with
545 * @return True if this is a valid synchronization; false otherwise
547 bool ModelAction::synchronize_with(const ModelAction *act)
555 bool ModelAction::has_synchronized_with(const ModelAction *act) const
557 return cv->synchronized_since(act);
561 * Check whether 'this' happens before act, according to the memory-model's
562 * happens before relation. This is checked via the ClockVector constructs.
563 * @return true if this action's thread has synchronized with act's thread
564 * since the execution of act, false otherwise.
566 bool ModelAction::happens_before(const ModelAction *act) const
568 return act->cv->synchronized_since(this);
571 const char * ModelAction::get_type_str() const
573 switch (this->type) {
574 case THREAD_CREATE: return "thread create";
575 case THREAD_START: return "thread start";
576 case THREAD_YIELD: return "thread yield";
577 case THREAD_JOIN: return "thread join";
578 case THREAD_FINISH: return "thread finish";
580 case PTHREAD_CREATE: return "pthread create";
581 case PTHREAD_JOIN: return "pthread join";
583 case ATOMIC_UNINIT: return "uninitialized";
584 case ATOMIC_READ: return "atomic read";
585 case ATOMIC_WRITE: return "atomic write";
586 case ATOMIC_RMW: return "atomic rmw";
587 case ATOMIC_FENCE: return "fence";
588 case ATOMIC_RMWR: return "atomic rmwr";
589 case ATOMIC_RMWRCAS: return "atomic rmwrcas";
590 case ATOMIC_RMWC: return "atomic rmwc";
591 case ATOMIC_INIT: return "init atomic";
592 case ATOMIC_LOCK: return "lock";
593 case ATOMIC_UNLOCK: return "unlock";
594 case ATOMIC_TRYLOCK: return "trylock";
595 case ATOMIC_WAIT: return "wait";
596 case ATOMIC_NOTIFY_ONE: return "notify one";
597 case ATOMIC_NOTIFY_ALL: return "notify all";
598 case ATOMIC_ANNOTATION: return "annotation";
599 default: return "unknown type";
603 const char * ModelAction::get_mo_str() const
605 switch (this->order) {
606 case std::memory_order_relaxed: return "relaxed";
607 case std::memory_order_acquire: return "acquire";
608 case std::memory_order_release: return "release";
609 case std::memory_order_acq_rel: return "acq_rel";
610 case std::memory_order_seq_cst: return "seq_cst";
611 default: return "unknown";
615 /** @brief Print nicely-formatted info about this ModelAction */
616 void ModelAction::print() const
618 const char *type_str = get_type_str(), *mo_str = get_mo_str();
620 model_print("%-4d %-2d %-14s %7s %14p %-#18" PRIx64,
621 seq_number, id_to_int(tid), type_str, mo_str, location, get_return_value());
624 model_print(" %-3d", reads_from->get_seq_number());
638 /** @brief Get a (likely) unique hash for this ModelAction */
639 unsigned int ModelAction::hash() const
641 unsigned int hash = (unsigned int)this->type;
642 hash ^= ((unsigned int)this->order) << 3;
643 hash ^= seq_number << 5;
644 hash ^= id_to_int(tid) << 6;
648 hash ^= reads_from->get_seq_number();
649 hash ^= get_reads_from_value();
655 * Only valid for LOCK, TRY_LOCK, UNLOCK, and WAIT operations.
656 * @return The mutex operated on by this action, if any; otherwise NULL
658 cdsc::mutex * ModelAction::get_mutex() const
660 if (is_trylock() || is_lock() || is_unlock())
661 return (cdsc::mutex *)get_location();
663 return (cdsc::mutex *)get_value();