Tango Fibers question - also, phobos/tango base merger

downs default_357-line at yahoo.de
Mon Oct 29 18:20:42 PDT 2007


Mikola Lysenko wrote:
> 
> Well, as one of the culprits implicated in writing the fiber switching code, I might be able to explain things.  First, let me clear one thing up:  
> 
> Tango does not store the float registers on Windows.  Since the C calling convention on both Posix and Win32 states that the FPU is not preserved across calls, there is no need to do such a thing.  You may be confusing the floating point stack with the necromancy involving the FS segment values, which are part of the thread information block on windows.  Those values are necessary for unwinding the stack in the event of an exception and absolutely must be saved in that exact order.  Doing otherwise risks horrible disaster if the system were to inadvertently switch threads in the middle of a fiber switch or if one of the fibers were to generate an exception (via throw or otherwise).
> 
> Many things went in to making the fiber switching on Tango fast, including optimizing register usage and restructuring the API such that each operation could be performed in O(1) time with a low constant cost.  Seemingly simple stuff like marking/unmarking GC regions had to be overhauled from the Phobos' O(n) algorithm to make them efficient.
> 
> Even without optimizations, just getting fiber switching *right* is very difficult.  There are many subtle issues involving garbage collection, thread synchronization and exception handling.  I spent many hours hunting down tiny race conditions and testing various extreme cases in order to track down and exterminate as many bugs as we could possibly find.  
> 
> For these reasons and more, I would strongly recommend that you just use Tango's Fibers rather than try to roll your own.  They are highly optimized and have been rigorously checked.  While it is possible to get a fairly rudimentary version of context switching working, I can't say that I would trust such an implementation.
> 
> -Mik

Hell, I'll *gladly* use them the moment they're available on Phobos.
Even getting my current version to work has been a week-long nightmare.

Phobos being the target platform for scrapple.tools though, until then,
I'll have no choice but to go with my own thang.

So I guess all I can hope for is that merging the Phobos and Tango base
code happen fast. Apropos, when that happens, will Fibers become a core
part of Phobos / the shared core?
I'd very much like to see that day.

In the meantime, thanks for the explanation.

 --downs



More information about the Digitalmars-d mailing list