params(params),
current_action(NULL),
diverge(NULL),
- nextThread(THREAD_ID_T_NONE),
+ nextThread(NULL),
action_trace(new action_list_t()),
thread_map(new HashTable<int, Thread *, int>()),
obj_map(new HashTable<const void *, action_list_t, uintptr_t, 4>()),
obj_thrd_map(new HashTable<void *, std::vector<action_list_t>, uintptr_t, 4 >()),
promises(new std::vector<Promise *>()),
+ lazy_sync_with_release(new HashTable<void *, std::list<ModelAction *>, uintptr_t, 4>()),
thrd_last_action(new std::vector<ModelAction *>(1)),
node_stack(new NodeStack()),
next_backtrack(NULL),
delete (*promises)[i];
delete promises;
+ delete lazy_sync_with_release;
+
delete thrd_last_action;
delete node_stack;
delete scheduler;
current_action = NULL;
next_thread_id = INITIAL_THREAD_ID;
used_sequence_numbers = 0;
- nextThread = 0;
+ nextThread = NULL;
next_backtrack = NULL;
failed_promise = false;
snapshotObject->backTrackBeforeStep(0);
return ++used_sequence_numbers;
}
-/**
- * Performs the "scheduling" for the model-checker. That is, it checks if the
- * model-checker has selected a "next thread to run" and returns it, if
- * available. This function should be called from the Scheduler routine, where
- * the Scheduler falls back to a default scheduling routine if needed.
- *
- * @return The next thread chosen by the model-checker. If the model-checker
- * makes no selection, retuns NULL.
- */
-Thread * ModelChecker::schedule_next_thread()
-{
- Thread *t;
- if (nextThread == THREAD_ID_T_NONE)
- return NULL;
- t = thread_map->get(id_to_int(nextThread));
-
- ASSERT(t != NULL);
-
- return t;
-}
-
/**
* Choose the next thread in the replay sequence.
*
* from the backtracking set. Otherwise, simply returns the next thread in the
* sequence that is being replayed.
*/
-thread_id_t ModelChecker::get_next_replay_thread()
+Thread * ModelChecker::get_next_replay_thread()
{
thread_id_t tid;
/* Have we completed exploring the preselected path? */
if (diverge == NULL)
- return THREAD_ID_T_NONE;
+ return NULL;
/* Else, we are trying to replay an execution */
ModelAction *next = node_stack->get_next()->get_action();
tid = next->get_tid();
}
DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
- return tid;
+ ASSERT(tid != THREAD_ID_T_NONE);
+ return thread_map->get(id_to_int(tid));
}
/**
return next;
}
-void ModelChecker::check_current_action(void)
+Thread * ModelChecker::check_current_action(ModelAction *curr)
{
- ModelAction *curr = this->current_action;
bool already_added = false;
- this->current_action = NULL;
- if (!curr) {
- DEBUG("trying to push NULL action...\n");
- return;
- }
+
+ ASSERT(curr);
if (curr->is_rmwc() || curr->is_rmw()) {
ModelAction *tmp = process_rmw(curr);
/* Assign reads_from values */
Thread *th = get_thread(curr->get_tid());
uint64_t value = VALUE_NONE;
+ bool updated = false;
if (curr->is_read()) {
const ModelAction *reads_from = curr->get_node()->get_read_from();
if (reads_from != NULL) {
value = reads_from->get_value();
/* Assign reads_from, perform release/acquire synchronization */
curr->read_from(reads_from);
- r_modification_order(curr,reads_from);
+ if (r_modification_order(curr,reads_from))
+ updated = true;
} else {
/* Read from future value */
value = curr->get_node()->get_future_value();
promises->push_back(valuepromise);
}
} else if (curr->is_write()) {
- w_modification_order(curr);
- resolve_promises(curr);
+ if (w_modification_order(curr))
+ updated = true;;
+ if (resolve_promises(curr))
+ updated = true;
}
+ if (updated)
+ resolve_release_sequences(curr->get_location());
+
th->set_return_value(value);
/* Add action to list. */
if (!already_added)
add_action_to_lists(curr);
- /** @todo Is there a better interface for setting the next thread rather
- than this field/convoluted approach? Perhaps like just returning
- it or something? */
-
- /* Do not split atomic actions. */
- if (curr->is_rmwr())
- nextThread = thread_current()->get_id();
- else
- nextThread = get_next_replay_thread();
-
Node *currnode = curr->get_node();
Node *parnode = currnode->get_parent();
next_backtrack = curr;
set_backtracking(curr);
+
+ /* Do not split atomic actions. */
+ if (curr->is_rmwr())
+ return thread_current();
+ else
+ return get_next_replay_thread();
}
/** @returns whether the current partial trace is feasible. */
* Updates the mo_graph with the constraints imposed from the current read.
* @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
*/
-void ModelChecker::r_modification_order(ModelAction * curr, const ModelAction *rf) {
+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 */
if (act->happens_before(curr)) {
if (act->is_read()) {
const ModelAction *prevreadfrom = act->get_reads_from();
- if (prevreadfrom != NULL && rf != prevreadfrom)
+ if (prevreadfrom != NULL && rf != prevreadfrom) {
mo_graph->addEdge(prevreadfrom, rf);
+ added = true;
+ }
} else if (rf != act) {
mo_graph->addEdge(act, rf);
+ added = true;
}
break;
}
}
}
+
+ return added;
}
/** Updates the mo_graph with the constraints imposed from the current read. */
/**
* Updates the mo_graph with the constraints imposed from the current write.
* @param curr The current action. Must be a write.
+ * @return True if modification order edges were added; false otherwise
*/
-void ModelChecker::w_modification_order(ModelAction * curr) {
+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->get_location());
- if (last_seq_cst != NULL)
+ if (last_seq_cst != NULL) {
mo_graph->addEdge(last_seq_cst, curr);
+ added = true;
+ }
}
/* Iterate over all threads */
mo_graph->addEdge(act->get_reads_from(), curr);
else
mo_graph->addEdge(act, curr);
+ added = true;
break;
} else if (act->is_read() && !act->is_synchronizing(curr) &&
!act->same_thread(curr)) {
}
}
}
+
+ return added;
+}
+
+/**
+ * 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,
+ std::vector<const ModelAction *> *release_heads) const
+{
+ ASSERT(rf->is_write());
+ if (!rf) {
+ /* read from future: need to settle this later */
+ return false; /* incomplete */
+ }
+ if (rf->is_release())
+ release_heads->push_back(rf);
+ if (rf->is_rmw()) {
+ if (rf->is_acquire())
+ return true; /* complete */
+ return release_seq_head(rf->get_reads_from(), release_heads);
+ }
+ 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 */
+ for (rit = list->rbegin(); rit != list->rend(); rit++)
+ if (*rit == rf)
+ break;
+
+ /* 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];
+ for (rit = list->rbegin(); rit != list->rend(); rit++) {
+ const ModelAction *act = *rit;
+ if (!act->is_write())
+ continue;
+ /* Reach synchronization -> this thread is complete */
+ if (act->happens_before(release))
+ break;
+ if (rf->happens_before(act))
+ continue;
+
+ /* Check modification order */
+ if (mo_graph->checkReachable(rf, act))
+ /* rf --mo--> act */
+ 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 (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,
+ std::vector<const ModelAction *> *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 */
+ std::list<ModelAction *> *list;
+ list = lazy_sync_with_release->get_safe_ptr(act->get_location());
+ list->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
+ * @return True if any updates occurred (new synchronization, new mo_graph edges)
+ */
+bool ModelChecker::resolve_release_sequences(void *location)
+{
+ std::list<ModelAction *> *list;
+ list = lazy_sync_with_release->getptr(location);
+ if (!list)
+ return false;
+
+ bool updated = false;
+ std::list<ModelAction *>::iterator it = list->begin();
+ while (it != list->end()) {
+ ModelAction *act = *it;
+ const ModelAction *rf = act->get_reads_from();
+ std::vector<const ModelAction *> 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) {
+ /* propagate synchronization to later actions */
+ action_list_t::reverse_iterator it = action_trace->rbegin();
+ while ((*it) != act) {
+ ModelAction *propagate = *it;
+ if (act->happens_before(propagate))
+ /** @todo new mo_graph edges along with
+ * this synchronization? */
+ propagate->synchronize_with(act);
+ }
+ }
+ if (complete)
+ it = list->erase(it);
+ else
+ it++;
+ }
+
+ return updated;
}
/**
* Resolve a set of Promises with a current write. The set is provided in the
* Node corresponding to @a write.
* @param write The ModelAction that is fulfilling Promises
+ * @return True if promises were resolved; false otherwise
*/
-void ModelChecker::resolve_promises(ModelAction *write)
+bool ModelChecker::resolve_promises(ModelAction *write)
{
+ bool resolved = false;
for (unsigned int i = 0, promise_index = 0; promise_index < promises->size(); i++) {
Promise *promise = (*promises)[promise_index];
if (write->get_node()->get_promise(i)) {
r_modification_order(read, write);
post_r_modification_order(read, write);
promises->erase(promises->begin() + promise_index);
+ resolved = true;
} else
promise_index++;
}
+ return resolved;
}
/**
curr = thread_current();
if (curr) {
if (curr->get_state() == THREAD_READY) {
- check_current_action();
+ if (current_action) {
+ nextThread = check_current_action(current_action);
+ current_action = NULL;
+ }
scheduler->add_thread(curr);
} else if (curr->get_state() == THREAD_RUNNING) {
/* Stopped while running; i.e., completed */
ASSERT(false);
}
}
- next = scheduler->next_thread();
+ next = scheduler->next_thread(nextThread);
/* Infeasible -> don't take any more steps */
if (!isfeasible())