2 ms·
I did continuations back in 1987, in Turbo Pascal, but I didn't know it until today. I wrote a cooperative multi-tasking library to support some things I neede
by mikewarot 5y ago
I did continuations back in 1987, in Turbo Pascal, but I didn't know it until today. I wrote a cooperative multi-tasking library to support some things I needed to have happen at the same time in MS-DOS.
The first time my procedure "fork" returned twice, it took me a while to wrap my head around it. I don't see why you couldn't implement continuations in modern Free Pascal/Lazarus, all you really have to do is make a copy of the stack(using the heap), then adjust BP and SP, everything else on the stack is relative to those two registers.
- touisteur 5y agoPosix has getcontext / setcontext and you can probably use/bind pcl [0] to wrap that properly. Boost has boost::context [1]. For an example of a binding to pcl you can look up ada-generators [2] which I use heavily (but am not the author). I've hard a hard time using continuations in languages with not-total capture. Ada has package variables (similar to singleton without the headache) that are not captured by call/cc libraries... There's also the secondary stack (specific to GNAT, not sure) for functions that return objects of unknown size at call site, which also are a pain to capture/restore... Good luck with those. I think continuations, to be really footgun-less, need to be integrated in the language & runtime, so that when you need to serialize the current state you can 'capture all the things'. A bit like python with its generator/automaton approach. [0] http://www.xmailserver.org/libpcl.html http://www.xmailserver.org/libpcl.html [1] http://www.devdoc.net/c/boost-1.65.1/libs/context/doc/html/context/cc.html http://www.devdoc.net/c/boost-1.65.1/libs/context/doc/html/c... [2] https://github.com/pmderodat/ada-generators https://github.com/pmderodat/ada-generators
- a1369209993 5y ago> I don't see why you couldn't implement continuations in modern Free Pascal/Lazarus, all you really have to do is make a copy of the stack(using the heap), then adjust BP and SP, everything else on the stack is relative to those two registers. Easy-to-understand but nonsystematic counterexample: what happens when the stack contains a pointer to dynamically allocated memory? If you shallow copy it, you have a double free. If you deep copy it, how? Do you even know how much memory to allocate? What if it's a file descriptor instead? (Fork only works because the OS can blindly duplicate the entire process and everything in it, and even then you have problems like IO buffers.)
- mikewarot 5y agoThis is quite similar to an unsafe use of a variable in a thread, and would have the same consequences. You have to make certain assumptions about the nature of the program as compiled, if those assumptions are wrong, poof!
- a1369209993 5y agoThe problem in this case is that the assumptions will be wrong for any nontrivial program. (Edit: pedantically, any nontrivial use of continuations in a program.)