2 #define __STDC_FORMAT_MACROS
8 #include "clockvector.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 seq_number(ACTION_INITIAL_CLOCK),
36 Thread *t = thread ? thread : thread_current();
37 this->tid = t->get_id();
40 /** @brief ModelAction destructor */
41 ModelAction::~ModelAction()
44 * We can't free the clock vector:
45 * Clock vectors are snapshotting state. When we delete model actions,
46 * they are at the end of the node list and have invalid old clock
47 * vectors which have already been rolled back to an unallocated state.
55 void ModelAction::copy_from_new(ModelAction *newaction)
57 seq_number = newaction->seq_number;
60 void ModelAction::set_seq_number(modelclock_t num)
62 ASSERT(seq_number == ACTION_INITIAL_CLOCK);
66 bool ModelAction::is_relseq_fixup() const
68 return type == MODEL_FIXUP_RELSEQ;
71 bool ModelAction::is_mutex_op() const
73 return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK;
76 bool ModelAction::is_lock() const
78 return type == ATOMIC_LOCK;
81 bool ModelAction::is_unlock() const
83 return type == ATOMIC_UNLOCK;
86 bool ModelAction::is_trylock() const
88 return type == ATOMIC_TRYLOCK;
91 bool ModelAction::is_success_lock() const
93 return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
96 bool ModelAction::is_failed_trylock() const
98 return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
101 bool ModelAction::is_read() const
103 return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMW;
106 bool ModelAction::is_write() const
108 return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT;
111 bool ModelAction::is_rmwr() const
113 return type == ATOMIC_RMWR;
116 bool ModelAction::is_rmw() const
118 return type == ATOMIC_RMW;
121 bool ModelAction::is_rmwc() const
123 return type == ATOMIC_RMWC;
126 bool ModelAction::is_fence() const
128 return type == ATOMIC_FENCE;
131 bool ModelAction::is_initialization() const
133 return type == ATOMIC_INIT;
136 bool ModelAction::is_acquire() const
139 case std::memory_order_acquire:
140 case std::memory_order_acq_rel:
141 case std::memory_order_seq_cst:
148 bool ModelAction::is_release() const
151 case std::memory_order_release:
152 case std::memory_order_acq_rel:
153 case std::memory_order_seq_cst:
160 bool ModelAction::is_seqcst() const
162 return order==std::memory_order_seq_cst;
165 bool ModelAction::same_var(const ModelAction *act) const
167 return location == act->location;
170 bool ModelAction::same_thread(const ModelAction *act) const
172 return tid == act->tid;
175 void ModelAction::copy_typeandorder(ModelAction * act) {
176 this->type = act->type;
177 this->order = act->order;
180 /** This method changes an existing read part of an RMW action into either:
181 * (1) a full RMW action in case of the completed write or
182 * (2) a READ action in case a failed action.
183 * @todo If the memory_order changes, we may potentially need to update our
186 void ModelAction::process_rmw(ModelAction * act) {
187 this->order=act->order;
189 this->type=ATOMIC_READ;
190 else if (act->is_rmw()) {
191 this->type=ATOMIC_RMW;
192 this->value=act->value;
196 /** The is_synchronizing method should only explore interleavings if:
197 * (1) the operations are seq_cst and don't commute or
198 * (2) the reordering may establish or break a synchronization relation.
199 * Other memory operations will be dealt with by using the reads_from
202 * @param act is the action to consider exploring a reordering.
203 * @return tells whether we have to explore a reordering.
205 bool ModelAction::could_synchronize_with(const ModelAction *act) const
207 //Same thread can't be reordered
208 if (same_thread(act))
211 // Different locations commute
215 // Explore interleavings of seqcst writes to guarantee total order
216 // of seq_cst operations that don't commute
217 if ((is_write() || act->is_write()) && is_seqcst() && act->is_seqcst())
220 // Explore synchronizing read/write pairs
221 if (is_read() && is_acquire() && act->is_write() && act->is_release())
224 // Otherwise handle by reads_from relation
228 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
230 //Must be different threads to reorder
231 if (same_thread(act))
234 //Try to reorder a lock past a successful lock
235 if (act->is_success_lock())
238 //Try to push a successful trylock past an unlock
239 if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
246 * Create a new clock vector for this action. Note that this function allows a
247 * user to clobber (and leak) a ModelAction's existing clock vector. A user
248 * should ensure that the vector has already either been rolled back
249 * (effectively "freed") or freed.
251 * @param parent A ModelAction from which to inherit a ClockVector
253 void ModelAction::create_cv(const ModelAction *parent)
256 cv = new ClockVector(parent->cv, this);
258 cv = new ClockVector(NULL, this);
261 void ModelAction::set_try_lock(bool obtainedlock) {
263 value=VALUE_TRYSUCCESS;
265 value=VALUE_TRYFAILED;
269 * Update the model action's read_from action
270 * @param act The action to read from; should be a write
271 * @return True if this read established synchronization
273 bool ModelAction::read_from(const ModelAction *act)
277 if (act != NULL && this->is_acquire()) {
278 rel_heads_list_t release_heads;
279 model->get_release_seq_heads(this, &release_heads);
280 int num_heads = release_heads.size();
281 for (unsigned int i = 0; i < release_heads.size(); i++)
282 if (!synchronize_with(release_heads[i])) {
283 model->set_bad_synchronization();
286 return num_heads > 0;
292 * Synchronize the current thread with the thread corresponding to the
293 * ModelAction parameter.
294 * @param act The ModelAction to synchronize with
295 * @return True if this is a valid synchronization; false otherwise
297 bool ModelAction::synchronize_with(const ModelAction *act) {
298 if (*this < *act && type != THREAD_JOIN && type != ATOMIC_LOCK)
300 model->check_promises(act->get_tid(), cv, act->cv);
305 bool ModelAction::has_synchronized_with(const ModelAction *act) const
307 return cv->has_synchronized_with(act->cv);
311 * Check whether 'this' happens before act, according to the memory-model's
312 * happens before relation. This is checked via the ClockVector constructs.
313 * @return true if this action's thread has synchronized with act's thread
314 * since the execution of act, false otherwise.
316 bool ModelAction::happens_before(const ModelAction *act) const
318 return act->cv->synchronized_since(this);
321 /** @brief Print nicely-formatted info about this ModelAction */
322 void ModelAction::print() const
324 const char *type_str, *mo_str;
325 switch (this->type) {
326 case MODEL_FIXUP_RELSEQ:
327 type_str = "relseq fixup";
330 type_str = "thread create";
333 type_str = "thread start";
336 type_str = "thread yield";
339 type_str = "thread join";
342 type_str = "thread finish";
345 type_str = "atomic read";
348 type_str = "atomic write";
351 type_str = "atomic rmw";
357 type_str = "atomic rmwr";
360 type_str = "atomic rmwc";
363 type_str = "init atomic";
372 type_str = "trylock";
375 type_str = "unknown type";
378 uint64_t valuetoprint=type==ATOMIC_READ?(reads_from!=NULL?reads_from->value:VALUE_NONE):value;
380 switch (this->order) {
381 case std::memory_order_relaxed:
384 case std::memory_order_acquire:
387 case std::memory_order_release:
390 case std::memory_order_acq_rel:
393 case std::memory_order_seq_cst:
401 printf("(%3d) Thread: %-2d Action: %-13s MO: %7s Loc: %14p Value: %-12" PRIu64,
402 seq_number, id_to_int(tid), type_str, mo_str, location, valuetoprint);
405 printf(" Rf: %d", reads_from->get_seq_number());