mratsim / weave

A state-of-the-art multithreading runtime: message-passing based, fast, scalable, ultra-low overhead
Other
532 stars 22 forks source link

Support continuations #151

Open mratsim opened 4 years ago

mratsim commented 4 years ago

Motivation

It would be nice to support multithreaded continuations in the form of:

let flowvar = spawn foo(a,b,c)
flowvar.then(bar(x, _, z))

let r = sync flowvar

Syntax to be ironed out.

The _ is a placeholder for the result of the first spawn to be passed to bar reusing the convention from dup https://github.com/nim-lang/Nim/issues/14405.

In terms of low-level, continuations can just be a syntax wrapper on top of the FlowEvent machinery. The "tricky" part is swapping the spawned FlowVar to the continuation flowvar but we might just require those to be mutable and disallow continuations on let Flowvar.

mratsim commented 4 years ago

Actually we probably need a sink to allow chaining, and so the continuation is returned in a new variable.

let flowvar = spawn foo(a,b,c)
let continued = flowvar.then(bar(x, _, z).then(baz(_))
# flowvar is now invalid and we need enforcing at compile-time
# by disallowing `=`

alternatively (or both), we copy dup semantics in a continueWith macro

let flowvar = spawn foo(a,b,c)
let continued = flowvar.continueWith:
                          bar(x, _, z)
                          baz(_)