add consume to run
[c11tester.git] / model.cc
index 53bb7b0d04b5f0ba6a34d1a9ed24218acdfbdbe3..a1204d0ab852352600edb95063476bd6f2976fa7 100644 (file)
--- a/model.cc
+++ b/model.cc
@@ -344,24 +344,70 @@ uint64_t ModelChecker::switch_to_master(ModelAction *act)
        return old->get_return_value();
 }
 
-void ModelChecker::continueExecution(Thread *old) 
+void ModelChecker::continueRunExecution(Thread *old) 
 {
        if (params.traceminsize != 0 &&
                        execution->get_curr_seq_num() > checkfree) {
                checkfree += params.checkthreshold;
                execution->collectActions();
        }
+       thread_chosen = false;
        curr_thread_num = 1;
-       thread_id_t tid = int_to_id(1);
-       Thread *thr = get_thread(tid);
-       scheduler->set_current_thread(thr);
-       if (Thread::swap(old, thr) < 0) {
-               perror("swap threads");
-               exit(EXIT_FAILURE);
+       Thread *thr = getNextThread();
+       if (thr != nullptr) {
+               scheduler->set_current_thread(thr);
+               if (Thread::swap(old, thr) < 0) {
+                       perror("swap threads");
+                       exit(EXIT_FAILURE);
+               }
+       } else
+               handleChosenThread(old);        
+}
+
+void ModelChecker::startRunExecution(ucontext_t *old) 
+{
+       if (params.traceminsize != 0 &&
+                       execution->get_curr_seq_num() > checkfree) {
+               checkfree += params.checkthreshold;
+               execution->collectActions();
        }
+       thread_chosen = false;
+       curr_thread_num = 1;
+       Thread *thr = getNextThread();
+       if (thr != nullptr) {
+               scheduler->set_current_thread(thr);
+               if (Thread::swap(old, thr) < 0) {
+                       perror("swap threads");
+                       exit(EXIT_FAILURE);
+               }
+       } else
+               handleChosenThread(old);        
 }
 
-void ModelChecker::finishExecution(Thread *old) 
+Thread* ModelChecker::getNextThread()
+{
+       Thread *nextThread = nullptr;
+       for (unsigned int i = curr_thread_num; i < get_num_threads(); i++) {
+               thread_id_t tid = int_to_id(i);
+               Thread *thr = get_thread(tid);
+               
+               if (!thr->is_complete() && !thr->get_pending()) {
+                       curr_thread_num = i;
+                       nextThread = thr;
+                       break;
+               }
+               ModelAction *act = thr->get_pending();
+               
+               if (act && execution->is_enabled(thr) && !execution->check_action_enabled(act)) {
+                       scheduler->sleep(thr);
+               }
+
+               chooseThread(act, thr);
+       }
+       return nextThread;
+}
+
+void ModelChecker::finishRunExecution(Thread *old) 
 {
        scheduler->set_current_thread(NULL);
        if (Thread::swap(old, &system_context) < 0) {
@@ -370,6 +416,11 @@ void ModelChecker::finishExecution(Thread *old)
        }
 }
 
+void ModelChecker::finishRunExecution(ucontext_t *old) 
+{
+       scheduler->set_current_thread(NULL);
+}
+
 void ModelChecker::consumeAction()
 {
        ModelAction *curr = chosen_thread->get_pending();
@@ -377,6 +428,26 @@ void ModelChecker::consumeAction()
        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) {
@@ -398,17 +469,7 @@ uint64_t ModelChecker::switch_thread(ModelAction *act)
        }
 
        old->set_pending(act);
-
-       Thread *next = NULL;
-       curr_thread_num++;
-       while (curr_thread_num < get_num_threads()) {
-               thread_id_t tid = int_to_id(curr_thread_num);
-               next = get_thread(tid);
-               if (!next->is_complete() && !next->get_pending())
-                       break;
-               curr_thread_num++;
-       }
-
+       
        if (old->is_waiting_on(old))
                assert_bug("Deadlock detected (thread %u)", curr_thread_num);
 
@@ -417,61 +478,85 @@ uint64_t ModelChecker::switch_thread(ModelAction *act)
        if (act2 && execution->is_enabled(old) && !execution->check_action_enabled(act2)) {
                scheduler->sleep(old);
        }
-       if (!thread_chosen && act2 && execution->is_enabled(old) && (old->get_state() != THREAD_BLOCKED) ) {
-               if (act2->is_write()) {
-                       std::memory_order order = act2->get_mo();
-                       if (order == std::memory_order_relaxed || \
-                                       order == std::memory_order_release) {
-                               chosen_thread = old;
-                               thread_chosen = true;
-                       }
-               } else if (act2->get_type() == THREAD_CREATE || \
-                                                       act2->get_type() == PTHREAD_CREATE || \
-                                                       act2->get_type() == THREAD_START || \
-                                                       act2->get_type() == THREAD_FINISH) {
-                       chosen_thread = old;
-                       thread_chosen = true;
-               }
-       }
+       chooseThread(act2, old);
+
+       curr_thread_num++;
+       Thread* next = getNextThread();
+       if (next != nullptr) 
+               handleNewValidThread(old, next);
+       else
+               handleChosenThread(old);
 
-       if (curr_thread_num < get_num_threads()) {
-               scheduler->set_current_thread(next);    
+       return old->get_return_value();
+}
 
-               if (Thread::swap(old, next) < 0) {
-                       perror("swap threads");
-                       exit(EXIT_FAILURE);
-               }               
+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 {
-               
-               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);         
-               }               
-               
+               /* Consume the next action for a Thread */
+               consumeAction();
+
+               if (should_terminate_execution())
+                       finishRunExecution(old);
+               else
+                       continueRunExecution(old);              
        }
-       return old->get_return_value();
 }
 
+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;
@@ -505,9 +590,7 @@ void ModelChecker::run()
                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);
+               startRunExecution(&system_context);
                finish_execution((exec+1) < params.maxexecutions);
                //restore random number generator state after rollback
                setstate(random_state);