2 #define __STDC_FORMAT_MACROS
8 #include "clockvector.h"
10 #include "threads-model.h"
11 #include "nodestack.h"
13 #define ACTION_INITIAL_CLOCK 0
16 * @brief Construct a new ModelAction
18 * @param type The type of action
19 * @param order The memory order of this action. A "don't care" for non-ATOMIC
20 * actions (e.g., THREAD_* or MODEL_* actions).
21 * @param loc The location that this action acts upon
22 * @param value (optional) A value associated with the action (e.g., the value
23 * read or written). Defaults to a given macro constant, for debugging purposes.
24 * @param thread (optional) The Thread in which this action occurred. If NULL
25 * (default), then a Thread is assigned according to the scheduler.
27 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc,
28 uint64_t value, Thread *thread) :
34 reads_from_promise(NULL),
35 last_fence_release(NULL),
37 seq_number(ACTION_INITIAL_CLOCK),
41 /* References to NULL atomic variables can end up here */
42 ASSERT(loc || type == ATOMIC_FENCE || type == MODEL_FIXUP_RELSEQ);
44 Thread *t = thread ? thread : thread_current();
45 this->tid = t->get_id();
48 /** @brief ModelAction destructor */
49 ModelAction::~ModelAction()
52 * We can't free the clock vector:
53 * Clock vectors are snapshotting state. When we delete model actions,
54 * they are at the end of the node list and have invalid old clock
55 * vectors which have already been rolled back to an unallocated state.
63 void ModelAction::copy_from_new(ModelAction *newaction)
65 seq_number = newaction->seq_number;
68 void ModelAction::set_seq_number(modelclock_t num)
70 /* ATOMIC_UNINIT actions should never have non-zero clock */
71 ASSERT(!is_uninitialized());
72 ASSERT(seq_number == ACTION_INITIAL_CLOCK);
76 bool ModelAction::is_thread_start() const
78 return type == THREAD_START;
81 bool ModelAction::is_relseq_fixup() const
83 return type == MODEL_FIXUP_RELSEQ;
86 bool ModelAction::is_mutex_op() const
88 return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK || type == ATOMIC_WAIT || type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
91 bool ModelAction::is_lock() const
93 return type == ATOMIC_LOCK;
96 bool ModelAction::is_wait() const {
97 return type == ATOMIC_WAIT;
100 bool ModelAction::is_notify() const {
101 return type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
104 bool ModelAction::is_notify_one() const {
105 return type == ATOMIC_NOTIFY_ONE;
108 bool ModelAction::is_unlock() const
110 return type == ATOMIC_UNLOCK;
113 bool ModelAction::is_trylock() const
115 return type == ATOMIC_TRYLOCK;
118 bool ModelAction::is_success_lock() const
120 return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
123 bool ModelAction::is_failed_trylock() const
125 return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
128 /** @return True if this operation is performed on a C/C++ atomic variable */
129 bool ModelAction::is_atomic_var() const
131 return is_read() || could_be_write();
134 bool ModelAction::is_uninitialized() const
136 return type == ATOMIC_UNINIT;
139 bool ModelAction::is_read() const
141 return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMW;
144 bool ModelAction::is_write() const
146 return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT || type == ATOMIC_UNINIT;
149 bool ModelAction::could_be_write() const
151 return is_write() || is_rmwr();
154 bool ModelAction::is_rmwr() const
156 return type == ATOMIC_RMWR;
159 bool ModelAction::is_rmw() const
161 return type == ATOMIC_RMW;
164 bool ModelAction::is_rmwc() const
166 return type == ATOMIC_RMWC;
169 bool ModelAction::is_fence() const
171 return type == ATOMIC_FENCE;
174 bool ModelAction::is_initialization() const
176 return type == ATOMIC_INIT;
179 bool ModelAction::is_relaxed() const
181 return order == std::memory_order_relaxed;
184 bool ModelAction::is_acquire() const
187 case std::memory_order_acquire:
188 case std::memory_order_acq_rel:
189 case std::memory_order_seq_cst:
196 bool ModelAction::is_release() const
199 case std::memory_order_release:
200 case std::memory_order_acq_rel:
201 case std::memory_order_seq_cst:
208 bool ModelAction::is_seqcst() const
210 return order == std::memory_order_seq_cst;
213 bool ModelAction::same_var(const ModelAction *act) const
215 if (act->is_wait() || is_wait()) {
216 if (act->is_wait() && is_wait()) {
217 if (((void *)value) == ((void *)act->value))
219 } else if (is_wait()) {
220 if (((void *)value) == act->location)
222 } else if (act->is_wait()) {
223 if (location == ((void *)act->value))
228 return location == act->location;
231 bool ModelAction::same_thread(const ModelAction *act) const
233 return tid == act->tid;
236 void ModelAction::copy_typeandorder(ModelAction * act)
238 this->type = act->type;
239 this->order = act->order;
243 * Get the Thread which is the operand of this action. This is only valid for
244 * THREAD_* operations (currently only for THREAD_CREATE and THREAD_JOIN). Note
245 * that this provides a central place for determining the conventions of Thread
246 * storage in ModelAction, where we generally aren't very type-safe (e.g., we
247 * store object references in a (void *) address.
249 * For THREAD_CREATE, this yields the Thread which is created.
250 * For THREAD_JOIN, this yields the Thread we are joining with.
252 * @return The Thread which this action acts on, if exists; otherwise NULL
254 Thread * ModelAction::get_thread_operand() const
256 if (type == THREAD_CREATE) {
257 /* THREAD_CREATE stores its (Thread *) in a thrd_t::priv */
258 thrd_t *thrd = (thrd_t *)get_location();
260 } else if (type == THREAD_JOIN)
261 /* THREAD_JOIN uses (Thread *) for location */
262 return (Thread *)get_location();
267 /** This method changes an existing read part of an RMW action into either:
268 * (1) a full RMW action in case of the completed write or
269 * (2) a READ action in case a failed action.
270 * @todo If the memory_order changes, we may potentially need to update our
273 void ModelAction::process_rmw(ModelAction *act)
275 this->order = act->order;
277 this->type = ATOMIC_READ;
278 else if (act->is_rmw()) {
279 this->type = ATOMIC_RMW;
280 this->value = act->value;
284 /** The is_synchronizing method should only explore interleavings if:
285 * (1) the operations are seq_cst and don't commute or
286 * (2) the reordering may establish or break a synchronization relation.
287 * Other memory operations will be dealt with by using the reads_from
290 * @param act is the action to consider exploring a reordering.
291 * @return tells whether we have to explore a reordering.
293 bool ModelAction::could_synchronize_with(const ModelAction *act) const
295 // Same thread can't be reordered
296 if (same_thread(act))
299 // Different locations commute
303 // Explore interleavings of seqcst writes/fences to guarantee total
304 // order of seq_cst operations that don't commute
305 if ((could_be_write() || act->could_be_write() || is_fence() || act->is_fence()) && is_seqcst() && act->is_seqcst())
308 // Explore synchronizing read/write pairs
309 if (is_acquire() && act->is_release() && is_read() && act->could_be_write())
312 // lock just released...we can grab lock
313 if ((is_lock() || is_trylock()) && (act->is_unlock() || act->is_wait()))
316 // lock just acquired...we can fail to grab lock
317 if (is_trylock() && act->is_success_lock())
320 // other thread stalling on lock...we can release lock
321 if (is_unlock() && (act->is_trylock() || act->is_lock()))
324 if (is_trylock() && (act->is_unlock() || act->is_wait()))
327 if (is_notify() && act->is_wait())
330 if (is_wait() && act->is_notify())
333 // Otherwise handle by reads_from relation
337 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
339 // Must be different threads to reorder
340 if (same_thread(act))
343 // Try to reorder a lock past a successful lock
344 if (act->is_success_lock())
347 // Try to push a successful trylock past an unlock
348 if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
351 // Try to push a successful trylock past a wait
352 if (act->is_wait() && is_trylock() && value == VALUE_TRYSUCCESS)
359 * Create a new clock vector for this action. Note that this function allows a
360 * user to clobber (and leak) a ModelAction's existing clock vector. A user
361 * should ensure that the vector has already either been rolled back
362 * (effectively "freed") or freed.
364 * @param parent A ModelAction from which to inherit a ClockVector
366 void ModelAction::create_cv(const ModelAction *parent)
369 cv = new ClockVector(parent->cv, this);
371 cv = new ClockVector(NULL, this);
374 void ModelAction::set_try_lock(bool obtainedlock) {
376 value = VALUE_TRYSUCCESS;
378 value = VALUE_TRYFAILED;
381 /** @return The Node associated with this ModelAction */
382 Node * ModelAction::get_node() const
384 /* UNINIT actions do not have a Node */
385 ASSERT(!is_uninitialized());
390 * Update the model action's read_from action
391 * @param act The action to read from; should be a write
393 void ModelAction::set_read_from(const ModelAction *act)
397 reads_from_promise = NULL;
398 if (act->is_uninitialized())
399 model->assert_bug("May read from uninitialized atomic\n");
403 * Set this action's read-from promise
404 * @param promise The promise to read from
406 void ModelAction::set_read_from_promise(Promise *promise)
409 reads_from_promise = promise;
414 * Synchronize the current thread with the thread corresponding to the
415 * ModelAction parameter.
416 * @param act The ModelAction to synchronize with
417 * @return True if this is a valid synchronization; false otherwise
419 bool ModelAction::synchronize_with(const ModelAction *act)
421 if (*this < *act && type != THREAD_JOIN && type != ATOMIC_LOCK)
423 model->check_promises(act->get_tid(), cv, act->cv);
428 bool ModelAction::has_synchronized_with(const ModelAction *act) const
430 return cv->synchronized_since(act);
434 * Check whether 'this' happens before act, according to the memory-model's
435 * happens before relation. This is checked via the ClockVector constructs.
436 * @return true if this action's thread has synchronized with act's thread
437 * since the execution of act, false otherwise.
439 bool ModelAction::happens_before(const ModelAction *act) const
441 return act->cv->synchronized_since(this);
444 /** @brief Print nicely-formatted info about this ModelAction */
445 void ModelAction::print() const
447 const char *type_str, *mo_str;
448 switch (this->type) {
449 case MODEL_FIXUP_RELSEQ:
450 type_str = "relseq fixup";
453 type_str = "thread create";
456 type_str = "thread start";
459 type_str = "thread yield";
462 type_str = "thread join";
465 type_str = "thread finish";
468 type_str = "uninitialized";
471 type_str = "atomic read";
474 type_str = "atomic write";
477 type_str = "atomic rmw";
483 type_str = "atomic rmwr";
486 type_str = "atomic rmwc";
489 type_str = "init atomic";
498 type_str = "trylock";
503 case ATOMIC_NOTIFY_ONE:
504 type_str = "notify one";
506 case ATOMIC_NOTIFY_ALL:
507 type_str = "notify all";
510 type_str = "unknown type";
513 uint64_t valuetoprint;
514 if (is_read() && reads_from)
515 valuetoprint = reads_from->value;
516 else if (is_read() && reads_from_promise)
517 valuetoprint = reads_from_promise->get_value();
519 valuetoprint = value;
521 switch (this->order) {
522 case std::memory_order_relaxed:
525 case std::memory_order_acquire:
528 case std::memory_order_release:
531 case std::memory_order_acq_rel:
534 case std::memory_order_seq_cst:
542 model_print("(%4d) Thread: %-2d Action: %-13s MO: %7s Loc: %14p Value: %-#18" PRIx64,
543 seq_number, id_to_int(tid), type_str, mo_str, location, valuetoprint);
546 model_print(" Rf: %-3d", reads_from->get_seq_number());
547 else if (reads_from_promise) {
548 int idx = model->get_promise_number(reads_from_promise);
550 model_print(" Rf: P%-2d", idx);
552 model_print(" RF: P? ");
554 model_print(" Rf: ? ");
566 /** @brief Get a (likely) unique hash for this ModelAction */
567 unsigned int ModelAction::hash() const
569 unsigned int hash = (unsigned int)this->type;
570 hash ^= ((unsigned int)this->order) << 3;
571 hash ^= seq_number << 5;
572 hash ^= id_to_int(tid) << 6;
574 if (is_read() && reads_from)
575 hash ^= reads_from->get_seq_number();