+/**
+ * Complete a THREAD_JOIN operation, by synchronizing with the THREAD_FINISH
+ * operation from the Thread it is joining with. Must be called after the
+ * completion of the Thread in question.
+ * @param join The THREAD_JOIN action
+ */
+void ModelChecker::do_complete_join(ModelAction *join)
+{
+ Thread *blocking = (Thread *)join->get_location();
+ ModelAction *act = get_last_action(blocking->get_id());
+ join->synchronize_with(act);
+}
+
+void ModelChecker::check_curr_backtracking(ModelAction * curr) {
+ Node *currnode = curr->get_node();
+ Node *parnode = currnode->get_parent();
+
+ if ((!parnode->backtrack_empty() ||
+ !currnode->read_from_empty() ||
+ !currnode->future_value_empty() ||
+ !currnode->promise_empty())
+ && (!priv->next_backtrack ||
+ *curr > *priv->next_backtrack)) {
+ priv->next_backtrack = curr;
+ }
+}
+
+bool ModelChecker::promises_expired() {
+ for (unsigned int promise_index = 0; promise_index < promises->size(); promise_index++) {
+ Promise *promise = (*promises)[promise_index];
+ if (promise->get_expiration()<priv->used_sequence_numbers) {
+ return true;
+ }
+ }
+ return false;
+}
+
+/** @return whether the current partial trace must be a prefix of a
+ * feasible trace. */
+bool ModelChecker::isfeasibleprefix() {
+ return promises->size() == 0 && pending_acq_rel_seq->size() == 0;
+}
+
+/** @return whether the current partial trace is feasible. */
+bool ModelChecker::isfeasible() {
+ if (DBG_ENABLED() && mo_graph->checkForRMWViolation())
+ DEBUG("Infeasible: RMW violation\n");
+
+ return !mo_graph->checkForRMWViolation() && isfeasibleotherthanRMW();
+}
+
+/** @return whether the current partial trace is feasible other than
+ * multiple RMW reading from the same store. */
+bool ModelChecker::isfeasibleotherthanRMW() {
+ if (DBG_ENABLED()) {
+ if (mo_graph->checkForCycles())
+ DEBUG("Infeasible: modification order cycles\n");
+ if (failed_promise)
+ DEBUG("Infeasible: failed promise\n");
+ if (too_many_reads)
+ DEBUG("Infeasible: too many reads\n");
+ if (promises_expired())
+ DEBUG("Infeasible: promises expired\n");
+ }
+ return !mo_graph->checkForCycles() && !failed_promise && !too_many_reads && !promises_expired();
+}
+
+/** Returns whether the current completed trace is feasible. */
+bool ModelChecker::isfinalfeasible() {
+ if (DBG_ENABLED() && promises->size() != 0)
+ DEBUG("Infeasible: unrevolved promises\n");
+
+ return isfeasible() && promises->size() == 0;
+}
+
+/** Close out a RMWR by converting previous RMWR into a RMW or READ. */
+ModelAction * ModelChecker::process_rmw(ModelAction *act) {
+ int tid = id_to_int(act->get_tid());
+ ModelAction *lastread = get_last_action(tid);
+ lastread->process_rmw(act);
+ if (act->is_rmw() && lastread->get_reads_from()!=NULL) {
+ mo_graph->addRMWEdge(lastread->get_reads_from(), lastread);
+ mo_graph->commitChanges();
+ }
+ return lastread;
+}
+
+/**
+ * Checks whether a thread has read from the same write for too many times
+ * without seeing the effects of a later write.
+ *
+ * Basic idea:
+ * 1) there must a different write that we could read from that would satisfy the modification order,
+ * 2) we must have read from the same value in excess of maxreads times, and
+ * 3) that other write must have been in the reads_from set for maxreads times.
+ *
+ * If so, we decide that the execution is no longer feasible.
+ */
+void ModelChecker::check_recency(ModelAction *curr, const ModelAction *rf) {
+ if (params.maxreads != 0) {
+
+ if (curr->get_node()->get_read_from_size() <= 1)
+ return;
+ //Must make sure that execution is currently feasible... We could
+ //accidentally clear by rolling back
+ if (!isfeasible())
+ return;
+ std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
+ int tid = id_to_int(curr->get_tid());
+
+ /* Skip checks */
+ if ((int)thrd_lists->size() <= tid)
+ return;
+ action_list_t *list = &(*thrd_lists)[tid];
+
+ action_list_t::reverse_iterator rit = list->rbegin();
+ /* Skip past curr */
+ for (; (*rit) != curr; rit++)
+ ;
+ /* go past curr now */
+ rit++;
+
+ action_list_t::reverse_iterator ritcopy = rit;
+ //See if we have enough reads from the same value
+ int count = 0;
+ for (; count < params.maxreads; rit++,count++) {
+ if (rit==list->rend())
+ return;
+ ModelAction *act = *rit;
+ if (!act->is_read())
+ return;
+
+ if (act->get_reads_from() != rf)
+ return;
+ if (act->get_node()->get_read_from_size() <= 1)
+ return;
+ }
+ for (int i = 0; i<curr->get_node()->get_read_from_size(); i++) {
+ //Get write
+ const ModelAction * write = curr->get_node()->get_read_from_at(i);
+
+ //Need a different write
+ if (write==rf)
+ continue;
+
+ /* Test to see whether this is a feasible write to read from*/
+ mo_graph->startChanges();
+ r_modification_order(curr, write);
+ bool feasiblereadfrom = isfeasible();
+ mo_graph->rollbackChanges();
+
+ if (!feasiblereadfrom)
+ continue;
+ rit = ritcopy;
+
+ bool feasiblewrite = true;
+ //new we need to see if this write works for everyone
+
+ for (int loop = count; loop>0; loop--,rit++) {
+ ModelAction *act=*rit;
+ bool foundvalue = false;
+ for (int j = 0; j<act->get_node()->get_read_from_size(); j++) {
+ if (act->get_node()->get_read_from_at(i)==write) {
+ foundvalue = true;
+ break;
+ }
+ }
+ if (!foundvalue) {
+ feasiblewrite = false;
+ break;
+ }
+ }
+ if (feasiblewrite) {
+ too_many_reads = true;
+ return;
+ }
+ }
+ }
+}
+
+/**
+ * Updates the mo_graph with the constraints imposed from the current
+ * read.
+ *
+ * Basic idea is the following: Go through each other thread and find
+ * the lastest action that happened before our read. Two cases:
+ *
+ * (1) The action is a write => that write must either occur before
+ * the write we read from or be the write we read from.
+ *
+ * (2) The action is a read => the write that that action read from
+ * must occur before the write we read from or be the same write.
+ *
+ * @param curr The current action. Must be a read.
+ * @param rf The action that curr reads from. Must be a write.
+ * @return True if modification order edges were added; false otherwise
+ */
+bool ModelChecker::r_modification_order(ModelAction *curr, const ModelAction *rf)
+{
+ std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
+ unsigned int i;
+ bool added = false;
+ ASSERT(curr->is_read());
+
+ /* Iterate over all threads */
+ for (i = 0; i < thrd_lists->size(); i++) {
+ /* Iterate over actions in thread, starting from most recent */
+ action_list_t *list = &(*thrd_lists)[i];
+ action_list_t::reverse_iterator rit;
+ for (rit = list->rbegin(); rit != list->rend(); rit++) {
+ ModelAction *act = *rit;
+
+ /*
+ * Include at most one act per-thread that "happens
+ * before" curr. Don't consider reflexively.
+ */
+ if (act->happens_before(curr) && act != curr) {
+ if (act->is_write()) {
+ if (rf != act) {
+ mo_graph->addEdge(act, rf);
+ added = true;
+ }
+ } else {
+ const ModelAction *prevreadfrom = act->get_reads_from();
+ if (prevreadfrom != NULL && rf != prevreadfrom) {
+ mo_graph->addEdge(prevreadfrom, rf);
+ added = true;
+ }
+ }
+ break;
+ }
+ }
+ }
+
+ return added;
+}
+
+/** This method fixes up the modification order when we resolve a
+ * promises. The basic problem is that actions that occur after the
+ * read curr could not property add items to the modification order
+ * for our read.
+ *
+ * So for each thread, we find the earliest item that happens after
+ * the read curr. This is the item we have to fix up with additional
+ * constraints. If that action is write, we add a MO edge between
+ * the Action rf and that action. If the action is a read, we add a
+ * MO edge between the Action rf, and whatever the read accessed.
+ *
+ * @param curr is the read ModelAction that we are fixing up MO edges for.
+ * @param rf is the write ModelAction that curr reads from.
+ *
+ */
+void ModelChecker::post_r_modification_order(ModelAction *curr, const ModelAction *rf)
+{
+ std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
+ unsigned int i;
+ ASSERT(curr->is_read());
+
+ /* Iterate over all threads */
+ for (i = 0; i < thrd_lists->size(); i++) {
+ /* Iterate over actions in thread, starting from most recent */
+ action_list_t *list = &(*thrd_lists)[i];
+ action_list_t::reverse_iterator rit;
+ ModelAction *lastact = NULL;
+
+ /* Find last action that happens after curr */
+ for (rit = list->rbegin(); rit != list->rend(); rit++) {
+ ModelAction *act = *rit;
+ if (curr->happens_before(act)) {
+ lastact = act;
+ } else
+ break;
+ }
+
+ /* Include at most one act per-thread that "happens before" curr */
+ if (lastact != NULL) {
+ if (lastact->is_read()) {
+ const ModelAction *postreadfrom = lastact->get_reads_from();
+ if (postreadfrom != NULL&&rf != postreadfrom)
+ mo_graph->addEdge(rf, postreadfrom);
+ } else if (rf != lastact) {
+ mo_graph->addEdge(rf, lastact);
+ }
+ break;
+ }
+ }
+}
+
+/**
+ * Updates the mo_graph with the constraints imposed from the current write.
+ *
+ * Basic idea is the following: Go through each other thread and find
+ * the lastest action that happened before our write. Two cases:
+ *
+ * (1) The action is a write => that write must occur before
+ * the current write
+ *
+ * (2) The action is a read => the write that that action read from
+ * must occur before the current write.
+ *
+ * This method also handles two other issues:
+ *
+ * (I) Sequential Consistency: Making sure that if the current write is
+ * seq_cst, that it occurs after the previous seq_cst write.
+ *
+ * (II) Sending the write back to non-synchronizing reads.
+ *
+ * @param curr The current action. Must be a write.
+ * @return True if modification order edges were added; false otherwise
+ */
+bool ModelChecker::w_modification_order(ModelAction *curr)
+{
+ std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
+ unsigned int i;
+ bool added = false;
+ ASSERT(curr->is_write());
+
+ if (curr->is_seqcst()) {
+ /* We have to at least see the last sequentially consistent write,
+ so we are initialized. */
+ ModelAction *last_seq_cst = get_last_seq_cst(curr);
+ if (last_seq_cst != NULL) {
+ mo_graph->addEdge(last_seq_cst, curr);
+ added = true;
+ }
+ }
+
+ /* Iterate over all threads */
+ for (i = 0; i < thrd_lists->size(); i++) {
+ /* Iterate over actions in thread, starting from most recent */
+ action_list_t *list = &(*thrd_lists)[i];
+ action_list_t::reverse_iterator rit;
+ for (rit = list->rbegin(); rit != list->rend(); rit++) {
+ ModelAction *act = *rit;
+ if (act == curr) {
+ /*
+ * If RMW, we already have all relevant edges,
+ * so just skip to next thread.
+ * If normal write, we need to look at earlier
+ * actions, so continue processing list.
+ */
+ if (curr->is_rmw())
+ break;
+ else
+ continue;
+ }
+
+ /*
+ * Include at most one act per-thread that "happens
+ * before" curr
+ */
+ if (act->happens_before(curr)) {
+ /*
+ * Note: if act is RMW, just add edge:
+ * act --mo--> curr
+ * The following edge should be handled elsewhere:
+ * readfrom(act) --mo--> act
+ */
+ if (act->is_write())
+ mo_graph->addEdge(act, curr);
+ else if (act->is_read() && act->get_reads_from() != NULL)
+ mo_graph->addEdge(act->get_reads_from(), curr);
+ added = true;
+ break;
+ } else if (act->is_read() && !act->is_synchronizing(curr) &&
+ !act->same_thread(curr)) {
+ /* We have an action that:
+ (1) did not happen before us
+ (2) is a read and we are a write
+ (3) cannot synchronize with us
+ (4) is in a different thread
+ =>
+ that read could potentially read from our write.
+ */
+ if (thin_air_constraint_may_allow(curr, act)) {
+ if (isfeasible() ||
+ (curr->is_rmw() && act->is_rmw() && curr->get_reads_from() == act->get_reads_from() && isfeasibleotherthanRMW())) {
+ struct PendingFutureValue pfv = {curr->get_value(),curr->get_seq_number()+params.maxfuturedelay,act};
+ futurevalues->push_back(pfv);
+ }
+ }
+ }
+ }
+ }
+
+ return added;
+}
+
+/** Arbitrary reads from the future are not allowed. Section 29.3
+ * part 9 places some constraints. This method checks one result of constraint
+ * constraint. Others require compiler support. */
+bool ModelChecker::thin_air_constraint_may_allow(const ModelAction * writer, const ModelAction *reader) {
+ if (!writer->is_rmw())
+ return true;
+
+ if (!reader->is_rmw())
+ return true;
+
+ for (const ModelAction *search = writer->get_reads_from(); search != NULL; search = search->get_reads_from()) {
+ if (search == reader)
+ return false;
+ if (search->get_tid() == reader->get_tid() &&
+ search->happens_before(reader))
+ break;
+ }
+
+ return true;
+}
+
+/**
+ * Finds the head(s) of the release sequence(s) containing a given ModelAction.
+ * The ModelAction under consideration is expected to be taking part in
+ * release/acquire synchronization as an object of the "reads from" relation.
+ * Note that this can only provide release sequence support for RMW chains
+ * which do not read from the future, as those actions cannot be traced until
+ * their "promise" is fulfilled. Similarly, we may not even establish the
+ * presence of a release sequence with certainty, as some modification order
+ * constraints may be decided further in the future. Thus, this function
+ * "returns" two pieces of data: a pass-by-reference vector of @a release_heads
+ * and a boolean representing certainty.
+ *
+ * @todo Finish lazy updating, when promises are fulfilled in the future
+ * @param rf The action that might be part of a release sequence. Must be a
+ * write.
+ * @param release_heads A pass-by-reference style return parameter. After
+ * execution of this function, release_heads will contain the heads of all the
+ * relevant release sequences, if any exists
+ * @return true, if the ModelChecker is certain that release_heads is complete;
+ * false otherwise
+ */
+bool ModelChecker::release_seq_head(const ModelAction *rf, rel_heads_list_t *release_heads) const
+{
+ /* Only check for release sequences if there are no cycles */
+ if (mo_graph->checkForCycles())
+ return false;
+
+ while (rf) {
+ ASSERT(rf->is_write());
+
+ if (rf->is_release())
+ release_heads->push_back(rf);
+ if (!rf->is_rmw())
+ break; /* End of RMW chain */
+
+ /** @todo Need to be smarter here... In the linux lock
+ * example, this will run to the beginning of the program for
+ * every acquire. */
+ /** @todo The way to be smarter here is to keep going until 1
+ * thread has a release preceded by an acquire and you've seen
+ * both. */
+
+ /* acq_rel RMW is a sufficient stopping condition */
+ if (rf->is_acquire() && rf->is_release())
+ return true; /* complete */
+
+ rf = rf->get_reads_from();
+ };
+ if (!rf) {
+ /* read from future: need to settle this later */
+ return false; /* incomplete */
+ }
+
+ if (rf->is_release())
+ return true; /* complete */
+
+ /* else relaxed write; check modification order for contiguous subsequence
+ * -> rf must be same thread as release */
+ int tid = id_to_int(rf->get_tid());
+ std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(rf->get_location());
+ action_list_t *list = &(*thrd_lists)[tid];
+ action_list_t::const_reverse_iterator rit;
+
+ /* Find rf in the thread list */
+ rit = std::find(list->rbegin(), list->rend(), rf);
+ ASSERT(rit != list->rend());
+
+ /* Find the last write/release */
+ for (; rit != list->rend(); rit++)
+ if ((*rit)->is_release())
+ break;
+ if (rit == list->rend()) {
+ /* No write-release in this thread */
+ return true; /* complete */
+ }
+ ModelAction *release = *rit;
+
+ ASSERT(rf->same_thread(release));
+
+ bool certain = true;
+ for (unsigned int i = 0; i < thrd_lists->size(); i++) {
+ if (id_to_int(rf->get_tid()) == (int)i)
+ continue;
+ list = &(*thrd_lists)[i];
+
+ /* Can we ensure no future writes from this thread may break
+ * the release seq? */
+ bool future_ordered = false;
+
+ ModelAction *last = get_last_action(int_to_id(i));
+ if (last && (rf->happens_before(last) ||
+ last->get_type() == THREAD_FINISH))
+ future_ordered = true;
+
+ for (rit = list->rbegin(); rit != list->rend(); rit++) {
+ const ModelAction *act = *rit;
+ /* Reach synchronization -> this thread is complete */
+ if (act->happens_before(release))
+ break;
+ if (rf->happens_before(act)) {
+ future_ordered = true;
+ continue;
+ }
+
+ /* Only writes can break release sequences */
+ if (!act->is_write())
+ continue;
+
+ /* Check modification order */
+ if (mo_graph->checkReachable(rf, act)) {
+ /* rf --mo--> act */
+ future_ordered = true;
+ continue;
+ }
+ if (mo_graph->checkReachable(act, release))
+ /* act --mo--> release */
+ break;
+ if (mo_graph->checkReachable(release, act) &&
+ mo_graph->checkReachable(act, rf)) {
+ /* release --mo-> act --mo--> rf */
+ return true; /* complete */
+ }
+ certain = false;
+ }
+ if (!future_ordered)
+ return false; /* This thread is uncertain */
+ }
+
+ if (certain)
+ release_heads->push_back(release);
+ return certain;
+}
+
+/**
+ * A public interface for getting the release sequence head(s) with which a
+ * given ModelAction must synchronize. This function only returns a non-empty
+ * result when it can locate a release sequence head with certainty. Otherwise,
+ * it may mark the internal state of the ModelChecker so that it will handle
+ * the release sequence at a later time, causing @a act to update its
+ * synchronization at some later point in execution.
+ * @param act The 'acquire' action that may read from a release sequence
+ * @param release_heads A pass-by-reference return parameter. Will be filled
+ * with the head(s) of the release sequence(s), if they exists with certainty.
+ * @see ModelChecker::release_seq_head
+ */
+void ModelChecker::get_release_seq_heads(ModelAction *act, rel_heads_list_t *release_heads)
+{
+ const ModelAction *rf = act->get_reads_from();
+ bool complete;
+ complete = release_seq_head(rf, release_heads);
+ if (!complete) {
+ /* add act to 'lazy checking' list */
+ pending_acq_rel_seq->push_back(act);
+ }
+}
+
+/**
+ * Attempt to resolve all stashed operations that might synchronize with a
+ * release sequence for a given location. This implements the "lazy" portion of
+ * determining whether or not a release sequence was contiguous, since not all
+ * modification order information is present at the time an action occurs.
+ *
+ * @param location The location/object that should be checked for release
+ * sequence resolutions. A NULL value means to check all locations.
+ * @param work_queue The work queue to which to add work items as they are
+ * generated
+ * @return True if any updates occurred (new synchronization, new mo_graph
+ * edges)
+ */
+bool ModelChecker::resolve_release_sequences(void *location, work_queue_t *work_queue)
+{
+ bool updated = false;
+ std::vector<ModelAction *>::iterator it = pending_acq_rel_seq->begin();
+ while (it != pending_acq_rel_seq->end()) {
+ ModelAction *act = *it;
+
+ /* Only resolve sequences on the given location, if provided */
+ if (location && act->get_location() != location) {
+ it++;
+ continue;
+ }
+
+ const ModelAction *rf = act->get_reads_from();
+ rel_heads_list_t release_heads;
+ bool complete;
+ complete = release_seq_head(rf, &release_heads);
+ for (unsigned int i = 0; i < release_heads.size(); i++) {
+ if (!act->has_synchronized_with(release_heads[i])) {
+ updated = true;
+ act->synchronize_with(release_heads[i]);
+ }
+ }
+
+ if (updated) {
+ /* Re-check act for mo_graph edges */
+ work_queue->push_back(MOEdgeWorkEntry(act));
+
+ /* propagate synchronization to later actions */
+ action_list_t::reverse_iterator rit = action_trace->rbegin();
+ for (; (*rit) != act; rit++) {
+ ModelAction *propagate = *rit;
+ if (act->happens_before(propagate)) {
+ propagate->synchronize_with(act);
+ /* Re-check 'propagate' for mo_graph edges */
+ work_queue->push_back(MOEdgeWorkEntry(propagate));
+ }
+ }
+ }
+ if (complete)
+ it = pending_acq_rel_seq->erase(it);
+ else
+ it++;
+ }
+
+ // If we resolved promises or data races, see if we have realized a data race.
+ if (checkDataRaces()) {
+ set_assert();
+ }
+
+ return updated;
+}
+
+/**
+ * Performs various bookkeeping operations for the current ModelAction. For
+ * instance, adds action to the per-object, per-thread action vector and to the
+ * action trace list of all thread actions.
+ *
+ * @param act is the ModelAction to add.
+ */