]> git.tdb.fi Git - r2c2.git/commitdiff
Make route planning threaded
authorMikko Rasa <tdb@tdb.fi>
Tue, 3 Feb 2015 21:47:17 +0000 (23:47 +0200)
committerMikko Rasa <tdb@tdb.fi>
Tue, 3 Feb 2015 21:47:17 +0000 (23:47 +0200)
It can take several seconds in some cases, which would cause an
unacceptable pause in simulation and control.

source/libr2c2/trainrouteplanner.cpp
source/libr2c2/trainrouteplanner.h
source/libr2c2/trainrouter.cpp

index 7ee655a1d435acff22ff55cf3ac19bbd38fa0830..b4f01ab6a860084901add49d3ab21de3405d49d0 100644 (file)
@@ -14,7 +14,9 @@ using namespace Msp;
 namespace R2C2 {
 
 TrainRoutePlanner::TrainRoutePlanner(Layout &layout):
-       result(PENDING)
+       goal(0),
+       result(PENDING),
+       thread(0)
 {
        const map<unsigned, Train *> &trains = layout.get_trains();
        for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
@@ -25,38 +27,40 @@ TrainRoutePlanner::TrainRoutePlanner(Layout &layout):
        }
 }
 
-void TrainRoutePlanner::plan()
+TrainRoutePlanner::~TrainRoutePlanner()
 {
-       steps.clear();
-       queue.clear();
-       result = PENDING;
+       delete thread;
+}
 
-       queue.push_back(RoutingStep());
-       RoutingStep &start = queue.back();
-       for(vector<TrainRoutingInfo>::iterator i=routed_trains.begin(); i!=routed_trains.end(); ++i)
-               start.trains.push_back(TrainRoutingState(*i));
-       start.update_estimate();
+TrainRoutePlanner::Result TrainRoutePlanner::plan()
+{
+       prepare_plan();
+       create_plan();
+       if(result==PENDING)
+               finalize_plan();
 
-       const RoutingStep *goal = 0;
-       while(!queue.empty())
-       {
-               const RoutingStep &step = get_step();
-               if(step.is_goal())
-               {
-                       goal = &step;
-                       break;
-               }
+       return result;
+}
 
-               add_steps(step);
-       }
+void TrainRoutePlanner::plan_async()
+{
+       if(thread)
+               throw logic_error("already planning");
+
+       prepare_plan();
+       thread = new PlanningThread(*this);
+}
 
-       if(goal)
+TrainRoutePlanner::Result TrainRoutePlanner::check()
+{
+       if(result==PENDING && goal)
        {
-               create_routes(*goal);
-               result = COMPLETE;
+               finalize_plan();
+               delete thread;
+               thread = 0;
        }
-       else
-               result = FAILED;
+
+       return result;
 }
 
 const list<Route *> &TrainRoutePlanner::get_routes_for(const Train &train) const
@@ -84,6 +88,37 @@ const TrainRoutePlanner::RoutingStep &TrainRoutePlanner::get_step()
        return steps.back();
 }
 
+void TrainRoutePlanner::prepare_plan()
+{
+       steps.clear();
+       queue.clear();
+       goal = 0;
+       result = PENDING;
+
+       queue.push_back(RoutingStep());
+       RoutingStep &start = queue.back();
+       for(vector<TrainRoutingInfo>::iterator i=routed_trains.begin(); i!=routed_trains.end(); ++i)
+               start.trains.push_back(TrainRoutingState(*i));
+       start.update_estimate();
+}
+
+void TrainRoutePlanner::create_plan()
+{
+       while(!queue.empty())
+       {
+               const RoutingStep &step = get_step();
+               if(step.is_goal())
+               {
+                       goal = &step;
+                       return;
+               }
+
+               add_steps(step);
+       }
+
+       result = FAILED;
+}
+
 void TrainRoutePlanner::add_steps(const RoutingStep &step)
 {
        list<RoutingStep> new_steps;
@@ -92,7 +127,7 @@ void TrainRoutePlanner::add_steps(const RoutingStep &step)
        queue.merge(new_steps);
 }
 
