2013-08-02 14:46:53 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 CERN
|
|
|
|
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
|
2019-02-16 20:53:25 +00:00
|
|
|
* Copyright (C) 2016-2019 KiCad Developers, see AUTHORS.txt for contributors.
|
2013-08-02 14:46:53 +00:00
|
|
|
*
|
|
|
|
* 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 2
|
|
|
|
* 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, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __COROUTINE_H
|
|
|
|
#define __COROUTINE_H
|
|
|
|
|
2018-12-24 14:16:20 +00:00
|
|
|
#include <cassert>
|
2013-08-02 14:46:53 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
|
2016-07-04 22:14:32 +00:00
|
|
|
#include <type_traits>
|
2013-08-02 14:46:53 +00:00
|
|
|
|
2017-03-07 13:30:15 +00:00
|
|
|
#include <system/libcontext.h>
|
2017-01-20 17:33:11 +00:00
|
|
|
#include <memory>
|
2016-08-02 09:39:39 +00:00
|
|
|
|
2013-08-02 14:46:53 +00:00
|
|
|
/**
|
2013-10-14 14:13:35 +00:00
|
|
|
* Class COROUNTINE.
|
|
|
|
* Implements 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.
|
|
|
|
*
|
2017-03-07 13:30:15 +00:00
|
|
|
* Uses libcontext library to do the actual context switching.
|
2013-10-14 14:13:35 +00:00
|
|
|
*
|
|
|
|
* 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.
|
2013-08-02 14:46:53 +00:00
|
|
|
*/
|
|
|
|
|
2016-07-04 22:14:32 +00:00
|
|
|
template <typename ReturnType, typename ArgType>
|
2013-08-08 12:59:59 +00:00
|
|
|
class COROUTINE
|
|
|
|
{
|
2016-09-21 09:54:58 +00:00
|
|
|
private:
|
|
|
|
class CALL_CONTEXT;
|
|
|
|
|
|
|
|
struct INVOCATION_ARGS
|
|
|
|
{
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
FROM_ROOT, // a stub was called/a corutine was resumed from the main-stack context
|
|
|
|
FROM_ROUTINE, // a stub was called/a coroutine was resumed fron a coroutine context
|
|
|
|
CONTINUE_AFTER_ROOT // a function sent a request to invoke a function on the main
|
|
|
|
// stack context
|
|
|
|
} type; // invocation type
|
|
|
|
COROUTINE* destination; // stores the coroutine pointer for the stub OR the coroutine
|
|
|
|
// ptr for the coroutine to be resumed if a
|
|
|
|
// root(main-stack)-call-was initiated.
|
|
|
|
CALL_CONTEXT* context; // pointer to the call context of the current callgraph this
|
|
|
|
// call context holds a reference to the main stack context
|
|
|
|
};
|
|
|
|
|
2017-03-07 13:30:15 +00:00
|
|
|
using CONTEXT_T = libcontext::fcontext_t;
|
2016-09-21 09:54:58 +00:00
|
|
|
using CALLEE_STORAGE = CONTEXT_T;
|
|
|
|
|
|
|
|
class CALL_CONTEXT
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void SetMainStack( CONTEXT_T* aStack )
|
|
|
|
{
|
|
|
|
m_mainStackContext = aStack;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RunMainStack( COROUTINE* aCor, std::function<void()> aFunc )
|
|
|
|
{
|
|
|
|
m_mainStackFunction = std::move( aFunc );
|
|
|
|
INVOCATION_ARGS args{ INVOCATION_ARGS::CONTINUE_AFTER_ROOT, aCor, this };
|
|
|
|
|
2017-03-07 13:30:15 +00:00
|
|
|
libcontext::jump_fcontext( &aCor->m_callee, *m_mainStackContext,
|
2016-09-21 09:54:58 +00:00
|
|
|
reinterpret_cast<intptr_t>( &args ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void Continue( INVOCATION_ARGS* args )
|
|
|
|
{
|
|
|
|
while( args->type == INVOCATION_ARGS::CONTINUE_AFTER_ROOT )
|
|
|
|
{
|
|
|
|
m_mainStackFunction();
|
|
|
|
args->type = INVOCATION_ARGS::FROM_ROOT;
|
|
|
|
args = args->destination->doResume( args );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
CONTEXT_T* m_mainStackContext;
|
|
|
|
std::function<void()> m_mainStackFunction;
|
|
|
|
};
|
|
|
|
|
2013-08-02 14:46:53 +00:00
|
|
|
public:
|
2014-07-09 11:50:27 +00:00
|
|
|
COROUTINE() :
|
2016-07-04 22:14:32 +00:00
|
|
|
COROUTINE( nullptr )
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
* Creates a coroutine from a member method of an object
|
|
|
|
*/
|
2013-10-14 14:13:35 +00:00
|
|
|
template <class T>
|
2016-08-02 09:39:39 +00:00
|
|
|
COROUTINE( T* object, ReturnType(T::*ptr)( ArgType ) ) :
|
2016-07-04 22:14:32 +00:00
|
|
|
COROUTINE( std::bind( ptr, object, std::placeholders::_1 ) )
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
* Creates a coroutine from a delegate object
|
|
|
|
*/
|
2016-07-04 22:14:32 +00:00
|
|
|
COROUTINE( std::function<ReturnType(ArgType)> aEntry ) :
|
|
|
|
m_func( std::move( aEntry ) ),
|
|
|
|
m_running( false ),
|
2016-08-02 09:39:39 +00:00
|
|
|
m_args( 0 ),
|
2019-02-16 04:14:40 +00:00
|
|
|
m_caller( nullptr ),
|
2019-02-16 20:53:25 +00:00
|
|
|
m_callContext( nullptr ),
|
2016-08-02 09:39:39 +00:00
|
|
|
m_callee( nullptr ),
|
2018-01-09 14:22:45 +00:00
|
|
|
m_retVal( 0 )
|
2014-07-09 11:50:27 +00:00
|
|
|
{
|
|
|
|
}
|
2013-08-02 14:46:53 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
~COROUTINE()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-07-04 22:14:32 +00:00
|
|
|
public:
|
2013-10-14 11:43:57 +00:00
|
|
|
/**
|
2017-02-23 07:30:29 +00:00
|
|
|
* Function KiYield()
|
2013-10-14 11:43:57 +00:00
|
|
|
*
|
|
|
|
* Stops 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.
|
|
|
|
*/
|
2017-02-23 07:30:29 +00:00
|
|
|
void KiYield()
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
2016-08-02 09:39:39 +00:00
|
|
|
jumpOut();
|
2013-10-14 11:43:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-02-23 07:30:29 +00:00
|
|
|
* Function KiYield()
|
2013-10-14 11:43:57 +00:00
|
|
|
*
|
2017-02-23 07:30:29 +00:00
|
|
|
* KiYield with a value - passes a value of given type to the caller.
|
2013-10-14 11:43:57 +00:00
|
|
|
* Useful for implementing generator objects.
|
|
|
|
*/
|
2017-02-23 07:30:29 +00:00
|
|
|
void KiYield( ReturnType& aRetVal )
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
2013-10-14 18:40:36 +00:00
|
|
|
m_retVal = aRetVal;
|
2016-08-02 09:39:39 +00:00
|
|
|
jumpOut();
|
|
|
|
}
|
|
|
|
|
2016-09-21 09:54:58 +00:00
|
|
|
/**
|
|
|
|
* Function SetEntry()
|
|
|
|
*
|
|
|
|
* Defines the entry point for the coroutine, if not set in the constructor.
|
|
|
|
*/
|
|
|
|
void SetEntry( std::function<ReturnType(ArgType)> aEntry )
|
|
|
|
{
|
|
|
|
m_func = std::move( aEntry );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function RunMainStack()
|
|
|
|
*
|
|
|
|
* 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).
|
|
|
|
*/
|
|
|
|
void RunMainStack( std::function<void()> func )
|
|
|
|
{
|
|
|
|
assert( m_callContext );
|
|
|
|
m_callContext->RunMainStack( this, std::move( func ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function Call()
|
|
|
|
*
|
|
|
|
* Starts execution of a coroutine, passing args as its arguments. Call this method
|
|
|
|
* from the application main stack only.
|
|
|
|
* @return true, if the coroutine has yielded and false if it has finished its
|
|
|
|
* execution (returned).
|
|
|
|
*/
|
|
|
|
bool Call( ArgType aArg )
|
|
|
|
{
|
|
|
|
CALL_CONTEXT ctx;
|
|
|
|
INVOCATION_ARGS args{ INVOCATION_ARGS::FROM_ROOT, this, &ctx };
|
|
|
|
ctx.Continue( doCall( &args, aArg ) );
|
|
|
|
|
|
|
|
return Running();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function Call()
|
|
|
|
*
|
|
|
|
* Starts execution of a coroutine, passing args as its arguments. Call this method
|
|
|
|
* for a nested coroutine invocation.
|
|
|
|
* @return true, if the coroutine has yielded and false if it has finished its
|
|
|
|
* execution (returned).
|
|
|
|
*/
|
|
|
|
bool Call( const COROUTINE& aCor, ArgType aArg )
|
|
|
|
{
|
|
|
|
INVOCATION_ARGS args{ INVOCATION_ARGS::FROM_ROUTINE, this, aCor.m_callContext };
|
|
|
|
doCall( &args, aArg );
|
|
|
|
// we will not be asked to continue
|
|
|
|
|
|
|
|
return Running();
|
|
|
|
}
|
|
|
|
|
2016-08-02 09:39:39 +00:00
|
|
|
/**
|
|
|
|
* Function Resume()
|
|
|
|
*
|
2016-09-21 09:54:58 +00:00
|
|
|
* Resumes execution of a previously yielded coroutine. Call this method only
|
|
|
|
* from the main application stack.
|
2016-08-02 09:39:39 +00:00
|
|
|
* @return true, if the coroutine has yielded again and false if it has finished its
|
|
|
|
* execution (returned).
|
|
|
|
*/
|
|
|
|
bool Resume()
|
|
|
|
{
|
2016-09-21 09:54:58 +00:00
|
|
|
CALL_CONTEXT ctx;
|
|
|
|
INVOCATION_ARGS args{ INVOCATION_ARGS::FROM_ROOT, this, &ctx };
|
|
|
|
ctx.Continue( doResume( &args ) );
|
|
|
|
|
|
|
|
return Running();
|
2013-10-14 11:43:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-21 09:54:58 +00:00
|
|
|
* Function Resume()
|
|
|
|
*
|
|
|
|
* Resumes execution of a previously yielded coroutine. Call this method
|
|
|
|
* for a nested coroutine invocation.
|
|
|
|
* @return true, if the coroutine has yielded again and false if it has finished its
|
|
|
|
* execution (returned).
|
|
|
|
*/
|
|
|
|
bool Resume( const COROUTINE& aCor )
|
|
|
|
{
|
|
|
|
INVOCATION_ARGS args{ INVOCATION_ARGS::FROM_ROUTINE, this, aCor.m_callContext };
|
|
|
|
doResume( &args );
|
|
|
|
// we will not be asked to continue
|
|
|
|
|
|
|
|
return Running();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function ReturnValue()
|
2013-10-14 11:43:57 +00:00
|
|
|
*
|
2017-02-23 10:36:52 +00:00
|
|
|
* Returns the yielded value (the argument KiYield() was called with)
|
2013-10-14 11:43:57 +00:00
|
|
|
*/
|
2016-09-21 09:54:58 +00:00
|
|
|
const ReturnType& ReturnValue() const
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
2016-09-21 09:54:58 +00:00
|
|
|
return m_retVal;
|
2013-10-14 11:43:57 +00:00
|
|
|
}
|
|
|
|
|
2016-09-21 09:54:58 +00:00
|
|
|
/**
|
|
|
|
* Function Running()
|
2013-10-14 11:43:57 +00:00
|
|
|
*
|
2016-09-21 09:54:58 +00:00
|
|
|
* @return true, if the coroutine is active
|
2013-10-14 11:43:57 +00:00
|
|
|
*/
|
2016-09-21 09:54:58 +00:00
|
|
|
bool Running() const
|
|
|
|
{
|
|
|
|
return m_running;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
INVOCATION_ARGS* doCall( INVOCATION_ARGS* aInvArgs, ArgType aArgs )
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
2016-08-02 09:39:39 +00:00
|
|
|
assert( m_func );
|
|
|
|
assert( !m_callee );
|
|
|
|
|
|
|
|
m_args = &aArgs;
|
|
|
|
|
|
|
|
assert( m_stack == nullptr );
|
2016-07-04 22:14:32 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
// fixme: Clean up stack stuff. Add a guard
|
2016-08-02 09:39:39 +00:00
|
|
|
size_t stackSize = c_defaultStackSize;
|
|
|
|
m_stack.reset( new char[stackSize] );
|
2013-08-08 12:59:59 +00:00
|
|
|
|
|
|
|
// align to 16 bytes
|
2016-09-21 09:54:58 +00:00
|
|
|
void* sp = (void*)((((ptrdiff_t) m_stack.get()) + stackSize - 0xf) & (~0x0f));
|
2013-08-08 12:59:59 +00:00
|
|
|
|
2014-07-09 11:50:27 +00:00
|
|
|
// correct the stack size
|
2016-09-21 09:54:58 +00:00
|
|
|
stackSize -= size_t( ( (ptrdiff_t) m_stack.get() + stackSize ) - (ptrdiff_t) sp );
|
2016-07-04 22:14:32 +00:00
|
|
|
|
2017-03-07 13:30:15 +00:00
|
|
|
m_callee = libcontext::make_fcontext( sp, stackSize, callerStub );
|
2013-08-08 12:59:59 +00:00
|
|
|
m_running = true;
|
2016-07-04 22:14:32 +00:00
|
|
|
|
2013-08-08 12:59:59 +00:00
|
|
|
// off we go!
|
2016-09-21 09:54:58 +00:00
|
|
|
return jumpIn( aInvArgs );
|
2013-10-14 11:43:57 +00:00
|
|
|
}
|
|
|
|
|
2016-09-21 09:54:58 +00:00
|
|
|
INVOCATION_ARGS* doResume( INVOCATION_ARGS* args )
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
2016-09-21 09:54:58 +00:00
|
|
|
return jumpIn( args );
|
2013-10-14 11:43:57 +00:00
|
|
|
}
|
2013-08-02 14:46:53 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
/* real entry point of the coroutine */
|
2013-10-14 18:40:36 +00:00
|
|
|
static void callerStub( intptr_t aData )
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
2016-09-21 09:54:58 +00:00
|
|
|
INVOCATION_ARGS& args = *reinterpret_cast<INVOCATION_ARGS*>( aData );
|
2013-10-14 11:43:57 +00:00
|
|
|
// get pointer to self
|
2016-09-21 09:54:58 +00:00
|
|
|
COROUTINE* cor = args.destination;
|
|
|
|
cor->m_callContext = args.context;
|
|
|
|
|
|
|
|
if( args.type == INVOCATION_ARGS::FROM_ROOT )
|
|
|
|
cor->m_callContext->SetMainStack( &cor->m_caller );
|
2016-08-02 09:39:39 +00:00
|
|
|
|
|
|
|
// call the coroutine method
|
|
|
|
cor->m_retVal = cor->m_func( *(cor->m_args) );
|
|
|
|
cor->m_running = false;
|
|
|
|
|
|
|
|
// go back to wherever we came from.
|
|
|
|
cor->jumpOut();
|
|
|
|
}
|
|
|
|
|
2016-09-21 09:54:58 +00:00
|
|
|
INVOCATION_ARGS* jumpIn( INVOCATION_ARGS* args )
|
2016-08-02 09:39:39 +00:00
|
|
|
{
|
2016-09-21 09:54:58 +00:00
|
|
|
args = reinterpret_cast<INVOCATION_ARGS*>(
|
2017-03-07 13:30:15 +00:00
|
|
|
libcontext::jump_fcontext( &m_caller, m_callee,
|
2016-09-21 09:54:58 +00:00
|
|
|
reinterpret_cast<intptr_t>( args ) )
|
|
|
|
);
|
|
|
|
|
|
|
|
return args;
|
2014-08-26 09:16:56 +00:00
|
|
|
}
|
|
|
|
|
2016-08-02 09:39:39 +00:00
|
|
|
void jumpOut()
|
2014-08-26 09:16:56 +00:00
|
|
|
{
|
2016-09-21 09:54:58 +00:00
|
|
|
INVOCATION_ARGS args{ INVOCATION_ARGS::FROM_ROUTINE, nullptr, nullptr };
|
|
|
|
INVOCATION_ARGS* ret;
|
|
|
|
ret = reinterpret_cast<INVOCATION_ARGS*>(
|
2017-03-07 13:30:15 +00:00
|
|
|
libcontext::jump_fcontext( &m_callee, m_caller,
|
2016-09-21 09:54:58 +00:00
|
|
|
reinterpret_cast<intptr_t>( &args ) )
|
|
|
|
);
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2016-09-21 09:54:58 +00:00
|
|
|
m_callContext = ret->context;
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2016-09-21 09:54:58 +00:00
|
|
|
if( ret->type == INVOCATION_ARGS::FROM_ROOT )
|
|
|
|
{
|
|
|
|
m_callContext->SetMainStack( &m_caller );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr int c_defaultStackSize = 2000000; // fixme: make configurable
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2016-07-04 22:14:32 +00:00
|
|
|
///< coroutine stack
|
2016-08-02 09:39:39 +00:00
|
|
|
std::unique_ptr<char[]> m_stack;
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2016-09-21 09:54:58 +00:00
|
|
|
std::function<ReturnType( ArgType )> m_func;
|
|
|
|
|
|
|
|
bool m_running;
|
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
///< pointer to coroutine entry arguments. Stripped of references
|
|
|
|
///< to avoid compiler errors.
|
2016-07-04 22:14:32 +00:00
|
|
|
typename std::remove_reference<ArgType>::type* m_args;
|
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
///< saved caller context
|
2016-09-21 09:54:58 +00:00
|
|
|
CONTEXT_T m_caller;
|
|
|
|
|
|
|
|
///< main stack information
|
|
|
|
CALL_CONTEXT* m_callContext;
|
2013-10-14 11:43:57 +00:00
|
|
|
|
|
|
|
///< saved coroutine context
|
2016-09-21 09:54:58 +00:00
|
|
|
CALLEE_STORAGE m_callee;
|
2016-08-02 09:39:39 +00:00
|
|
|
|
|
|
|
ReturnType m_retVal;
|
2013-08-02 14:46:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|