← Back to team overview

kicad-developers team mailing list archive

Re: KiCad Coroutines

 

On 07.01.2016 13:34, Lorenzo Marcantonio wrote:
> On Thu, Jan 07, 2016 at 12:08:22PM +0000, Mário Luzeiro wrote:
>> Hi all,
>> 
>> Sorry I don't know nothing about co-routines. Just curious, Why
>> and where kicad is using it? And why and where kicad need
>> multi-threading?
> 
> It's not multithreading, it's suspending a thread of work in the
> new tool code.
> 
> Some thing like this: - Select a tool (suspend for a selection) -
> Other things happens, then user select a thing - (restart the tool
> coroutine) Check thing selected and so on
> 
> The goal is *not* to use multiple cores or doing things in
> background; the idea is to stop inside a function and then resume
> from there when something desiderable happens.
> 

I agree, the whole idea was to simplify event handling in the tool, so
instead of writing a complex state machine, you could just suspend the
execution of your tool code to wait for a matching event:

@Edwin: Coroutines are not a replacement for threads. They *aren't*
there for improving performance, just to simplify event handling in
complex state machines. For example:

case 1) - without coroutines - we need to code a (possibly large) FSM
by hand. This doesn't scale well in terms of code complexity,
especially if there are nested state machines .

void handle_event(event)
{
switch(state)
{
case state1:
if (event == event1) {
do something();
state = state2;
}
case state2:
if (event == event2) {
do_something();
state = state3;
}
case state3:
/// and so on
}
}

case 2) - with coroutines - we can execute our FSM in sequential code
and receive events using a wait() function, which yields the current
coroutine (i.e. transfers the control to the master event loop in case
of pcbnew) and wakes up the execution of the tool that called wait()
when a matching event arrives:

void my_tool()
{
	wait(event1);
	do_something();
	wait(event2);
	do_something_else();
}

All of the stack switching "magic" is hidden. You can find an example in
include/tool/examples/coroutine_example.cpp


Concerning boost::context, I see two options:
- get rid of the coroutines (i.e. rewrite all event loops/calls in the
GAL tools). Painful but doable, I would prefer to spend my time doing
something more productive (e.g. porting the remaining tools to GAL).
- turn boost::context into a single .cpp file library that one can
just add to the project and forget about it (think of ClipperLib as an
example).

As a side comment, I'm not surprised that boost devs insist on using
MASM under Windows - the syntax of the GNU assembler on x86/x86_64
platforms is just disgusting.

Tom



Follow ups

References