2 #define __STDC_FORMAT_MACROS
7 #include "clockvector.h"
9 #include "threads-model.h"
10 #include "nodestack.h"
12 #define ACTION_INITIAL_CLOCK 0
15 * @brief Construct a new ModelAction
17 * @param type The type of action
18 * @param order The memory order of this action. A "don't care" for non-ATOMIC
19 * actions (e.g., THREAD_* or MODEL_* actions).
20 * @param loc The location that this action acts upon
21 * @param value (optional) A value associated with the action (e.g., the value
22 * read or written). Defaults to a given macro constant, for debugging purposes.
23 * @param thread (optional) The Thread in which this action occurred. If NULL
24 * (default), then a Thread is assigned according to the scheduler.
26 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc,
27 uint64_t value, Thread *thread) :
33 reads_from_promise(NULL),
34 last_fence_release(NULL),
36 seq_number(ACTION_INITIAL_CLOCK),
40 /* References to NULL atomic variables can end up here */
41 ASSERT(loc || type == ATOMIC_FENCE || type == MODEL_FIXUP_RELSEQ);
43 Thread *t = thread ? thread : thread_current();
44 this->tid = t->get_id();
47 /** @brief ModelAction destructor */
48 ModelAction::~ModelAction()
51 * We can't free the clock vector:
52 * Clock vectors are snapshotting state. When we delete model actions,
53 * they are at the end of the node list and have invalid old clock
54 * vectors which have already been rolled back to an unallocated state.
62 void ModelAction::copy_from_new(ModelAction *newaction)
64 seq_number = newaction->seq_number;
67 void ModelAction::set_seq_number(modelclock_t num)
69 /* ATOMIC_UNINIT actions should never have non-zero clock */
70 ASSERT(!is_uninitialized());
71 ASSERT(seq_number == ACTION_INITIAL_CLOCK);
75 bool ModelAction::is_thread_start() const
77 return type == THREAD_START;
80 bool ModelAction::is_thread_join() const
82 return type == THREAD_JOIN;
85 bool ModelAction::is_relseq_fixup() const
87 return type == MODEL_FIXUP_RELSEQ;
90 bool ModelAction::is_mutex_op() const
92 return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK || type == ATOMIC_WAIT || type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
95 bool ModelAction::is_lock() const
97 return type == ATOMIC_LOCK;
100 bool ModelAction::is_wait() const {
101 return type == ATOMIC_WAIT;
104 bool ModelAction::is_notify() const {
105 return type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
108 bool ModelAction::is_notify_one() const {
109 return type == ATOMIC_NOTIFY_ONE;
112 bool ModelAction::is_unlock() const
114 return type == ATOMIC_UNLOCK;
117 bool ModelAction::is_trylock() const
119 return type == ATOMIC_TRYLOCK;
122 bool ModelAction::is_success_lock() const
124 return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
127 bool ModelAction::is_failed_trylock() const
129 return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
132 /** @return True if this operation is performed on a C/C++ atomic variable */
133 bool ModelAction::is_atomic_var() const
135 return is_read() || could_be_write();
138 bool ModelAction::is_uninitialized() const
140 return type == ATOMIC_UNINIT;
143 bool ModelAction::is_read() const
145 return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMW;
148 bool ModelAction::is_write() const
150 return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT || type == ATOMIC_UNINIT;
153 bool ModelAction::could_be_write() const
155 return is_write() || is_rmwr();
158 bool ModelAction::is_yield() const
160 return type == THREAD_YIELD;
163 bool ModelAction::is_rmwr() const
165 return type == ATOMIC_RMWR;
168 bool ModelAction::is_rmw() const
170 return type == ATOMIC_RMW;
173 bool ModelAction::is_rmwc() const
175 return type == ATOMIC_RMWC;
178 bool ModelAction::is_fence() const
180 return type == ATOMIC_FENCE;
183 bool ModelAction::is_initialization() const
185 return type == ATOMIC_INIT;
188 bool ModelAction::is_relaxed() const
190 return order == std::memory_order_relaxed;
193 bool ModelAction::is_acquire() const
196 case std::memory_order_acquire:
197 case std::memory_order_acq_rel:
198 case std::memory_order_seq_cst:
205 bool ModelAction::is_release() const
208 case std::memory_order_release:
209 case std::memory_order_acq_rel:
210 case std::memory_order_seq_cst:
217 bool ModelAction::is_seqcst() const
219 return order == std::memory_order_seq_cst;
222 bool ModelAction::same_var(const ModelAction *act) const
224 if (act->is_wait() || is_wait()) {
225 if (act->is_wait() && is_wait()) {
226 if (((void *)value) == ((void *)act->value))
228 } else if (is_wait()) {
229 if (((void *)value) == act->location)
231 } else if (act->is_wait()) {
232 if (location == ((void *)act->value))
237 return location == act->location;
240 bool ModelAction::same_thread(const ModelAction *act) const
242 return tid == act->tid;
245 void ModelAction::copy_typeandorder(ModelAction * act)
247 this->type = act->type;
248 this->order = act->order;
252 * Get the Thread which is the operand of this action. This is only valid for
253 * THREAD_* operations (currently only for THREAD_CREATE and THREAD_JOIN). Note
254 * that this provides a central place for determining the conventions of Thread
255 * storage in ModelAction, where we generally aren't very type-safe (e.g., we
256 * store object references in a (void *) address.
258 * For THREAD_CREATE, this yields the Thread which is created.
259 * For THREAD_JOIN, this yields the Thread we are joining with.
261 * @return The Thread which this action acts on, if exists; otherwise NULL
263 Thread * ModelAction::get_thread_operand() const
265 if (type == THREAD_CREATE) {
266 /* THREAD_CREATE stores its (Thread *) in a thrd_t::priv */
267 thrd_t *thrd = (thrd_t *)get_location();
269 } else if (type == THREAD_JOIN)
270 /* THREAD_JOIN uses (Thread *) for location */
271 return (Thread *)get_location();
276 /** This method changes an existing read part of an RMW action into either:
277 * (1) a full RMW action in case of the completed write or
278 * (2) a READ action in case a failed action.
279 * @todo If the memory_order changes, we may potentially need to update our
282 void ModelAction::process_rmw(ModelAction *act)
284 this->order = act->order;
286 this->type = ATOMIC_READ;
287 else if (act->is_rmw()) {
288 this->type = ATOMIC_RMW;
289 this->value = act->value;
293 /** The is_synchronizing method should only explore interleavings if:
294 * (1) the operations are seq_cst and don't commute or
295 * (2) the reordering may establish or break a synchronization relation.
296 * Other memory operations will be dealt with by using the reads_from
299 * @param act is the action to consider exploring a reordering.
300 * @return tells whether we have to explore a reordering.
302 bool ModelAction::could_synchronize_with(const ModelAction *act) const
304 // Same thread can't be reordered
305 if (same_thread(act))
308 // Different locations commute
312 // Explore interleavings of seqcst writes/fences to guarantee total
313 // order of seq_cst operations that don't commute
314 if ((could_be_write() || act->could_be_write() || is_fence() || act->is_fence()) && is_seqcst() && act->is_seqcst())
317 // Explore synchronizing read/write pairs
318 if (is_acquire() && act->is_release() && is_read() && act->could_be_write())
321 // lock just released...we can grab lock
322 if ((is_lock() || is_trylock()) && (act->is_unlock() || act->is_wait()))
325 // lock just acquired...we can fail to grab lock
326 if (is_trylock() && act->is_success_lock())
329 // other thread stalling on lock...we can release lock
330 if (is_unlock() && (act->is_trylock() || act->is_lock()))
333 if (is_trylock() && (act->is_unlock() || act->is_wait()))
336 if (is_notify() && act->is_wait())
339 if (is_wait() && act->is_notify())
342 // Otherwise handle by reads_from relation
346 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
348 // Must be different threads to reorder
349 if (same_thread(act))
352 // Try to reorder a lock past a successful lock
353 if (act->is_success_lock())
356 // Try to push a successful trylock past an unlock
357 if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
360 // Try to push a successful trylock past a wait
361 if (act->is_wait() && is_trylock() && value == VALUE_TRYSUCCESS)
368 * Create a new clock vector for this action. Note that this function allows a
369 * user to clobber (and leak) a ModelAction's existing clock vector. A user
370 * should ensure that the vector has already either been rolled back
371 * (effectively "freed") or freed.
373 * @param parent A ModelAction from which to inherit a ClockVector
375 void ModelAction::create_cv(const ModelAction *parent)
378 cv = new ClockVector(parent->cv, this);
380 cv = new ClockVector(NULL, this);
383 void ModelAction::set_try_lock(bool obtainedlock) {
385 value = VALUE_TRYSUCCESS;
387 value = VALUE_TRYFAILED;
391 * @brief Get the value read by this load
393 * We differentiate this function from ModelAction::get_write_value and
394 * ModelAction::get_value for the purpose of RMW's, which may have both a
395 * 'read' and a 'write' value.
397 * Note: 'this' must be a load.
399 * @return The value read by this load
401 uint64_t ModelAction::get_reads_from_value() const
405 return reads_from->get_write_value();
406 else if (reads_from_promise)
407 return reads_from_promise->get_value();
408 return VALUE_NONE; /* Only for new actions with no reads-from */
412 * @brief Get the value written by this store
414 * We differentiate this function from ModelAction::get_reads_from_value and
415 * ModelAction::get_value for the purpose of RMW's, which may have both a
416 * 'read' and a 'write' value.
418 * Note: 'this' must be a store.
420 * @return The value written by this store
422 uint64_t ModelAction::get_write_value() const
429 * @brief Get the value returned by this action
431 * For atomic reads (including RMW), an operation returns the value it read.
432 * For atomic writes, an operation returns the value it wrote. For other
433 * operations, the return value varies (sometimes is a "don't care"), but the
434 * value is simply stored in the "value" field.
436 * @return This action's return value
438 uint64_t ModelAction::get_return_value() const
441 return get_reads_from_value();
443 return get_write_value();
448 /** @return The Node associated with this ModelAction */
449 Node * ModelAction::get_node() const
451 /* UNINIT actions do not have a Node */
452 ASSERT(!is_uninitialized());
457 * Update the model action's read_from action
458 * @param act The action to read from; should be a write
460 void ModelAction::set_read_from(const ModelAction *act)
464 reads_from_promise = NULL;
465 if (act->is_uninitialized())
466 model->assert_bug("May read from uninitialized atomic\n");
470 * Set this action's read-from promise
471 * @param promise The promise to read from
473 void ModelAction::set_read_from_promise(Promise *promise)
476 reads_from_promise = promise;
481 * Synchronize the current thread with the thread corresponding to the
482 * ModelAction parameter.
483 * @param act The ModelAction to synchronize with
484 * @return True if this is a valid synchronization; false otherwise
486 bool ModelAction::synchronize_with(const ModelAction *act)
488 if (*this < *act && type != THREAD_JOIN && type != ATOMIC_LOCK)
490 model->check_promises(act->get_tid(), cv, act->cv);
495 bool ModelAction::has_synchronized_with(const ModelAction *act) const
497 return cv->synchronized_since(act);
501 * Check whether 'this' happens before act, according to the memory-model's
502 * happens before relation. This is checked via the ClockVector constructs.
503 * @return true if this action's thread has synchronized with act's thread
504 * since the execution of act, false otherwise.
506 bool ModelAction::happens_before(const ModelAction *act) const
508 return act->cv->synchronized_since(this);
511 /** @brief Print nicely-formatted info about this ModelAction */
512 void ModelAction::print() const
514 const char *type_str, *mo_str;
515 switch (this->type) {
516 case MODEL_FIXUP_RELSEQ:
517 type_str = "relseq fixup";
520 type_str = "thread create";
523 type_str = "thread start";
526 type_str = "thread yield";
529 type_str = "thread join";
532 type_str = "thread finish";
535 type_str = "uninitialized";
538 type_str = "atomic read";
541 type_str = "atomic write";
544 type_str = "atomic rmw";
550 type_str = "atomic rmwr";
553 type_str = "atomic rmwc";
556 type_str = "init atomic";
565 type_str = "trylock";
570 case ATOMIC_NOTIFY_ONE:
571 type_str = "notify one";
573 case ATOMIC_NOTIFY_ALL:
574 type_str = "notify all";
577 type_str = "unknown type";
580 switch (this->order) {
581 case std::memory_order_relaxed:
584 case std::memory_order_acquire:
587 case std::memory_order_release:
590 case std::memory_order_acq_rel:
593 case std::memory_order_seq_cst:
601 model_print("(%4d) Thread: %-2d Action: %-13s MO: %7s Loc: %14p Value: %-#18" PRIx64,
602 seq_number, id_to_int(tid), type_str, mo_str, location, get_return_value());
605 model_print(" Rf: %-3d", reads_from->get_seq_number());
606 else if (reads_from_promise) {
607 int idx = model->get_promise_number(reads_from_promise);
609 model_print(" Rf: P%-2d", idx);
611 model_print(" RF: P? ");
613 model_print(" Rf: ? ");
625 /** @brief Get a (likely) unique hash for this ModelAction */
626 unsigned int ModelAction::hash() const
628 unsigned int hash = (unsigned int)this->type;
629 hash ^= ((unsigned int)this->order) << 3;
630 hash ^= seq_number << 5;
631 hash ^= id_to_int(tid) << 6;
635 hash ^= reads_from->get_seq_number();
636 else if (reads_from_promise)
637 hash ^= model->get_promise_number(reads_from_promise);
638 hash ^= get_reads_from_value();
644 * @brief Checks the NodeStack to see if a ModelAction is in our may-read-from set
645 * @param write The ModelAction to check for
646 * @return True if the ModelAction is found; false otherwise
648 bool ModelAction::may_read_from(const ModelAction *write) const
650 for (int i = 0; i < node->get_read_from_past_size(); i++)
651 if (node->get_read_from_past(i) == write)
657 * @brief Checks the NodeStack to see if a Promise is in our may-read-from set
658 * @param promise The Promise to check for
659 * @return True if the Promise is found; false otherwise
661 bool ModelAction::may_read_from(const Promise *promise) const
663 for (int i = 0; i < node->get_read_from_promise_size(); i++)
664 if (node->get_read_from_promise(i) == promise)
670 * Only valid for LOCK, TRY_LOCK, UNLOCK, and WAIT operations.
671 * @return The mutex operated on by this action, if any; otherwise NULL
673 std::mutex * ModelAction::get_mutex() const
675 if (is_trylock() || is_lock() || is_unlock())
676 return (std::mutex *)get_location();
678 return (std::mutex *)get_value();