From: Mikko Rasa Date: Sun, 30 Mar 2014 13:43:54 +0000 (+0300) Subject: Make deadlock detection more robust X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=932c76bc0358eeaf1c18cc950201226262d0df17;p=r2c2.git Make deadlock detection more robust Deadlock cycles involving more than two trains can now be detected. --- diff --git a/source/libr2c2/trainrouteplanner.cpp b/source/libr2c2/trainrouteplanner.cpp index 45e83e2..e8b5d90 100644 --- a/source/libr2c2/trainrouteplanner.cpp +++ b/source/libr2c2/trainrouteplanner.cpp @@ -336,17 +336,24 @@ bool TrainRoutePlanner::RoutingStep::update_states() bool TrainRoutePlanner::RoutingStep::check_deadlocks() const { - for(unsigned i=0; i::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(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;