2 #define __STDC_FORMAT_MACROS
8 #include "clockvector.h"
12 #define ACTION_INITIAL_CLOCK 0
14 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, uint64_t value) :
20 seq_number(ACTION_INITIAL_CLOCK),
23 Thread *t = thread_current();
24 this->tid = t->get_id();
27 /** @brief ModelAction destructor */
28 ModelAction::~ModelAction()
31 * We can't free the clock vector:
32 * Clock vectors are snapshotting state. When we delete model actions,
33 * they are at the end of the node list and have invalid old clock
34 * vectors which have already been rolled back to an unallocated state.
42 void ModelAction::copy_from_new(ModelAction *newaction)
44 seq_number = newaction->seq_number;
47 void ModelAction::set_seq_number(modelclock_t num)
49 ASSERT(seq_number == ACTION_INITIAL_CLOCK);
53 bool ModelAction::is_relseq_fixup() const
55 return type == MODEL_FIXUP_RELSEQ;
58 bool ModelAction::is_mutex_op() const
60 return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK;
63 bool ModelAction::is_lock() const
65 return type == ATOMIC_LOCK;
68 bool ModelAction::is_unlock() const
70 return type == ATOMIC_UNLOCK;
73 bool ModelAction::is_trylock() const
75 return type == ATOMIC_TRYLOCK;
78 bool ModelAction::is_success_lock() const
80 return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
83 bool ModelAction::is_failed_trylock() const
85 return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
88 bool ModelAction::is_read() const
90 return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMW;
93 bool ModelAction::is_write() const
95 return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT;
98 bool ModelAction::is_rmwr() const
100 return type == ATOMIC_RMWR;
103 bool ModelAction::is_rmw() const
105 return type == ATOMIC_RMW;
108 bool ModelAction::is_rmwc() const
110 return type == ATOMIC_RMWC;
113 bool ModelAction::is_fence() const
115 return type == ATOMIC_FENCE;
118 bool ModelAction::is_initialization() const
120 return type == ATOMIC_INIT;
123 bool ModelAction::is_acquire() const
126 case std::memory_order_acquire:
127 case std::memory_order_acq_rel:
128 case std::memory_order_seq_cst:
135 bool ModelAction::is_release() const
138 case std::memory_order_release:
139 case std::memory_order_acq_rel:
140 case std::memory_order_seq_cst:
147 bool ModelAction::is_seqcst() const
149 return order==std::memory_order_seq_cst;
152 bool ModelAction::same_var(const ModelAction *act) const
154 return location == act->location;
157 bool ModelAction::same_thread(const ModelAction *act) const
159 return tid == act->tid;
162 void ModelAction::copy_typeandorder(ModelAction * act) {
163 this->type = act->type;
164 this->order = act->order;
167 /** This method changes an existing read part of an RMW action into either:
168 * (1) a full RMW action in case of the completed write or
169 * (2) a READ action in case a failed action.
170 * @todo If the memory_order changes, we may potentially need to update our
173 void ModelAction::process_rmw(ModelAction * act) {
174 this->order=act->order;
176 this->type=ATOMIC_READ;
177 else if (act->is_rmw()) {
178 this->type=ATOMIC_RMW;
179 this->value=act->value;
183 /** The is_synchronizing method should only explore interleavings if:
184 * (1) the operations are seq_cst and don't commute or
185 * (2) the reordering may establish or break a synchronization relation.
186 * Other memory operations will be dealt with by using the reads_from
189 * @param act is the action to consider exploring a reordering.
190 * @return tells whether we have to explore a reordering.
192 bool ModelAction::could_synchronize_with(const ModelAction *act) const
194 //Same thread can't be reordered
195 if (same_thread(act))
198 // Different locations commute
202 // Explore interleavings of seqcst writes to guarantee total order
203 // of seq_cst operations that don't commute
204 if ((is_write() || act->is_write()) && is_seqcst() && act->is_seqcst())
207 // Explore synchronizing read/write pairs
208 if (is_read() && is_acquire() && act->is_write() && act->is_release())
211 // Otherwise handle by reads_from relation
215 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
217 //Must be different threads to reorder
218 if (same_thread(act))
221 //Try to reorder a lock past a successful lock
222 if (act->is_success_lock())
225 //Try to push a successful trylock past an unlock
226 if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
233 * Create a new clock vector for this action. Note that this function allows a
234 * user to clobber (and leak) a ModelAction's existing clock vector. A user
235 * should ensure that the vector has already either been rolled back
236 * (effectively "freed") or freed.
238 * @param parent A ModelAction from which to inherit a ClockVector
240 void ModelAction::create_cv(const ModelAction *parent)
243 cv = new ClockVector(parent->cv, this);
245 cv = new ClockVector(NULL, this);
248 void ModelAction::set_try_lock(bool obtainedlock) {
250 value=VALUE_TRYSUCCESS;
252 value=VALUE_TRYFAILED;
256 * Update the model action's read_from action
257 * @param act The action to read from; should be a write
258 * @return True if this read established synchronization
260 bool ModelAction::read_from(const ModelAction *act)
264 if (act != NULL && this->is_acquire()) {
265 rel_heads_list_t release_heads;
266 model->get_release_seq_heads(this, &release_heads);
267 int num_heads = release_heads.size();
268 for (unsigned int i = 0; i < release_heads.size(); i++)
269 if (!synchronize_with(release_heads[i])) {
270 model->set_bad_synchronization();
273 return num_heads > 0;
279 * Synchronize the current thread with the thread corresponding to the
280 * ModelAction parameter.
281 * @param act The ModelAction to synchronize with
282 * @return True if this is a valid synchronization; false otherwise
284 bool ModelAction::synchronize_with(const ModelAction *act) {
285 if (*this < *act && type != THREAD_JOIN && type != ATOMIC_LOCK)
287 model->check_promises(act->get_tid(), cv, act->cv);
292 bool ModelAction::has_synchronized_with(const ModelAction *act) const
294 return cv->has_synchronized_with(act->cv);
298 * Check whether 'this' happens before act, according to the memory-model's
299 * happens before relation. This is checked via the ClockVector constructs.
300 * @return true if this action's thread has synchronized with act's thread
301 * since the execution of act, false otherwise.
303 bool ModelAction::happens_before(const ModelAction *act) const
305 return act->cv->synchronized_since(this);
308 /** @brief Print nicely-formatted info about this ModelAction */
309 void ModelAction::print() const
311 const char *type_str, *mo_str;
312 switch (this->type) {
313 case MODEL_FIXUP_RELSEQ:
314 type_str = "relseq fixup";
317 type_str = "thread create";
320 type_str = "thread start";
323 type_str = "thread yield";
326 type_str = "thread join";
329 type_str = "thread finish";
332 type_str = "atomic read";
335 type_str = "atomic write";
338 type_str = "atomic rmw";
344 type_str = "atomic rmwr";
347 type_str = "atomic rmwc";
350 type_str = "init atomic";
359 type_str = "trylock";
362 type_str = "unknown type";
365 uint64_t valuetoprint=type==ATOMIC_READ?(reads_from!=NULL?reads_from->value:VALUE_NONE):value;
367 switch (this->order) {
368 case std::memory_order_relaxed:
371 case std::memory_order_acquire:
374 case std::memory_order_release:
377 case std::memory_order_acq_rel:
380 case std::memory_order_seq_cst:
388 printf("(%3d) Thread: %-2d Action: %-13s MO: %7s Loc: %14p Value: %-12" PRIu64,
389 seq_number, id_to_int(tid), type_str, mo_str, location, valuetoprint);
392 printf(" Rf: %d", reads_from->get_seq_number());