]> git.tdb.fi Git - r2c2.git/commitdiff
Store routing information in TrainRoutePlanner to avoid threading problems
authorMikko Rasa <tdb@tdb.fi>
Thu, 5 Feb 2015 08:40:19 +0000 (10:40 +0200)
committerMikko Rasa <tdb@tdb.fi>
Thu, 5 Feb 2015 08:40:19 +0000 (10:40 +0200)
TrainRouter removes checkpoints when the train passes them, which will
cause problems if the planner is still running.

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

index 99700699c452a21a81ef51bdad8dd25b5cab8e5e..163bb281eacd019997e8975802f670a0d904ce6f 100644 (file)
@@ -22,7 +22,7 @@ TrainRoutePlanner::TrainRoutePlanner(Layout &layout):
        for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
        {
                TrainRoutingInfo info(*i->second);
-               if(info.router && info.router->get_destination())
+               if(info.destination)
                        routed_trains.push_back(info);
        }
 }
@@ -206,8 +206,24 @@ void TrainRoutePlanner::finalize_plan()
 TrainRoutePlanner::TrainRoutingInfo::TrainRoutingInfo(Train &t):
        train(&t),
        speed(train->get_maximum_speed()),
-       router(train->get_ai_of_type<TrainRouter>())
+       router(train->get_ai_of_type<TrainRouter>()),
+       destination(0),
+       has_duration(false)
 {
+       if(router)
+       {
+               destination = router->get_destination();
+               waypoints.resize(router->get_n_waypoints());
+               metrics.resize(waypoints.size()+1);
+               metrics[0] = &router->get_metric(-1);
+               for(unsigned i=0; i<waypoints.size(); ++i)
+               {
+                       waypoints[i] = &router->get_waypoint(i);
+                       metrics[i+1] = &router->get_metric(i);
+               }
+               has_duration = router->get_trip_duration();
+       }
+
        // If no maximum speed is specified, use a sensible default
        if(!speed)
                speed = 20*train->get_layout().get_catalogue().get_scale();
@@ -249,7 +265,7 @@ TrainRoutePlanner::TrainRoutingState::TrainRoutingState(TrainRoutingInfo &inf):
        state(MOVING),
        delay(info->router->get_departure_delay()),
        duration(info->router->get_trip_duration()),
-       waypoint(info->router->get_n_waypoints() ? 0 : -1),
+       waypoint(info->waypoints.empty() ? -1 : 0),
        blocked_by(-1)
 {
        const Vehicle *veh = &info->train->get_vehicle(0);
@@ -308,7 +324,7 @@ Time::TimeDelta TrainRoutePlanner::TrainRoutingState::get_time_to_next_track() c
 
 bool TrainRoutePlanner::TrainRoutingState::is_occupying(Track &trk) const
 {
-       if(state==ARRIVED && !duration && info->router->get_trip_duration())
+       if(state==ARRIVED && !duration && info->has_duration)
                return false;
 
        OccupiedTrack *occ = occupied_tracks;
@@ -320,18 +336,17 @@ bool TrainRoutePlanner::TrainRoutingState::is_occupying(Track &trk) const
 
 bool TrainRoutePlanner::TrainRoutingState::check_arrival()
 {
-       TrainRouter &router = *info->router;
        TrackIter next_track = track.next(path);
 
-       if(waypoint<0 && router.is_destination(*track) && !router.is_destination(*next_track))
+       if(waypoint<0 && info->destination->has_track(*track) && !info->destination->has_track(*next_track))
        {
                state = ARRIVED;
                return true;
        }
-       else if(waypoint>=0 && router.is_waypoint(waypoint, *track) && !router.is_waypoint(waypoint, *next_track))
+       else if(waypoint>=0 && info->waypoints[waypoint]->has_track(*track) && !info->waypoints[waypoint]->has_track(*next_track))
        {
                ++waypoint;
-               if(waypoint>=static_cast<int>(router.get_n_waypoints()))
+               if(waypoint>=static_cast<int>(info->waypoints.size()))
                        waypoint = -1;
        }
 
@@ -400,7 +415,7 @@ void TrainRoutePlanner::TrainRoutingState::advance_track(unsigned next_path)
 void TrainRoutePlanner::TrainRoutingState::update_estimate()
 {
        TrackIter iter = track.reverse(path);
-       float distance = info->router->get_metric(waypoint).get_distance_from(*iter.track(), iter.entry());
+       float distance = info->metrics[waypoint+1]->get_distance_from(*iter.track(), iter.entry());
        distance += track->get_type().get_path_length(path)-offset;
        remaining_estimate = distance;
 }
index c4850d46a61c00185f9630b8259885d0582b5f5e..fa97920e4d02730469ab1d5a190377891cf63126 100644 (file)
@@ -33,6 +33,10 @@ private:
                Train *train;
                float speed;
                TrainRouter *router;
+               const TrackChain *destination;
+               std::vector<const TrackChain *> waypoints;
+               std::vector<const TrainRouteMetric *> metrics;
+               bool has_duration;
                std::list<Route *> routes;
                Track *track_history[2];
                std::list<TrainRouter::SequencePoint> sequence;
index 9245774f605309d597591fa01e95eb60f8b20c38..6af6154c862a29af52fb0e59bccc09b0f97b1ff2 100644 (file)
@@ -137,14 +137,6 @@ void TrainRouter::set_destination(const TrackChain &d)
        metrics_stale = true;
 }
 
-bool TrainRouter::is_destination(Track &track) const
-{
-       if(destination)
-               return destination->has_track(track);
-       else
-               return false;
-}
-
 void TrainRouter::add_waypoint(const TrackChain &wp)
 {
        waypoints.push_back(&wp);
@@ -152,12 +144,12 @@ void TrainRouter::add_waypoint(const TrackChain &wp)
        metrics_stale = true;
 }
 
-bool TrainRouter::is_waypoint(unsigned index, Track &track) const
+const TrackChain &TrainRouter::get_waypoint(unsigned index) const
 {
        if(index>=waypoints.size())
                throw out_of_range("TrainRouter::is_waypoint");
 
-       return waypoints[index]->has_track(track);
+       return *waypoints[index];
 }
 
 const TrainRouteMetric &TrainRouter::get_metric(int index) const
@@ -438,6 +430,8 @@ void TrainRouter::create_metrics()
 
        for(unsigned i=metrics.size(); --i>0; )
                metrics[i]->chain_to(*metrics[(i+1)%metrics.size()]);
+
+       metrics_stale = false;
 }
 
 Route *TrainRouter::create_lead_route(Route *lead, const Route *target)
@@ -492,19 +486,20 @@ bool TrainRouter::advance_to_track(RouteList::iterator &route, Track &track)
 
 void TrainRouter::start_planning(Layout &layout)
 {
-       RefPtr<TrainRoutePlanner> planner = new TrainRoutePlanner(layout);
-
+       vector<TrainRouter *> routers;
        const map<unsigned, Train *> &trains = layout.get_trains();
+       routers.reserve(trains.size());
        for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
                if(TrainRouter *router = i->second->get_ai_of_type<TrainRouter>())
-               {
-                       if(router->metrics_stale)
-                       {
-                               router->create_metrics();
-                               router->metrics_stale = false;
-                       }
-                       router->planner = planner;
-               }
+                       routers.push_back(router);
+
+       for(vector<TrainRouter *>::const_iterator i=routers.begin(); i!=routers.end(); ++i)
+               if((*i)->metrics_stale)
+                       (*i)->create_metrics();
+
+       RefPtr<TrainRoutePlanner> planner = new TrainRoutePlanner(layout);
+       for(vector<TrainRouter *>::const_iterator i=routers.begin(); i!=routers.end(); ++i)
+               (*i)->planner = planner;
 
        planner->plan_async();
 }
index 91b7bac1a4e0730afb2e7b278911f4d74b4c5334..2b59603dff6b9e8ef2753c36917a3335931e64c7 100644 (file)
@@ -85,10 +85,9 @@ private:
 public:
        void set_destination(const TrackChain &);
        const TrackChain *get_destination() const { return destination; }
-       bool is_destination(Track &) const;
        void add_waypoint(const TrackChain &);
        unsigned get_n_waypoints() const { return waypoints.size(); }
-       bool is_waypoint(unsigned, Track &) const;
+       const TrackChain &get_waypoint(unsigned) const;
        const TrainRouteMetric &get_metric(int = -1) const;
        void set_departure_delay(const Msp::Time::TimeDelta &);
        const Msp::Time::TimeDelta &get_departure_delay() const { return delay; }