2 #define __STDC_FORMAT_MACROS
8 #include "clockvector.h"
11 #include "nodestack.h"
13 #define ACTION_INITIAL_CLOCK 0
15 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, uint64_t value) :
22 seq_number(ACTION_INITIAL_CLOCK),
25 Thread *t = thread_current();
26 this->tid = t->get_id();
29 /** @brief ModelAction destructor */
30 ModelAction::~ModelAction()
33 * We can't free the clock vector:
34 * Clock vectors are snapshotting state. When we delete model actions,
35 * they are at the end of the node list and have invalid old clock
36 * vectors which have already been rolled back to an unallocated state.
44 void ModelAction::copy_from_new(ModelAction *newaction)
46 seq_number = newaction->seq_number;
49 void ModelAction::set_seq_number(modelclock_t num)
51 ASSERT(seq_number == ACTION_INITIAL_CLOCK);
55 bool ModelAction::is_mutex_op() const
57 return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK;
60 bool ModelAction::is_lock() const
62 return type == ATOMIC_LOCK;
65 bool ModelAction::is_unlock() const
67 return type == ATOMIC_UNLOCK;
70 bool ModelAction::is_trylock() const
72 return type == ATOMIC_TRYLOCK;
75 bool ModelAction::is_success_lock() const
77 return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
80 bool ModelAction::is_failed_trylock() const
82 return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
85 bool ModelAction::is_read() const
87 return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMW;
90 bool ModelAction::is_write() const
92 return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT;
95 bool ModelAction::is_rmwr() const
97 return type == ATOMIC_RMWR;
100 bool ModelAction::is_rmw() const
102 return type == ATOMIC_RMW;
105 bool ModelAction::is_rmwc() const
107 return type == ATOMIC_RMWC;
110 bool ModelAction::is_fence() const
112 return type == ATOMIC_FENCE;
115 bool ModelAction::is_initialization() const
117 return type == ATOMIC_INIT;
120 bool ModelAction::is_acquire() const
123 case std::memory_order_acquire:
124 case std::memory_order_acq_rel:
125 case std::memory_order_seq_cst:
132 bool ModelAction::is_release() const
135 case std::memory_order_release:
136 case std::memory_order_acq_rel:
137 case std::memory_order_seq_cst:
144 bool ModelAction::is_seqcst() const
146 return order==std::memory_order_seq_cst;
149 bool ModelAction::same_var(const ModelAction *act) const
151 return location == act->location;
154 bool ModelAction::same_thread(const ModelAction *act) const
156 return tid == act->tid;
159 void ModelAction::copy_typeandorder(ModelAction * act) {
160 this->type = act->type;
161 this->order = act->order;
164 /** This method changes an existing read part of an RMW action into either:
165 * (1) a full RMW action in case of the completed write or
166 * (2) a READ action in case a failed action.
167 * @todo If the memory_order changes, we may potentially need to update our
170 void ModelAction::process_rmw(ModelAction * act) {
171 this->order=act->order;
173 this->type=ATOMIC_READ;
174 else if (act->is_rmw()) {
175 this->type=ATOMIC_RMW;
176 this->value=act->value;
180 /** The is_synchronizing method should only explore interleavings if:
181 * (1) the operations are seq_cst and don't commute or
182 * (2) the reordering may establish or break a synchronization relation.
183 * Other memory operations will be dealt with by using the reads_from
186 * @param act is the action to consider exploring a reordering.
187 * @return tells whether we have to explore a reordering.
189 bool ModelAction::could_synchronize_with(const ModelAction *act) const
191 //Same thread can't be reordered
192 if (same_thread(act))
195 // Different locations commute
199 // Explore interleavings of seqcst writes to guarantee total order
200 // of seq_cst operations that don't commute
201 if ((is_write() || act->is_write()) && is_seqcst() && act->is_seqcst())
204 // Explore synchronizing read/write pairs
205 if (is_read() && is_acquire() && act->is_write() && act->is_release())
208 // Otherwise handle by reads_from relation
212 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
214 //Must be different threads to reorder
215 if (same_thread(act))
218 //Try to reorder a lock past a successful lock
219 if (act->is_success_lock())
222 //Try to push a successful trylock past an unlock
223 if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
230 * Create a new clock vector for this action. Note that this function allows a
231 * user to clobber (and leak) a ModelAction's existing clock vector. A user
232 * should ensure that the vector has already either been rolled back
233 * (effectively "freed") or freed.
235 * @param parent A ModelAction from which to inherit a ClockVector
237 void ModelAction::create_cv(const ModelAction *parent)
240 cv = new ClockVector(parent->cv, this);
242 cv = new ClockVector(NULL, this);
245 void ModelAction::set_try_lock(bool obtainedlock) {
247 value=VALUE_TRYSUCCESS;
249 value=VALUE_TRYFAILED;
253 * Update the model action's read_from action
254 * @param act The action to read from; should be a write
255 * @return True if this read established synchronization
257 bool ModelAction::read_from(const ModelAction *act)
261 if (act != NULL && this->is_acquire()) {
262 rel_heads_list_t release_heads;
263 model->get_release_seq_heads(this, &release_heads);
264 int num_heads = release_heads.size();
265 for (unsigned int i = 0; i < release_heads.size(); i++)
266 if (!synchronize_with(release_heads[i])) {
267 model->set_bad_synchronization();
270 return num_heads > 0;
276 * Synchronize the current thread with the thread corresponding to the
277 * ModelAction parameter.
278 * @param act The ModelAction to synchronize with
279 * @return True if this is a valid synchronization; false otherwise
281 bool ModelAction::synchronize_with(const ModelAction *act) {
282 if (*this < *act && type != THREAD_JOIN && type != ATOMIC_LOCK)
284 model->check_promises(act->get_tid(), cv, act->cv);
289 bool ModelAction::has_synchronized_with(const ModelAction *act) const
291 return cv->has_synchronized_with(act->cv);
295 * Check whether 'this' happens before act, according to the memory-model's
296 * happens before relation. This is checked via the ClockVector constructs.
297 * @return true if this action's thread has synchronized with act's thread
298 * since the execution of act, false otherwise.
300 bool ModelAction::happens_before(const ModelAction *act) const
302 return act->cv->synchronized_since(this);
305 /** @brief Print nicely-formatted info about this ModelAction */
306 void ModelAction::print() const
308 const char *type_str, *mo_str;
309 switch (this->type) {
311 type_str = "thread create";
314 type_str = "thread start";
317 type_str = "thread yield";
320 type_str = "thread join";
323 type_str = "thread finish";
326 type_str = "atomic read";
329 type_str = "atomic write";
332 type_str = "atomic rmw";
338 type_str = "atomic rmwr";
341 type_str = "atomic rmwc";
344 type_str = "init atomic";
353 type_str = "trylock";
356 type_str = "unknown type";
359 uint64_t valuetoprint=type==ATOMIC_READ?(reads_from!=NULL?reads_from->value:VALUE_NONE):value;
361 switch (this->order) {
362 case std::memory_order_relaxed:
365 case std::memory_order_acquire:
368 case std::memory_order_release:
371 case std::memory_order_acq_rel:
374 case std::memory_order_seq_cst:
382 printf("(%3d) Thread: %-2d Action: %-13s MO: %7s Loc: %14p Value: %-12" PRIu64,
383 seq_number, id_to_int(tid), type_str, mo_str, location, valuetoprint);
386 printf(" Rf: %d", reads_from->get_seq_number());