4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file linkgraphschedule.h Declaration of link graph schedule used for cargo distribution. */
12 #ifndef LINKGRAPHSCHEDULE_H
13 #define LINKGRAPHSCHEDULE_H
15 #include "linkgraph.h"
20 * A handler doing "something" on a link graph component. It must not keep any
21 * state as it is called concurrently from different threads.
23 class ComponentHandler
{
26 * Destroy the handler. Must be given due to virtual Run.
28 virtual ~ComponentHandler() {}
31 * Run the handler. A link graph handler must not read or write any data
32 * outside the given component as that would create a potential desync.
33 * @param job Link graph component to run the handler on.
35 virtual void Run(LinkGraphJob
&job
) const = 0;
38 class LinkGraphSchedule
{
42 typedef std::list
<LinkGraph
*> GraphList
;
43 typedef std::list
<LinkGraphJob
*> JobList
;
44 friend const SaveLoad
*GetLinkGraphScheduleDesc();
47 ComponentHandler
*handlers
[6]; ///< Handlers to be run for each job.
48 GraphList schedule
; ///< Queue for new jobs.
49 JobList running
; ///< Currently running jobs.
52 /* This is a tick where not much else is happening, so a small lag might go unnoticed. */
53 static const uint SPAWN_JOIN_TICK
= 21; ///< Tick when jobs are spawned or joined every day.
54 static LinkGraphSchedule instance
;
56 static void Run(void *j
);
62 void ShiftDates(int interval
);
65 * Queue a link graph for execution.
66 * @param lg Link graph to be queued.
68 void Queue(LinkGraph
*lg
)
70 assert(LinkGraph::Get(lg
->index
) == lg
);
71 this->schedule
.push_back(lg
);
75 * Remove a link graph from the execution queue.
76 * @param lg Link graph to be removed.
78 void Unqueue(LinkGraph
*lg
) { this->schedule
.remove(lg
); }
81 #endif /* LINKGRAPHSCHEDULE_H */