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 177 178 179
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program 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, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "ultima/ultima8/kernel/process.h"
#include "ultima/ultima8/kernel/kernel.h"
#include "ultima/ultima8/ultima8.h"
namespace Ultima {
namespace Ultima8 {
DEFINE_RUNTIME_CLASSTYPE_CODE(Process)
Process::Process(ObjId it, uint16 ty)
: _pid(0xFFFF), _flags(0), _itemNum(it), _type(ty), _result(0), _ticksPerRun(2) {
Kernel::get_instance()->assignPID(this);
if (GAME_IS_CRUSADER) {
// Default kernel ticks per run of processes in Crusader
_ticksPerRun = 1;
}
}
void Process::fail() {
_flags |= PROC_FAILED;
terminate();
}
void Process::terminate() {
if (_flags & PROC_TERMINATED)
return;
Kernel *kernel = Kernel::get_instance();
// wake up waiting processes
for (Std::vector<ProcId>::iterator i = _waiting.begin();
i != _waiting.end(); ++i) {
Process *p = kernel->getProcess(*i);
if (p)
p->wakeUp(_result);
}
_waiting.clear();
_flags |= PROC_TERMINATED;
}
void Process::wakeUp(uint32 result) {
_result = result;
_flags &= ~PROC_SUSPENDED;
Kernel::get_instance()->setNextProcess(this);
onWakeUp();
}
void Process::waitFor(ProcId pid) {
assert(pid != _pid);
if (pid) {
Kernel *kernel = Kernel::get_instance();
// add this process to waiting list of other process
Process *p = kernel->getProcess(pid);
assert(p);
if (p->getProcessFlags() & PROC_TERMINATED) {
//warning("Proc %d wait for proc %d which is already terminated", _pid, pid);
return;
}
p->_waiting.push_back(_pid);
// Note: The original games sync itemnum between processes
// here if either one is zero, but that seems to break things
// for us so we don't do it.
}
_flags |= PROC_SUSPENDED;
}
void Process::waitFor(Process *proc) {
assert(this != proc);
ProcId pid = 0;
if (proc) pid = proc->getPid();
waitFor(pid);
}
void Process::suspend() {
_flags |= PROC_SUSPENDED;
}
Common::String Process::dumpInfo() const {
Common::String info = Common::String::format(
"Process %d class %s, item %d, type %x, status ",
getPid(), GetClassType()._className, _itemNum, _type);
if (_flags & PROC_ACTIVE) info += "A";
if (_flags & PROC_SUSPENDED) info += "S";
if (_flags & PROC_TERMINATED) info += "T";
if (_flags & PROC_TERM_DEFERRED) info += "t";
if (_flags & PROC_FAILED) info += "F";
if (_flags & PROC_RUNPAUSED) info += "R";
if (_flags & PROC_TERM_DISPOSE) info += "D";
if (!_waiting.empty()) {
info += ", notify: ";
for (Std::vector<ProcId>::const_iterator i = _waiting.begin(); i != _waiting.end(); ++i) {
if (i != _waiting.begin()) info += ", ";
info += Common::String::format("%d", *i);
}
}
return info;
}
void Process::saveData(Common::WriteStream *ws) {
ws->writeUint16LE(_pid);
ws->writeUint32LE(_flags);
ws->writeUint16LE(_itemNum);
ws->writeUint16LE(_type);
ws->writeUint32LE(_result);
ws->writeUint32LE(static_cast<uint32>(_waiting.size()));
for (unsigned int i = 0; i < _waiting.size(); ++i)
ws->writeUint16LE(_waiting[i]);
}
bool Process::loadData(Common::ReadStream *rs, uint32 version) {
_pid = rs->readUint16LE();
_flags = rs->readUint32LE();
_itemNum = rs->readUint16LE();
_type = rs->readUint16LE();
_result = rs->readUint32LE();
uint32 waitcount = rs->readUint32LE();
if (waitcount > 1024*1024) {
warning("Improbable waitcount %d for proc %d. Corrupt save?", waitcount, _pid);
return false;
}
_waiting.resize(waitcount);
for (unsigned int i = 0; i < waitcount; ++i)
_waiting[i] = rs->readUint16LE();
return true;
}
bool Process::validateWaiters() const {
for (Std::vector<ProcId>::const_iterator i = _waiting.begin();
i != _waiting.end(); ++i) {
const Process *p = Kernel::get_instance()->getProcess(*i);
if (!p) {
// This can happen if a waiting process gets forcibly terminated.
warning("Invalid procid %d in waitlist for proc %d. Maybe a bug?", *i, _pid);
} else if (!p->is_suspended()) {
// This should never happen.
warning("Procid %d in waitlist for proc %d but not marked suspended", *i, _pid);
return false;
}
}
return true;
}
} // End of namespace Ultima8
} // End of namespace Ultima
|