File: vthread.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (243 lines) | stat: -rw-r--r-- 7,353 bytes parent folder | download | duplicates (2)
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
/* 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 "mtropolis/coroutines.h"
#include "mtropolis/coroutine_manager.h"
#include "mtropolis/vthread.h"

namespace MTropolis {

VThreadTaskData::VThreadTaskData() {
}

VThreadTaskData::~VThreadTaskData() {
}

VThreadStackChunk::VThreadStackChunk(size_t capacity)
	: _memory(nullptr), _size(capacity), _topFrame(nullptr) {

	_memory = static_cast<byte *>(malloc(capacity));
	if (!_memory)
		error("Out of memory");
}

VThreadStackChunk::VThreadStackChunk(VThreadStackChunk &&other)
	: _memory(other._memory), _size(other._size), _topFrame(other._topFrame) {
	other._memory = nullptr;
	other._size = 0;
	other._topFrame = nullptr;
}

VThreadStackChunk::~VThreadStackChunk() {
	if (_memory)
		free(_memory);
}

#ifdef MTROPOLIS_DEBUG_ENABLE
void VThreadTaskData::debugInit(const char *name) {
	_debugName = name;
}

void VThreadTaskData::debugInspect(IDebugInspectionReport *report) const {
}
#endif

VThread::VThread(ICoroutineManager *coroManager)
	: _numActiveStackChunks(0), _coroManager(coroManager) {
}

VThread::~VThread() {
	while (popFrame()) {
	}
}

VThreadState VThread::step() {
	while (hasTasks()) {
		VThreadStackFrame *frame = _stackChunks[_numActiveStackChunks - 1]._topFrame;

		VThreadState state = frame->data->execute(this);
		if (state != kVThreadReturn)
			return state;
	}

	return kVThreadReturn;
}

bool VThread::hasTasks() const {
	return _numActiveStackChunks > 0;
}

bool VThread::reserveFrameInChunk(VThreadStackChunk *chunk, size_t frameAlignment, size_t frameSize, VThreadStackFrame *&outFramePtr, size_t dataAlignment, size_t dataSize, void *&outDataPtr) {
	VThreadStackFrame *framePtr = nullptr;
	void *dataPtr = nullptr;

	uintptr address = 0;
	size_t bytesAvailable = 0;

	if (chunk->_topFrame) {
		address = reinterpret_cast<uintptr>(chunk->_topFrame);
		bytesAvailable = static_cast<size_t>(reinterpret_cast<const byte *>(chunk->_topFrame) - chunk->_memory);
	} else {
		address = reinterpret_cast<uintptr>(chunk->_memory + chunk->_size);
		bytesAvailable = chunk->_size;
	}

	if (bytesAvailable < dataSize)
		return false;

	bytesAvailable -= dataSize;
	address -= dataSize;

	size_t dataAlignPadding = static_cast<size_t>(address % dataAlignment);

	if (bytesAvailable < dataAlignPadding)
		return false;

	bytesAvailable -= dataAlignPadding;
	address -= dataAlignPadding;

	dataPtr = reinterpret_cast<void *>(address);

	if (bytesAvailable < frameSize)
		return false;

	bytesAvailable -= frameSize;
	address -= frameSize;

	size_t frameAlignPadding = static_cast<size_t>(address % frameAlignment);

	if (bytesAvailable < frameAlignPadding)
		return false;

	bytesAvailable -= frameAlignPadding;
	address -= frameAlignPadding;

	framePtr = reinterpret_cast<VThreadStackFrame *>(address);

	chunk->_topFrame = framePtr;

	outDataPtr = dataPtr;
	outFramePtr = framePtr;

	return true;
}

void VThread::reserveFrame(size_t frameAlignment, size_t frameSize, VThreadStackFrame *&outFramePtr, size_t dataAlignment, size_t dataSize, void *&outDataPtr, bool &outIsNewChunk) {
	// See if this fits in the last active chunk
	if (_numActiveStackChunks > 0) {
		VThreadStackChunk &lastChunk = _stackChunks[_numActiveStackChunks - 1];

		if (reserveFrameInChunk(&lastChunk, frameAlignment, frameSize, outFramePtr, dataAlignment, dataSize, outDataPtr)) {
			outIsNewChunk = false;
			return;
		}
	}

	// Didn't fit, this is the first one in the chunk
	size_t requiredSize = (frameAlignment - 1) + (dataAlignment - 1) + frameSize + dataSize;

	if (_numActiveStackChunks >= _stackChunks.size() || _stackChunks[_numActiveStackChunks]._size < requiredSize) {
		// Doesn't fit in the next chunk, deallocate the chunk and all subsequent chunks and reallocate

		const size_t kChunkMinSize = 1024 * 1024;	// 1MB chunks
		size_t chunkSize = requiredSize;
		if (chunkSize < kChunkMinSize)
			chunkSize = kChunkMinSize;

		while (_stackChunks.size() > _numActiveStackChunks)
			_stackChunks.pop_back();

		_stackChunks.push_back(VThreadStackChunk(chunkSize));
	}

	VThreadStackChunk &lastChunk = _stackChunks[_numActiveStackChunks++];

	bool reservedOK = reserveFrameInChunk(&lastChunk, frameAlignment, frameSize, outFramePtr, dataAlignment, dataSize, outDataPtr);
	assert(reservedOK);
	(void)reservedOK;

	outIsNewChunk = true;
}

void VThread::pushCoroutineInternal(CompiledCoroutine **compiledCoroPtr, CoroutineCompileFunction_t compileFunction, bool isVoidReturn, const CoroutineParamsBase &params, const CoroutineReturnValueRefBase &returnValueRef) {
	const CompiledCoroutine *compiledCoro = *compiledCoroPtr;
	if (compiledCoro == nullptr) {
		_coroManager->compileCoroutine(compiledCoroPtr, compileFunction, isVoidReturn);
		compiledCoro = *compiledCoroPtr;
		assert(compiledCoro);
	}

	pushCoroutineFrame(compiledCoro, params, returnValueRef);
}

VThreadTaskData *VThread::pushCoroutineFrame(const CompiledCoroutine *compiledCoro, const CoroutineParamsBase &params, const CoroutineReturnValueRefBase &returnValueRef) {
	const size_t frameAlignment = alignof(VThreadStackFrame);
	size_t dataAlignment = 0;
	size_t dataSize = 0;

	compiledCoro->_getFrameParameters(dataSize, dataAlignment);

	VThreadStackFrame *prevFrame = nullptr;
	if (_numActiveStackChunks > 0)
		prevFrame = _stackChunks[_numActiveStackChunks - 1]._topFrame;

	VThreadStackFrame *framePtr = nullptr;
	void *dataPtr = nullptr;
	bool isNewChunk = false;
	reserveFrame(frameAlignment, sizeof(VThreadStackFrame), framePtr, dataAlignment, dataSize, dataPtr, isNewChunk);

	VThreadStackFrame *frame = new (framePtr) VThreadStackFrame();
	VThreadTaskData *frameData = compiledCoro->_frameConstructor(dataPtr, compiledCoro, params, returnValueRef);

	frame->data = frameData;
	frame->prevFrame = prevFrame;
	frame->isLastInChunk = isNewChunk;

	return frameData;
}

bool VThread::popFrame() {
	if (_numActiveStackChunks == 0)
		return false;

	VThreadStackChunk &lastChunk = _stackChunks[_numActiveStackChunks - 1];
	VThreadStackFrame *topFrame = lastChunk._topFrame;

	VThreadStackFrame *secondFrame = topFrame->prevFrame;
	bool isLastFrameInChunk = topFrame->isLastInChunk;

	if (isLastFrameInChunk) {
		lastChunk._topFrame = nullptr;
		_numActiveStackChunks--;
	} else {
		assert(reinterpret_cast<byte *>(secondFrame) >= lastChunk._memory);
		assert(reinterpret_cast<byte *>(secondFrame) < lastChunk._memory + lastChunk._size);

		lastChunk._topFrame = secondFrame;
	}

	topFrame->data->~VThreadTaskData();
	topFrame->~VThreadStackFrame();

	return true;
}

} // End of namespace MTropolis