]> git.tdb.fi Git - r2c2.git/commitdiff
Make deadlock detection more robust
authorMikko Rasa <tdb@tdb.fi>
Sun, 30 Mar 2014 13:43:54 +0000 (16:43 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 30 Mar 2014 16:35:54 +0000 (19:35 +0300)
Deadlock cycles involving more than two trains can now be detected.

source/libr2c2/trainrouteplanner.cpp

index 45e83e267fb2b37e8b260630442e4a055089f667..e8b5d90107b59b7dad25a455d0f7d4bc678c2a5b 100644 (file)
@@ -336,17 +336,24 @@ bool TrainRoutePlanner::RoutingStep::update_states()
 
 bool TrainRoutePlanner::RoutingStep::check_deadlocks() const
 {
-       for(unsigned i=0; i<trains.size(); ++i)
+       for(vector<TrainRoutingState>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
        {
-               const TrainRoutingState &train = trains[i];
-               if(train.state!=BLOCKED)
+               if(i->state!=BLOCKED)
                        continue;
 
-               if(train.blocked_by<0)
+               if(i->blocked_by<0)
                        return true;
 
-               if(trains[train.blocked_by].blocked_by==static_cast<int>(i))
-                       return true;
+               int slow = i->blocked_by;
+               int fast = trains[slow].blocked_by;
+               while(fast>=0 && trains[fast].blocked_by>=0)
+               {
+                       if(fast==slow)
+                               return true;
+
+                       slow = trains[slow].blocked_by;
+                       fast = trains[trains[fast].blocked_by].blocked_by;
+               }
        }
 
        return false;