2 #define __STDC_FORMAT_MACROS
8 #include "clockvector.h"
11 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, uint64_t value) :
19 Thread *t = thread_current();
20 this->tid = t->get_id();
21 this->seq_number = model->get_next_seq_num();
24 ModelAction::~ModelAction()
30 void ModelAction::copy_from_new(ModelAction *newaction)
32 seq_number = newaction->seq_number;
35 bool ModelAction::is_mutex_op() const
37 return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK;
40 bool ModelAction::is_lock() const
42 return type == ATOMIC_LOCK;
45 bool ModelAction::is_unlock() const
47 return type == ATOMIC_UNLOCK;
50 bool ModelAction::is_trylock() const
52 return type == ATOMIC_TRYLOCK;
55 bool ModelAction::is_success_lock() const
57 return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
60 bool ModelAction::is_failed_trylock() const
62 return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
65 bool ModelAction::is_read() const
67 return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMW;
70 bool ModelAction::is_write() const
72 return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT;
75 bool ModelAction::is_rmwr() const
77 return type == ATOMIC_RMWR;
80 bool ModelAction::is_rmw() const
82 return type == ATOMIC_RMW;
85 bool ModelAction::is_rmwc() const
87 return type == ATOMIC_RMWC;
90 bool ModelAction::is_fence() const
92 return type == ATOMIC_FENCE;
95 bool ModelAction::is_initialization() const
97 return type == ATOMIC_INIT;
100 bool ModelAction::is_acquire() const
103 case std::memory_order_acquire:
104 case std::memory_order_acq_rel:
105 case std::memory_order_seq_cst:
112 bool ModelAction::is_release() const
115 case std::memory_order_release:
116 case std::memory_order_acq_rel:
117 case std::memory_order_seq_cst:
124 bool ModelAction::is_seqcst() const
126 return order==std::memory_order_seq_cst;
129 bool ModelAction::same_var(const ModelAction *act) const
131 return location == act->location;
134 bool ModelAction::same_thread(const ModelAction *act) const
136 return tid == act->tid;
139 void ModelAction::copy_typeandorder(ModelAction * act) {
140 this->type = act->type;
141 this->order = act->order;
144 /** This method changes an existing read part of an RMW action into either:
145 * (1) a full RMW action in case of the completed write or
146 * (2) a READ action in case a failed action.
147 * @todo If the memory_order changes, we may potentially need to update our
150 void ModelAction::process_rmw(ModelAction * act) {
151 this->order=act->order;
153 this->type=ATOMIC_READ;
154 else if (act->is_rmw()) {
155 this->type=ATOMIC_RMW;
156 this->value=act->value;
160 /** The is_synchronizing method should only explore interleavings if:
161 * (1) the operations are seq_cst and don't commute or
162 * (2) the reordering may establish or break a synchronization relation.
163 * Other memory operations will be dealt with by using the reads_from
166 * @param act is the action to consider exploring a reordering.
167 * @return tells whether we have to explore a reordering.
169 bool ModelAction::is_synchronizing(const ModelAction *act) const
171 //Same thread can't be reordered
172 if (same_thread(act))
175 // Different locations commute
179 // Explore interleavings of seqcst writes to guarantee total order
180 // of seq_cst operations that don't commute
181 if (is_write() && is_seqcst() && act->is_write() && act->is_seqcst())
184 // Explore synchronizing read/write pairs
185 if (is_read() && is_acquire() && act->is_write() && act->is_release())
187 if (is_write() && is_release() && act->is_read() && act->is_acquire())
190 // Otherwise handle by reads_from relation
194 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
196 //Must be different threads to reorder
197 if (same_thread(act))
200 //Try to reorder a lock past a successful lock
201 if (act->is_success_lock())
204 //Try to push a successful trylock past an unlock
205 if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
212 * Create a new clock vector for this action. Note that this function allows a
213 * user to clobber (and leak) a ModelAction's existing clock vector. A user
214 * should ensure that the vector has already either been rolled back
215 * (effectively "freed") or freed.
217 * @param parent A ModelAction from which to inherit a ClockVector
219 void ModelAction::create_cv(const ModelAction *parent)
222 cv = new ClockVector(parent->cv, this);
224 cv = new ClockVector(NULL, this);
227 void ModelAction::set_try_lock(bool obtainedlock) {
229 value=VALUE_TRYSUCCESS;
231 value=VALUE_TRYFAILED;
234 /** Update the model action's read_from action */
235 void ModelAction::read_from(const ModelAction *act)
239 if (act != NULL && this->is_acquire()) {
240 rel_heads_list_t release_heads;
241 model->get_release_seq_heads(this, &release_heads);
242 for (unsigned int i = 0; i < release_heads.size(); i++)
243 if (!synchronize_with(release_heads[i]))
244 model->set_bad_synchronization();
249 * Synchronize the current thread with the thread corresponding to the
250 * ModelAction parameter.
251 * @param act The ModelAction to synchronize with
252 * @return True if this is a valid synchronization; false otherwise
254 bool ModelAction::synchronize_with(const ModelAction *act) {
255 if (*this < *act && type != THREAD_JOIN && type != ATOMIC_LOCK)
257 model->check_promises(cv, act->cv);
262 bool ModelAction::has_synchronized_with(const ModelAction *act) const
264 return cv->has_synchronized_with(act->cv);
268 * Check whether 'this' happens before act, according to the memory-model's
269 * happens before relation. This is checked via the ClockVector constructs.
270 * @return true if this action's thread has synchronized with act's thread
271 * since the execution of act, false otherwise.
273 bool ModelAction::happens_before(const ModelAction *act) const
275 return act->cv->synchronized_since(this);
279 * Print nicely-formatted info about this ModelAction
281 * @param print_cv True if we want to print clock vector data. Might be false,
282 * for instance, in situations where the clock vector might be invalid
284 void ModelAction::print(bool print_cv) const
286 const char *type_str, *mo_str;
287 switch (this->type) {
289 type_str = "thread create";
292 type_str = "thread start";
295 type_str = "thread yield";
298 type_str = "thread join";
301 type_str = "thread finish";
304 type_str = "atomic read";
307 type_str = "atomic write";
310 type_str = "atomic rmw";
316 type_str = "atomic rmwr";
319 type_str = "atomic rmwc";
322 type_str = "init atomic";
331 type_str = "trylock";
334 type_str = "unknown type";
337 uint64_t valuetoprint=type==ATOMIC_READ?(reads_from!=NULL?reads_from->value:VALUE_NONE):value;
339 switch (this->order) {
340 case std::memory_order_relaxed:
343 case std::memory_order_acquire:
346 case std::memory_order_release:
349 case std::memory_order_acq_rel:
352 case std::memory_order_seq_cst:
360 printf("(%3d) Thread: %-2d Action: %-13s MO: %7s Loc: %14p Value: %-12" PRIu64,
361 seq_number, id_to_int(tid), type_str, mo_str, location, valuetoprint);
364 printf(" Rf: %d", reads_from->get_seq_number());
368 if (cv && print_cv) {