(svn r27729) -Codechange: Do not count static NewGRF when checking for the maximum...
[openttd.git] / src / linkgraph / linkgraphschedule.h
blobec22be3161f010e9a7a2d7637a9cb13375f9ada0
1 /* $Id$ */
3 /*
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/>.
8 */
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"
17 class LinkGraphJob;
19 /**
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 {
24 public:
25 /**
26 * Destroy the handler. Must be given due to virtual Run.
28 virtual ~ComponentHandler() {}
30 /**
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 {
39 private:
40 LinkGraphSchedule();
41 ~LinkGraphSchedule();
42 typedef std::list<LinkGraph *> GraphList;
43 typedef std::list<LinkGraphJob *> JobList;
44 friend const SaveLoad *GetLinkGraphScheduleDesc();
46 protected:
47 ComponentHandler *handlers[6]; ///< Handlers to be run for each job.
48 GraphList schedule; ///< Queue for new jobs.
49 JobList running; ///< Currently running jobs.
51 public:
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);
57 static void Clear();
59 void SpawnNext();
60 void JoinNext();
61 void SpawnAll();
62 void ShiftDates(int interval);
64 /**
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);
74 /**
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 */