uncomment out code
[c11tester.git] / model.cc
index a1204d0ab852352600edb95063476bd6f2976fa7..078a1ce75849ebc48334958a7842b22dd6c958d1 100644 (file)
--- a/model.cc
+++ b/model.cc
@@ -346,11 +346,13 @@ uint64_t ModelChecker::switch_to_master(ModelAction *act)
 
 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 *thr = getNextThread();
@@ -366,11 +368,13 @@ void ModelChecker::continueRunExecution(Thread *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();
@@ -402,7 +406,7 @@ Thread* ModelChecker::getNextThread()
                        scheduler->sleep(thr);
                }
 
-               chooseThread(act, thr);
+       chooseThread(act, thr);
        }
        return nextThread;
 }
@@ -424,8 +428,18 @@ void ModelChecker::finishRunExecution(ucontext_t *old)
 void ModelChecker::consumeAction()
 {
        ModelAction *curr = chosen_thread->get_pending();
-       chosen_thread->set_pending(NULL);
-       chosen_thread = execution->take_step(curr);
+       Thread * th = thread_current();
+       if (curr->get_type() == THREAD_FINISH && th != NULL)  {
+               // Thread finish must be consumed in the master context
+               scheduler->set_current_thread(NULL);
+               if (Thread::swap(th, &system_context) < 0) {
+                       perror("swap threads");
+                       exit(EXIT_FAILURE);
+               }
+       } else {
+               chosen_thread->set_pending(NULL);
+               chosen_thread = execution->take_step(curr);
+       }
 }
 
 void ModelChecker::chooseThread(ModelAction *act, Thread *thr)
@@ -473,19 +487,19 @@ uint64_t ModelChecker::switch_thread(ModelAction *act)
        if (old->is_waiting_on(old))
                assert_bug("Deadlock detected (thread %u)", curr_thread_num);
 
-       ModelAction *act2 = old->get_pending();
-               
-       if (act2 && execution->is_enabled(old) && !execution->check_action_enabled(act2)) {
+       if (act && execution->is_enabled(old) && !execution->check_action_enabled(act)) {
                scheduler->sleep(old);
        }
-       chooseThread(act2, old);
+       chooseThread(act, old);
 
        curr_thread_num++;
        Thread* next = getNextThread();
        if (next != nullptr) 
                handleNewValidThread(old, next);
-       else
+       else {
+               old->set_state(THREAD_READY);   // Just to avoid the first ASSERT in ModelExecution::take_step 
                handleChosenThread(old);
+       }
 
        return old->get_return_value();
 }
@@ -545,7 +559,9 @@ void ModelChecker::handleChosenThread(ucontext_t *old)
                        finishRunExecution(old);
                else
                        startRunExecution(old); 
-       } else {
+       } else
+
+       {
                /* Consume the next action for a Thread */
                consumeAction();
 
@@ -588,9 +604,12 @@ void ModelChecker::run()
        checkfree = params.checkthreshold;
        for(int exec = 0;exec < params.maxexecutions;exec++) {
                chosen_thread = init_thread;
-               thread_chosen = false;
-               curr_thread_num = 1;
-               startRunExecution(&system_context);
+               do {
+                       thread_chosen = false;
+                       curr_thread_num = 1;
+                       startRunExecution(&system_context);
+               } while (!should_terminate_execution());
+
                finish_execution((exec+1) < params.maxexecutions);
                //restore random number generator state after rollback
                setstate(random_state);