1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 #include "ultima/ultima8/kernel/process.h"
24 #include "ultima/ultima8/kernel/kernel.h"
25 #include "ultima/ultima8/ultima8.h"
26
27 namespace Ultima {
28 namespace Ultima8 {
29
DEFINE_RUNTIME_CLASSTYPE_CODE(Process)30 DEFINE_RUNTIME_CLASSTYPE_CODE(Process)
31
32 Process::Process(ObjId it, uint16 ty)
33 : _pid(0xFFFF), _flags(0), _itemNum(it), _type(ty), _result(0), _ticksPerRun(2) {
34 Kernel::get_instance()->assignPID(this);
35 if (GAME_IS_CRUSADER) {
36 // Default kernel ticks per run of processes in Crusader
37 _ticksPerRun = 1;
38 }
39 }
40
fail()41 void Process::fail() {
42 assert(!(_flags & PROC_TERMINATED));
43
44 _flags |= PROC_FAILED;
45 terminate();
46 }
47
terminate()48 void Process::terminate() {
49 assert(!(_flags & PROC_TERMINATED));
50
51 Kernel *kernel = Kernel::get_instance();
52
53 // wake up waiting processes
54 for (Std::vector<ProcId>::iterator i = _waiting.begin();
55 i != _waiting.end(); ++i) {
56 Process *p = kernel->getProcess(*i);
57 if (p)
58 p->wakeUp(_result);
59 }
60 _waiting.clear();
61
62 _flags |= PROC_TERMINATED;
63 }
64
wakeUp(uint32 result)65 void Process::wakeUp(uint32 result) {
66 _result = result;
67
68 _flags &= ~PROC_SUSPENDED;
69
70 Kernel::get_instance()->setNextProcess(this);
71
72 onWakeUp();
73 }
74
waitFor(ProcId pid)75 void Process::waitFor(ProcId pid) {
76 assert(pid != _pid);
77 if (pid) {
78 Kernel *kernel = Kernel::get_instance();
79
80 // add this process to waiting list of other process
81 Process *p = kernel->getProcess(pid);
82 assert(p);
83 if (p->getProcessFlags() & PROC_TERMINATED) {
84 //warning("Proc %d wait for proc %d which is already terminated", _pid, pid);
85 return;
86 }
87 p->_waiting.push_back(_pid);
88
89 // Note: The original games sync itemnum between processes
90 // here if either one is zero, but that seems to break things
91 // for us so we don't do it.
92 }
93
94 _flags |= PROC_SUSPENDED;
95 }
96
waitFor(Process * proc)97 void Process::waitFor(Process *proc) {
98 assert(this != proc);
99 ProcId pid = 0;
100 if (proc) pid = proc->getPid();
101
102 waitFor(pid);
103 }
104
suspend()105 void Process::suspend() {
106 _flags |= PROC_SUSPENDED;
107 }
108
dumpInfo() const109 void Process::dumpInfo() const {
110 Common::String info = Common::String::format(
111 "Process %d class %s, item %d, type %x, status ",
112 getPid(), GetClassType()._className, _itemNum, _type);
113
114 if (_flags & PROC_ACTIVE) info += "A";
115 if (_flags & PROC_SUSPENDED) info += "S";
116 if (_flags & PROC_TERMINATED) info += "T";
117 if (_flags & PROC_TERM_DEFERRED) info += "t";
118 if (_flags & PROC_FAILED) info += "F";
119 if (_flags & PROC_RUNPAUSED) info += "R";
120 if (!_waiting.empty()) {
121 info += ", notify: ";
122 for (Std::vector<ProcId>::const_iterator i = _waiting.begin(); i != _waiting.end(); ++i) {
123 if (i != _waiting.begin()) info += ", ";
124 info += Common::String::format("%d", *i);
125 }
126 }
127
128 g_debugger->debugPrintf("%s\n", info.c_str());
129 }
130
saveData(Common::WriteStream * ws)131 void Process::saveData(Common::WriteStream *ws) {
132 ws->writeUint16LE(_pid);
133 ws->writeUint32LE(_flags);
134 ws->writeUint16LE(_itemNum);
135 ws->writeUint16LE(_type);
136 ws->writeUint32LE(_result);
137 ws->writeUint32LE(static_cast<uint32>(_waiting.size()));
138 for (unsigned int i = 0; i < _waiting.size(); ++i)
139 ws->writeUint16LE(_waiting[i]);
140 }
141
loadData(Common::ReadStream * rs,uint32 version)142 bool Process::loadData(Common::ReadStream *rs, uint32 version) {
143 _pid = rs->readUint16LE();
144 _flags = rs->readUint32LE();
145 _itemNum = rs->readUint16LE();
146 _type = rs->readUint16LE();
147 _result = rs->readUint32LE();
148 uint32 waitcount = rs->readUint32LE();
149
150 if (waitcount > 1024*1024) {
151 warning("Improbable waitcount %d for proc %d. Corrupt save?", waitcount, _pid);
152 return false;
153 }
154
155 _waiting.resize(waitcount);
156 for (unsigned int i = 0; i < waitcount; ++i)
157 _waiting[i] = rs->readUint16LE();
158
159 return true;
160 }
161
validateWaiters() const162 bool Process::validateWaiters() const {
163 for (Std::vector<ProcId>::const_iterator i = _waiting.begin();
164 i != _waiting.end(); ++i) {
165 const Process *p = Kernel::get_instance()->getProcess(*i);
166 if (!p) {
167 // This can happen if a waiting process gets forcibly terminated.
168 warning("Invalid procid %d in waitlist for proc %d. Maybe a bug?", *i, _pid);
169 } else if (!p->is_suspended()) {
170 // This should never happen.
171 warning("Procid %d in waitlist for proc %d but not marked suspended", *i, _pid);
172 return false;
173 }
174 }
175 return true;
176 }
177
178 } // End of namespace Ultima8
179 } // End of namespace Ultima
180