开发者

How to make spaghetti stack with C?

开发者 https://www.devze.com 2023-04-04 03:32 出处:网络
I\'m trying to make coroutine and continuation with C. I realized I need some kind of spaghetti stack. Is it possible to execute a function within a new call-stack? Of course withinsingle thread. How

I'm trying to make coroutine and continuation with C. I realized I need some kind of spaghetti stack. Is it possible to execute a function within a new call-stack? Of course within single thread. How can I make 开发者_运维百科a new, separated call-stack, and execute some code on it?


check out makecontext/swapcontext.

If those aren't available, then you could use setjmp/longjmp, but those are a little more complex.


If you are on Windows, you can look into the Fibers API. Evil Otto's answer is more portable/POSIX compliant, however.


I am still not sure about continuations, but at least for coroutine, there's no good way to do this within C. Because this is out of C abstraction. setjmp/longjmp has limitation of scope may disrupts C++/Objective-C exception system, makecontext/swapcontext has been deprecated. Though some systems still support the functions, but at least Apple OS does not support it anymore.

The only acceptable solution to me was libtask library. Which uses architecture specific assembly codes to save and restore register state, and it also manages the call stacks. I think it is using monolithic stack and still have size limit, but anyway, it's almost the only solution I could find which reliably working.

I think Clang's Segmented Stack (or GCC's Split Stack) feature will enable very generic coroutine support on C level. And I hope to write coroutine code on LLVM freely later.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号