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 bool ModelAction::is_success_lock() const {
31 return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
34 bool ModelAction::is_failed_trylock() const {
35 return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
38 bool ModelAction::is_read() const
40 return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMW;
43 bool ModelAction::is_write() const
45 return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT;
48 bool ModelAction::is_rmwr() const
50 return type == ATOMIC_RMWR;
53 bool ModelAction::is_rmw() const
55 return type == ATOMIC_RMW;
58 bool ModelAction::is_rmwc() const
60 return type == ATOMIC_RMWC;
63 bool ModelAction::is_fence() const
65 return type == ATOMIC_FENCE;
68 bool ModelAction::is_initialization() const
70 return type == ATOMIC_INIT;
73 bool ModelAction::is_acquire() const
76 case std::memory_order_acquire:
77 case std::memory_order_acq_rel:
78 case std::memory_order_seq_cst:
85 bool ModelAction::is_release() const
88 case std::memory_order_release:
89 case std::memory_order_acq_rel:
90 case std::memory_order_seq_cst:
97 bool ModelAction::is_seqcst() const
99 return order==std::memory_order_seq_cst;
102 bool ModelAction::same_var(const ModelAction *act) const
104 return location == act->location;
107 bool ModelAction::same_thread(const ModelAction *act) const
109 return tid == act->tid;
112 void ModelAction::copy_typeandorder(ModelAction * act) {
113 this->type=act->type;
114 this->order=act->order;
117 /** This method changes an existing read part of an RMW action into either:
118 * (1) a full RMW action in case of the completed write or
119 * (2) a READ action in case a failed action.
120 * @todo If the memory_order changes, we may potentially need to update our
123 void ModelAction::process_rmw(ModelAction * act) {
124 this->order=act->order;
126 this->type=ATOMIC_READ;
127 else if (act->is_rmw()) {
128 this->type=ATOMIC_RMW;
129 this->value=act->value;
133 /** The is_synchronizing method should only explore interleavings if:
134 * (1) the operations are seq_cst and don't commute or
135 * (2) the reordering may establish or break a synchronization relation.
136 * Other memory operations will be dealt with by using the reads_from
139 * @param act is the action to consider exploring a reordering.
140 * @return tells whether we have to explore a reordering.
142 bool ModelAction::is_synchronizing(const ModelAction *act) const
144 //Same thread can't be reordered
145 if (same_thread(act))
148 // Different locations commute
152 // Explore interleavings of seqcst writes to guarantee total order
153 // of seq_cst operations that don't commute
154 if (is_write() && is_seqcst() && act->is_write() && act->is_seqcst())
157 // Explore synchronizing read/write pairs
158 if (is_read() && is_acquire() && act->is_write() && act->is_release())
160 if (is_write() && is_release() && act->is_read() && act->is_acquire())
163 // Otherwise handle by reads_from relation
167 void ModelAction::create_cv(const ModelAction *parent)
173 cv = new ClockVector(parent->cv, this);
175 cv = new ClockVector(NULL, this);
178 /** Update the model action's read_from action */
179 void ModelAction::read_from(const ModelAction *act)
183 if (act != NULL && this->is_acquire()) {
184 rel_heads_list_t release_heads;
185 model->get_release_seq_heads(this, &release_heads);
186 for (unsigned int i = 0; i < release_heads.size(); i++)
187 synchronize_with(release_heads[i]);
192 * Synchronize the current thread with the thread corresponding to the
193 * ModelAction parameter.
194 * @param act The ModelAction to synchronize with
196 void ModelAction::synchronize_with(const ModelAction *act) {
197 ASSERT(*act < *this || type == THREAD_JOIN);
198 model->check_promises(cv, act->cv);
202 bool ModelAction::has_synchronized_with(const ModelAction *act) const
204 return cv->has_synchronized_with(act->cv);
208 * Check whether 'this' happens before act, according to the memory-model's
209 * happens before relation. This is checked via the ClockVector constructs.
210 * @return true if this action's thread has synchronized with act's thread
211 * since the execution of act, false otherwise.
213 bool ModelAction::happens_before(const ModelAction *act) const
215 return act->cv->synchronized_since(this);
218 void ModelAction::print(void) const
220 const char *type_str, *mo_str;
221 switch (this->type) {
223 type_str = "thread create";
226 type_str = "thread start";
229 type_str = "thread yield";
232 type_str = "thread join";
235 type_str = "thread finish";
238 type_str = "atomic read";
241 type_str = "atomic write";
244 type_str = "atomic rmw";
250 type_str = "atomic rmwr";
253 type_str = "atomic rmwc";
256 type_str = "init atomic";
259 type_str = "unknown type";
262 uint64_t valuetoprint=type==ATOMIC_READ?(reads_from!=NULL?reads_from->value:VALUE_NONE):value;
264 switch (this->order) {
265 case std::memory_order_relaxed:
268 case std::memory_order_acquire:
271 case std::memory_order_release:
274 case std::memory_order_acq_rel:
277 case std::memory_order_seq_cst:
285 printf("(%3d) Thread: %-2d Action: %-13s MO: %7s Loc: %14p Value: %-12" PRIu64,
286 seq_number, id_to_int(tid), type_str, mo_str, location, valuetoprint);
289 printf(" Rf: %d", reads_from->get_seq_number());