+ } else {
+ chosen_thread->set_pending(NULL);
+ chosen_thread = execution->take_step(curr);
+ }
+}
+
+void ModelChecker::chooseThread(ModelAction *act, Thread *thr)
+{
+ if (!thread_chosen && act && execution->is_enabled(thr) && (thr->get_state() != THREAD_BLOCKED) ) {
+ if (act->is_write()) {
+ std::memory_order order = act->get_mo();
+ if (order == std::memory_order_relaxed || \
+ order == std::memory_order_release) {
+ chosen_thread = thr;
+ thread_chosen = true;
+ }
+ } else if (act->get_type() == THREAD_CREATE || \
+ act->get_type() == PTHREAD_CREATE || \
+ act->get_type() == THREAD_START || \
+ act->get_type() == THREAD_FINISH) {
+ chosen_thread = thr;
+ thread_chosen = true;
+ }
+ }
+}
+
+uint64_t ModelChecker::switch_thread(ModelAction *act)
+{
+ if (modellock) {
+ static bool fork_message_printed = false;
+
+ if (!fork_message_printed) {
+ model_print("Fork handler or dead thread trying to call into model checker...\n");
+ fork_message_printed = true;
+ }
+ 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);
+
+ if (old->is_waiting_on(old))
+ assert_bug("Deadlock detected (thread %u)", curr_thread_num);
+
+ if (act && execution->is_enabled(old) && !execution->check_action_enabled(act)) {
+ scheduler->sleep(old);
+ }
+ chooseThread(act, old);
+
+ curr_thread_num++;
+ Thread* next = getNextThread();
+ if (next != nullptr)
+ handleNewValidThread(old, next);
+ else {
+ old->set_state(THREAD_READY); // Just to avoid the first ASSERT in ModelExecution::take_step
+ handleChosenThread(old);
+ }
+
+ return old->get_return_value();
+}
+
+void ModelChecker::handleNewValidThread(Thread *old, Thread *next)
+{
+ 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())
+ finishRunExecution(old);
+ if (!chosen_thread)
+ chosen_thread = get_next_thread();
+ if (!chosen_thread || chosen_thread->is_model_thread())
+ finishRunExecution(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())
+ finishRunExecution(old);
+ else
+ continueRunExecution(old);
+ } else {
+ /* Consume the next action for a Thread */
+ consumeAction();
+
+ if (should_terminate_execution())
+ finishRunExecution(old);
+ else
+ continueRunExecution(old);
+ }
+}
+
+void ModelChecker::handleChosenThread(ucontext_t *old)
+{
+ if (execution->has_asserted())
+ finishRunExecution(old);
+ if (!chosen_thread)
+ chosen_thread = get_next_thread();
+ if (!chosen_thread || chosen_thread->is_model_thread())
+ finishRunExecution(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())
+ finishRunExecution(old);
+ else
+ startRunExecution(old);
+ } else
+
+ {
+ /* Consume the next action for a Thread */
+ consumeAction();
+
+ if (should_terminate_execution())
+ finishRunExecution(old);
+ else
+ startRunExecution(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;