+ 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,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;
+}
+
+/** 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::mo_may_allow(const ModelAction * writer, const ModelAction *reader) {
+ std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(reader->get_location());
+
+ //Get write that follows reader action
+ action_list_t *list = &(*thrd_lists)[id_to_int(reader->get_tid())];
+ action_list_t::reverse_iterator rit;
+ ModelAction *first_write_after_read=NULL;
+
+ for (rit = list->rbegin(); rit != list->rend(); rit++) {
+ ModelAction *act = *rit;
+ if (act==reader)
+ break;
+ if (act->is_write())
+ first_write_after_read=act;
+ }
+
+ if (first_write_after_read==NULL)
+ return true;
+
+ return !mo_graph->checkReachable(first_write_after_read, writer);
+}
+
+
+
+/**
+ * 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.
+ *
+ * @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 with certainty
+ * @param pending A pass-by-reference style return parameter which is only used
+ * when returning false (i.e., uncertain). Returns most information regarding
+ * an uncertain release sequence, including any write operations that might
+ * break the sequence.
+ * @return true, if the ModelChecker is certain that release_heads is complete;
+ * false otherwise
+ */
+bool ModelChecker::release_seq_heads(const ModelAction *rf,
+ rel_heads_list_t *release_heads,
+ struct release_seq *pending) 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 */
+ pending->rf = NULL;
+ 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));
+
+ pending->writes.clear();
+
+ 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));
+ Thread *th = get_thread(int_to_id(i));
+ if ((last && rf->happens_before(last)) ||
+ !scheduler->is_enabled(th) ||
+ th->is_complete())
+ future_ordered = true;
+
+ ASSERT(!th->is_model_thread() || future_ordered);
+
+ 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 */
+ }
+ /* act may break release sequence */
+ pending->writes.push_back(act);
+ certain = false;
+ }
+ if (!future_ordered)
+ certain = false; /* This thread is uncertain */
+ }
+
+ if (certain) {
+ release_heads->push_back(release);
+ pending->writes.clear();
+ } else {
+ pending->release = release;
+ pending->rf = rf;
+ }
+ 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_heads
+ */
+void ModelChecker::get_release_seq_heads(ModelAction *act, rel_heads_list_t *release_heads)
+{
+ const ModelAction *rf = act->get_reads_from();
+ struct release_seq *sequence = (struct release_seq *)snapshot_calloc(1, sizeof(struct release_seq));
+ sequence->acquire = act;
+
+ if (!release_seq_heads(rf, release_heads, sequence)) {
+ /* add act to 'lazy checking' list */
+ pending_rel_seqs->push_back(sequence);
+ } else {
+ snapshot_free(sequence);
+ }
+}
+
+/**
+ * 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< struct release_seq *, SnapshotAlloc<struct release_seq *> >::iterator it = pending_rel_seqs->begin();
+ while (it != pending_rel_seqs->end()) {
+ struct release_seq *pending = *it;
+ ModelAction *act = pending->acquire;
+
+ /* 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_heads(rf, &release_heads, pending);
+ for (unsigned int i = 0; i < release_heads.size(); i++) {
+ if (!act->has_synchronized_with(release_heads[i])) {
+ if (act->synchronize_with(release_heads[i]))
+ updated = true;
+ else
+ set_bad_synchronization();