Continuations, exception handling, and message filtering in Viua VM
Published:
I have been thinking about this topic for quite some time, and decided to finally write something down, instead of just letting rough ideas accumulate in my head.
This is not the final form of the design, but rather a "stream of consciousness" style post whose main purpose is to help me organise my thoughts and ideas. Therefore, whenever I mention "continuation", "coroutine", "exception", or some other technical term, please keep in mind that I do so in the context of the Viua virtual machine. So, if I say something outrageous or that would be wildly incorrect in the context of some other system, remember that I am not talking about that other system, but about Viua.
In fact, this post is not "rather" stream of consciousness style text, but just is a stream of consciousness, since it is a direct transcription of what I have written yesterday while sitting at a café. I think this is the best possible indication of the quality and rigorousness that can be expected from what follows - it is quite literally napkin math.
Continuations and Coroutines
In contrast to processes (which are the primitive used for isolation, fault-tolerance, parallelism, and preemptive concurrency), continuations and coroutines are primitives used for cooperative concurrency. The latter two are used for handling intra-process concurrent events like exceptions and message filtering, or to implement certain algorithms.
A Continuation
A point at which execution of a stack can continue.
Conceptually, every single instruction implicitly receives a
continuation pointing at the next instruction (or, in the case of
the halt or top-frame return instructions,
to the void continuation which represents stopping
execution of a process).
Every continuation is one-shot ie, is consumed and
destroyed by the continue instruction that received it
as input.
However, continuations are copyable (ie, can be used as
inputs to the copy instruction); simply creating a
continuation does not copy the whole stack, but copying a
continuation does also copy the stack.
Since continuations are intra- but not inter-process they are not
serialisable and thus cannot be sent to a different process
or saved to memory (neither volatile memory eg, RAM, nor persistent
memory eg, files).
It could look something like this:
; create a continuation
abrakadabra $conti
; follow a continuation
continue $conti
The abrakadabra instruction would create an object
representing the continuation, and the continue
instruction would then follow the continuation at some later point
in execution.
Continuations and Stacks
Stacks are reference-counted and automatically destroyed by the virtual machine when their reference count reaches zero ie, when no continuation points to them.
The reference count of stacks is checked whenever the active stack of a process is switched, and at no other point. This ensures that an active stack with no continuations can execute until it naturally finishes execution (either by returning from a top frame, or by following a continuation). It is important not to check the reference count of the active stack, because it is impossible for the main stack of a newly created process to have a non-zero reference count.
Continuations vs Exceptions and Message Filtering
Continuations pass no information explicitly — they simply point at "stack and instruction" pairs. Therefore, the most pertinent question is what information must be returned by either the message filter or the exception handler. How to make continuations work for events that need to supply information back to whomever caused them?
Message Filtering
The idea behind message filtering is simple.
Code running on Viua must be able to implement Erlang's
receive
expression, which looks like this:
receive
Pattern1 ->
IfPattern1Matched;
...;
PatternN ->
IfPatternNMatched;
end
This is the easy case: a function is called with a read-only pointer to a memory area occupied by a decoded message, and returns a boolean value informing the virtual machine whether the message matches the expected pattern or not; if the function returned a false value the virtual machine calls it again, but gives it a pointer to the next received message, and continues doing so until the function either accepts a message or there are no further messages in the queue.
What if the function received three parameters instead of just the read-only pointer?
- the pointer to the message
- the continuation past the filter invocation
- some optional user data, for quality-of-life's sake
Then, a continue could mean "this message is accepted,
please continue execution" and a return "this message
is not accepted, and here is the reason why".
However, the same effect can be achieved with a void return for
acceptance and a non-void return for rejection.
The only problem I see here is that it could lead to accidental
accepts; so it would probably be better to have a void return signal
a rejection with no reason, and a non-void return be inspected by
the virtual machine to see what should be done with the message.
However (again), this model requires special-purpose machinery to be introduced where the virtual machine analyses top-frame returns in detail in some cases. It would be more elegant to have the behaviour explicit and more like this: a message-filtering invocation starts, and the function receives four parameters:
- the pointer to the message
- a continuation past the filter ie, an "explicit acceptance" token
- a continuation to the next message ie, an explicit "not this one, but maybe the next" token
- some optional user data
Then, acceptance becomes very explicit and rejection is easy, the default result, and the implicit fallback in case of uncertainty (let's call it a "dating-inspired design").
Exception Handling
This is also kind of simple, actually.
An exception is either serviced (and the process can continue
execution), or it is not (and the process must crash).
The same pattern can be used here as is used for message filtering
ie, explicit servicing via continue, and implicit crash
via return.
Exception handlers must fix register and memory state sometimes eg,
provide a substitute value on overflow or division by zero, but this
can be handled by specifying that the virtual machine expects to
find fixed values in specific registers (since for exceptions it
could create "special" continuations anyway).
Or, it could be something like having the exception handler allocate
a call frame and return to normal execution with a magical
continue_with_frame instruction that would carry the
fixed values as arguments.
At this point I have finished eating my Apfelkuchen and drinking my cocoa-flavoured coffee, so it was time to go.
On an unrelated note, who can drink coffee tasting like coffee and enjoy it? I still can't stand the taste of the regular stuff, and always order the sort that tastes the least like proper-adult coffee; or just flat out order a cup of cocoa if that is an option (I feel secure enough in my masculinity to do so, thank you very much). Better yet, add some crème Chantilly on top for extra uncoffeeness!