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
|
/* 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/>.
*
*/
#ifndef MTROPOLIS_VTHREAD_H
#define MTROPOLIS_VTHREAD_H
#include "mtropolis/coroutine_protos.h"
#include "mtropolis/coroutine_return_value.h"
#include "mtropolis/debug.h"
namespace MTropolis {
struct ICoroutineManager;
class VThread;
// Virtual thread, really a task stack
enum VThreadState {
kVThreadReturn,
kVThreadSuspended,
kVThreadError,
};
struct VThreadFaultIdentifier {
};
template<typename T>
struct VThreadFaultIdentifierSingleton {
static VThreadFaultIdentifier _identifier;
};
template<typename T>
VThreadFaultIdentifier VThreadFaultIdentifierSingleton<T>::_identifier;
class VThreadTaskData : public Debuggable {
public:
VThreadTaskData();
virtual ~VThreadTaskData();
virtual VThreadState execute(VThread *thread) = 0;
#ifdef MTROPOLIS_DEBUG_ENABLE
public:
void debugInit(const char *name);
protected:
SupportStatus debugGetSupportStatus() const override { return kSupportStatusDone; }
const char *debugGetTypeName() const override { return "Task"; }
const Common::String &debugGetName() const override { return _debugName; }
void debugInspect(IDebugInspectionReport *report) const override;
Common::String _debugName;
#endif
};
struct VThreadStackFrame;
class VThreadStackChunk {
public:
explicit VThreadStackChunk(size_t capacity);
VThreadStackChunk(VThreadStackChunk &&other);
~VThreadStackChunk();
VThreadStackFrame *_topFrame;
byte *_memory;
size_t _size;
private:
VThreadStackChunk() = delete;
VThreadStackChunk(const VThreadStackChunk &) = delete;
};
struct VThreadStackFrame {
VThreadTaskData *data;
VThreadStackFrame *prevFrame;
bool isLastInChunk;
};
template<typename TClass, typename TData>
class VThreadMethodData : public VThreadTaskData {
public:
VThreadMethodData(const VThreadFaultIdentifier *faultID, TClass *target, VThreadState (TClass::*method)(const TData &data));
VThreadMethodData(const VThreadMethodData &other);
VThreadMethodData(VThreadMethodData &&other);
VThreadState execute(VThread *thread) override;
TData &getData();
private:
const VThreadFaultIdentifier *_faultID;
TClass *_target;
VThreadState (TClass::*_method)(const TData &data);
TData _data;
};
template<typename TData>
class VThreadFunctionData : public VThreadTaskData {
public:
explicit VThreadFunctionData(const VThreadFaultIdentifier *faultID, VThreadState (*func)(const TData &data));
VThreadFunctionData(const VThreadFunctionData &other);
VThreadFunctionData(VThreadFunctionData &&other);
VThreadState execute(VThread *thread) override;
TData &getData();
private:
const VThreadFaultIdentifier *_faultID;
VThreadState (*_func)(const TData &data);
TData _data;
};
class VThread {
public:
explicit VThread(ICoroutineManager *coroManager);
~VThread();
template<typename TClass, typename TData>
TData *pushTask(const char *name, TClass *obj, VThreadState (TClass::*method)(const TData &data));
template<typename TData>
TData *pushTask(const char *name, VThreadState (*func)(const TData &data));
VThreadState step();
bool hasTasks() const;
bool popFrame();
VThreadTaskData *pushCoroutineFrame(const CompiledCoroutine *compiledCoro, const CoroutineParamsBase ¶ms, const CoroutineReturnValueRefBase &returnValueRef);
template<typename TCoroutine, typename TReturnValue, typename ...TParams>
void pushCoroutineWithReturn(TReturnValue *returnValuePtr, TParams &&...args);
template<typename TCoroutine, typename TReturnValue>
void pushCoroutineWithReturn(TReturnValue *returnValuePtr);
template<typename TCoroutine, typename... TParams>
void pushCoroutine(TParams &&...args);
template<typename TCoroutine>
void pushCoroutine();
private:
void reserveFrame(size_t frameAlignment, size_t frameSize, VThreadStackFrame *&outFramePtr, size_t dataAlignment, size_t dataSize, void *&outDataPtr, bool &outIsNewChunk);
static bool reserveFrameInChunk(VThreadStackChunk *chunk, size_t frameAlignment, size_t frameSize, VThreadStackFrame *&outFramePtr, size_t dataAlignment, size_t dataSize, void *&outDataPtr);
void pushCoroutineInternal(CompiledCoroutine **compiledCoroPtr, CoroutineCompileFunction_t compileFunc, bool isVoidReturn, const CoroutineParamsBase ¶ms, const CoroutineReturnValueRefBase &returnValueRef);
template<typename TClass, typename TData>
TData *pushTaskWithFaultHandler(const VThreadFaultIdentifier *faultID, const char *name, TClass *obj, VThreadState (TClass::*method)(const TData &data));
template<typename TData>
TData *pushTaskWithFaultHandler(const VThreadFaultIdentifier *faultID, const char *name, VThreadState (*func)(const TData &data));
Common::Array<VThreadStackChunk> _stackChunks;
ICoroutineManager *_coroManager;
uint _numActiveStackChunks;
};
template<typename TClass, typename TData>
VThreadMethodData<TClass, TData>::VThreadMethodData(const VThreadFaultIdentifier *faultID, TClass *target, VThreadState (TClass::*method)(const TData &data))
: _faultID(faultID), _target(target), _method(method) {
}
template<typename TClass, typename TData>
VThreadMethodData<TClass, TData>::VThreadMethodData(const VThreadMethodData& other)
: _faultID(other._faultID), _target(other._target), _method(other._method), _data(other._data) {
}
template<typename TClass, typename TData>
VThreadMethodData<TClass, TData>::VThreadMethodData(VThreadMethodData &&other)
: _faultID(other._faultID), _target(other._target), _method(other._method), _data(static_cast<TData &&>(other._data)) {
}
template<typename TClass, typename TData>
VThreadState VThreadMethodData<TClass, TData>::execute(VThread *thread) {
TData data(static_cast<TData &&>(_data));
TClass *target = _target;
VThreadState (TClass::*method)(const TData &) = _method;
thread->popFrame();
return (target->*method)(data);
}
template<typename TClass, typename TData>
TData &VThreadMethodData<TClass, TData>::getData() {
return _data;
}
template<typename TData>
VThreadFunctionData<TData>::VThreadFunctionData(const VThreadFaultIdentifier *faultID, VThreadState (*func)(const TData &data))
: _faultID(faultID), _func(func) {
}
template<typename TData>
VThreadFunctionData<TData>::VThreadFunctionData(const VThreadFunctionData &other)
: _faultID(other._faultID), _func(other._func), _data(other._data) {
}
template<typename TData>
VThreadFunctionData<TData>::VThreadFunctionData(VThreadFunctionData &&other)
: _faultID(other._faultID), _func(other._func), _data(static_cast<TData &&>(other._data)) {
}
template<typename TData>
VThreadState VThreadFunctionData<TData>::execute(VThread *thread) {
TData data(static_cast<TData &&>(_data));
VThreadState (*func)(const TData &) = _func;
thread->popFrame();
return func(data);
}
template<typename TData>
TData &VThreadFunctionData<TData>::getData() {
return _data;
}
template<typename TCoroutine, typename TReturnValue, typename... TParams>
void VThread::pushCoroutineWithReturn(TReturnValue *returnValuePtr, TParams &&...args) {
assert(returnValuePtr != nullptr);
this->pushCoroutineInternal(&TCoroutine::ms_compiledCoro, TCoroutine::compileCoroutine, CoroutineReturnValueRef<typename TCoroutine::ReturnValue_t>::isVoid(), typename TCoroutine::Params(Common::forward<TParams>(args)...), CoroutineReturnValueRef<typename TCoroutine::ReturnValue_t>(returnValuePtr));
}
template<typename TCoroutine, typename TReturnValue>
void VThread::pushCoroutineWithReturn(TReturnValue *returnValuePtr) {
assert(returnValuePtr != nullptr);
this->pushCoroutineInternal(&TCoroutine::ms_compiledCoro, TCoroutine::compileCoroutine, CoroutineReturnValueRef<typename TCoroutine::ReturnValue_t>::isVoid(), typename TCoroutine::Params(), CoroutineReturnValueRef<typename TCoroutine::ReturnValue_t>(returnValuePtr));
}
template<typename TCoroutine, typename... TParams>
void VThread::pushCoroutine(TParams &&...args) {
this->pushCoroutineInternal(&TCoroutine::ms_compiledCoro, TCoroutine::compileCoroutine, CoroutineReturnValueRef<typename TCoroutine::ReturnValue_t>::isVoid(), typename TCoroutine::Params(Common::forward<TParams>(args)...), CoroutineReturnValueRef<typename TCoroutine::ReturnValue_t>());
}
template<typename TCoroutine>
void VThread::pushCoroutine() {
this->pushCoroutineInternal(&TCoroutine::ms_compiledCoro, TCoroutine::compileCoroutine, CoroutineReturnValueRef<typename TCoroutine::ReturnValue_t>::isVoid(), typename TCoroutine::Params(), CoroutineReturnValueRef<typename TCoroutine::ReturnValue_t>());
}
template<typename TClass, typename TData>
TData *VThread::pushTask(const char *name, TClass *obj, VThreadState (TClass::*method)(const TData &data)) {
return this->pushTaskWithFaultHandler(nullptr, name, obj, method);
}
template<typename TData>
TData *VThread::pushTask(const char *name, VThreadState (*func)(const TData &data)) {
return this->pushTaskWithFaultHandler(nullptr, name, func);
}
template<typename TClass, typename TData>
TData *VThread::pushTaskWithFaultHandler(const VThreadFaultIdentifier *faultID, const char *name, TClass *obj, VThreadState (TClass::*method)(const TData &data)) {
typedef VThreadMethodData<TClass, TData> FrameData_t;
const size_t frameAlignment = alignof(VThreadStackFrame);
const size_t dataAlignment = alignof(FrameData_t);
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, sizeof(FrameData_t), dataPtr, isNewChunk);
VThreadStackFrame *frame = new (framePtr) VThreadStackFrame();
FrameData_t *frameData = new (dataPtr) FrameData_t(faultID, obj, method);
frame->data = frameData;
frame->prevFrame = prevFrame;
frame->isLastInChunk = isNewChunk;
#ifdef MTROPOLIS_DEBUG_ENABLE
frameData->debugInit(name);
#endif
return &frameData->getData();
}
template<typename TData>
TData *VThread::pushTaskWithFaultHandler(const VThreadFaultIdentifier *faultID, const char *name, VThreadState (*func)(const TData &data)) {
typedef VThreadFunctionData<TData> FrameData_t;
const size_t frameAlignment = alignof(VThreadStackFrame);
const size_t dataAlignment = alignof(FrameData_t);
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, sizeof(FrameData_t), dataPtr, isNewChunk);
VThreadStackFrame *frame = new (framePtr) VThreadStackFrame();
FrameData_t *frameData = new (dataPtr) FrameData_t(faultID, func);
frame->data = frameData;
frame->prevFrame = prevFrame;
frame->isLastInChunk = isNewChunk;
#ifdef MTROPOLIS_DEBUG_ENABLE
frameData->debugInit(name);
#endif
return &frameData->getData();
}
} // End of namespace MTropolis
#endif
|