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) :
35 seq_number(ACTION_INITIAL_CLOCK),
39 /* References to NULL atomic variables can end up here */
40 ASSERT(loc || type == MODEL_FIXUP_RELSEQ);
42 Thread *t = thread ? thread : thread_current();
43 this->tid = t->get_id();
46 /** @brief ModelAction destructor */
47 ModelAction::~ModelAction()
50 * We can't free the clock vector:
51 * Clock vectors are snapshotting state. When we delete model actions,
52 * they are at the end of the node list and have invalid old clock
53 * vectors which have already been rolled back to an unallocated state.
61 void ModelAction::copy_from_new(ModelAction *newaction)
63 seq_number = newaction->seq_number;
66 void ModelAction::set_seq_number(modelclock_t num)
68 ASSERT(seq_number == ACTION_INITIAL_CLOCK);
72 bool ModelAction::is_relseq_fixup() const
74 return type == MODEL_FIXUP_RELSEQ;
77 bool ModelAction::is_mutex_op() const
79 return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK;
82 bool ModelAction::is_lock() const
84 return type == ATOMIC_LOCK;
87 bool ModelAction::is_unlock() const
89 return type == ATOMIC_UNLOCK;
92 bool ModelAction::is_trylock() const
94 return type == ATOMIC_TRYLOCK;
97 bool ModelAction::is_success_lock() const
99 return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
102 bool ModelAction::is_failed_trylock() const
104 return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
107 bool ModelAction::is_read() const
109 return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMW;
112 bool ModelAction::is_write() const
114 return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT;
117 bool ModelAction::could_be_write() const
119 return is_write() || is_rmwr();
122 bool ModelAction::is_rmwr() const
124 return type == ATOMIC_RMWR;
127 bool ModelAction::is_rmw() const
129 return type == ATOMIC_RMW;
132 bool ModelAction::is_rmwc() const
134 return type == ATOMIC_RMWC;
137 bool ModelAction::is_fence() const
139 return type == ATOMIC_FENCE;
142 bool ModelAction::is_initialization() const
144 return type == ATOMIC_INIT;
147 bool ModelAction::is_acquire() const
150 case std::memory_order_acquire:
151 case std::memory_order_acq_rel:
152 case std::memory_order_seq_cst:
159 bool ModelAction::is_release() const
162 case std::memory_order_release:
163 case std::memory_order_acq_rel:
164 case std::memory_order_seq_cst:
171 bool ModelAction::is_seqcst() const
173 return order==std::memory_order_seq_cst;
176 bool ModelAction::same_var(const ModelAction *act) const
178 return location == act->location;
181 bool ModelAction::same_thread(const ModelAction *act) const
183 return tid == act->tid;
186 void ModelAction::copy_typeandorder(ModelAction * act) {
187 this->type = act->type;
188 this->order = act->order;
191 /** This method changes an existing read part of an RMW action into either:
192 * (1) a full RMW action in case of the completed write or
193 * (2) a READ action in case a failed action.
194 * @todo If the memory_order changes, we may potentially need to update our
197 void ModelAction::process_rmw(ModelAction * act) {
198 this->order=act->order;
200 this->type=ATOMIC_READ;
201 else if (act->is_rmw()) {
202 this->type=ATOMIC_RMW;
203 this->value=act->value;
207 /** The is_synchronizing method should only explore interleavings if:
208 * (1) the operations are seq_cst and don't commute or
209 * (2) the reordering may establish or break a synchronization relation.
210 * Other memory operations will be dealt with by using the reads_from
213 * @param act is the action to consider exploring a reordering.
214 * @return tells whether we have to explore a reordering.
216 bool ModelAction::could_synchronize_with(const ModelAction *act) const
218 //Same thread can't be reordered
219 if (same_thread(act))
222 // Different locations commute
226 // Explore interleavings of seqcst writes to guarantee total order
227 // of seq_cst operations that don't commute
228 if ((could_be_write() || act->could_be_write()) && is_seqcst() && act->is_seqcst())
231 // Explore synchronizing read/write pairs
232 if (is_read() && is_acquire() && act->could_be_write() && act->is_release())
235 // Otherwise handle by reads_from relation
239 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
241 //Must be different threads to reorder
242 if (same_thread(act))
245 //Try to reorder a lock past a successful lock
246 if (act->is_success_lock())
249 //Try to push a successful trylock past an unlock
250 if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
257 * Create a new clock vector for this action. Note that this function allows a
258 * user to clobber (and leak) a ModelAction's existing clock vector. A user
259 * should ensure that the vector has already either been rolled back
260 * (effectively "freed") or freed.
262 * @param parent A ModelAction from which to inherit a ClockVector
264 void ModelAction::create_cv(const ModelAction *parent)
267 cv = new ClockVector(parent->cv, this);
269 cv = new ClockVector(NULL, this);
272 void ModelAction::set_try_lock(bool obtainedlock) {
274 value=VALUE_TRYSUCCESS;
276 value=VALUE_TRYFAILED;
280 * Update the model action's read_from action
281 * @param act The action to read from; should be a write
282 * @return True if this read established synchronization
284 bool ModelAction::read_from(const ModelAction *act)
288 if (act != NULL && this->is_acquire()) {
289 rel_heads_list_t release_heads;
290 model->get_release_seq_heads(this, &release_heads);
291 int num_heads = release_heads.size();
292 for (unsigned int i = 0; i < release_heads.size(); i++)
293 if (!synchronize_with(release_heads[i])) {
294 model->set_bad_synchronization();
297 return num_heads > 0;
303 * Synchronize the current thread with the thread corresponding to the
304 * ModelAction parameter.
305 * @param act The ModelAction to synchronize with
306 * @return True if this is a valid synchronization; false otherwise
308 bool ModelAction::synchronize_with(const ModelAction *act) {
309 if (*this < *act && type != THREAD_JOIN && type != ATOMIC_LOCK)
311 model->check_promises(act->get_tid(), cv, act->cv);
316 bool ModelAction::has_synchronized_with(const ModelAction *act) const
318 return cv->has_synchronized_with(act->cv);
322 * Check whether 'this' happens before act, according to the memory-model's
323 * happens before relation. This is checked via the ClockVector constructs.
324 * @return true if this action's thread has synchronized with act's thread
325 * since the execution of act, false otherwise.
327 bool ModelAction::happens_before(const ModelAction *act) const
329 return act->cv->synchronized_since(this);
332 /** @brief Print nicely-formatted info about this ModelAction */
333 void ModelAction::print() const
335 const char *type_str, *mo_str;
336 switch (this->type) {
337 case MODEL_FIXUP_RELSEQ:
338 type_str = "relseq fixup";
341 type_str = "thread create";
344 type_str = "thread start";
347 type_str = "thread yield";
350 type_str = "thread join";
353 type_str = "thread finish";
356 type_str = "atomic read";
359 type_str = "atomic write";
362 type_str = "atomic rmw";
368 type_str = "atomic rmwr";
371 type_str = "atomic rmwc";
374 type_str = "init atomic";
383 type_str = "trylock";
386 type_str = "unknown type";
389 uint64_t valuetoprint=type==ATOMIC_READ?(reads_from!=NULL?reads_from->value:VALUE_NONE):value;
391 switch (this->order) {
392 case std::memory_order_relaxed:
395 case std::memory_order_acquire:
398 case std::memory_order_release:
401 case std::memory_order_acq_rel:
404 case std::memory_order_seq_cst:
412 printf("(%4d) Thread: %-2d Action: %-13s MO: %7s Loc: %14p Value: %-#18" PRIx64,
413 seq_number, id_to_int(tid), type_str, mo_str, location, valuetoprint);
416 printf(" Rf: %-3d", reads_from->get_seq_number());
430 /** @brief Print nicely-formatted info about this ModelAction */
431 unsigned int ModelAction::hash() const
433 unsigned int hash=(unsigned int) this->type;
434 hash^=((unsigned int)this->order)<<3;
440 hash^=reads_from->get_seq_number();