[deleted]
Major crack smoking. This doesn't work in any kind of general way, you would have to find the right hack per compiler, per build setting, etc.
Fortunately, C++ has a mechanism built in that lets you associate some data per function call and make those variable references automatically rebased to that chunk of memory - it's called member variables, just use that!
Does that work on a machine with callee-saved registers? (I.e. does that work on anything except x86?)
ReplyDeleteLua has a native coroutine patch that allegedly works on Windiws, OSX, BSD, and Linux. http://coco.luajit.org/ I see code in the patch for x86, x64, PPC, ARM, MIPS, SPARC and "LP64" (Itanic?). I think you can just pull the C macros out of it and use them, and discard the Lua-specific stuff.
ReplyDeleteIt looks like the windows code uses Fibers. The knock on Fibers that I've heard is that some system calls are not compatible with Fibers, so you have to be careful, or bad things happen. Hearsay, not based on firsthand knowledge, I don't remember details.
Did you see that - http://stackoverflow.com/questions/4352451/coroutine-demo-source-2/4352706#4352706 ?
ReplyDeleteHmm.. that looks very similar to the broken one I wrote. Broken in the sense that it's not remotely portable. It's pretty easy to make this work on any one compiler/CPU.
ReplyDeleteReally not worth bothering with when you can just use member variables.
Well, my version is actually portable - I tested it with MSC/IntelC/gcc linux/windows x86/x64/arm. Also it makes sense as a speed optimization (easy to add buffering to bytewise processing loop), and _not_ using any globals/structure fields is a good speed optimization too.
ReplyDeleteFor example, compare http://nishi.dreamhosters.com/u/rc_v3.rar and http://nishi.dreamhosters.com/u/o01_v0.rar