summaryrefslogtreecommitdiff
path: root/src/linkgraph/linkgraphschedule.h
blob: 911fa389a15d2f97ea7828f1ff45f8da896c0baf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* $Id$ */

/*
 * This file is part of OpenTTD.
 * 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.
 * 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.
 * 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/>.
 */

/** @file linkgraphschedule.h Declaration of link graph schedule used for cargo distribution. */

#ifndef LINKGRAPHSCHEDULE_H
#define LINKGRAPHSCHEDULE_H

#include "linkgraph.h"

class LinkGraphJob;

/**
 * A handler doing "something" on a link graph component. It must not keep any
 * state as it is called concurrently from different threads.
 */
class ComponentHandler {
public:
	/**
	 * Destroy the handler. Must be given due to virtual Run.
	 */
	virtual ~ComponentHandler() {}

	/**
	 * Run the handler. A link graph handler must not read or write any data
	 * outside the given component as that would create a potential desync.
	 * @param job Link graph component to run the handler on.
	 */
	virtual void Run(LinkGraphJob &job) const = 0;
};

class LinkGraphSchedule {
private:
	LinkGraphSchedule();
	~LinkGraphSchedule();
	typedef std::list<LinkGraph *> GraphList;
	typedef std::list<LinkGraphJob *> JobList;
	friend const SaveLoad *GetLinkGraphScheduleDesc();

protected:
	ComponentHandler *handlers[6]; ///< Handlers to be run for each job.
	GraphList schedule;            ///< Queue for new jobs.
	JobList running;               ///< Currently running jobs.

	void SpawnThread(LinkGraphJob *job);
	void JoinThread(LinkGraphJob *job);

public:
	/* This is a tick where not much else is happening, so a small lag might go unnoticed. */
	static const uint SPAWN_JOIN_TICK = 21; ///< Tick when jobs are spawned or joined every day.

	static LinkGraphSchedule *Instance();
	static void Run(void *j);
	static void Clear();

	void SpawnNext();
	void JoinNext();
	void SpawnAll();

	/**
	 * Queue a link graph for execution.
	 * @param lg Link graph to be queued.
	 */
	void Queue(LinkGraph *lg)
	{
		assert(LinkGraph::Get(lg->index) == lg);
		this->schedule.push_back(lg);
	}

	/**
	 * Remove a link graph from the execution queue.
	 * @param lg Link graph to be removed.
	 */
	void Unqueue(LinkGraph *lg) { this->schedule.remove(lg); }
};

#endif /* LINKGRAPHSCHEDULE_H */