2 #define __STDC_FORMAT_MACROS
8 #include "clockvector.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 Thread *t = thread ? thread : thread_current();
40 this->tid = t->get_id();
43 /** @brief ModelAction destructor */
44 ModelAction::~ModelAction()
47 * We can't free the clock vector:
48 * Clock vectors are snapshotting state. When we delete model actions,
49 * they are at the end of the node list and have invalid old clock
50 * vectors which have already been rolled back to an unallocated state.
58 void ModelAction::copy_from_new(ModelAction *newaction)
60 seq_number = newaction->seq_number;
63 void ModelAction::set_seq_number(modelclock_t num)
65 ASSERT(seq_number == ACTION_INITIAL_CLOCK);
69 bool ModelAction::is_relseq_fixup() const
71 return type == MODEL_FIXUP_RELSEQ;
74 bool ModelAction::is_mutex_op() const
76 return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK;
79 bool ModelAction::is_lock() const
81 return type == ATOMIC_LOCK;
84 bool ModelAction::is_unlock() const
86 return type == ATOMIC_UNLOCK;
89 bool ModelAction::is_trylock() const
91 return type == ATOMIC_TRYLOCK;
94 bool ModelAction::is_success_lock() const
96 return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
99 bool ModelAction::is_failed_trylock() const
101 return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
104 bool ModelAction::is_read() const
106 return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMW;
109 bool ModelAction::is_write() const
111 return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT;
114 bool ModelAction::is_rmwr() const
116 return type == ATOMIC_RMWR;
119 bool ModelAction::is_rmw() const
121 return type == ATOMIC_RMW;
124 bool ModelAction::is_rmwc() const
126 return type == ATOMIC_RMWC;
129 bool ModelAction::is_fence() const
131 return type == ATOMIC_FENCE;
134 bool ModelAction::is_initialization() const
136 return type == ATOMIC_INIT;
139 bool ModelAction::is_acquire() const
142 case std::memory_order_acquire:
143 case std::memory_order_acq_rel:
144 case std::memory_order_seq_cst:
151 bool ModelAction::is_release() const
154 case std::memory_order_release:
155 case std::memory_order_acq_rel:
156 case std::memory_order_seq_cst:
163 bool ModelAction::is_seqcst() const
165 return order==std::memory_order_seq_cst;
168 bool ModelAction::same_var(const ModelAction *act) const
170 return location == act->location;
173 bool ModelAction::same_thread(const ModelAction *act) const
175 return tid == act->tid;
178 void ModelAction::copy_typeandorder(ModelAction * act) {
179 this->type = act->type;
180 this->order = act->order;
183 /** This method changes an existing read part of an RMW action into either:
184 * (1) a full RMW action in case of the completed write or
185 * (2) a READ action in case a failed action.
186 * @todo If the memory_order changes, we may potentially need to update our
189 void ModelAction::process_rmw(ModelAction * act) {
190 this->order=act->order;
192 this->type=ATOMIC_READ;
193 else if (act->is_rmw()) {
194 this->type=ATOMIC_RMW;
195 this->value=act->value;
199 /** The is_synchronizing method should only explore interleavings if:
200 * (1) the operations are seq_cst and don't commute or
201 * (2) the reordering may establish or break a synchronization relation.
202 * Other memory operations will be dealt with by using the reads_from
205 * @param act is the action to consider exploring a reordering.
206 * @return tells whether we have to explore a reordering.
208 bool ModelAction::could_synchronize_with(const ModelAction *act) const
210 //Same thread can't be reordered
211 if (same_thread(act))
214 // Different locations commute
218 // Explore interleavings of seqcst writes to guarantee total order
219 // of seq_cst operations that don't commute
220 if ((is_write() || act->is_write()) && is_seqcst() && act->is_seqcst())
223 // Explore synchronizing read/write pairs
224 if (is_read() && is_acquire() && act->is_write() && act->is_release())
227 // Otherwise handle by reads_from relation
231 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
233 //Must be different threads to reorder
234 if (same_thread(act))
237 //Try to reorder a lock past a successful lock
238 if (act->is_success_lock())
241 //Try to push a successful trylock past an unlock
242 if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
249 * Create a new clock vector for this action. Note that this function allows a
250 * user to clobber (and leak) a ModelAction's existing clock vector. A user
251 * should ensure that the vector has already either been rolled back
252 * (effectively "freed") or freed.
254 * @param parent A ModelAction from which to inherit a ClockVector
256 void ModelAction::create_cv(const ModelAction *parent)
259 cv = new ClockVector(parent->cv, this);
261 cv = new ClockVector(NULL, this);
264 void ModelAction::set_try_lock(bool obtainedlock) {
266 value=VALUE_TRYSUCCESS;
268 value=VALUE_TRYFAILED;
272 * Update the model action's read_from action
273 * @param act The action to read from; should be a write
274 * @return True if this read established synchronization
276 bool ModelAction::read_from(const ModelAction *act)
280 if (act != NULL && this->is_acquire()) {
281 rel_heads_list_t release_heads;
282 model->get_release_seq_heads(this, &release_heads);
283 int num_heads = release_heads.size();
284 for (unsigned int i = 0; i < release_heads.size(); i++)
285 if (!synchronize_with(release_heads[i])) {
286 model->set_bad_synchronization();
289 return num_heads > 0;
295 * Synchronize the current thread with the thread corresponding to the
296 * ModelAction parameter.
297 * @param act The ModelAction to synchronize with
298 * @return True if this is a valid synchronization; false otherwise
300 bool ModelAction::synchronize_with(const ModelAction *act) {
301 if (*this < *act && type != THREAD_JOIN && type != ATOMIC_LOCK)
303 model->check_promises(act->get_tid(), cv, act->cv);
308 bool ModelAction::has_synchronized_with(const ModelAction *act) const
310 return cv->has_synchronized_with(act->cv);
314 * Check whether 'this' happens before act, according to the memory-model's
315 * happens before relation. This is checked via the ClockVector constructs.
316 * @return true if this action's thread has synchronized with act's thread
317 * since the execution of act, false otherwise.
319 bool ModelAction::happens_before(const ModelAction *act) const
321 return act->cv->synchronized_since(this);
324 /** @brief Print nicely-formatted info about this ModelAction */
325 void ModelAction::print() const
327 const char *type_str, *mo_str;
328 switch (this->type) {
329 case MODEL_FIXUP_RELSEQ:
330 type_str = "relseq fixup";
333 type_str = "thread create";
336 type_str = "thread start";
339 type_str = "thread yield";
342 type_str = "thread join";
345 type_str = "thread finish";
348 type_str = "atomic read";
351 type_str = "atomic write";
354 type_str = "atomic rmw";
360 type_str = "atomic rmwr";
363 type_str = "atomic rmwc";
366 type_str = "init atomic";
375 type_str = "trylock";
378 type_str = "unknown type";
381 uint64_t valuetoprint=type==ATOMIC_READ?(reads_from!=NULL?reads_from->value:VALUE_NONE):value;
383 switch (this->order) {
384 case std::memory_order_relaxed:
387 case std::memory_order_acquire:
390 case std::memory_order_release:
393 case std::memory_order_acq_rel:
396 case std::memory_order_seq_cst:
404 printf("(%3d) Thread: %-2d Action: %-13s MO: %7s Loc: %14p Value: %-12" PRIu64,
405 seq_number, id_to_int(tid), type_str, mo_str, location, valuetoprint);
408 printf(" Rf: %d", reads_from->get_seq_number());