← Back to team overview

kicad-developers team mailing list archive

Re: KiCad Coroutines

 

Lorenzo Marcantonio <l.marcantonio@xxxxxxxxxxxxx> writes:

> On Thu, Jan 07, 2016 at 11:50:51AM +0100, jp charras wrote:
>
>> Perhaps some ideas can be taken here:
>> https://github.com/roxma/cpp_learn/tree/master/cpp/linux_programming/coroutine
>
> That's thread based, the goal is to do it without creating more threads
> (which are problematic for many reasons)

Actually, pthreads here is only used as a semi-portable way to create a new
CPU stack.

All the co-routines run in the context of the main thread. The newly spawned
threads exit immediately after saving a copy of their CPU
stack. Essentially, pthread_create() is used to emulate makecontext() which
has been deprecated in Posix. Without makecontext(), pthread_create() is one
of few portable ways to switch the CPU to a new stack.

So the use of threads here will not affect anything else in KiCAD. There may
be other reasons to prefer a different co-routine implementation (or no
co-routines at all), of course.

In my experience, it is important not to underestimate the value of
co-routines for larger event-driven systems / state machines. Small systems
are easy to make, sure. But they scale very poorly in terms of
maintainability. As the number of states / event-callbacks increases, the
complexity scales as N**2, as every state can potentially interact with
every other. Co-routines allows to preserve the familiar call-return control
flow, which scales much better as the system size increases.

Unfortunately co-routines are not familiar to many developers, and have poor
cross-platform support, which makes them harder to use.

Hope this helps,

 - Kristian.


Follow ups

References