← Back to team overview

kicad-developers team mailing list archive

Re: KiCad Coroutines

 

On 1/3/2016 1:18 PM, "Torsten Hüter" wrote:
> Hi Wayne,
> 
> I've recently tried to compile KiCad on Windows (64Bit) and I've noticed that PcbNew is crashing at context switching - but I had no problems early 2015 with that. I've seen that you have already filed a bug report [1]. I did some research and this thing seems to cause constantly trouble, because some assembly "hackery" is required and only MASM is officially supported [2].
> 
> Rather than trying to fix Boost, makes it sense to consider an alternative way or library for the same purpose?
> 
> I've found so far these alternatives as back-end for "stackful" coroutines:
> 
> (1) Fibers on Windows
> 
> https://msdn.microsoft.com/de-de/library/windows/desktop/ms682661%28v=vs.85%29.aspx
> https://en.wikipedia.org/wiki/Fiber_%28computer_science%29

What version(s) of Windows support fibers?  I didn't see any indication
on the links above.

> 
> This seems to be the safest alternative for Windows, because Fibers are fully supported by the OS and are thread-safe.
> 
> (2) POSIX ucontext.h - setcontext, getcontext, makecontext and swapcontext
> 
> Very similar compared to Windows Fibers, but deprecated since POSIX.1-2008, quote from https://en.wikipedia.org/wiki/Setcontext :
> 
> "[..] POSIX.1-2004 obsoleted these functions, and in POSIX.1-2008 they were removed [..]"
> 
> It is still possible to use these functions on Linux, but these are not future-proof and there are no replacement functions.
> 
> An interesting talk about this topic can be found here:
> http://stackoverflow.com/questions/4298986/is-there-something-to-replace-the-ucontext-h-functions
> 
> (3) POSIX Threads
> 
> https://computing.llnl.gov/tutorials/pthreads/
> 
> It is possible to emulate Fibers with pthreads, here is a good article about this subject:
> 
> http://sjansen.blogspot.de/2008/04/coroutines-in-cc-beyond-duffs-device.html
> 
> The downside is the larger overhead, but this shouldn't be a big issue for KiCad, I'm sure that not more than a few coroutines are created for the tool framework. Quote from the above article:
> 
> "[..] As an interesting sidenote, I'm not sure how debuggers such as gdb and valgrind handle applications that switch their stacks. It might be that using threads actually makes things easier to debug in this case (as the tools already have support for threads). [..]"
> 
> Pthreads offer also some mechanisms for stack overflow detection.
> 
> (4) setjmp / longjmp
> 
> https://en.wikipedia.org/wiki/Setjmp.h
> 
> Context switching is also possible using these functions and some libraries like "picoro" use that for Coroutines. However stack handling is difficult, especially stack overflow protection is not easy to implement.
> 
> http://dotat.at/cgi/git/picoro.git
> 
> --
> 
> Summary: Fibers (Windows) and Pthreads (all POSIX-compliant operating systems) seem to be the most promising alternatives for context switching. This way it would be possible to get rid of the Boost.Context dependency while using standard libraries.
> 
> What do you think, should I invest more time into this topic (refactoring of coroutine.h using these alternatives and write a stress test application)?

I don't have any strong opinions either way.  However, I did not write
the code.  Tom may have a stronger opinion about this than I do since he
wrote the code.  Personally I'm not the biggest fan of coroutines.  I
know the make the code easier to read but I'm not convinced they are
necessary in an event driven design.

Thanks for the links.  They are interesting.

Cheers,

Wayne


> 
> Thanks, 
> Torsten
> 
> [1] KiCad issues with Boost 1.59, bug report, https://github.com/Alexpux/MINGW-packages/issues/836
> [2] Boost context requirements, http://www.boost.org/doc/libs/1_60_0/libs/context/doc/html/context/requirements.html
> 
> _______________________________________________
> Mailing list: https://launchpad.net/~kicad-developers
> Post to     : kicad-developers@xxxxxxxxxxxxxxxxxxx
> Unsubscribe : https://launchpad.net/~kicad-developers
> More help   : https://help.launchpad.net/ListHelp
> 



Follow ups

References