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();
54 /** @brief ModelAction destructor */
55 ModelAction::~ModelAction()
58 * We can't free the clock vector:
59 * Clock vectors are snapshotting state. When we delete model actions,
60 * they are at the end of the node list and have invalid old clock
61 * vectors which have already been rolled back to an unallocated state.
69 void ModelAction::copy_from_new(ModelAction *newaction)
71 seq_number = newaction->seq_number;
74 void ModelAction::set_seq_number(modelclock_t num)
76 /* ATOMIC_UNINIT actions should never have non-zero clock */
77 ASSERT(!is_uninitialized());
78 ASSERT(seq_number == ACTION_INITIAL_CLOCK);
82 bool ModelAction::is_thread_start() const
84 return type == THREAD_START;
87 bool ModelAction::is_thread_join() const
89 return type == THREAD_JOIN || type == PTHREAD_JOIN;
92 bool ModelAction::is_mutex_op() const
94 return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK || type == ATOMIC_WAIT || type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
97 bool ModelAction::is_lock() const
99 return type == ATOMIC_LOCK;
102 bool ModelAction::is_wait() const {
103 return type == ATOMIC_WAIT;
106 bool ModelAction::is_notify() const {
107 return type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
110 bool ModelAction::is_notify_one() const {
111 return type == ATOMIC_NOTIFY_ONE;
114 bool ModelAction::is_unlock() const
116 return type == ATOMIC_UNLOCK;
119 bool ModelAction::is_trylock() const
121 return type == ATOMIC_TRYLOCK;
124 bool ModelAction::is_success_lock() const
126 return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
129 bool ModelAction::is_failed_trylock() const
131 return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
134 /** @return True if this operation is performed on a C/C++ atomic variable */
135 bool ModelAction::is_atomic_var() const
137 return is_read() || could_be_write();
140 bool ModelAction::is_uninitialized() const
142 return type == ATOMIC_UNINIT;
145 bool ModelAction::is_read() const
147 return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMW;
150 bool ModelAction::is_write() const
152 return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT || type == ATOMIC_UNINIT;
155 bool ModelAction::could_be_write() const
157 return is_write() || is_rmwr();
160 bool ModelAction::is_yield() const
162 return type == THREAD_YIELD;
165 bool ModelAction::is_rmwr() const
167 return type == ATOMIC_RMWR;
170 bool ModelAction::is_rmw() const
172 return type == ATOMIC_RMW;
175 bool ModelAction::is_rmwc() const
177 return type == ATOMIC_RMWC;
180 bool ModelAction::is_fence() const
182 return type == ATOMIC_FENCE;
185 bool ModelAction::is_initialization() const
187 return type == ATOMIC_INIT;
190 bool ModelAction::is_annotation() const
192 return type == ATOMIC_ANNOTATION;
195 bool ModelAction::is_relaxed() const
197 return order == std::memory_order_relaxed;
200 bool ModelAction::is_acquire() const
203 case std::memory_order_acquire:
204 case std::memory_order_acq_rel:
205 case std::memory_order_seq_cst:
212 bool ModelAction::is_release() const
215 case std::memory_order_release:
216 case std::memory_order_acq_rel:
217 case std::memory_order_seq_cst:
224 bool ModelAction::is_seqcst() const
226 return order == std::memory_order_seq_cst;
229 bool ModelAction::same_var(const ModelAction *act) const
231 if (act->is_wait() || is_wait()) {
232 if (act->is_wait() && is_wait()) {
233 if (((void *)value) == ((void *)act->value))
235 } else if (is_wait()) {
236 if (((void *)value) == act->location)
238 } else if (act->is_wait()) {
239 if (location == ((void *)act->value))
244 return location == act->location;
247 bool ModelAction::same_thread(const ModelAction *act) const
249 return tid == act->tid;
252 void ModelAction::copy_typeandorder(ModelAction * act)
254 this->type = act->type;
255 this->order = act->order;
259 * Get the Thread which is the operand of this action. This is only valid for
260 * THREAD_* operations (currently only for THREAD_CREATE and THREAD_JOIN). Note
261 * that this provides a central place for determining the conventions of Thread
262 * storage in ModelAction, where we generally aren't very type-safe (e.g., we
263 * store object references in a (void *) address.
265 * For THREAD_CREATE, this yields the Thread which is created.
266 * For THREAD_JOIN, this yields the Thread we are joining with.
268 * @return The Thread which this action acts on, if exists; otherwise NULL
270 Thread * ModelAction::get_thread_operand() const
272 if (type == THREAD_CREATE) {
273 /* thread_operand is set in execution.cc */
274 return thread_operand;
275 } else if (type == PTHREAD_CREATE) {
276 return thread_operand;
277 } else if (type == THREAD_JOIN) {
278 /* THREAD_JOIN uses (Thread *) for location */
279 return (Thread *)get_location();
280 } else if (type == PTHREAD_JOIN) {
282 // thread_operand is stored in execution::pthread_map;
283 return (Thread *)get_location();
289 * @brief Convert the read portion of an RMW
291 * Changes an existing read part of an RMW action into either:
292 * -# a full RMW action in case of the completed write or
293 * -# a READ action in case a failed action.
295 * @todo If the memory_order changes, we may potentially need to update our
298 * @param act The second half of the RMW (either RMWC or RMW)
300 void ModelAction::process_rmw(ModelAction *act)
302 this->order = act->order;
304 this->type = ATOMIC_READ;
305 else if (act->is_rmw()) {
306 this->type = ATOMIC_RMW;
307 this->value = act->value;
312 * @brief Check if this action should be backtracked with another, due to
313 * potential synchronization
315 * The is_synchronizing method should only explore interleavings if:
316 * -# the operations are seq_cst and don't commute or
317 * -# the reordering may establish or break a synchronization relation.
319 * Other memory operations will be dealt with by using the reads_from relation.
321 * @param act The action to consider exploring a reordering
322 * @return True, if we have to explore a reordering; otherwise false
324 bool ModelAction::could_synchronize_with(const ModelAction *act) const
326 // Same thread can't be reordered
327 if (same_thread(act))
330 // Different locations commute
331 if (!same_var(act) && !is_fence() && !act->is_fence())
334 // Explore interleavings of seqcst writes/fences to guarantee total
335 // order of seq_cst operations that don't commute
336 if ((could_be_write() || act->could_be_write() || is_fence() || act->is_fence()) && is_seqcst() && act->is_seqcst())
339 // Explore synchronizing read/write pairs
340 if (is_acquire() && act->is_release() && is_read() && act->could_be_write())
343 // lock just released...we can grab lock
344 if ((is_lock() || is_trylock()) && (act->is_unlock() || act->is_wait()))
347 // lock just acquired...we can fail to grab lock
348 if (is_trylock() && act->is_success_lock())
351 // other thread stalling on lock...we can release lock
352 if (is_unlock() && (act->is_trylock() || act->is_lock()))
355 if (is_trylock() && (act->is_unlock() || act->is_wait()))
358 if (is_notify() && act->is_wait())
361 if (is_wait() && act->is_notify())
364 // Otherwise handle by reads_from relation
368 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
370 // Must be different threads to reorder
371 if (same_thread(act))
374 // Try to reorder a lock past a successful lock
375 if (act->is_success_lock())
378 // Try to push a successful trylock past an unlock
379 if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
382 // Try to push a successful trylock past a wait
383 if (act->is_wait() && is_trylock() && value == VALUE_TRYSUCCESS)
390 * Create a new clock vector for this action. Note that this function allows a
391 * user to clobber (and leak) a ModelAction's existing clock vector. A user
392 * should ensure that the vector has already either been rolled back
393 * (effectively "freed") or freed.
395 * @param parent A ModelAction from which to inherit a ClockVector
397 void ModelAction::create_cv(const ModelAction *parent)
400 cv = new ClockVector(parent->cv, this);
402 cv = new ClockVector(NULL, this);
405 void ModelAction::set_try_lock(bool obtainedlock)
407 value = obtainedlock ? VALUE_TRYSUCCESS : VALUE_TRYFAILED;
411 * @brief Get the value read by this load
413 * We differentiate this function from ModelAction::get_write_value and
414 * ModelAction::get_value for the purpose of RMW's, which may have both a
415 * 'read' and a 'write' value.
417 * Note: 'this' must be a load.
419 * @return The value read by this load
421 uint64_t ModelAction::get_reads_from_value() const
425 return reads_from->get_write_value();
426 return VALUE_NONE; /* Only for new actions with no reads-from */
430 * @brief Get the value written by this store
432 * We differentiate this function from ModelAction::get_reads_from_value and
433 * ModelAction::get_value for the purpose of RMW's, which may have both a
434 * 'read' and a 'write' value.
436 * Note: 'this' must be a store.
438 * @return The value written by this store
440 uint64_t ModelAction::get_write_value() const
447 * @brief Get the value returned by this action
449 * For atomic reads (including RMW), an operation returns the value it read.
450 * For atomic writes, an operation returns the value it wrote. For other
451 * operations, the return value varies (sometimes is a "don't care"), but the
452 * value is simply stored in the "value" field.
454 * @return This action's return value
456 uint64_t ModelAction::get_return_value() const
459 return get_reads_from_value();
461 return get_write_value();
466 /** @return The Node associated with this ModelAction */
467 Node * ModelAction::get_node() const
469 /* UNINIT actions do not have a Node */
470 ASSERT(!is_uninitialized());
475 * Update the model action's read_from action
476 * @param act The action to read from; should be a write
478 void ModelAction::set_read_from(const ModelAction *act)
484 if (act->is_uninitialized()) { // WL
485 uint64_t val = *((uint64_t *) location);
486 ModelAction * act_initialized = (ModelAction *)act;
487 act_initialized->set_value(val);
488 reads_from = (const ModelAction *)act_initialized;
490 // disabled by WL, because LLVM IR is unable to detect atomic init
491 /* model->assert_bug("May read from uninitialized atomic:\n"
492 " action %d, thread %d, location %p (%s, %s)",
493 seq_number, id_to_int(tid), location,
494 get_type_str(), get_mo_str());
500 * Synchronize the current thread with the thread corresponding to the
501 * ModelAction parameter.
502 * @param act The ModelAction to synchronize with
503 * @return True if this is a valid synchronization; false otherwise
505 bool ModelAction::synchronize_with(const ModelAction *act)
513 bool ModelAction::has_synchronized_with(const ModelAction *act) const
515 return cv->synchronized_since(act);
519 * Check whether 'this' happens before act, according to the memory-model's
520 * happens before relation. This is checked via the ClockVector constructs.
521 * @return true if this action's thread has synchronized with act's thread
522 * since the execution of act, false otherwise.
524 bool ModelAction::happens_before(const ModelAction *act) const
526 return act->cv->synchronized_since(this);
529 const char * ModelAction::get_type_str() const
531 switch (this->type) {
532 case THREAD_CREATE: return "thread create";
533 case THREAD_START: return "thread start";
534 case THREAD_YIELD: return "thread yield";
535 case THREAD_JOIN: return "thread join";
536 case THREAD_FINISH: return "thread finish";
538 case PTHREAD_CREATE: return "pthread create";
539 case PTHREAD_JOIN: return "pthread join";
541 case ATOMIC_UNINIT: return "uninitialized";
542 case ATOMIC_READ: return "atomic read";
543 case ATOMIC_WRITE: return "atomic write";
544 case ATOMIC_RMW: return "atomic rmw";
545 case ATOMIC_FENCE: return "fence";
546 case ATOMIC_RMWR: return "atomic rmwr";
547 case ATOMIC_RMWC: return "atomic rmwc";
548 case ATOMIC_INIT: return "init atomic";
549 case ATOMIC_LOCK: return "lock";
550 case ATOMIC_UNLOCK: return "unlock";
551 case ATOMIC_TRYLOCK: return "trylock";
552 case ATOMIC_WAIT: return "wait";
553 case ATOMIC_NOTIFY_ONE: return "notify one";
554 case ATOMIC_NOTIFY_ALL: return "notify all";
555 case ATOMIC_ANNOTATION: return "annotation";
556 default: return "unknown type";
560 const char * ModelAction::get_mo_str() const
562 switch (this->order) {
563 case std::memory_order_relaxed: return "relaxed";
564 case std::memory_order_acquire: return "acquire";
565 case std::memory_order_release: return "release";
566 case std::memory_order_acq_rel: return "acq_rel";
567 case std::memory_order_seq_cst: return "seq_cst";
568 default: return "unknown";
572 /** @brief Print nicely-formatted info about this ModelAction */
573 void ModelAction::print() const
575 const char *type_str = get_type_str(), *mo_str = get_mo_str();
577 model_print("%-4d %-2d %-14s %7s %14p %-#18" PRIx64,
578 seq_number, id_to_int(tid), type_str, mo_str, location, get_return_value());
581 model_print(" %-3d", reads_from->get_seq_number());
595 /** @brief Get a (likely) unique hash for this ModelAction */
596 unsigned int ModelAction::hash() const
598 unsigned int hash = (unsigned int)this->type;
599 hash ^= ((unsigned int)this->order) << 3;
600 hash ^= seq_number << 5;
601 hash ^= id_to_int(tid) << 6;
605 hash ^= reads_from->get_seq_number();
606 hash ^= get_reads_from_value();
612 * Only valid for LOCK, TRY_LOCK, UNLOCK, and WAIT operations.
613 * @return The mutex operated on by this action, if any; otherwise NULL
615 cdsc::mutex * ModelAction::get_mutex() const
617 if (is_trylock() || is_lock() || is_unlock())
618 return (cdsc::mutex *)get_location();
620 return (cdsc::mutex *)get_value();