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
|
2020-12-27 00:41:04 +00:00
|
|
|
* Copyright (C) 2016-2020 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
2013-08-02 14:46:53 +00:00
|
|
|
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
2019-08-10 15:26:23 +00:00
|
|
|
#ifdef KICAD_USE_VALGRIND
|
|
|
|
#include <valgrind/valgrind.h>
|
|
|
|
#endif
|
2021-04-16 16:39:31 +00:00
|
|
|
#ifdef KICAD_SANITIZE_THREADS
|
|
|
|
#include <sanitizer/tsan_interface.h>
|
|
|
|
#endif
|
|
|
|
#ifdef KICAD_SANITIZE_ADDRESS
|
|
|
|
#include <sanitizer/asan_interface.h>
|
|
|
|
#endif
|
2019-08-10 15:26:23 +00:00
|
|
|
|
2019-12-28 18:17:55 +00:00
|
|
|
#include <libcontext.h>
|
2021-04-16 16:39:31 +00:00
|
|
|
#include <functional>
|
2017-01-20 17:33:11 +00:00
|
|
|
#include <memory>
|
2020-01-13 01:44:19 +00:00
|
|
|
#include <advanced_config.h>
|
2016-08-02 09:39:39 +00:00
|
|
|
|
2021-04-16 16:49:20 +00:00
|
|
|
#include <trace_helpers.h>
|
|
|
|
#include <wx/log.h>
|
|
|
|
|
2013-08-02 14:46:53 +00:00
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Implement a coroutine.
|
|
|
|
*
|
|
|
|
* Wikipedia has a good explanation:
|
2013-10-14 14:13:35 +00:00
|
|
|
*
|
|
|
|
* "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
|
|
|
|
{
|
2019-08-10 15:26:23 +00:00
|
|
|
FROM_ROOT, // a stub was called/a coroutine was resumed from the main-stack context
|
|
|
|
FROM_ROUTINE, // a stub was called/a coroutine was resumed from a coroutine context
|
2016-09-21 09:54:58 +00:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2021-04-16 16:39:31 +00:00
|
|
|
struct CONTEXT_T
|
|
|
|
{
|
|
|
|
libcontext::fcontext_t ctx; // The context itself
|
|
|
|
#ifdef KICAD_SANITIZE_THREADS
|
|
|
|
void* tsan_fiber; // The TSAN fiber for this context
|
|
|
|
bool own_tsan_fiber; // Do we own this TSAN fiber? (we only delete fibers we own)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
CONTEXT_T() :
|
|
|
|
ctx( nullptr )
|
|
|
|
#ifdef KICAD_SANITIZE_THREADS
|
|
|
|
,tsan_fiber( nullptr )
|
|
|
|
,own_tsan_fiber( true )
|
|
|
|
#endif
|
|
|
|
{}
|
|
|
|
|
|
|
|
~CONTEXT_T()
|
|
|
|
{
|
|
|
|
#ifdef KICAD_SANITIZE_THREADS
|
2021-04-24 16:43:49 +00:00
|
|
|
// Only destroy the fiber when we own it
|
2021-04-16 16:39:31 +00:00
|
|
|
if( own_tsan_fiber )
|
|
|
|
__tsan_destroy_fiber( tsan_fiber );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
};
|
2016-09-21 09:54:58 +00:00
|
|
|
|
|
|
|
class CALL_CONTEXT
|
|
|
|
{
|
|
|
|
public:
|
2020-01-11 20:42:37 +00:00
|
|
|
CALL_CONTEXT() :
|
|
|
|
m_mainStackContext( nullptr )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-07-22 13:39:04 +00:00
|
|
|
~CALL_CONTEXT()
|
|
|
|
{
|
2021-04-16 16:39:31 +00:00
|
|
|
if( m_mainStackContext )
|
|
|
|
libcontext::release_fcontext( m_mainStackContext->ctx );
|
2020-07-22 13:39:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-21 09:54:58 +00:00
|
|
|
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 };
|
|
|
|
|
2021-04-16 16:39:31 +00:00
|
|
|
#ifdef KICAD_SANITIZE_THREADS
|
|
|
|
// Tell TSAN we are changing fibers
|
|
|
|
__tsan_switch_to_fiber( m_mainStackContext->tsan_fiber, 0 );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
libcontext::jump_fcontext( &( aCor->m_callee.ctx ), m_mainStackContext->ctx,
|
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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Create a coroutine from a member method of an object.
|
2013-10-14 11:43:57 +00:00
|
|
|
*/
|
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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Create a coroutine from a delegate object.
|
2013-10-14 11:43:57 +00:00
|
|
|
*/
|
2016-07-04 22:14:32 +00:00
|
|
|
COROUTINE( std::function<ReturnType(ArgType)> aEntry ) :
|
|
|
|
m_func( std::move( aEntry ) ),
|
|
|
|
m_running( false ),
|
2021-04-22 21:20:34 +00:00
|
|
|
m_args( nullptr ),
|
2021-04-16 16:39:31 +00:00
|
|
|
m_caller(),
|
2019-02-16 20:53:25 +00:00
|
|
|
m_callContext( nullptr ),
|
2021-04-16 16:39:31 +00:00
|
|
|
m_callee(),
|
2018-01-09 14:22:45 +00:00
|
|
|
m_retVal( 0 )
|
2019-08-10 15:26:23 +00:00
|
|
|
#ifdef KICAD_USE_VALGRIND
|
2021-04-24 16:43:49 +00:00
|
|
|
,m_valgrind_stack( 0 )
|
2021-04-16 16:39:31 +00:00
|
|
|
#endif
|
|
|
|
#ifdef KICAD_SANITIZE_ADDRESS
|
|
|
|
,asan_stack( nullptr )
|
2019-08-10 15:26:23 +00:00
|
|
|
#endif
|
2014-07-09 11:50:27 +00:00
|
|
|
{
|
2020-11-16 11:16:44 +00:00
|
|
|
m_stacksize = ADVANCED_CFG::GetCfg().m_CoroutineStackSize;
|
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()
|
|
|
|
{
|
2019-08-10 15:26:23 +00:00
|
|
|
#ifdef KICAD_USE_VALGRIND
|
2021-04-24 16:43:49 +00:00
|
|
|
VALGRIND_STACK_DEREGISTER( m_valgrind_stack );
|
2019-08-10 15:26:23 +00:00
|
|
|
#endif
|
2021-04-24 16:43:49 +00:00
|
|
|
|
2021-04-16 16:39:31 +00:00
|
|
|
if( m_caller.ctx )
|
|
|
|
libcontext::release_fcontext( m_caller.ctx );
|
2021-04-24 16:43:49 +00:00
|
|
|
|
2021-04-16 16:39:31 +00:00
|
|
|
if( m_callee.ctx )
|
|
|
|
libcontext::release_fcontext( m_callee.ctx );
|
2013-10-14 11:43:57 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 22:14:32 +00:00
|
|
|
public:
|
2013-10-14 11:43:57 +00:00
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Stop execution of the coroutine and returns control to the caller.
|
2013-10-14 11:43:57 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* KiYield with a value.
|
2013-10-14 11:43:57 +00:00
|
|
|
*
|
2020-12-27 00:41:04 +00:00
|
|
|
* Passe a value of given type to the caller. Useful for implementing generator objects.
|
2013-10-14 11:43:57 +00:00
|
|
|
*/
|
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
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Run a functor inside the application main stack context.
|
2016-09-21 09:54:58 +00:00
|
|
|
*
|
|
|
|
* 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 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Start execution of a coroutine, passing args as its arguments.
|
|
|
|
*
|
|
|
|
* Call this method from the application main stack only.
|
2016-09-21 09:54:58 +00:00
|
|
|
*
|
2020-12-27 00:41:04 +00:00
|
|
|
* @return true if the coroutine has yielded and false if it has finished its
|
|
|
|
* execution (returned).
|
2016-09-21 09:54:58 +00:00
|
|
|
*/
|
|
|
|
bool Call( ArgType aArg )
|
|
|
|
{
|
|
|
|
CALL_CONTEXT ctx;
|
|
|
|
INVOCATION_ARGS args{ INVOCATION_ARGS::FROM_ROOT, this, &ctx };
|
2021-04-16 16:39:31 +00:00
|
|
|
|
|
|
|
#ifdef KICAD_SANITIZE_THREADS
|
|
|
|
// Get the TSAN fiber for the current stack here
|
|
|
|
m_caller.tsan_fiber = __tsan_get_current_fiber();
|
|
|
|
m_caller.own_tsan_fiber = false;
|
|
|
|
#endif
|
2021-04-16 16:49:20 +00:00
|
|
|
|
|
|
|
wxLogTrace( kicadTraceCoroutineStack, "COROUTINE::Call (from root)" );
|
|
|
|
|
2016-09-21 09:54:58 +00:00
|
|
|
ctx.Continue( doCall( &args, aArg ) );
|
|
|
|
|
|
|
|
return Running();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Start execution of a coroutine, passing args as its arguments.
|
2016-09-21 09:54:58 +00:00
|
|
|
*
|
2020-12-27 00:41:04 +00:00
|
|
|
* Call this method for a nested coroutine invocation.
|
|
|
|
*
|
|
|
|
* @return true if the coroutine has yielded and false if it has finished its
|
|
|
|
* execution (returned).
|
2016-09-21 09:54:58 +00:00
|
|
|
*/
|
|
|
|
bool Call( const COROUTINE& aCor, ArgType aArg )
|
|
|
|
{
|
|
|
|
INVOCATION_ARGS args{ INVOCATION_ARGS::FROM_ROUTINE, this, aCor.m_callContext };
|
2021-04-16 16:49:20 +00:00
|
|
|
|
2022-02-09 11:14:08 +00:00
|
|
|
wxLogTrace( kicadTraceCoroutineStack, wxT( "COROUTINE::Call (from routine)" ) );
|
2021-04-16 16:49:20 +00:00
|
|
|
|
2016-09-21 09:54:58 +00:00
|
|
|
doCall( &args, aArg );
|
|
|
|
// we will not be asked to continue
|
|
|
|
|
|
|
|
return Running();
|
|
|
|
}
|
|
|
|
|
2016-08-02 09:39:39 +00:00
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Resume execution of a previously yielded coroutine.
|
|
|
|
*
|
|
|
|
* Call this method only from the main application stack.
|
|
|
|
*
|
|
|
|
* @return true if the coroutine has yielded again and false if it has finished its
|
|
|
|
* execution (returned).
|
|
|
|
*/
|
2016-08-02 09:39:39 +00:00
|
|
|
bool Resume()
|
|
|
|
{
|
2016-09-21 09:54:58 +00:00
|
|
|
CALL_CONTEXT ctx;
|
|
|
|
INVOCATION_ARGS args{ INVOCATION_ARGS::FROM_ROOT, this, &ctx };
|
2021-04-16 16:39:31 +00:00
|
|
|
|
|
|
|
#ifdef KICAD_SANITIZE_THREADS
|
|
|
|
// Get the TSAN fiber for the current stack here
|
|
|
|
m_caller.tsan_fiber = __tsan_get_current_fiber();
|
|
|
|
m_caller.own_tsan_fiber = false;
|
|
|
|
#endif
|
2021-04-16 16:49:20 +00:00
|
|
|
|
2022-02-09 11:14:08 +00:00
|
|
|
wxLogTrace( kicadTraceCoroutineStack, wxT( "COROUTINE::Resume (from root)" ) );
|
2021-04-16 16:49:20 +00:00
|
|
|
|
2016-09-21 09:54:58 +00:00
|
|
|
ctx.Continue( doResume( &args ) );
|
|
|
|
|
|
|
|
return Running();
|
2013-10-14 11:43:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Resume 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).
|
|
|
|
*/
|
2016-09-21 09:54:58 +00:00
|
|
|
bool Resume( const COROUTINE& aCor )
|
|
|
|
{
|
|
|
|
INVOCATION_ARGS args{ INVOCATION_ARGS::FROM_ROUTINE, this, aCor.m_callContext };
|
2021-04-16 16:49:20 +00:00
|
|
|
|
2022-02-09 11:14:08 +00:00
|
|
|
wxLogTrace( kicadTraceCoroutineStack, wxT( "COROUTINE::Resume (from routine)" ) );
|
2021-04-16 16:49:20 +00:00
|
|
|
|
2016-09-21 09:54:58 +00:00
|
|
|
doResume( &args );
|
|
|
|
// we will not be asked to continue
|
|
|
|
|
|
|
|
return Running();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Return 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
|
|
|
/**
|
2020-12-27 00:41:04 +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 );
|
2021-04-16 16:39:31 +00:00
|
|
|
assert( !( m_callee.ctx ) );
|
2016-08-02 09:39:39 +00:00
|
|
|
|
|
|
|
m_args = &aArgs;
|
|
|
|
|
|
|
|
assert( m_stack == nullptr );
|
2016-07-04 22:14:32 +00:00
|
|
|
|
2019-08-15 04:25:05 +00:00
|
|
|
size_t stackSize = m_stacksize;
|
2019-03-06 15:19:44 +00:00
|
|
|
void* sp = nullptr;
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
#ifndef LIBCONTEXT_HAS_OWN_STACK
|
|
|
|
|
2019-03-06 15:19:44 +00:00
|
|
|
// fixme: Clean up stack stuff. Add a guard
|
2016-08-02 09:39:39 +00:00
|
|
|
m_stack.reset( new char[stackSize] );
|
2013-08-08 12:59:59 +00:00
|
|
|
|
|
|
|
// align to 16 bytes
|
2019-03-06 15:19:44 +00:00
|
|
|
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 );
|
2019-08-10 15:26:23 +00:00
|
|
|
|
|
|
|
#ifdef KICAD_USE_VALGRIND
|
2021-04-24 16:43:49 +00:00
|
|
|
m_valgrind_stack = VALGRIND_STACK_REGISTER( sp, m_stack.get() );
|
2019-08-10 15:26:23 +00:00
|
|
|
#endif
|
2020-12-27 00:41:04 +00:00
|
|
|
#endif
|
2016-07-04 22:14:32 +00:00
|
|
|
|
2021-04-16 16:39:31 +00:00
|
|
|
#ifdef KICAD_SANITIZE_THREADS
|
|
|
|
// Create a new fiber to go with the new context
|
|
|
|
m_callee.tsan_fiber = __tsan_create_fiber( 0 );
|
|
|
|
m_callee.own_tsan_fiber = true;
|
2021-04-18 17:20:09 +00:00
|
|
|
|
|
|
|
__tsan_set_fiber_name( m_callee.tsan_fiber, "Coroutine fiber" );
|
2021-04-16 16:39:31 +00:00
|
|
|
#endif
|
2021-04-16 16:49:20 +00:00
|
|
|
|
2022-02-09 11:14:08 +00:00
|
|
|
wxLogTrace( kicadTraceCoroutineStack, wxT( "COROUTINE::doCall" ) );
|
2021-04-16 16:49:20 +00:00
|
|
|
|
2021-04-16 16:39:31 +00:00
|
|
|
m_callee.ctx = 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 );
|
2020-12-27 00:41:04 +00:00
|
|
|
|
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
|
|
|
{
|
2021-04-16 16:39:31 +00:00
|
|
|
#ifdef KICAD_SANITIZE_THREADS
|
|
|
|
// Tell TSAN we are changing fibers to the callee
|
|
|
|
__tsan_switch_to_fiber( m_callee.tsan_fiber, 0 );
|
|
|
|
#endif
|
2021-04-16 16:49:20 +00:00
|
|
|
|
2022-02-09 11:14:08 +00:00
|
|
|
wxLogTrace( kicadTraceCoroutineStack, wxT( "COROUTINE::jumpIn" ) );
|
2021-04-16 16:49:20 +00:00
|
|
|
|
2016-09-21 09:54:58 +00:00
|
|
|
args = reinterpret_cast<INVOCATION_ARGS*>(
|
2021-04-16 16:39:31 +00:00
|
|
|
libcontext::jump_fcontext( &( m_caller.ctx ), m_callee.ctx,
|
2020-12-27 00:41:04 +00:00
|
|
|
reinterpret_cast<intptr_t>( args ) )
|
2016-09-21 09:54:58 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
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;
|
2020-12-27 00:41:04 +00:00
|
|
|
|
2021-04-16 16:39:31 +00:00
|
|
|
#ifdef KICAD_SANITIZE_THREADS
|
|
|
|
// Tell TSAN we are changing fibers back to the caller
|
|
|
|
__tsan_switch_to_fiber( m_caller.tsan_fiber, 0 );
|
|
|
|
#endif
|
2021-04-16 16:49:20 +00:00
|
|
|
|
2022-02-09 11:14:08 +00:00
|
|
|
wxLogTrace( kicadTraceCoroutineStack, wxT( "COROUTINE::jumpOut" ) );
|
2021-04-16 16:49:20 +00:00
|
|
|
|
2016-09-21 09:54:58 +00:00
|
|
|
ret = reinterpret_cast<INVOCATION_ARGS*>(
|
2021-04-16 16:39:31 +00:00
|
|
|
libcontext::jump_fcontext( &( m_callee.ctx ), m_caller.ctx,
|
2020-12-27 00:41:04 +00:00
|
|
|
reinterpret_cast<intptr_t>( &args ) )
|
2016-09-21 09:54:58 +00:00
|
|
|
);
|
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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-08-15 04:25:05 +00:00
|
|
|
int m_stacksize;
|
|
|
|
|
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
|
2021-04-16 16:39:31 +00:00
|
|
|
CONTEXT_T m_callee;
|
2016-08-02 09:39:39 +00:00
|
|
|
|
|
|
|
ReturnType m_retVal;
|
2019-08-10 15:26:23 +00:00
|
|
|
|
|
|
|
#ifdef KICAD_USE_VALGRIND
|
2021-04-24 16:43:49 +00:00
|
|
|
uint32_t m_valgrind_stack;
|
2019-08-10 15:26:23 +00:00
|
|
|
#endif
|
2021-04-16 16:39:31 +00:00
|
|
|
#ifdef KICAD_SANITIZE_ADDRESS
|
|
|
|
void* asan_stack;
|
|
|
|
#endif
|
2013-08-02 14:46:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|