aboutsummaryrefslogtreecommitdiff
path: root/engines/illusions/thread.cpp
blob: b7ea41b53a8c13462fa5519d94364f505c3e2930 (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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/* 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "illusions/illusions.h"
#include "illusions/thread.h"
#include "illusions/actor.h"

namespace Illusions {

// Thread

Thread::Thread(IllusionsEngine *vm, uint32 threadId, uint32 callingThreadId, uint notifyFlags)
	: _vm(vm), _threadId(threadId), _callingThreadId(callingThreadId), _notifyFlags(notifyFlags),
	_pauseCtr(0), _terminated(false) {
}

Thread::~Thread() {
}

int Thread::onUpdate() {
	return kTSTerminate;
}

void Thread::onSuspend() {
}

void Thread::onNotify() {
}

void Thread::onPause() {
}

void Thread::onUnpause() {
}

void Thread::onResume() {
}

void Thread::onTerminated() {
}

void Thread::onKill() {
	_vm->_controls->threadIsDead(_threadId);
	terminate();
}

uint32 Thread::sendMessage(int msgNum, uint32 msgValue) {
	return 0;
}

void Thread::pause() {
	if (!_terminated) {
		++_pauseCtr;
		if (_pauseCtr == 1)
			onPause();
	}
}

void Thread::unpause() {
	if (!_terminated) {
		--_pauseCtr;
		if (_pauseCtr == 0)
			onUnpause();
	}
}

void Thread::resume() {
	if (!_terminated) {
		--_pauseCtr;
		if (_pauseCtr == 0)
			onResume();
	}
}

void Thread::suspend() {
	if (!_terminated) {
		++_pauseCtr;
		if (_pauseCtr == 1)
			onSuspend();
	}
}

void Thread::notify() {
	if (!_terminated) {
		--_pauseCtr;
		if (_pauseCtr == 0)
			onNotify();
	}
}

int Thread::update() {
	// NOTE Deletion of terminated threads handled in caller
	int status = kTSYield;
	if (!_terminated && _pauseCtr <= 0) {
		status = onUpdate();
		if (status == kTSTerminate)
			terminate();
		else if (status == kTSSuspend)
			suspend();
	}
	return status;
}

void Thread::terminate() {
	if (!_terminated) {
		if (!(_notifyFlags & 1))
			_vm->notifyThreadId(_callingThreadId);
		_callingThreadId = 0;
		onTerminated();
		_terminated = true;
	}
}

// ThreadList

ThreadList::ThreadList(IllusionsEngine *vm)
	: _vm(vm) {
}

void ThreadList::startThread(Thread *thread) {
	// TODO sceneId has to be set by the Thread class
	_threads.push_back(thread);
}

void ThreadList::updateThreads() {
	// TODO Move outer loop to caller
	while (1) {
		Iterator it = _threads.begin();
		while (it != _threads.end()) {
			Thread *thread = *it;
			if (thread->_terminated) {
				delete thread;
				it = _threads.erase(it);
			} else {
				int status = kTSRun;
				while (!thread->_terminated && status != kTSTerminate && status != kTSYield)
					status = thread->update();
				++it;
			}
		}
		if (_vm->_rerunThreads)
			_vm->_rerunThreads = false;
		else
			break;		
	}
}

Thread *ThreadList::findThread(uint32 threadId) {
	for (Iterator it = _threads.begin(); it != _threads.end(); ++it)
		if ((*it)->_threadId == threadId && !(*it)->_terminated)
			return (*it);
	return 0;
}

void ThreadList::suspendId(uint32 threadId) {
	Thread *thread = findThread(threadId);
	if (thread)
		thread->suspend();
}

void ThreadList::notifyId(uint32 threadId) {
	Thread *thread = findThread(threadId);
	if (thread)
		thread->notify();
}

void ThreadList::notifyTimerThreads(uint32 callingThreadId) {
	for (Iterator it = _threads.begin(); it != _threads.end(); ++it) {
		Thread *thread = *it;
		if (thread->_type == kTTTimerThread && thread->_callingThreadId == callingThreadId)
			thread->notify();
	}
}

void ThreadList::suspendTimerThreads(uint32 callingThreadId) {
	for (Iterator it = _threads.begin(); it != _threads.end(); ++it) {
		Thread *thread = *it;
		if (thread->_type == kTTTimerThread && thread->_callingThreadId == callingThreadId)
			thread->suspend();
	}
}

void ThreadList::terminateThreads(uint32 threadId) {
	for (Iterator it = _threads.begin(); it != _threads.end(); ++it) {
		Thread *thread = *it;
		if (thread->_threadId != threadId)
			thread->terminate();
	}
}

void ThreadList::terminateActiveThreads(uint32 threadId) {
	for (Iterator it = _threads.begin(); it != _threads.end(); ++it) {
		Thread *thread = *it;
		if (thread->_pauseCtr <= 0 && thread->_threadId != threadId)
			thread->terminate();
	}
}

void ThreadList::terminateThreadsBySceneId(uint32 sceneId, uint32 threadId) {
	for (Iterator it = _threads.begin(); it != _threads.end(); ++it) {
		Thread *thread = *it;
		if (thread->_sceneId == sceneId && thread->_threadId != threadId)
			thread->terminate();
	}
}

void ThreadList::suspendThreadsBySceneId(uint32 sceneId, uint32 threadId) {
	for (Iterator it = _threads.begin(); it != _threads.end(); ++it) {
		Thread *thread = *it;
		if (thread->_sceneId == sceneId && thread->_threadId != threadId)
			thread->suspend();
	}
}

void ThreadList::notifyThreads(uint32 threadId) {
	for (Iterator it = _threads.begin(); it != _threads.end(); ++it) {
		Thread *thread = *it;
		if (thread->_threadId != threadId)
			thread->notify();
	}
}

void ThreadList::notifyThreadsBySceneId(uint32 sceneId, uint32 threadId) {
	for (Iterator it = _threads.begin(); it != _threads.end(); ++it) {
		Thread *thread = *it;
		if (thread->_sceneId == sceneId && thread->_threadId != threadId)
			thread->notify();
	}
}

void ThreadList::pauseThreads(uint32 threadId) {
	for (Iterator it = _threads.begin(); it != _threads.end(); ++it) {
		Thread *thread = *it;
		if (thread->_threadId != threadId)
			thread->pause();
	}
}

void ThreadList::unpauseThreads(uint32 threadId) {
	for (Iterator it = _threads.begin(); it != _threads.end(); ++it) {
		Thread *thread = *it;
		if (thread->_threadId != threadId)
			thread->unpause();
	}
}

void ThreadList::suspendThreads(uint32 threadId) {
	for (Iterator it = _threads.begin(); it != _threads.end(); ++it) {
		Thread *thread = *it;
		if (thread->_threadId != threadId)
			thread->suspend();
	}
}

void ThreadList::resumeThreads(uint32 threadId) {
	for (Iterator it = _threads.begin(); it != _threads.end(); ++it) {
		Thread *thread = *it;
		if (thread->_threadId != threadId)
			thread->resume();
	}
}

void ThreadList::endTalkThreads() {
	for (Iterator it = _threads.begin(); it != _threads.end(); ++it) {
		Thread *thread = *it;
		if (thread->_type == kTTTalkThread)
			thread->terminate();
	}
}

void ThreadList::endTalkThreadsNoNotify() {
	for (Iterator it = _threads.begin(); it != _threads.end(); ++it) {
		Thread *thread = *it;
		if (thread->_type == kTTTalkThread && thread->_callingThreadId == 0)
			thread->terminate();
	}
}

void ThreadList::terminateThreadChain(uint32 threadId) {
	while (threadId) {
		Thread *thread = findThread(threadId);
		thread->terminate();
		threadId = thread->_callingThreadId;
	}
}

void ThreadList::killThread(uint32 threadId) {

	if (!threadId)
		return;
	
	Thread *thread = findThread(threadId);
	if (!thread)
		return;
		
	for (Iterator it = _threads.begin(); it != _threads.end(); ++it) {
		Thread *childThread = *it;
		if (childThread->_callingThreadId == threadId)
			killThread(childThread->_threadId);
	}

	thread->onKill();

}

void ThreadList::setThreadSceneId(uint32 threadId, uint32 sceneId) {
	Thread *thread = findThread(threadId);
	if (thread)
		thread->_sceneId = sceneId;
}

uint32 ThreadList::getThreadSceneId(uint32 threadId) {
	Thread *thread = findThread(threadId);
	return thread ? thread->_sceneId : 0;
}

bool ThreadList::isActiveThread(int msgNum) {
	// Check if at least one thread returns a non-null value for the message
	for (Iterator it = _threads.begin(); it != _threads.end(); ++it) {
		Thread *thread = *it;
		if (!thread->_terminated && thread->_pauseCtr <= 0 &&
			thread->sendMessage(msgNum, 0) != 0)
			return true;
	}
	return false;
}

} // End of namespace Illusions