OpenTTD Source  20240917-master-g9ab0a47812
linkgraphschedule.cpp
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 <http://www.gnu.org/licenses/>.
6  */
7 
10 #include "../stdafx.h"
11 #include "linkgraphschedule.h"
12 #include "init.h"
13 #include "demands.h"
14 #include "mcf.h"
15 #include "flowmapper.h"
16 #include "../framerate_type.h"
17 #include "../command_func.h"
18 #include "../network/network.h"
19 #include "../misc_cmd.h"
20 
21 #include "../safeguards.h"
22 
29 
34 {
35  if (this->schedule.empty()) return;
36  LinkGraph *next = this->schedule.front();
37  LinkGraph *first = next;
38  while (next->Size() < 2) {
39  this->schedule.splice(this->schedule.end(), this->schedule, this->schedule.begin());
40  next = this->schedule.front();
41  if (next == first) return;
42  }
43  assert(next == LinkGraph::Get(next->index));
44  this->schedule.pop_front();
46  LinkGraphJob *job = new LinkGraphJob(*next);
47  job->SpawnThread();
48  this->running.push_back(job);
49  } else {
50  NOT_REACHED();
51  }
52 }
53 
59 {
60  if (this->running.empty()) return false;
61  const LinkGraphJob *next = this->running.front();
62  return next->IsScheduledToBeJoined() && !next->IsJobCompleted();
63 }
64 
69 {
70  if (this->running.empty()) return;
71  LinkGraphJob *next = this->running.front();
72  if (!next->IsScheduledToBeJoined()) return;
73  this->running.pop_front();
74  LinkGraphID id = next->LinkGraphIndex();
75  delete next; // implicitly joins the thread
76  if (LinkGraph::IsValidID(id)) {
77  LinkGraph *lg = LinkGraph::Get(id);
78  this->Unqueue(lg); // Unqueue to avoid double-queueing recycled IDs.
79  this->Queue(lg);
80  }
81 }
82 
87 /* static */ void LinkGraphSchedule::Run(LinkGraphJob *job)
88 {
89  for (const auto &handler : instance.handlers) {
90  if (job->IsJobAborted()) return;
91  handler->Run(*job);
92  }
93 
94  /*
95  * Readers of this variable in another thread may see an out of date value.
96  * However this is OK as this will only happen just as a job is completing,
97  * and the real synchronisation is provided by the thread join operation.
98  * In the worst case the main thread will be paused for longer than
99  * strictly necessary before joining.
100  * This is just a hint variable to avoid performing the join excessively
101  * early and blocking the main thread.
102  */
103 
104  job->job_completed.store(true, std::memory_order_release);
105 }
106 
112 {
113  for (auto &it : this->running) {
114  it->SpawnThread();
115  }
116 }
117 
121 /* static */ void LinkGraphSchedule::Clear()
122 {
123  for (auto &it : instance.running) {
124  it->AbortJob();
125  }
126  instance.running.clear();
127  instance.schedule.clear();
128 }
129 
135 void LinkGraphSchedule::ShiftDates(TimerGameEconomy::Date interval)
136 {
137  for (LinkGraph *lg : LinkGraph::Iterate()) lg->ShiftDates(interval);
138  for (LinkGraphJob *lgj : LinkGraphJob::Iterate()) lgj->ShiftJoinDate(interval);
139 }
140 
145 {
146  this->handlers[0] = new InitHandler;
147  this->handlers[1] = new DemandHandler;
148  this->handlers[2] = new MCFHandler<MCF1stPass>;
149  this->handlers[3] = new FlowMapper(false);
150  this->handlers[4] = new MCFHandler<MCF2ndPass>;
151  this->handlers[5] = new FlowMapper(true);
152 }
153 
158 {
159  this->Clear();
160  for (const auto &handler : this->handlers) {
161  delete handler;
162  }
163 }
164 
173 {
175  /* We are paused waiting on a job, check the job every tick. */
176  if (!LinkGraphSchedule::instance.IsJoinWithUnfinishedJobDue()) {
178  }
179  } else if (_pause_mode == PM_UNPAUSED &&
183  /* Perform check two TimerGameEconomy::date_fract ticks before we would join, to make
184  * sure it also works in multiplayer. */
186  }
187 }
188 
195 {
196  if (LinkGraphSchedule::instance.IsJoinWithUnfinishedJobDue()) {
198  }
199 }
200 
206 {
209  if (offset == 0) {
212  if (!_networking || _network_server) {
215  } else {
218  }
219  }
220 }
221 
222 
LinkGraphSchedule::Clear
static void Clear()
Clear all link graphs and jobs from the schedule.
Definition: linkgraphschedule.cpp:121
mcf.h
Pool::PoolItem<&_link_graph_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:339
LinkGraphSchedule::JoinNext
void JoinNext()
Join the next finished job, if available.
Definition: linkgraphschedule.cpp:68
LinkGraph
A connected component of a link graph.
Definition: linkgraph.h:37
DemandHandler
Stateless, thread safe demand handler.
Definition: demands.h:28
LinkGraphSchedule::SPAWN_JOIN_TICK
static const uint SPAWN_JOIN_TICK
Tick when jobs are spawned or joined every day.
Definition: linkgraphschedule.h:51
LinkGraphJob
Class for calculation jobs to be run on link graphs.
Definition: linkgraphjob.h:29
LinkGraphSchedule::running
JobList running
Currently running jobs.
Definition: linkgraphschedule.h:47
_network_server
bool _network_server
network-server is active
Definition: network.cpp:66
LinkGraphSchedule
Definition: linkgraphschedule.h:36
LinkGraphJob::IsJobCompleted
bool IsJobCompleted() const
Check if job has actually finished.
Definition: linkgraphjob.h:193
LinkGraphSchedule::instance
static LinkGraphSchedule instance
Static instance of LinkGraphSchedule.
Definition: linkgraphschedule.h:52
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:238
demands.h
PerformanceMeasurer
RAII class for measuring simple elements of performance.
Definition: framerate_type.h:92
LinkGraphJob::job_completed
std::atomic< bool > job_completed
Is the job still running. This is accessed by multiple threads and reads may be stale.
Definition: linkgraphjob.h:168
TimerGameEconomy::date_fract
static DateFract date_fract
Fractional part of the day.
Definition: timer_game_economy.h:38
PFE_GL_LINKGRAPH
@ PFE_GL_LINKGRAPH
Time spent waiting for link graph background jobs.
Definition: framerate_type.h:56
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:194
LinkGraph::Size
NodeID Size() const
Get the current size of the component.
Definition: linkgraph.h:230
LinkGraphSchedule::SpawnAll
void SpawnAll()
Start all threads in the running list.
Definition: linkgraphschedule.cpp:111
PerformanceMeasurer::SetInactive
static void SetInactive(PerformanceElement elem)
Mark a performance element as not currently in use.
Definition: framerate_gui.cpp:286
LinkGraphJob::LinkGraphIndex
LinkGraphID LinkGraphIndex() const
Get the ID of the underlying link graph.
Definition: linkgraphjob.h:263
OnTick_LinkGraph
void OnTick_LinkGraph()
Spawn or join a link graph job or compress a link graph if any link graph is due to do so.
Definition: linkgraphschedule.cpp:205
LinkGraphSettings::recalc_interval
uint16_t recalc_interval
time (in days) between subsequent checks for link graphs to be calculated.
Definition: settings_type.h:544
LinkGraphSchedule::Queue
void Queue(LinkGraph *lg)
Queue a link graph for execution.
Definition: linkgraphschedule.h:67
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
LinkGraphJob::SpawnThread
void SpawnThread()
Spawn a thread if possible and run the link graph job in the thread.
Definition: linkgraphjob.cpp:61
LinkGraphJob::IsScheduledToBeJoined
bool IsScheduledToBeJoined() const
Check if job is supposed to be finished.
Definition: linkgraphjob.h:214
init.h
_pause_mode
PauseMode _pause_mode
The current pause mode.
Definition: gfx.cpp:50
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:57
LinkGraphSchedule::SpawnNext
void SpawnNext()
Start the next job in the schedule.
Definition: linkgraphschedule.cpp:33
_networking
bool _networking
are we in networking mode?
Definition: network.cpp:65
LinkGraphSchedule::LinkGraphSchedule
LinkGraphSchedule()
Create a link graph schedule and initialize its handlers.
Definition: linkgraphschedule.cpp:144
GameSettings::linkgraph
LinkGraphSettings linkgraph
settings for link graph calculations
Definition: settings_type.h:604
LinkGraphSchedule::schedule
GraphList schedule
Queue for new jobs.
Definition: linkgraphschedule.h:46
linkgraphschedule.h
PM_PAUSED_LINK_GRAPH
@ PM_PAUSED_LINK_GRAPH
A game paused due to the link graph schedule lagging.
Definition: openttd.h:76
FlowMapper
Map the path trees generated by the MCF solver into flows.
Definition: flowmapper.h:22
Pool::PoolItem<&_link_graph_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:388
LinkGraphJob::IsJobAborted
bool IsJobAborted() const
Check if job has been aborted.
Definition: linkgraphjob.h:200
LinkGraphSchedule::IsJoinWithUnfinishedJobDue
bool IsJoinWithUnfinishedJobDue() const
Check if the next job is supposed to be finished, but has not yet completed.
Definition: linkgraphschedule.cpp:58
MCFHandler
Link graph handler for MCF.
Definition: mcf.h:76
Pool::PoolItem<&_link_graph_job_pool >::CanAllocateItem
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
Definition: pool_type.hpp:309
CommandHelper
Definition: command_func.h:93
LinkGraphSchedule::Run
static void Run(LinkGraphJob *job)
Run all handlers for the given Job.
Definition: linkgraphschedule.cpp:87
TimerGameConst< struct Economy >::SECONDS_PER_DAY
static constexpr int SECONDS_PER_DAY
approximate seconds per day, not for precise calculations
Definition: timer_game_common.h:153
LinkGraphSchedule::handlers
ComponentHandler * handlers[6]
Handlers to be run for each job.
Definition: linkgraphschedule.h:45
LinkGraphSchedule::Unqueue
void Unqueue(LinkGraph *lg)
Remove a link graph from the execution queue.
Definition: linkgraphschedule.h:77
Pool::PoolItem<&_link_graph_pool >::IsValidID
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:328
PM_UNPAUSED
@ PM_UNPAUSED
A normal unpaused game.
Definition: openttd.h:69
flowmapper.h
LinkGraphSchedule::~LinkGraphSchedule
~LinkGraphSchedule()
Delete a link graph schedule and its handlers.
Definition: linkgraphschedule.cpp:157
InitHandler
Stateless, thread safe initialization handler.
Definition: init.h:12
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:172
TimerGameEconomy::date
static Date date
Current date in days (day counter).
Definition: timer_game_economy.h:37