22 #ifndef MTROPOLIS_VTHREAD_H 23 #define MTROPOLIS_VTHREAD_H 25 #include "mtropolis/coroutine_protos.h" 26 #include "mtropolis/coroutine_return_value.h" 27 #include "mtropolis/debug.h" 31 struct ICoroutineManager;
57 virtual VThreadState execute(
VThread *thread) = 0;
59 #ifdef MTROPOLIS_DEBUG_ENABLE 61 void debugInit(
const char *name);
64 SupportStatus debugGetSupportStatus()
const override {
return kSupportStatusDone; }
65 const char *debugGetTypeName()
const override {
return "Task"; }
66 const Common::String &debugGetName()
const override {
return _debugName; }
96 template<
typename TClass,
typename TData>
103 VThreadState execute(
VThread *thread)
override;
110 VThreadState (TClass::*_method)(
const TData &data);
114 template<
typename TData>
123 VThreadState execute(
VThread *thread)
override;
129 VThreadState (*_func)(
const TData &data);
138 template<
typename TClass,
typename TData>
139 TData *pushTask(
const char *name, TClass *obj, VThreadState (TClass::*method)(
const TData &data));
141 template<
typename TData>
142 TData *pushTask(
const char *name, VThreadState (*func)(
const TData &data));
146 bool hasTasks()
const;
152 template<
typename TCoroutine,
typename TReturnValue,
typename ...TParams>
153 void pushCoroutineWithReturn(TReturnValue *returnValuePtr, TParams &&...args);
155 template<
typename TCoroutine,
typename TReturnValue>
156 void pushCoroutineWithReturn(TReturnValue *returnValuePtr);
158 template<
typename TCoroutine,
typename... TParams>
159 void pushCoroutine(TParams &&...args);
161 template<
typename TCoroutine>
162 void pushCoroutine();
166 void reserveFrame(
size_t frameAlignment,
size_t frameSize,
VThreadStackFrame *&outFramePtr,
size_t dataAlignment,
size_t dataSize,
void *&outDataPtr,
bool &outIsNewChunk);
167 static bool reserveFrameInChunk(
VThreadStackChunk *chunk,
size_t frameAlignment,
size_t frameSize,
VThreadStackFrame *&outFramePtr,
size_t dataAlignment,
size_t dataSize,
void *&outDataPtr);
171 template<
typename TClass,
typename TData>
172 TData *pushTaskWithFaultHandler(
const VThreadFaultIdentifier *faultID,
const char *name, TClass *obj, VThreadState (TClass::*method)(
const TData &data));
174 template<
typename TData>
175 TData *pushTaskWithFaultHandler(
const VThreadFaultIdentifier *faultID,
const char *name, VThreadState (*func)(
const TData &data));
179 uint _numActiveStackChunks;
182 template<
typename TClass,
typename TData>
184 : _faultID(faultID), _target(target), _method(method) {
187 template<
typename TClass,
typename TData>
189 : _faultID(other._faultID), _target(other._target), _method(other._method), _data(other._data) {
192 template<
typename TClass,
typename TData>
194 : _faultID(other._faultID), _target(other._target), _method(other._method), _data(static_cast<TData &&>(other._data)) {
197 template<
typename TClass,
typename TData>
199 TData data(static_cast<TData &&>(_data));
201 TClass *target = _target;
202 VThreadState (TClass::*method)(
const TData &) = _method;
206 return (target->*method)(data);
209 template<
typename TClass,
typename TData>
214 template<
typename TData>
216 : _faultID(faultID), _func(func) {
219 template<
typename TData>
221 : _faultID(other._faultID), _func(other._func), _data(other._data) {
224 template<
typename TData>
226 : _faultID(other._faultID), _func(other._func), _data(static_cast<TData &&>(other._data)) {
229 template<
typename TData>
231 TData data(static_cast<TData &&>(_data));
233 VThreadState (*func)(
const TData &) = _func;
240 template<
typename TData>
245 template<
typename TCoroutine,
typename TReturnValue,
typename... TParams>
246 void VThread::pushCoroutineWithReturn(TReturnValue *returnValuePtr, TParams &&...args) {
247 assert(returnValuePtr !=
nullptr);
251 template<
typename TCoroutine,
typename TReturnValue>
252 void VThread::pushCoroutineWithReturn(TReturnValue *returnValuePtr) {
253 assert(returnValuePtr !=
nullptr);
257 template<
typename TCoroutine,
typename... TParams>
258 void VThread::pushCoroutine(TParams &&...args) {
262 template<
typename TCoroutine>
263 void VThread::pushCoroutine() {
267 template<
typename TClass,
typename TData>
268 TData *VThread::pushTask(
const char *name, TClass *obj, VThreadState (TClass::*method)(
const TData &data)) {
269 return this->pushTaskWithFaultHandler(
nullptr, name, obj, method);
272 template<
typename TData>
273 TData *VThread::pushTask(
const char *name, VThreadState (*func)(
const TData &data)) {
274 return this->pushTaskWithFaultHandler(
nullptr, name, func);
277 template<
typename TClass,
typename TData>
278 TData *VThread::pushTaskWithFaultHandler(
const VThreadFaultIdentifier *faultID,
const char *name, TClass *obj, VThreadState (TClass::*method)(
const TData &data)) {
282 const size_t dataAlignment =
alignof(FrameData_t);
285 if (_numActiveStackChunks > 0)
286 prevFrame = _stackChunks[_numActiveStackChunks - 1]._topFrame;
289 void *dataPtr =
nullptr;
290 bool isNewChunk =
false;
291 reserveFrame(frameAlignment,
sizeof(
VThreadStackFrame), framePtr, dataAlignment,
sizeof(FrameData_t), dataPtr, isNewChunk);
294 FrameData_t *frameData =
new (dataPtr) FrameData_t(faultID, obj, method);
296 frame->data = frameData;
297 frame->prevFrame = prevFrame;
298 frame->isLastInChunk = isNewChunk;
300 #ifdef MTROPOLIS_DEBUG_ENABLE 301 frameData->debugInit(name);
304 return &frameData->getData();
307 template<
typename TData>
308 TData *VThread::pushTaskWithFaultHandler(
const VThreadFaultIdentifier *faultID,
const char *name, VThreadState (*func)(
const TData &data)) {
312 const size_t dataAlignment =
alignof(FrameData_t);
315 if (_numActiveStackChunks > 0)
316 prevFrame = _stackChunks[_numActiveStackChunks - 1]._topFrame;
319 void *dataPtr =
nullptr;
320 bool isNewChunk =
false;
321 reserveFrame(frameAlignment,
sizeof(
VThreadStackFrame), framePtr, dataAlignment,
sizeof(FrameData_t), dataPtr, isNewChunk);
324 FrameData_t *frameData =
new (dataPtr) FrameData_t(faultID, func);
326 frame->data = frameData;
327 frame->prevFrame = prevFrame;
328 frame->isLastInChunk = isNewChunk;
330 #ifdef MTROPOLIS_DEBUG_ENABLE 331 frameData->debugInit(name);
334 return &frameData->getData();
Definition: coroutine_return_value.h:27
Definition: coroutine_protos.h:42
Definition: coroutines.h:50
Definition: vthread.h:115
Definition: vthread.h:133
Definition: coroutine_return_value.h:31
Definition: coroutine_manager.h:34