I have been experimenting with continuation closures (aka callbacks) in an Actors-based environment. Actors are objects that can only run on one thread at a time, and it doesn’t matter which thread. But they preserve single-thread semantics even in the face of SMP.
Blocking activities are the bane of threading and Actors, but you can’t always know when a function call will block, nor if it is actually blocking or just taking a long time to complete. Regardless, it is possible for an Actor to construct a callback closure and pass along as a reply-to argument. When activated, it sends a special message back to the Actor to have itself run its own continuation - to preserve single thread semantics. But a simple lexical closure is missing some things...
By using an expansion of the =BIND protocol from P. Graham, you can define extensions of closures that I call “Dynamic Closures”, so that when the closure is executed, it restores most of the dynamic environment that was in place when the closure was created. This is needed for HANDLER-BIND, CATCH, and many others. It does not restore active dynamic bindings unless you also use extended forms =LET and =LET*. And UNWIND-PROTECT remains problematic. (We need a DYNAMIC-WIND, which has its own problems. You can’t regenerate an irreversible past…)
But when I constructed =LET and =LET* it became immediately apparent that Delimited Continuations (if they exist) will have problems there too. It seems that, in general, the only safe Continuations would have to capture the entire stack in order to capture all the dynamic bindings also in effect at the time of creation. I am not overtly using Delimited Continuations, although you could do that with something like CL-CONT. I personally think that =BIND conveys a more Lisp centric display of what is happening, as constrasted with the hidden syntactic and semantic effects of ASYNC/AWAIT style.
So I did a search for “Dynamic Closures” in Lisp to see if anyone else had gone down this rabbit hole. I found a page by Paul Khuong talking about “Common Cold” as applied to SBCL and making serializable closures for web programming. But no libraries come up in searches.
Anyone else gone there?
- David McClain
_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.comhttp://www.lispworks.com/support/lisp-hug.html