marekjm / viuavm

Parallel virtual machine designed to reliably run massively concurrent programs
https://viuavm.org/
GNU General Public License v3.0
71 stars 11 forks source link

Coroutines #135

Closed marekjm closed 6 years ago

marekjm commented 8 years ago

Viua VM should support coroutines as first-class objects.

Coroutines in Viua VM would be a way to implement deterministic, synchronised concurrency - in contrast to the usual unsynchronised, process-based concurrency. The exact semantics will be described in a separate comment and documentation section.

marekjm commented 7 years ago

Coroutines will implement cooperative concurrency. They will be running in a single process, and:

Exceptions will not be shared across coroutines, unless a coroutine does not handle an exception - then the exception is passed up the "coroutine stack". If the topmost coroutine is unable to handle the exception the process is crashed and the watchdog (if any) is called.

Creating a coroutine

coroutine %1 some_coroutine/1
; register %1 contains a coroutine handle for 'some_coroutine/1'

Calling a coroutine

; gather some parameters in a frame
frame ^[...]
; call the coroutine like a normal function
fcall %2 %1
; register %2 now contains the result of the computation
; register %1 contains a coroutine with a (potentially) changed state

Yielding from a coroutine

; from inside a coroutine...
yield %3
; here, arguments from before yield are unavailable, and
; are replaced by arguments from the frame that resumed the coroutine

Exhausting a coroutine

Exhausting a coroutine just resets it to a clean state.

; from inside a coroutine...
; pop last frame to exhaust a coroutine
return

Nesting coroutine calls

Coroutine calls can nest, i.e. one coroutine may call another coroutine. Coroutine calls may interweave, i.e. coroutine higher in the stack may call a coroutine lower in the stack (if it has a handle to it).

Exception handling for interweaved coroutines

If coroutine calls interweave and, during exception handling, an exception enters a previously killed coroutine the process is immediately crashed.


Coroutine calls and argument shadowing

With each call a coroutine gets a new set of arguments. The previous ones are discarded and lost. A coroutine is responsible for creating a backup of the arguments it needs.


Passing coroutine handles between processes

Coroutine handles may be passed between processes without errors. However, if a coroutine is resumed on a different process than the one that created it - the resuming process is immediately crashed.

marekjm commented 6 years ago

Generators may be simulated using closures in Viua VM, as they (closures) preserve their internal state between calls. However, they can return only from their top-most frame.

I don't know if coroutines are really necessary... Implementing them correctly is non-trivial, and everything that can be done with them can be implemented in terms of processes and message passing.

marekjm commented 6 years ago

Tracked by issue 0f06de7750c569736550a37067e17607c16dcd85.