ModelChecker *model;
/** @brief Constructor */
-ModelChecker::ModelChecker() :
+ModelChecker::ModelChecker(struct model_params params) :
/* Initialize default scheduler */
scheduler(new Scheduler()),
/* First thread created will have id INITIAL_THREAD_ID */
next_thread_id(INITIAL_THREAD_ID),
used_sequence_numbers(0),
num_executions(0),
+ params(params),
current_action(NULL),
diverge(NULL),
nextThread(THREAD_ID_T_NONE),
num_executions++;
- bool feasible = isfinalfeasible();
- if (feasible || DBG_ENABLED())
- print_summary(feasible);
+ if (isfinalfeasible() || DBG_ENABLED())
+ print_summary();
if ((diverge = model->get_next_backtrack()) == NULL)
return false;
/* Include at most one act per-thread that "happens before" curr */
if (act->happens_before(curr)) {
- if (act->is_read()) {
+ if (act->is_read())
cyclegraph->addEdge(curr, act->get_reads_from());
- } else
+ else
cyclegraph->addEdge(curr, act);
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 (act->get_node()->add_future_value(curr->get_value())&&
- (!next_backtrack || *act > * next_backtrack))
- next_backtrack = act;
- }
+ } 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 (act->get_node()->add_future_value(curr->get_value()) &&
+ (!next_backtrack || *act > *next_backtrack))
+ next_backtrack = act;
}
}
}
printf("---------------------------------------------------------------------\n");
}
-void ModelChecker::print_summary(bool feasible)
+void ModelChecker::print_summary()
{
printf("\n");
printf("Number of executions: %d\n", num_executions);
scheduler->print();
- if (!feasible)
+ if (!isfinalfeasible())
printf("INFEASIBLE EXECUTION!\n");
print_list(action_trace);
printf("\n");
}
-int ModelChecker::add_thread(Thread *t)
+/**
+ * Add a Thread to the system for the first time. Should only be called once
+ * per thread.
+ * @param t The Thread to add
+ */
+void ModelChecker::add_thread(Thread *t)
{
thread_map->put(id_to_int(t->get_id()), t);
scheduler->add_thread(t);
- return 0;
}
void ModelChecker::remove_thread(Thread *t)
old->set_state(THREAD_READY);
return Thread::swap(old, get_system_context());
}
+
+/**
+ * Takes the next step in the execution, if possible.
+ * @return Returns true (success) if a step was taken and false otherwise.
+ */
+bool ModelChecker::take_step() {
+ Thread *curr, *next;
+
+ curr = thread_current();
+ if (curr) {
+ if (curr->get_state() == THREAD_READY) {
+ check_current_action();
+ scheduler->add_thread(curr);
+ } else if (curr->get_state() == THREAD_RUNNING) {
+ /* Stopped while running; i.e., completed */
+ curr->complete();
+ } else {
+ ASSERT(false);
+ }
+ }
+ next = scheduler->next_thread();
+
+ /* Infeasible -> don't take any more steps */
+ if (!isfeasible())
+ return false;
+
+ if (next)
+ next->set_state(THREAD_RUNNING);
+ DEBUG("(%d, %d)\n", curr ? curr->get_id() : -1, next ? next->get_id() : -1);
+
+ /* next == NULL -> don't take any more steps */
+ if (!next)
+ return false;
+ /* Return false only if swap fails with an error */
+ return (Thread::swap(get_system_context(), next) == 0);
+}
+
+/** Runs the current execution until threre are no more steps to take. */
+void ModelChecker::finish_execution() {
+ DBG();
+
+ while (take_step());
+}