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 "../thread/thread.h"
16 #include "linkgraph.h"
22 * A handler doing "something" on a link graph component. It must not keep any
23 * state as it is called concurrently from different threads.
25 class ComponentHandler
{
28 * Destroy the handler. Must be given due to virtual Run.
30 virtual ~ComponentHandler() {}
33 * Run the handler. A link graph handler must not read or write any data
34 * outside the given component as that would create a potential desync.
35 * @param job Link graph component to run the handler on.
37 virtual void Run(LinkGraphJob
&job
) const = 0;
40 class LinkGraphSchedule
{
44 typedef std::list
<LinkGraph
*> GraphList
;
45 typedef std::list
<std::unique_ptr
<LinkGraphJob
>> JobList
;
46 friend const SaveLoad
*GetLinkGraphScheduleDesc();
49 std::unique_ptr
<ComponentHandler
> handlers
[6]; ///< Handlers to be run for each job.
50 GraphList schedule
; ///< Queue for new jobs.
51 JobList running
; ///< Currently running jobs.
54 /* This is a tick where not much else is happening, so a small lag might go unnoticed. */
55 static const uint SPAWN_JOIN_TICK
= 21; ///< Tick when jobs are spawned or joined every day.
56 static LinkGraphSchedule instance
;
58 static void Run(void *j
);
62 bool IsJoinWithUnfinishedJobDue() const;
65 void ShiftDates(int interval
);
68 * Queue a link graph for execution.
69 * @param lg Link graph to be queued.
71 void Queue(LinkGraph
*lg
)
73 assert(LinkGraph::Get(lg
->index
) == lg
);
74 this->schedule
.push_back(lg
);
78 * Remove a link graph from the execution queue.
79 * @param lg Link graph to be removed.
81 void Unqueue(LinkGraph
*lg
) { this->schedule
.remove(lg
); }
84 class LinkGraphJobGroup
: public std::enable_shared_from_this
<LinkGraphJobGroup
> {
88 bool joined_thread
= false; ///< True if thread has already been joined
89 std::unique_ptr
<ThreadObject
> thread
; ///< Thread the job group is running in or NULL if it's running in the main thread.
90 const std::vector
<LinkGraphJob
*> jobs
; ///< The set of jobs in this job set
93 struct constructor_token
{ };
94 static void Run(void *group
);
99 LinkGraphJobGroup(constructor_token token
, std::vector
<LinkGraphJob
*> jobs
);
105 JobInfo(LinkGraphJob
*job
);
106 JobInfo(LinkGraphJob
*job
, uint cost_estimate
) :
107 job(job
), cost_estimate(cost_estimate
) { }
110 static void ExecuteJobSet(std::vector
<JobInfo
> jobs
);
113 #endif /* LINKGRAPHSCHEDULE_H */