fix getNextThread func
[c11tester.git] / model.cc
index d3692c47c80c5731cc32e9bddc920aa5a7d8bdad..ac394d9a49ec9f164051c04328fe35af04e22d85 100644 (file)
--- a/model.cc
+++ b/model.cc
@@ -366,13 +366,14 @@ void ModelChecker::continueExecution(Thread *old)
 
 Thread* ModelChecker::getNextThread()
 {
-       Thread *thr = nullptr;
+       Thread *nextThread = nullptr;
        for (unsigned int i = curr_thread_num; i < get_num_threads(); i++) {
                thread_id_t tid = int_to_id(i);
-               thr = get_thread(tid);
+               Thread *thr = get_thread(tid);
                
                if (!thr->is_complete() && !thr->get_pending()) {
                        curr_thread_num = i;
+                       nextThread = thr;
                        break;
                }
                ModelAction *act = thr->get_pending();
@@ -383,7 +384,7 @@ Thread* ModelChecker::getNextThread()
 
                chooseThread(act, thr);
        }
-       return thr;
+       return nextThread;
 }
 
 void ModelChecker::finishExecution(Thread *old) 
@@ -402,21 +403,21 @@ void ModelChecker::consumeAction()
        chosen_thread = execution->take_step(curr);
 }
 
-void ModelChecker::chooseThread(ModelAction *act, Thread *old)
+void ModelChecker::chooseThread(ModelAction *act, Thread *thr)
 {
-       if (!thread_chosen && act && execution->is_enabled(old) && (old->get_state() != THREAD_BLOCKED) ) {
+       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 = old;
+                               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 = old;
+                       chosen_thread = thr;
                        thread_chosen = true;
                }
        }       
@@ -443,6 +444,16 @@ uint64_t ModelChecker::switch_thread(ModelAction *act)
        }
 
        old->set_pending(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)) {
+               scheduler->sleep(old);
+       }
+       chooseThread(act2, old);
 
        curr_thread_num++;
        Thread* next = getNextThread();
@@ -456,16 +467,6 @@ uint64_t ModelChecker::switch_thread(ModelAction *act)
 
 void ModelChecker::handleNewValidThread(Thread *old, Thread *next)
 {
-       if (old->is_waiting_on(old))
-               assert_bug("Deadlock detected (thread %u)", curr_thread_num-1);
-
-       ModelAction *act = old->get_pending();
-               
-       if (act && execution->is_enabled(old) && !execution->check_action_enabled(act)) {
-               scheduler->sleep(old);
-       }
-       chooseThread(act, old);
-
        scheduler->set_current_thread(next);    
 
        if (Thread::swap(old, next) < 0) {
@@ -537,7 +538,12 @@ void ModelChecker::run()
                curr_thread_num = 1;
                thread_id_t tid = int_to_id(1);
                Thread *thr = get_thread(tid);
-               switch_from_master(thr);
+               if (!thr->get_pending())
+                       switch_from_master(thr);
+               else {
+                       consumeAction();
+                       switch_from_master(thr);
+               }
                finish_execution((exec+1) < params.maxexecutions);
                //restore random number generator state after rollback
                setstate(random_state);