How to implement continuations?

后端 未结 12 1565
清酒与你
清酒与你 2020-12-22 16:33

I\'m working on a Scheme interpreter written in C. Currently it uses the C runtime stack as its own stack, which is presenting a minor problem with implementing continuation

12条回答
  •  感情败类
    2020-12-22 17:02

    Continuations basically consist of the saved state of the stack and CPU registers at the point of context switches. At the very least you don't have to copy the entire stack to the heap when switching, you could only redirect the stack pointer.

    Continuations are trivially implemented using fibers. http://en.wikipedia.org/wiki/Fiber_%28computer_science%29 . The only things that need careful encapsulation are parameter passing and return values.

    In Windows fibers are done using the CreateFiber/SwitchToFiber family of calls. in Posix-compliant systems it can be done with makecontext/swapcontext.

    boost::coroutine has a working implementation of coroutines for C++ that can serve as a reference point for implementation.

提交回复
热议问题