+ delete act;
+ return 0;
+ }
+ DBG();
+ Thread *old = thread_current();
+ ASSERT(!old->get_pending());
+
+ if (inspect_plugin != NULL) {
+ inspect_plugin->inspectModelAction(act);
+ }
+
+ old->set_pending(act);
+
+ curr_thread_num++;
+ Thread* next = getNextThread();
+ if (next != nullptr)
+ handleNewValidThread(old, next);
+ else
+ handleChosenThread(old);
+
+ return old->get_return_value();
+}
+
+void ModelChecker::handleNewValidThread(Thread *old, Thread *next)
+{
+ if (old->is_waiting_on(old))
+ assert_bug("Deadlock detected (thread %u)", curr_thread_num-1);
+
+ ModelAction *act = old->get_pending();
+
+ if (act && execution->is_enabled(old) && !execution->check_action_enabled(act)) {
+ scheduler->sleep(old);
+ }
+ chooseThread(act, old);
+
+ scheduler->set_current_thread(next);
+
+ if (Thread::swap(old, next) < 0) {
+ perror("swap threads");
+ exit(EXIT_FAILURE);
+ }
+}
+
+void ModelChecker::handleChosenThread(Thread *old)
+{
+ if (execution->has_asserted())
+ finishExecution(old);
+ if (!chosen_thread)
+ chosen_thread = get_next_thread();
+ if (!chosen_thread || chosen_thread->is_model_thread())
+ finishExecution(old);
+ if (chosen_thread->just_woken_up()) {
+ chosen_thread->set_wakeup_state(false);
+ chosen_thread->set_pending(NULL);
+ chosen_thread = NULL;
+ // Allow this thread to stash the next pending action
+ if (should_terminate_execution())
+ finishExecution(old);
+ else
+ continueExecution(old);
+ } else {
+ /* Consume the next action for a Thread */
+ consumeAction();
+
+ if (should_terminate_execution())
+ finishExecution(old);
+ else
+ continueExecution(old);
+ }
+}
+
+static void runChecker() {
+ model->run();
+ delete model;
+}
+
+void ModelChecker::startChecker() {
+ startExecution(get_system_context(), runChecker);
+ snapshot = take_snapshot();
+ initMainThread();
+}
+
+bool ModelChecker::should_terminate_execution()
+{
+ if (execution->have_bug_reports()) {
+ execution->set_assert();
+ return true;
+ } else if (execution->isFinished()) {
+ return true;