Files
@ r20718:cc912091a170
Branch filter:
Location: cpp/openttd-patchpack/source/src/linkgraph/linkgraphschedule.cpp
r20718:cc912091a170
4.9 KiB
text/x-c
(svn r25756) -Update from WebTranslator v3.0:
indonesian - 2 changes by UseYourIllusion
indonesian - 2 changes by UseYourIllusion
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | /* $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.cpp Definition of link graph schedule used for cargo distribution. */
#include "../stdafx.h"
#include "linkgraphschedule.h"
#include "init.h"
#include "demands.h"
#include "mcf.h"
#include "flowmapper.h"
/**
* Spawn a thread if possible and run the link graph job in the thread. If
* that's not possible run the job right now in the current thread.
* @param job Job to be executed.
*/
void LinkGraphSchedule::SpawnThread(LinkGraphJob *job)
{
if (!ThreadObject::New(&(LinkGraphSchedule::Run), job, &job->thread)) {
job->thread = NULL;
/* Of course this will hang a bit.
* On the other hand, if you want to play games which make this hang noticably
* on a platform without threads then you'll probably get other problems first.
* OK:
* If someone comes and tells me that this hangs for him/her, I'll implement a
* smaller grained "Step" method for all handlers and add some more ticks where
* "Step" is called. No problem in principle.
*/
LinkGraphSchedule::Run(job);
}
}
/**
* Join the calling thread with the given job's thread if threading is enabled.
* @param job Job whose execution thread is to be joined.
*/
void LinkGraphSchedule::JoinThread(LinkGraphJob *job)
{
if (job->thread != NULL) {
job->thread->Join();
delete job->thread;
job->thread = NULL;
}
}
/**
* Start the next job in the schedule.
*/
void LinkGraphSchedule::SpawnNext()
{
if (this->schedule.empty()) return;
LinkGraph *next = this->schedule.front();
assert(next == LinkGraph::Get(next->index));
this->schedule.pop_front();
if (LinkGraphJob::CanAllocateItem()) {
LinkGraphJob *job = new LinkGraphJob(*next);
this->SpawnThread(job);
this->running.push_back(job);
} else {
NOT_REACHED();
}
}
/**
* Join the next finished job, if available.
*/
void LinkGraphSchedule::JoinNext()
{
if (this->running.empty()) return;
LinkGraphJob *next = this->running.front();
if (!next->IsFinished()) return;
this->running.pop_front();
LinkGraphID id = next->LinkGraphIndex();
this->JoinThread(next);
delete next;
if (LinkGraph::IsValidID(id)) {
LinkGraph *lg = LinkGraph::Get(id);
this->Unqueue(lg); // Unqueue to avoid double-queueing recycled IDs.
this->Queue(lg);
}
}
/**
* Run all handlers for the given Job. This method is tailored to
* ThreadObject::New.
* @param j Pointer to a link graph job.
*/
/* static */ void LinkGraphSchedule::Run(void *j)
{
LinkGraphJob *job = (LinkGraphJob *)j;
LinkGraphSchedule *schedule = LinkGraphSchedule::Instance();
for (uint i = 0; i < lengthof(schedule->handlers); ++i) {
schedule->handlers[i]->Run(*job);
}
}
/**
* Start all threads in the running list. This is only useful for save/load.
* Usually threads are started when the job is created.
*/
void LinkGraphSchedule::SpawnAll()
{
for (JobList::iterator i = this->running.begin(); i != this->running.end(); ++i) {
this->SpawnThread(*i);
}
}
/**
* Clear all link graphs and jobs from the schedule.
*/
/* static */ void LinkGraphSchedule::Clear()
{
LinkGraphSchedule *inst = LinkGraphSchedule::Instance();
for (JobList::iterator i(inst->running.begin()); i != inst->running.end(); ++i) {
inst->JoinThread(*i);
}
inst->running.clear();
inst->schedule.clear();
}
/**
* Create a link graph schedule and initialize its handlers.
*/
LinkGraphSchedule::LinkGraphSchedule()
{
this->handlers[0] = new InitHandler;
this->handlers[1] = new DemandHandler;
this->handlers[2] = new MCFHandler<MCF1stPass>;
this->handlers[3] = new FlowMapper;
this->handlers[4] = new MCFHandler<MCF2ndPass>;
this->handlers[5] = new FlowMapper;
}
/**
* Delete a link graph schedule and its handlers.
*/
LinkGraphSchedule::~LinkGraphSchedule()
{
this->Clear();
for (uint i = 0; i < lengthof(this->handlers); ++i) {
delete this->handlers[i];
}
}
/**
* Retrieve the link graph schedule or create it if necessary.
*/
/* static */ LinkGraphSchedule *LinkGraphSchedule::Instance()
{
static LinkGraphSchedule inst;
return &inst;
}
/**
* Spawn or join a link graph job or compress a link graph if any link graph is
* due to do so.
*/
void OnTick_LinkGraph()
{
if (_date_fract != LinkGraphSchedule::SPAWN_JOIN_TICK) return;
Date offset = _date % _settings_game.linkgraph.recalc_interval;
if (offset == 0) {
LinkGraphSchedule::Instance()->SpawnNext();
} else if (offset == _settings_game.linkgraph.recalc_interval / 2) {
LinkGraphSchedule::Instance()->JoinNext();
}
}
|