+ 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;
+ }
+ 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;
+ thread_chosen = false;
+ curr_thread_num = 1;
+ thread_id_t tid = int_to_id(1);
+ Thread *thr = get_thread(tid);
+ switch_from_master(thr);
+ finish_execution((exec+1) < params.maxexecutions);
+ //restore random number generator state after rollback
+ setstate(random_state);
+ }