-void TrainRoutePlanner::create_routes(const RoutingStep &goal)
+void TrainRoutePlanner::finalize_plan()
 {
        for(vector<TrainRoutingInfo>::iterator i=routed_trains.begin(); i!=routed_trains.end(); ++i)
        {
@@ -104,7 +139,7 @@ void TrainRoutePlanner::create_routes(const RoutingStep &goal)
 
        map<Track *, TrainRouter::SequencePoint *> sequenced_tracks;
        unsigned sequence = steps.size();
-       for(const RoutingStep *i=&goal; i; i=i->prev)
+       for(const RoutingStep *i=goal; i; i=i->prev)
                for(vector<TrainRoutingState>::const_iterator j=i->trains.begin(); j!=i->trains.end(); ++j)
                {
                        Track **history = j->info->track_history;
@@ -166,6 +201,8 @@ void TrainRoutePlanner::create_routes(const RoutingStep &goal)
                                --sequence;
                        }
                }
+
+       result = COMPLETE;
 }
 
 
@@ -548,4 +585,16 @@ bool TrainRoutePlanner::RoutingStep::operator<(const RoutingStep &other) const
        return cost_estimate<other.cost_estimate;
 }
 
+
+TrainRoutePlanner::PlanningThread::PlanningThread(TrainRoutePlanner &p):
+       planner(p)
+{
+       launch();
+}
+
+void TrainRoutePlanner::PlanningThread::main()
+{
+       planner.create_plan();
+}
+
 } // namespace R2C2
index f5bbf570d2fecc52d7027eaf850f410ce8178580..54ecadb8e905cd19138ef5070f11cd88b16da75e 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <list>
 #include <vector>
+#include <msp/core/thread.h>
 #include <msp/time/timedelta.h>
 #include "trackiter.h"
 #include "trainrouter.h"
@@ -112,23 +113,42 @@ private:
                bool operator<(const RoutingStep &) const;
        };
 
+       class PlanningThread: public Msp::Thread
+       {
+       private:
+               TrainRoutePlanner &planner;
+
+       public:
+               PlanningThread(TrainRoutePlanner &);
+
+       private:
+               virtual void main();
+       };
+
        std::vector<TrainRoutingInfo> routed_trains;
        std::list<RoutingStep> steps;
        std::list<RoutingStep> queue;
+       const RoutingStep *goal;
        Result result;
+       PlanningThread *thread;
 
 public:
        TrainRoutePlanner(Layout &);
+       ~TrainRoutePlanner();
 
-       void plan();
+       Result plan();
+       void plan_async();
+       Result check();
        Result get_result() { return result; }
        const std::list<Route *> &get_routes_for(const Train &) const;
        const std::list<TrainRouter::SequencePoint> &get_sequence_for(const Train &) const;
 private:
        const TrainRoutingInfo &get_train_info(const Train &) const;
        const RoutingStep &get_step();
+       void prepare_plan();
+       void create_plan();
        void add_steps(const RoutingStep &);
-       void create_routes(const RoutingStep &);
+       void finalize_plan();
 };
 
 } // namespace R2C2
index 1c05f829906ee7933432a80709927acf864751b1..bbb87eae116eb56326c911900eec1f0d09fe9303 100644 (file)
@@ -222,7 +222,7 @@ void TrainRouter::tick(const Time::TimeDelta &dt)
        if(destination_changed && !planner)
                start_planning(train.get_layout());
 
-       if(planner && planner->get_result()!=TrainRoutePlanner::PENDING)
+       if(planner && planner->check()!=TrainRoutePlanner::PENDING)
        {
                destination_changed = false;
                if(planner->get_result()==TrainRoutePlanner::COMPLETE)
@@ -494,7 +494,7 @@ void TrainRouter::start_planning(Layout &layout)
                        router->planner = planner;
                }
 
-       planner->plan();
+       planner->plan_async();
 }