OpenTTD Source: src/linkgraph/linkgraphschedule.h Source File

OpenTTD Source 15.0-RC2

linkgraphschedule.h
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#ifndef LINKGRAPHSCHEDULE_H
11#define LINKGRAPHSCHEDULE_H
12
13#include "linkgraph.h"
14
15class LinkGraphJob;
16
21 class ComponentHandler {
22public:
26 virtual ~ComponentHandler() = default;
27
33 virtual void Run(LinkGraphJob &job) const = 0;
34};
35
36 class LinkGraphSchedule {
37private:
38 LinkGraphSchedule();
39 ~LinkGraphSchedule();
40 typedef std::list<LinkGraph *> GraphList;
41 typedef std::list<LinkGraphJob *> JobList;
42 friend SaveLoadTable GetLinkGraphScheduleDesc();
43
44protected:
45 std::array<std::unique_ptr<ComponentHandler>, 6> handlers{};
46 GraphList schedule;
47 JobList running;
48
49public:
50 /* This is a tick where not much else is happening, so a small lag might go unnoticed. */
51 static const uint SPAWN_JOIN_TICK = 21;
52 static LinkGraphSchedule instance;
53
54 static void Run(LinkGraphJob *job);
55 static void Clear();
56
57 void SpawnNext();
58 bool IsJoinWithUnfinishedJobDue() const;
59 void JoinNext();
60 void SpawnAll();
61 void ShiftDates(TimerGameEconomy::Date interval);
62
67 void Queue(LinkGraph *lg)
68 {
69 assert(LinkGraph::Get(lg->index) == lg);
70 this->schedule.push_back(lg);
71 }
72
77 void Dequeue(LinkGraph *lg) { this->schedule.remove(lg); }
78};
79
80void StateGameLoop_LinkGraphPauseControl();
81void AfterLoad_LinkGraphPauseControl();
82
83#endif /* LINKGRAPHSCHEDULE_H */
ComponentHandler
A handler doing "something" on a link graph component.
Definition linkgraphschedule.h:21
ComponentHandler::~ComponentHandler
virtual ~ComponentHandler()=default
Destroy the handler.
ComponentHandler::Run
virtual void Run(LinkGraphJob &job) const =0
Run the handler.
LinkGraphJob
Class for calculation jobs to be run on link graphs.
Definition linkgraphjob.h:29
LinkGraphSchedule
Definition linkgraphschedule.h:36
LinkGraphSchedule::LinkGraphSchedule
LinkGraphSchedule()
Create a link graph schedule and initialize its handlers.
Definition linkgraphschedule.cpp:144
LinkGraphSchedule::schedule
GraphList schedule
Queue for new jobs.
Definition linkgraphschedule.h:46
LinkGraphSchedule::SpawnNext
void SpawnNext()
Start the next job in the schedule.
Definition linkgraphschedule.cpp:33
LinkGraphSchedule::instance
static LinkGraphSchedule instance
Static instance of LinkGraphSchedule.
Definition linkgraphschedule.h:52
LinkGraphSchedule::Clear
static void Clear()
Clear all link graphs and jobs from the schedule.
Definition linkgraphschedule.cpp:121
LinkGraphSchedule::ShiftDates
void ShiftDates(TimerGameEconomy::Date interval)
Shift all dates (join dates and edge annotations) of link graphs and link graph jobs by the number of...
Definition linkgraphschedule.cpp:135
LinkGraphSchedule::~LinkGraphSchedule
~LinkGraphSchedule()
Delete a link graph schedule and its handlers.
Definition linkgraphschedule.cpp:157
LinkGraphSchedule::GetLinkGraphScheduleDesc
friend SaveLoadTable GetLinkGraphScheduleDesc()
Get a SaveLoad array for the link graph schedule.
Definition linkgraph_sl.cpp:219
LinkGraphSchedule::IsJoinWithUnfinishedJobDue
bool IsJoinWithUnfinishedJobDue() const
Check if the next job is supposed to be finished, but has not yet completed.
Definition linkgraphschedule.cpp:58
LinkGraphSchedule::SPAWN_JOIN_TICK
static const uint SPAWN_JOIN_TICK
Tick when jobs are spawned or joined every day.
Definition linkgraphschedule.h:51
LinkGraphSchedule::Queue
void Queue(LinkGraph *lg)
Queue a link graph for execution.
Definition linkgraphschedule.h:67
LinkGraphSchedule::Run
static void Run(LinkGraphJob *job)
Run all handlers for the given Job.
Definition linkgraphschedule.cpp:87
LinkGraphSchedule::JoinNext
void JoinNext()
Join the next finished job, if available.
Definition linkgraphschedule.cpp:68
LinkGraphSchedule::Dequeue
void Dequeue(LinkGraph *lg)
Remove a link graph from the execution queue.
Definition linkgraphschedule.h:77
LinkGraphSchedule::handlers
std::array< std::unique_ptr< ComponentHandler >, 6 > handlers
Handlers to be run for each job.
Definition linkgraphschedule.h:45
LinkGraphSchedule::running
JobList running
Currently running jobs.
Definition linkgraphschedule.h:47
LinkGraphSchedule::SpawnAll
void SpawnAll()
Start all threads in the running list.
Definition linkgraphschedule.cpp:111
LinkGraph
A connected component of a link graph.
Definition linkgraph.h:37
linkgraph.h
Declaration of link graph classes used for cargo distribution.
StateGameLoop_LinkGraphPauseControl
void StateGameLoop_LinkGraphPauseControl()
Pause the game if in 2 TimerGameEconomy::date_fract ticks, we would do a join with the next link grap...
Definition linkgraphschedule.cpp:169
AfterLoad_LinkGraphPauseControl
void AfterLoad_LinkGraphPauseControl()
Pause the game on load if we would do a join with the next link graph job, but it is still running,...
Definition linkgraphschedule.cpp:191
SaveLoadTable
std::span< const struct SaveLoad > SaveLoadTable
A table of SaveLoad entries.
Definition saveload.h:530
Pool::PoolItem<&_link_graph_pool >::Get
static Titem * Get(auto index)
Returns Titem with given index.
Definition pool_type.hpp:387
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition pool_type.hpp:286