|
KiCad PCB EDA Suite
|
Implement a coroutine. More...
#include <coroutine.h>
Classes | |
| class | CALL_CONTEXT |
| struct | CONTEXT_T |
| struct | INVOCATION_ARGS |
| struct | STACK_DELETER |
| A functor that frees the stack. 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) |
| INVOCATION_ARGS * | doResume (INVOCATION_ARGS *args) |
| INVOCATION_ARGS * | jumpIn (INVOCATION_ARGS *args) |
| void | jumpOut () |
Static Private Member Functions | |
| static size_t | SystemPageSize () |
| The size of the mappable memory page size. | |
| static void * | MapMemory (size_t aAllocSize) |
| Map a page-aligned memory region into our address space. | |
| static void | GuardMemory (void *aAddress, size_t aGuardSize) |
| Change protection of memory page(s) to act as stack guards. | |
| static void | callerStub (intptr_t aData) |
Private Attributes | |
| std::unique_ptr< char[], struct STACK_DELETER > | m_stack |
| Coroutine stack. | |
| int | m_stacksize |
| std::function< ReturnType(ArgType)> | m_func |
| bool | m_running |
| std::remove_reference< ArgType >::type * | m_args |
| Pointer to coroutine entry arguments stripped of references to avoid compiler errors. | |
| CONTEXT_T | m_caller |
| Saved caller context. | |
| CALL_CONTEXT * | m_callContext |
| Main stack information. | |
| CONTEXT_T | m_callee |
| Saved coroutine context. | |
| 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 79 of file coroutine.h.
|
inline |
Definition at line 187 of file coroutine.h.
Referenced by COROUTINE< ReturnType, ArgType >::CALL_CONTEXT::RunMainStack().
|
inline |
Create a coroutine from a member method of an object.
Definition at line 196 of file coroutine.h.
|
inline |
Create a coroutine from a delegate object.
Definition at line 204 of file coroutine.h.
|
inline |
Definition at line 225 of file coroutine.h.
|
inline |
Start execution of a coroutine, passing args as its arguments.
Call this method from the application main stack only.
Definition at line 282 of file coroutine.h.
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 308 of file coroutine.h.
|
inlinestaticprivate |
Definition at line 514 of file coroutine.h.
|
inlineprivate |
Definition at line 383 of file coroutine.h.
Referenced by COROUTINE< int, int >::Call(), and COROUTINE< int, int >::Call().
|
inlineprivate |
Definition at line 508 of file coroutine.h.
Referenced by COROUTINE< ReturnType, ArgType >::CALL_CONTEXT::Continue(), COROUTINE< int, int >::Resume(), and COROUTINE< int, int >::Resume().
|
inlinestaticprivate |
Change protection of memory page(s) to act as stack guards.
Definition at line 494 of file coroutine.h.
Referenced by COROUTINE< int, int >::doCall().
|
inlineprivate |
Definition at line 544 of file coroutine.h.
Referenced by COROUTINE< int, int >::doCall(), and COROUTINE< int, int >::doResume().
|
inlineprivate |
Definition at line 570 of file coroutine.h.
Referenced by COROUTINE< int, int >::callerStub(), COROUTINE< int, int >::KiYield(), and COROUTINE< int, int >::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 245 of file coroutine.h.
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 255 of file coroutine.h.
|
inlinestaticprivate |
Map a page-aligned memory region into our address space.
Definition at line 477 of file coroutine.h.
Referenced by COROUTINE< int, int >::doCall().
|
inline |
Resume execution of a previously yielded coroutine.
Call this method only from the main application stack.
Definition at line 328 of file coroutine.h.
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 354 of file coroutine.h.
|
inline |
Return the yielded value (the argument KiYield() was called with).
Definition at line 369 of file coroutine.h.
|
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 268 of file coroutine.h.
Referenced by TOOL_MANAGER::RunMainStack().
|
inline |
Definition at line 377 of file coroutine.h.
Referenced by COROUTINE< int, int >::Call(), COROUTINE< int, int >::Call(), TOOL_MANAGER::dispatchInternal(), COROUTINE< int, int >::Resume(), and COROUTINE< int, int >::Resume().
|
inlinestaticprivate |
The size of the mappable memory page size.
Definition at line 457 of file coroutine.h.
Referenced by COROUTINE< int, int >::doCall().
|
private |
Pointer to coroutine entry arguments stripped of references to avoid compiler errors.
Definition at line 618 of file coroutine.h.
Referenced by COROUTINE< int, int >::callerStub().
|
private |
Main stack information.
Definition at line 624 of file coroutine.h.
Referenced by COROUTINE< int, int >::Call(), COROUTINE< int, int >::callerStub(), and COROUTINE< int, int >::Resume().
|
private |
Saved coroutine context.
Definition at line 627 of file coroutine.h.
Referenced by COROUTINE< ReturnType, ArgType >::CALL_CONTEXT::RunMainStack().
|
private |
Saved caller context.
Definition at line 621 of file coroutine.h.
Referenced by COROUTINE< int, int >::callerStub().
|
private |
Definition at line 613 of file coroutine.h.
Referenced by COROUTINE< int, int >::callerStub().
|
private |
Definition at line 629 of file coroutine.h.
Referenced by COROUTINE< int, int >::callerStub().
|
private |
Definition at line 615 of file coroutine.h.
Referenced by COROUTINE< int, int >::callerStub().
|
private |
Coroutine stack.
Definition at line 608 of file coroutine.h.
|
private |
Definition at line 611 of file coroutine.h.