KiCad PCB EDA Suite
|
Implement a coroutine. More...
#include <coroutine.h>
Classes | |
class | CALL_CONTEXT |
struct | CONTEXT_T |
struct | INVOCATION_ARGS |
struct | STACK_DELETER |
The size of the mappable memory page size. More... | |
Public Member Functions | |
COROUTINE () | |
template<class T > | |
COROUTINE (T *object, ReturnType(T::*ptr)(ArgType)) | |
Create a coroutine from a member method of an object. | |
COROUTINE (std::function< ReturnType(ArgType)> aEntry) | |
Create a coroutine from a delegate object. | |
~COROUTINE () | |
void | KiYield () |
Stop execution of the coroutine and returns control to the caller. | |
void | KiYield (ReturnType &aRetVal) |
KiYield with a value. | |
void | RunMainStack (std::function< void()> func) |
Run a functor inside the application main stack context. | |
bool | Call (ArgType aArg) |
Start execution of a coroutine, passing args as its arguments. | |
bool | Call (const COROUTINE &aCor, ArgType aArg) |
Start execution of a coroutine, passing args as its arguments. | |
bool | Resume () |
Resume execution of a previously yielded coroutine. | |
bool | Resume (const COROUTINE &aCor) |
Resume execution of a previously yielded coroutine. | |
const ReturnType & | ReturnValue () const |
Return the yielded value (the argument KiYield() was called with). | |
bool | Running () const |
Private Member Functions | |
INVOCATION_ARGS * | doCall (INVOCATION_ARGS *aInvArgs, ArgType aArgs) |
A functor that frees the stack. | |
INVOCATION_ARGS * | doResume (INVOCATION_ARGS *args) |
INVOCATION_ARGS * | jumpIn (INVOCATION_ARGS *args) |
void | jumpOut () |
coroutine stack | |
Static Private Member Functions | |
static size_t | SystemPageSize () |
Map a page-aligned memory region into our address space. | |
static void * | MapMemory (size_t aAllocSize) |
Change protection of memory page(s) to act as stack guards. | |
static void | GuardMemory (void *aAddress, size_t aGuardSize) |
static void | callerStub (intptr_t aData) |
Private Attributes | |
std::unique_ptr< char[], struct STACK_DELETER > | m_stack |
int | m_stacksize |
std::function< ReturnType(ArgType)> | m_func |
bool | m_running |
pointer to coroutine entry arguments. | |
std::remove_reference< ArgType >::type * | m_args |
saved caller context | |
CONTEXT_T | m_caller |
main stack information | |
CALL_CONTEXT * | m_callContext |
saved coroutine context | |
CONTEXT_T | m_callee |
ReturnType | m_retVal |
Implement a coroutine.
Wikipedia has a good explanation:
"Coroutines are computer program components that generalize subroutines to allow multiple entry points for suspending and resuming execution at certain locations. Coroutines are well-suited for implementing more familiar program components such as cooperative tasks, exceptions, event loop, iterators, infinite lists and pipes."
In other words, a coroutine can be considered a lightweight thread - which can be preempted only when it deliberately yields the control to the caller. This way, we avoid concurrency problems such as locking / race conditions.
Uses libcontext library to do the actual context switching.
This particular version takes a DELEGATE as an entry point, so it can invoke methods within a given object as separate coroutines.
See coroutine_example.cpp for sample code.
Definition at line 83 of file coroutine.h.
|
inline |
Definition at line 180 of file coroutine.h.
|
inline |
Create a coroutine from a member method of an object.
Definition at line 189 of file coroutine.h.
|
inline |
Create a coroutine from a delegate object.
Definition at line 197 of file coroutine.h.
References ADVANCED_CFG::GetCfg(), ADVANCED_CFG::m_CoroutineStackSize, and COROUTINE< ReturnType, ArgType >::m_stacksize.
|
inline |
Definition at line 215 of file coroutine.h.
References COROUTINE< ReturnType, ArgType >::CONTEXT_T::ctx, COROUTINE< ReturnType, ArgType >::m_callee, and COROUTINE< ReturnType, ArgType >::m_caller.
|
inline |
Start execution of a coroutine, passing args as its arguments.
Call this method from the application main stack only.
Definition at line 272 of file coroutine.h.
References COROUTINE< ReturnType, ArgType >::CALL_CONTEXT::Continue(), COROUTINE< ReturnType, ArgType >::doCall(), COROUTINE< ReturnType, ArgType >::INVOCATION_ARGS::FROM_ROOT, kicadTraceCoroutineStack, COROUTINE< ReturnType, ArgType >::m_caller, and COROUTINE< ReturnType, ArgType >::Running().
Referenced by TOOL_MANAGER::dispatchInternal().
|
inline |
Start execution of a coroutine, passing args as its arguments.
Call this method for a nested coroutine invocation.
Definition at line 298 of file coroutine.h.
References COROUTINE< ReturnType, ArgType >::doCall(), COROUTINE< ReturnType, ArgType >::INVOCATION_ARGS::FROM_ROUTINE, kicadTraceCoroutineStack, COROUTINE< ReturnType, ArgType >::m_callContext, and COROUTINE< ReturnType, ArgType >::Running().
|
inlinestaticprivate |
Definition at line 497 of file coroutine.h.
References COROUTINE< ReturnType, ArgType >::INVOCATION_ARGS::context, COROUTINE< ReturnType, ArgType >::INVOCATION_ARGS::destination, COROUTINE< ReturnType, ArgType >::INVOCATION_ARGS::FROM_ROOT, COROUTINE< ReturnType, ArgType >::jumpOut(), COROUTINE< ReturnType, ArgType >::m_args, COROUTINE< ReturnType, ArgType >::m_callContext, COROUTINE< ReturnType, ArgType >::m_caller, COROUTINE< ReturnType, ArgType >::m_func, COROUTINE< ReturnType, ArgType >::m_retVal, COROUTINE< ReturnType, ArgType >::m_running, COROUTINE< ReturnType, ArgType >::CALL_CONTEXT::SetMainStack(), and COROUTINE< ReturnType, ArgType >::INVOCATION_ARGS::type.
Referenced by COROUTINE< ReturnType, ArgType >::doCall().
|
inlineprivate |
A functor that frees the stack.
Definition at line 373 of file coroutine.h.
References COROUTINE< ReturnType, ArgType >::callerStub(), COROUTINE< ReturnType, ArgType >::CONTEXT_T::ctx, COROUTINE< ReturnType, ArgType >::GuardMemory(), COROUTINE< ReturnType, ArgType >::jumpIn(), kicadTraceCoroutineStack, COROUTINE< ReturnType, ArgType >::m_args, COROUTINE< ReturnType, ArgType >::m_callee, COROUTINE< ReturnType, ArgType >::m_func, COROUTINE< ReturnType, ArgType >::m_running, COROUTINE< ReturnType, ArgType >::m_stack, COROUTINE< ReturnType, ArgType >::m_stacksize, COROUTINE< ReturnType, ArgType >::MapMemory(), and COROUTINE< ReturnType, ArgType >::SystemPageSize().
Referenced by COROUTINE< ReturnType, ArgType >::Call().
|
inlineprivate |
Definition at line 491 of file coroutine.h.
References COROUTINE< ReturnType, ArgType >::jumpIn().
Referenced by COROUTINE< ReturnType, ArgType >::CALL_CONTEXT::Continue(), and COROUTINE< ReturnType, ArgType >::Resume().
|
inlinestaticprivate |
Definition at line 477 of file coroutine.h.
References kicadTraceCoroutineStack, and res.
Referenced by COROUTINE< ReturnType, ArgType >::doCall().
|
inlineprivate |
Definition at line 516 of file coroutine.h.
References COROUTINE< ReturnType, ArgType >::CONTEXT_T::ctx, kicadTraceCoroutineStack, COROUTINE< ReturnType, ArgType >::m_callee, and COROUTINE< ReturnType, ArgType >::m_caller.
Referenced by COROUTINE< ReturnType, ArgType >::doCall(), and COROUTINE< ReturnType, ArgType >::doResume().
|
inlineprivate |
coroutine stack
Definition at line 533 of file coroutine.h.
References COROUTINE< ReturnType, ArgType >::INVOCATION_ARGS::context, COROUTINE< ReturnType, ArgType >::CONTEXT_T::ctx, COROUTINE< ReturnType, ArgType >::INVOCATION_ARGS::FROM_ROOT, COROUTINE< ReturnType, ArgType >::INVOCATION_ARGS::FROM_ROUTINE, kicadTraceCoroutineStack, COROUTINE< ReturnType, ArgType >::m_callContext, COROUTINE< ReturnType, ArgType >::m_callee, COROUTINE< ReturnType, ArgType >::m_caller, COROUTINE< ReturnType, ArgType >::CALL_CONTEXT::SetMainStack(), and COROUTINE< ReturnType, ArgType >::INVOCATION_ARGS::type.
Referenced by COROUTINE< ReturnType, ArgType >::callerStub(), and COROUTINE< ReturnType, ArgType >::KiYield().
|
inline |
Stop execution of the coroutine and returns control to the caller.
After a yield, Call() or Resume() methods invoked by the caller will immediately return true, indicating that we are not done yet, just asleep.
Definition at line 235 of file coroutine.h.
References COROUTINE< ReturnType, ArgType >::jumpOut().
Referenced by TOOL_MANAGER::ScheduleWait().
|
inline |
KiYield with a value.
Passe a value of given type to the caller. Useful for implementing generator objects.
Definition at line 245 of file coroutine.h.
References COROUTINE< ReturnType, ArgType >::jumpOut(), and COROUTINE< ReturnType, ArgType >::m_retVal.
|
inlinestaticprivate |
Change protection of memory page(s) to act as stack guards.
Definition at line 460 of file coroutine.h.
Referenced by COROUTINE< ReturnType, ArgType >::doCall().
|
inline |
Resume execution of a previously yielded coroutine.
Call this method only from the main application stack.
Definition at line 318 of file coroutine.h.
References COROUTINE< ReturnType, ArgType >::CALL_CONTEXT::Continue(), COROUTINE< ReturnType, ArgType >::doResume(), COROUTINE< ReturnType, ArgType >::INVOCATION_ARGS::FROM_ROOT, kicadTraceCoroutineStack, COROUTINE< ReturnType, ArgType >::m_caller, and COROUTINE< ReturnType, ArgType >::Running().
Referenced by TOOL_MANAGER::dispatchInternal(), and TOOL_MANAGER::ShutdownTool().
|
inline |
Resume execution of a previously yielded coroutine.
Call this method for a nested coroutine invocation.
Definition at line 344 of file coroutine.h.
References COROUTINE< ReturnType, ArgType >::doResume(), COROUTINE< ReturnType, ArgType >::INVOCATION_ARGS::FROM_ROUTINE, kicadTraceCoroutineStack, COROUTINE< ReturnType, ArgType >::m_callContext, and COROUTINE< ReturnType, ArgType >::Running().
|
inline |
Return the yielded value (the argument KiYield() was called with).
Definition at line 359 of file coroutine.h.
References COROUTINE< ReturnType, ArgType >::m_retVal.
|
inline |
Run a functor inside the application main stack context.
Call this function for example if the operation will spawn a webkit browser instance which will walk the stack to the upper border of the address space on mac osx systems because its javascript needs garbage collection (for example if you paste text into an edit box).
Definition at line 258 of file coroutine.h.
References COROUTINE< ReturnType, ArgType >::m_callContext, and COROUTINE< ReturnType, ArgType >::CALL_CONTEXT::RunMainStack().
Referenced by TOOL_MANAGER::RunMainStack().
|
inline |
Definition at line 367 of file coroutine.h.
References COROUTINE< ReturnType, ArgType >::m_running.
Referenced by COROUTINE< ReturnType, ArgType >::Call(), TOOL_MANAGER::dispatchInternal(), and COROUTINE< ReturnType, ArgType >::Resume().
|
inlinestaticprivate |
Map a page-aligned memory region into our address space.
Definition at line 442 of file coroutine.h.
Referenced by COROUTINE< ReturnType, ArgType >::doCall().
|
private |
saved caller context
Definition at line 571 of file coroutine.h.
Referenced by COROUTINE< ReturnType, ArgType >::callerStub(), and COROUTINE< ReturnType, ArgType >::doCall().
|
private |
saved coroutine context
Definition at line 577 of file coroutine.h.
Referenced by COROUTINE< ReturnType, ArgType >::Call(), COROUTINE< ReturnType, ArgType >::callerStub(), COROUTINE< ReturnType, ArgType >::jumpOut(), COROUTINE< ReturnType, ArgType >::Resume(), and COROUTINE< ReturnType, ArgType >::RunMainStack().
|
private |
Definition at line 580 of file coroutine.h.
Referenced by COROUTINE< ReturnType, ArgType >::doCall(), COROUTINE< ReturnType, ArgType >::jumpIn(), COROUTINE< ReturnType, ArgType >::jumpOut(), COROUTINE< ReturnType, ArgType >::CALL_CONTEXT::RunMainStack(), and COROUTINE< ReturnType, ArgType >::~COROUTINE().
|
private |
main stack information
Definition at line 574 of file coroutine.h.
Referenced by COROUTINE< ReturnType, ArgType >::Call(), COROUTINE< ReturnType, ArgType >::callerStub(), COROUTINE< ReturnType, ArgType >::jumpIn(), COROUTINE< ReturnType, ArgType >::jumpOut(), COROUTINE< ReturnType, ArgType >::Resume(), and COROUTINE< ReturnType, ArgType >::~COROUTINE().
|
private |
Definition at line 565 of file coroutine.h.
Referenced by COROUTINE< ReturnType, ArgType >::callerStub(), and COROUTINE< ReturnType, ArgType >::doCall().
|
private |
Definition at line 582 of file coroutine.h.
Referenced by COROUTINE< ReturnType, ArgType >::callerStub(), COROUTINE< ReturnType, ArgType >::KiYield(), and COROUTINE< ReturnType, ArgType >::ReturnValue().
|
private |
pointer to coroutine entry arguments.
Stripped of references to avoid compiler errors.
Definition at line 567 of file coroutine.h.
Referenced by COROUTINE< ReturnType, ArgType >::callerStub(), COROUTINE< ReturnType, ArgType >::doCall(), and COROUTINE< ReturnType, ArgType >::Running().
|
private |
Definition at line 560 of file coroutine.h.
Referenced by COROUTINE< ReturnType, ArgType >::doCall().
|
private |
Definition at line 563 of file coroutine.h.
Referenced by COROUTINE< ReturnType, ArgType >::COROUTINE(), and COROUTINE< ReturnType, ArgType >::doCall().