+ 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);
+ return;
+ }
+ if (!chosen_thread)
+ chosen_thread = get_next_thread();
+ if (!chosen_thread || chosen_thread->is_model_thread()) {
+ finishRunExecution(old);
+ return;
+ }
+ 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;
+ }
+ return false;
+}
+
+/** @brief Run ModelChecker for the user program */
+void ModelChecker::run()
+{
+ //Need to initial random number generator state to avoid resets on rollback
+ char random_state[256];
+ initstate(423121, random_state, sizeof(random_state));
+ checkfree = params.checkthreshold;
+ for(int exec = 0;exec < params.maxexecutions;exec++) {
+ chosen_thread = init_thread;
+ break_execution = false;
+ do {
+ if (break_execution)