Why psync's schedules are safe #
A small model of what #[psync::graph] does, with the three facts its correctness rests on.
- A program is a list of operations on a state (
Loc → Val). Each operation declares a footprint: the locations it may read and write. Locations stand for variables and fields (the access analysis), and for files, services and other outside resources (the effect analysis). - Two operations are independent when neither writes what the other reads or writes. This is exactly when psync draws no edge between them.
Proven here:
commute: independent operations commute.interleave_correct: running chains of a level concurrently, under any interleaving that keeps each chain's own order, gives the same final state as running them one after another, provided the chains are pairwise independent. This is what each level's join does.levels_correct: a schedule of levels gives the state of running its chains in order.speculation_harmless: operations that write nothing (reads) leave the state unchanged, so running extra ones after an exit sequential code would have taken changes nothing. psync's speculation rule never runs a write early, so only such operations are ever extra.reorders_correct: a reordering that moves operations earlier only past independent ones has the effect of the program as written.replay_correct: recording every statement's exit outcome and replaying the first one in program order gives the state and the exit of sequential execution.recording_slot: recording an outcome in a slot is an ordinary operation.
Not modeled: the
analyses that compute footprints (the access and effect analyses, and cargo psync check), and
the generated Rust (type- and borrow-checked by rustc). The theorems state what those parts must
provide: correct footprints, and edges between every pair of operations that are not
independent.
Notation used in the theorem statements #
- $s : \mathrm{Loc} \to \mathrm{Val}$ is a state; an operation $o$ has a step $\mathrm{run}_o$, a read footprint $R_o$ and a write footprint $W_o$.
- $[\![ xs ]\!](s)$ runs the list of operations $xs$ in order from $s$ (
runAll); $\mathbin{+\!\!+}$ is list concatenation. - $a \mathrel{I} b$ (
Independent) means $W_a \cap (R_b \cup W_b) = \emptyset$ and $W_b \cap R_a = \emptyset$. - $a \parallel b$ is the set of interleavings of $a$ and $b$ that keep each one's order
(
Interleave); $\Vert_{c \in cs}\, c$ interleaves several chains (InterleaveAll).
$$a \mathrel{I} b \implies b \mathrel{I} a$$
Running a list of operations in order.
Equations
- Psync.runAll [] x✝ = x✝
- Psync.runAll (o :: os) x✝ = Psync.runAll os (o.run x✝)
Instances For
$$(\forall x \in xs,\ x \mathrel{I} y) \implies [\![ xs ]\!](\mathrm{run}_y(s)) = \mathrm{run}_y\bigl([\![ xs ]\!](s)\bigr)$$
An operation independent of every operation of a list commutes with the whole list.
m interleaves the chains a and b, keeping the order within each.
- nil {Loc Val : Type} : Interleave [] [] []
- left {Loc Val : Type} {x : Op Loc Val} {a b m : List (Op Loc Val)} : Interleave a b m → Interleave (x :: a) b (x :: m)
- right {Loc Val : Type} {y : Op Loc Val} {a b m : List (Op Loc Val)} : Interleave a b m → Interleave a (y :: b) (y :: m)
Instances For
$$(\forall x \in a,\ \forall y \in b,\ x \mathrel{I} y) \;\wedge\; m \in a \parallel b \implies [\![ m ]\!](s) = [\![ a \mathbin{+\!\!+} b ]\!](s)$$
2. Concurrent chains: any interleaving of two independent chains has the effect of running the first, then the second. (For more chains, interleave one with the interleaving of the rest.)
Interleaving several chains: the first with an interleaving of the rest.
- nil {Loc Val : Type} : InterleaveAll [] []
- cons {Loc Val : Type} {c : List (Op Loc Val)} {cs : List (List (Op Loc Val))} {m r : List (Op Loc Val)} : InterleaveAll cs r → Interleave c r m → InterleaveAll (c :: cs) m
Instances For
$$\mathrm{PairwiseInd}(cs) \;\wedge\; m \in \textstyle\Vert_{c \in cs}\, c \implies [\![ m ]\!](s) = [\![ c_1 \mathbin{+\!\!+} \cdots \mathbin{+\!\!+} c_k ]\!](s)$$
A whole level: any interleaving of pairwise independent chains has the effect of running the chains one after another.
How a schedule runs: each level's chains, pairwise independent, in some interleaving.
- nil {Loc Val : Type} : Runs [] []
- cons {Loc Val : Type} {cs : List (List (Op Loc Val))} {m : List (Op Loc Val)} {levels : List (List (List (Op Loc Val)))} {ms : List (List (Op Loc Val))} : PairwiseIndependent cs → InterleaveAll cs m → Runs levels ms → Runs (cs :: levels) (m :: ms)
Instances For
$$\mathrm{Runs}(\mathit{levels}, ms) \implies [\![ \mathrm{flatten}(ms) ]\!](s) = [\![ \mathrm{flatten}(\mathrm{map}\ \mathrm{flatten}\ \mathit{levels}) ]\!](s)$$
3. A schedule is a list of levels run one after another: whatever interleaving each level takes, the result is that of running every chain in order.
$$(\forall o \in \mathit{extra},\ W_o = \emptyset) \implies [\![ \mathit{extra} ]\!](s) = s$$
4. Operations that write nothing leave the state as it was.
Reorders P L: L is P with operations moved earlier only past operations independent of
them. Concretely, the first operation x of P sits in L after a run A of operations that are
all independent of x, and the rest of L reorders the rest of P the same way. psync's
schedule (levels of chains, from the dependency graph) has this property: an operation scheduled
ahead of an earlier one has no edge to it, and psync draws an edge exactly when two operations are
not independent.
- nil {Loc Val : Type} : Reorders [] []
- cons {Loc Val : Type} {x : Op Loc Val} {P A B : List (Op Loc Val)} : (∀ (y : Op Loc Val), y ∈ A → Independent y x) → Reorders P (A ++ B) → Reorders (x :: P) (A ++ x :: B)
Instances For
$$\mathrm{Reorders}(P, L) \implies [\![ L ]\!](s) = [\![ P ]\!](s)$$
5. Such a reordering has the effect of the program as written. Together with
levels_correct, the concurrent execution of psync's schedule ends in the state the sequential
program ends in.
Early exits #
A statement such as let x = f().await? may exit: whether it does, and with which value, depends
on what it reads. psync never takes an exit inside a chain: it records the outcome in an exit slot
and stops only that chain, and after the join it replays the recorded exits in program order
(exits.rs). Writes never start before an earlier statement that may exit, so the only work done
after the first exit is reads.
The theorems below say that this gives exactly the sequential behaviour: the same final state and the same exit.
Sequential execution with exits: run each operation; stop at the first that exits.
Equations
Instances For
Every operation's outcome, in program order, when all of them run (what the exit slots hold).
Equations
- Psync.outcomes [] x✝ = []
- Psync.outcomes (x_2 :: xs) x✝ = x_2.exit x✝ :: Psync.outcomes xs (x_2.op.run x✝)
Instances For
Replay: the first recorded exit in program order.
Equations
- Psync.firstExit [] = none
- Psync.firstExit (some e :: tail) = some e
- Psync.firstExit (none :: r) = Psync.firstExit r
Instances For
psync's speculation rule, along a run: every operation after the first one that exits writes nothing.
Equations
Instances For
$$\mathrm{SpecSafe}(xs, s) \implies \bigl([\![ \mathrm{op}(xs) ]\!](s),\ \mathrm{firstExit}(\mathrm{outcomes}(xs, s))\bigr) = \mathrm{seq}_X(xs, s)$$
6. Running every operation, recording the outcomes and replaying the first exit in program order gives the state and the exit of sequential execution, provided only reads run past the first exit.
What psync's generated code runs for a statement that may exit: the statement, which also
writes its outcome into its own exit slot k (encoded as a value by enc).
Equations
- One or more equations did not get rendered due to their size.
Instances For
$$\mathrm{run}_{\mathrm{rec}_k(x)}(s)(k) = \mathrm{enc}\bigl(\mathrm{exit}_x(s)\bigr)$$
7. The slot holds the statement's outcome. Since recording is an ordinary Op, every
theorem above applies to programs whose statements record their exits: a schedule of independent
chains (independence now includes the slots, which no other statement touches) leaves the same
values in every slot as program order, so the replay reads the same outcomes.