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 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_relseq_fixup() const
82 return type == MODEL_FIXUP_RELSEQ;
85 bool ModelAction::is_mutex_op() const
87 return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK || type == ATOMIC_WAIT || type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
90 bool ModelAction::is_lock() const
92 return type == ATOMIC_LOCK;
95 bool ModelAction::is_wait() const {
96 return type == ATOMIC_WAIT;
99 bool ModelAction::is_notify() const {
100 return type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
103 bool ModelAction::is_notify_one() const {
104 return type == ATOMIC_NOTIFY_ONE;
107 bool ModelAction::is_unlock() const
109 return type == ATOMIC_UNLOCK;
112 bool ModelAction::is_trylock() const
114 return type == ATOMIC_TRYLOCK;
117 bool ModelAction::is_success_lock() const
119 return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
122 bool ModelAction::is_failed_trylock() const
124 return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
127 /** @return True if this operation is performed on a C/C++ atomic variable */
128 bool ModelAction::is_atomic_var() const
130 return is_read() || could_be_write();
133 bool ModelAction::is_uninitialized() const
135 return type == ATOMIC_UNINIT;
138 bool ModelAction::is_read() const
140 return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMW;
143 bool ModelAction::is_write() const
145 return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT || type == ATOMIC_UNINIT;
148 bool ModelAction::could_be_write() const
150 return is_write() || is_rmwr();
153 bool ModelAction::is_rmwr() const
155 return type == ATOMIC_RMWR;
158 bool ModelAction::is_rmw() const
160 return type == ATOMIC_RMW;
163 bool ModelAction::is_rmwc() const
165 return type == ATOMIC_RMWC;
168 bool ModelAction::is_fence() const
170 return type == ATOMIC_FENCE;
173 bool ModelAction::is_initialization() const
175 return type == ATOMIC_INIT;
178 bool ModelAction::is_relaxed() const
180 return order == std::memory_order_relaxed;
183 bool ModelAction::is_acquire() const
186 case std::memory_order_acquire:
187 case std::memory_order_acq_rel:
188 case std::memory_order_seq_cst:
195 bool ModelAction::is_release() const
198 case std::memory_order_release:
199 case std::memory_order_acq_rel:
200 case std::memory_order_seq_cst:
207 bool ModelAction::is_seqcst() const
209 return order == std::memory_order_seq_cst;
212 bool ModelAction::same_var(const ModelAction *act) const
214 if (act->is_wait() || is_wait()) {
215 if (act->is_wait() && is_wait()) {
216 if (((void *)value) == ((void *)act->value))
218 } else if (is_wait()) {
219 if (((void *)value) == act->location)
221 } else if (act->is_wait()) {
222 if (location == ((void *)act->value))
227 return location == act->location;
230 bool ModelAction::same_thread(const ModelAction *act) const
232 return tid == act->tid;
235 void ModelAction::copy_typeandorder(ModelAction * act)
237 this->type = act->type;
238 this->order = act->order;
242 * Get the Thread which is the operand of this action. This is only valid for
243 * THREAD_* operations (currently only for THREAD_CREATE and THREAD_JOIN). Note
244 * that this provides a central place for determining the conventions of Thread
245 * storage in ModelAction, where we generally aren't very type-safe (e.g., we
246 * store object references in a (void *) address.
248 * For THREAD_CREATE, this yields the Thread which is created.
249 * For THREAD_JOIN, this yields the Thread we are joining with.
251 * @return The Thread which this action acts on, if exists; otherwise NULL
253 Thread * ModelAction::get_thread_operand() const
255 if (type == THREAD_CREATE)
256 /* THREAD_CREATE uses (Thread *) for location */
257 return (Thread *)get_location();
258 else if (type == THREAD_JOIN)
259 /* THREAD_JOIN uses (Thread *) for location */
260 return (Thread *)get_location();
265 /** This method changes an existing read part of an RMW action into either:
266 * (1) a full RMW action in case of the completed write or
267 * (2) a READ action in case a failed action.
268 * @todo If the memory_order changes, we may potentially need to update our
271 void ModelAction::process_rmw(ModelAction *act)
273 this->order = act->order;
275 this->type = ATOMIC_READ;
276 else if (act->is_rmw()) {
277 this->type = ATOMIC_RMW;
278 this->value = act->value;
282 /** The is_synchronizing method should only explore interleavings if:
283 * (1) the operations are seq_cst and don't commute or
284 * (2) the reordering may establish or break a synchronization relation.
285 * Other memory operations will be dealt with by using the reads_from
288 * @param act is the action to consider exploring a reordering.
289 * @return tells whether we have to explore a reordering.
291 bool ModelAction::could_synchronize_with(const ModelAction *act) const
293 // Same thread can't be reordered
294 if (same_thread(act))
297 // Different locations commute
301 // Explore interleavings of seqcst writes/fences to guarantee total
302 // order of seq_cst operations that don't commute
303 if ((could_be_write() || act->could_be_write() || is_fence() || act->is_fence())
304 && is_seqcst() && act->is_seqcst())
307 // Explore synchronizing read/write/fence pairs
308 if (is_acquire() && act->is_release() && (is_read() || is_fence()) &&
309 (act->could_be_write() || act->is_fence()))
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)
396 reads_from_promise = NULL;
397 if (act && act->is_uninitialized())
398 model->assert_bug("May read from uninitialized atomic\n");
402 * Set this action's read-from promise
403 * @param promise The promise to read from
405 void ModelAction::set_read_from_promise(const Promise *promise)
408 reads_from_promise = promise;
413 * Synchronize the current thread with the thread corresponding to the
414 * ModelAction parameter.
415 * @param act The ModelAction to synchronize with
416 * @return True if this is a valid synchronization; false otherwise
418 bool ModelAction::synchronize_with(const ModelAction *act)
420 if (*this < *act && type != THREAD_JOIN && type != ATOMIC_LOCK)
422 model->check_promises(act->get_tid(), cv, act->cv);
427 bool ModelAction::has_synchronized_with(const ModelAction *act) const
429 return cv->synchronized_since(act);
433 * Check whether 'this' happens before act, according to the memory-model's
434 * happens before relation. This is checked via the ClockVector constructs.
435 * @return true if this action's thread has synchronized with act's thread
436 * since the execution of act, false otherwise.
438 bool ModelAction::happens_before(const ModelAction *act) const
440 return act->cv->synchronized_since(this);
443 /** @brief Print nicely-formatted info about this ModelAction */
444 void ModelAction::print() const
446 const char *type_str, *mo_str;
447 switch (this->type) {
448 case MODEL_FIXUP_RELSEQ:
449 type_str = "relseq fixup";
452 type_str = "thread create";
455 type_str = "thread start";
458 type_str = "thread yield";
461 type_str = "thread join";
464 type_str = "thread finish";
467 type_str = "uninitialized";
470 type_str = "atomic read";
473 type_str = "atomic write";
476 type_str = "atomic rmw";
482 type_str = "atomic rmwr";
485 type_str = "atomic rmwc";
488 type_str = "init atomic";
497 type_str = "trylock";
502 case ATOMIC_NOTIFY_ONE:
503 type_str = "notify one";
505 case ATOMIC_NOTIFY_ALL:
506 type_str = "notify all";
509 type_str = "unknown type";
512 uint64_t valuetoprint;
513 if (is_read() && reads_from)
514 valuetoprint = reads_from->value;
515 else if (is_read() && reads_from_promise)
516 valuetoprint = reads_from_promise->get_value();
518 valuetoprint = value;
520 switch (this->order) {
521 case std::memory_order_relaxed:
524 case std::memory_order_acquire:
527 case std::memory_order_release:
530 case std::memory_order_acq_rel:
533 case std::memory_order_seq_cst:
541 model_print("(%4d) Thread: %-2d Action: %-13s MO: %7s Loc: %14p Value: %-#18" PRIx64,
542 seq_number, id_to_int(tid), type_str, mo_str, location, valuetoprint);
545 model_print(" Rf: %-3d", reads_from->get_seq_number());
547 model_print(" Rf: ? ");
559 /** @brief Get a (likely) unique hash for this ModelAction */
560 unsigned int ModelAction::hash() const
562 unsigned int hash = (unsigned int)this->type;
563 hash ^= ((unsigned int)this->order) << 3;
564 hash ^= seq_number << 5;
565 hash ^= id_to_int(tid) << 6;
567 if (is_read() && reads_from)
568 hash ^= reads_from->get_seq_number();