Ranvier.Heap.Concurrency

Reading settings

Look

Mode

Definitions

Proofs

Imports

Arguments

References

References open

Saved in this browser.

ModulesGuarantees

Modules

Audit0Extract12

Base

Prelude9Syntax43

Reference

Semantics63Properties202

Pure

ObserverSet29SourceList104State16Edges12Marking117Engine103Check204Runs259Scheduler241Ownership49WholeGraph562Refinement465Witnesses244Optimised271Ledger6Collections302Concurrency141Extensions123Deltas68FastPaths435Combinators106MemoTable147MemoTableWitnesses62MemoTableFastPaths113

Heap

ObserverSet46SourceList89Ghost160Marking22Run286Scheduler7Ownership29Adequacy3Collections38Concurrency41Extensions17Deltas10MemoTable28

Conformance

Expected64Check119
On this page

On this page

Heap concurrency, thread affinity, and inbox contractsHeapLang code for concurrency, holder lock, inbox, and PendingDispatchaffinity_valguarded_enter_codeserialised_enter_codeserialised_release_codepost_queued_codepd_try_claim_codepd_try_close_codeaff_enter_coderead_memo_fast_codesignal_set_after_acquire_codesignal_set_fast_split_codenull_comparer_codeencode_comparer_codeeval_comparer_sentinel_codecheck_fast_write_token_codesignal_set_token_fast_codeis_concurrency_stateenter_res_valguarded_enter_specserialised_acquire_specserialised_release_specpost_queued_specpending_dispatch_claim_specpending_dispatch_close_specHot/cold fast-path specifications for Memo.Value and Signal.Valueaff_enter_specread_memo_fast_specsignal_set_fast_split_speccmp_outcome_valeq_prim_codenullable_comparer_argsdefault_comparer_null_sentinel_speccheck_fast_write_token_specfast_write_token_specCoalescing SignalPort HeapLang specificationssignal_port_post_off_codesignal_port_post_inline_codesignal_port_drain_codeis_signal_portsignal_port_post_off_specsignal_port_post_inline_specsignal_port_drain_spec
This definition

Ranvier.Heap.Concurrency

Heap concurrency, thread affinity, and inbox contracts

This module lifts the concurrency, Serialised reentrant holder, inbox handover, and PendingDispatch contracts of Pure/Concurrency.v to HeapLang separation logic:

  1. guarded_enter_spec: Guarded thread-affinity gate (AssertOnGraphThread / Enter).
  2. serialised_acquire_spec and serialised_release_spec: Serialised reentrant holder lock (CAS on holder, context check, and Release with non-empty inbox drain re-post).
  3. post_queued_spec: cross-thread inbox enqueue (PostQueued) and drain wake post.
  4. pending_dispatch_claim_spec and pending_dispatch_close_spec: atomic CAS claim (TryClaim) and cancellation/abandon (TryClose) on PendingDispatch.
  5. read_memo_fast_spec, signal_set_fast_split_spec, default_comparer_null_sentinel_spec, and fast_write_token_spec: .NET hot/cold fast-path specifications for Memo.Value, Signal.Value, ComparerSentinel, and Graph.fastWriteToken / OwnerThreadToken.
7
From stdpp Require Import prelude gmap.
From Stdlib Require Import ZArith.
From Ranvier.Base Require Import Prelude Syntax.
From Ranvier.Pure Require Import State Engine Scheduler Concurrency.
From iris.heap_lang Require Import proofmode notation.
From Ranvier.Heap Require Import ObserverSet SourceList Ghost Marking Run Scheduler.
Open Scope nat_scope.

HeapLang code for concurrency, holder lock, inbox, and PendingDispatch

Layout of a 6-cell concurrency control block cs:

  • cs +ₗ 0: affinity (0 = Guarded, 1 = Unchecked, 2 = Serialised)
  • cs +ₗ 1: ownerThread
  • cs +ₗ 2: context
  • cs +ₗ 3: holder (0 when free, me > 0 when held)
  • cs +ₗ 4: inbox (HeapLang list of work item ids)
  • cs +ₗ 5: pumpPosted (boolean)
Definition affinity_val (aff : thread_affinity) : val :=
  match aff with
  | AffGuarded => #0
  | AffUnchecked => #1
  | AffSerialised => #2
  end.
References
Definition guarded_enter_code : val :=
  λ: "cs" "me",
    let: "owner" := !("cs" +ₗ #1) in
    if: "me" = "owner" then SOME #false
    else NONE.
References

Uses

  • false
  • heap_lang.val
  • NONE
  • SOME
Definition serialised_enter_code : val :=
  λ: "cs" "me" "ctx",
    let: "h" := !("cs" +ₗ #3) in
    if: "h" = "me" then SOME #false
    else
      let: "c" := !("cs" +ₗ #2) in
      if: "ctx" = "c" then
        if: CAS ("cs" +ₗ #3) #0 "me" then SOME #true
        else NONE
      else NONE.
References

Uses

  • false
  • true
  • heap_lang.val
  • CAS
  • NONE
  • SOME
Definition serialised_release_code : val :=
  λ: "cs",
    ("cs" +ₗ #3) <- #0;;
    let: "inb" := !("cs" +ₗ #4) in
    match: "inb" with
      NONE => #()
    | SOME <> => ("cs" +ₗ #5) <- #true
    end.
References

Uses

  • true
  • heap_lang.val
Definition post_queued_code : val :=
  λ: "cs" "w",
    let: "inb" := !("cs" +ₗ #4) in
    ("cs" +ₗ #4) <- vlist_snoc_code "inb" "w";;
    if: !"cs" = #2 then ("cs" +ₗ #5) <- #true
    else (if: CAS ("cs" +ₗ #5) #false #true then #() else #()).
References

Uses

Definition pd_try_claim_code : val :=
  λ: "pd",
    CAS "pd" #0 #1.
References

Uses

  • heap_lang.val
  • CAS
Definition pd_try_close_code : val :=
  λ: "pd",
    CAS "pd" #0 #2.
References

Uses

  • heap_lang.val
  • CAS
Definition aff_enter_code : val :=
  λ: "cs" "me" "ctx",
    let: "aff" := !"cs" in
    if: "aff" = #0 then guarded_enter_code "cs" "me"
    else if: "aff" = #1 then SOME #false
    else serialised_enter_code "cs" "me" "ctx".
References

Uses

Definition read_memo_fast_code : val :=
  λ: "g" "n",
    match: graph_get_node_code "g" "n" with
      NONE => read_code "g" "n" #true
    | SOME "d" =>
        let: "k" := Fst (Fst (Fst (Fst "d"))) in
        if: "k" = #1 then
          let: "fr" := Fst (Fst (Snd (Fst "d"))) in
          let: "st" := Snd (Snd (Fst (Fst "d"))) in
          if: (BinOp OrOp "fr" (BinOp AndOp "st" #3)) = #0 then
            track_code "g" "n";;
            InjL (Snd (Fst (Fst (Fst "d"))))
          else
            read_code "g" "n" #true
        else
          read_code "g" "n" #true
    end.
References

Uses

3 more
  • heap_lang.OrOp
  • heap_lang.Snd
  • heap_lang.val
Definition signal_set_after_acquire_code : val :=
  λ: "cs" "g" "n" "v",
    match: graph_get_node_code "g" "n" with
      NONE => serialised_release_code "cs";; NONE
    | SOME "d" =>
        let: "cur" := Snd (Fst (Fst (Fst "d"))) in
        if: "cur" = "v" then
          serialised_release_code "cs";; SOME #()
        else
          write_signal_code "g" "n" "v";;
          serialised_release_code "cs";;
          SOME #()
    end.
References

Uses

Definition signal_set_fast_split_code : val :=
  λ: "cs" "me" "ctx" "g" "n" "v",
    match: aff_enter_code "cs" "me" "ctx" with
      NONE => NONE
    | SOME "must_rel" =>
        if: "must_rel" then
          signal_set_after_acquire_code "cs" "g" "n" "v"
        else
          match: graph_get_node_code "g" "n" with
            NONE => NONE
          | SOME "d" =>
              let: "cur" := Snd (Fst (Fst (Fst "d"))) in
              if: "cur" = "v" then SOME #()
              else
                write_signal_code "g" "n" "v";;
                SOME #()
          end
    end.
References

Uses

1 more
  • SOME
Definition null_comparer_code : val :=
  λ: <> <>, InjR (#0, #0).
References

Uses

  • heap_lang.InjR
  • heap_lang.val
Definition encode_comparer_code : val :=
  λ: "is_val_type" "is_val_def" "is_ref_id" "opt_cmp",
    match: "opt_cmp" with
      NONE => SOME null_comparer_code
    | SOME "f" =>
        if: "is_val_type" then
          (if: "is_val_def" then NONE else SOME "f")
        else
          (if: "is_ref_id" then NONE else SOME "f")
    end.
References

Uses

Definition eval_comparer_sentinel_code : val :=
  λ: "is_val_type" "enc" "a" "b",
    if: "is_val_type" then
      match: "enc" with
        NONE => InjL ("a" = "b")
      | SOME "cmp" => "cmp" "a" "b"
      end
    else
      match: "enc" with
        NONE => InjL ("a" = "b")
      | SOME "cmp" => "cmp" "a" "b"
      end.
References

Uses

  • heap_lang.InjL
  • heap_lang.val
Definition check_fast_write_token_code : val :=
  λ: "tok_loc" "caller_tok",
    let: "tok" := !"tok_loc" in
    if: #0 ≤ "tok" then
      (if: "tok" = #0 then #true else "tok" = "caller_tok")
    else #false.
References

Uses

  • false
  • true
  • heap_lang.val
Definition signal_set_token_fast_code : val :=
  λ: "tok_loc" "caller_tok" "cs" "me" "ctx" "g" "n" "v",
    if: check_fast_write_token_code "tok_loc" "caller_tok" then
      match: graph_get_node_code "g" "n" with
        NONE => NONE
      | SOME "d" =>
          let: "cur" := Snd (Fst (Fst (Fst "d"))) in
          if: "cur" = "v" then SOME #()
          else
            write_signal_code "g" "n" "v";;
            SOME #()
      end
    else
      signal_set_fast_split_code "cs" "me" "ctx" "g" "n" "v".
References
Section heap_concurrency.
Context `{!heapGS Σ, !graphG Σ}.
References

Uses

Definition is_concurrency_state (cs_loc : loc) (cs : concurrency_state) : iProp Σ :=
  cs_loc ↦ affinity_val (cs_affinity cs) ∗
  (cs_loc +ₗ 1) ↦ #(Z.of_nat (cs_owner_thread cs)) ∗
  (cs_loc +ₗ 2) ↦ #(Z.of_nat (cs_context cs)) ∗
  (cs_loc +ₗ 3) ↦ #(Z.of_nat (cs_holder cs)) ∗
  (cs_loc +ₗ 4) ↦ list_val (cs_inbox cs) ∗
  (cs_loc +ₗ 5) ↦ #(cs_pump_posted cs).
References
Definition enter_res_val (res : option (concurrency_state * bool)) : val :=
  match res with
  | None => NONEV
  | Some (_, b) => SOMEV #b
  end.
References

Uses

✓ No axioms

guarded_enter_code checks me = cs_owner_thread without mutating is_concurrency_state, returning SOME #false on the owner thread and NONE off-thread.

Arguments

cs_loc : loc
the concurrency control block location.
me : nat
the calling thread id.
ctx : nat
the calling context id.
the concurrency state with cs_affinity cs = AffGuarded.
Theorem guarded_enter_spec (cs_loc : loc) (me ctx : nat) (cs : concurrency_state) :
  cs_affinity cs = AffGuarded ->
  {{{ is_concurrency_state cs_loc cs }}}
    guarded_enter_code #cs_loc #(Z.of_nat me)
  {{{ RET enter_res_val (graph_enter me ctx cs); is_concurrency_state cs_loc cs }}}.

Arguments

cs_loc : loc
the concurrency control block location.
me : nat
the calling thread id.
ctx : nat
the calling context id.
the concurrency state with cs_affinity cs = AffGuarded.
Qed
Proof.
  intros Haff.
  iIntros (Φ) "(H0 & H1 & H2 & H3 & H4 & H5) HΦ".
  rewrite /guarded_enter_code /graph_enter Haff /=.
  wp_lam. wp_pures. wp_load. wp_pures.
  destruct (decide (me = cs_owner_thread cs)) as [->|Hne].
  - rewrite bool_decide_eq_true_2 //. wp_pures. iModIntro.
    iApply "HΦ". rewrite /is_concurrency_state Haff /=. iFrame.
  - rewrite bool_decide_eq_false_2; [|intros Heq; injection Heq as Heq; apply Hne; lia].
    wp_pures. iModIntro.
    iApply "HΦ". rewrite /is_concurrency_state Haff /=. iFrame.
Qed.
References
✓ No axioms

serialised_enter_code implements reentrant Serialised acquisition via CAS on holder (cs_loc +ₗ 3) and context verification (cs_loc +ₗ 2).

Covers all three outcomes of graph_enter under AffSerialised:

  • Reentrant entry (cs_holder cs = me): returns SOME #false and preserves cs.
  • Outermost entry (cs_holder cs = 0 and ctx = cs_context cs): CAS transitions holder from 0 to me and returns SOME #true.
  • Contended or wrong-context entry: returns NONE and leaves cs untouched.

Arguments

cs_loc : loc
the concurrency control block location.
me : nat
the calling thread id (me > 0).
ctx : nat
the calling thread's SynchronizationContext id.
the concurrency state with cs_affinity cs = AffSerialised.
Theorem serialised_acquire_spec (cs_loc : loc) (me ctx : nat) (cs : concurrency_state) :
  cs_affinity cs = AffSerialised ->
  {{{ is_concurrency_state cs_loc cs }}}
    serialised_enter_code #cs_loc #(Z.of_nat me) #(Z.of_nat ctx)
  {{{ RET enter_res_val (graph_enter me ctx cs);
      is_concurrency_state cs_loc
        (match graph_enter me ctx cs with Some (cs', _) => cs' | None => cs end) }}}.

Arguments

cs_loc : loc
the concurrency control block location.
me : nat
the calling thread id (me > 0).
ctx : nat
the calling thread's SynchronizationContext id.
the concurrency state with cs_affinity cs = AffSerialised.
Qed
Proof.
  intros Haff.
  iIntros (Φ) "(H0 & H1 & H2 & H3 & H4 & H5) HΦ".
  rewrite /serialised_enter_code /graph_enter Haff /=.
  wp_lam. wp_pures. wp_load. wp_pures.
  destruct (decide (cs_holder cs = me)) as [Hhold|Hne].
  - rewrite Hhold bool_decide_eq_true_2 //. wp_pures. iModIntro.
    iApply "HΦ". rewrite /is_concurrency_state Haff Hhold /=. iFrame.
  - rewrite bool_decide_eq_false_2; [|intros Heq; injection Heq as Heq; apply Hne; lia].
    wp_pures. wp_load. wp_pures.
    destruct (decide (ctx = cs_context cs /\ cs_holder cs = 0)) as [[Hctx Hfree]|Hbad].
    + rewrite Hctx bool_decide_eq_true_2 //. wp_pures.
      rewrite Hfree.
      wp_cmpxchg_suc; [done|]. wp_pures. iModIntro.
      iApply "HΦ". rewrite /is_concurrency_state /=. iFrame.
    + destruct (decide (ctx = cs_context cs)) as [Hctx|Hctx_ne].
      * assert (Hnh : cs_holder cs ≠ 0) by (intros H0; apply Hbad; split; done).
        rewrite Hctx bool_decide_eq_true_2 //. wp_pures.
        wp_cmpxchg_fail; [intros Heq; injection Heq as Heq; apply Hnh; lia|].
        wp_pures. iModIntro. iApply "HΦ". rewrite /is_concurrency_state Haff /=. iFrame.
      * rewrite bool_decide_eq_false_2; [|intros Heq; injection Heq as Heq; apply Hctx_ne; lia].
        wp_pures. iModIntro. iApply "HΦ". rewrite /is_concurrency_state Haff /=. iFrame.
Qed.
References
✓ No axioms

serialised_release_code clears holder to 0 and sets pumpPosted = true whenever cs_inbox is non-empty (graph_release).

Arguments

cs_loc : loc
the concurrency control block location.
the concurrency state before Release.
Theorem serialised_release_spec (cs_loc : loc) (cs : concurrency_state) :
  {{{ is_concurrency_state cs_loc cs }}}
    serialised_release_code #cs_loc
  {{{ RET #(); is_concurrency_state cs_loc (graph_release cs) }}}.

Arguments

cs_loc : loc
the concurrency control block location.
the concurrency state before Release.
Qed
Proof.
  iIntros (Φ) "(H0 & H1 & H2 & H3 & H4 & H5) HΦ".
  rewrite /serialised_release_code /graph_release.
  wp_lam. wp_pures. wp_store. wp_load.
  destruct (cs_inbox cs) as [|w ws] eqn:Einb; simpl; wp_pures.
  - iModIntro. iApply "HΦ". rewrite /is_concurrency_state /=. iFrame.
  - wp_store. iModIntro. iApply "HΦ". rewrite /is_concurrency_state /=. iFrame.
Qed.
References
✓ No axioms

post_queued_code appends work item w to the FIFO inbox and leaves pumpPosted = true: by a store under Serialised, and otherwise by a CAS from false that posts a drain only when none is pending (post_queued).

Arguments

cs_loc : loc
the concurrency control block location.
w : nat
the work item id enqueued.
the concurrency state before PostQueued.
Theorem post_queued_spec (cs_loc : loc) (w : nat) (cs : concurrency_state) :
  {{{ is_concurrency_state cs_loc cs }}}
    post_queued_code #cs_loc #(Z.of_nat w)
  {{{ RET #();
      ⌜drain_guaranteed (post_queued w cs)⌝ ∗
      is_concurrency_state cs_loc (post_queued w cs) }}}.

Arguments

cs_loc : loc
the concurrency control block location.
w : nat
the work item id enqueued.
the concurrency state before PostQueued.
Qed
Proof.
  iIntros (Φ) "(H0 & H1 & H2 & H3 & H4 & H5) HΦ".
  rewrite /post_queued_code /post_queued.
  wp_lam. wp_pures. wp_load. wp_pures.
  rewrite (list_val_eq (cs_inbox cs)).
  wp_apply (vlist_snoc_spec (fun k : nat => #(Z.of_nat k)) (cs_inbox cs) w); [done|].
  iIntros "_". wp_store. wp_load.
  (* `Serialised` posts on every call; the other affinities post when `CAS` moves `pumpPosted` from false to true. *)
  destruct (cs_affinity cs); simpl; wp_pures.
  - destruct (cs_pump_posted cs); [wp_cmpxchg_fail|wp_cmpxchg_suc]; wp_pures; iModIntro; iApply "HΦ";
      (iSplit;
        [iPureIntro; intros _; left; reflexivity
        |rewrite /is_concurrency_state /= (list_val_eq (cs_inbox cs ++ [w])); iFrame]).
  - destruct (cs_pump_posted cs); [wp_cmpxchg_fail|wp_cmpxchg_suc]; wp_pures; iModIntro; iApply "HΦ";
      (iSplit;
        [iPureIntro; intros _; left; reflexivity
        |rewrite /is_concurrency_state /= (list_val_eq (cs_inbox cs ++ [w])); iFrame]).
  - wp_store. iModIntro. iApply "HΦ". iSplit.
    + iPureIntro. intros _. left. reflexivity.
    + rewrite /is_concurrency_state /= (list_val_eq (cs_inbox cs ++ [w])). iFrame.
Qed.
References
✓ No axioms

pd_try_claim_code atomically transitions a PendingDispatch cell from 0 (Waiting) to 1 (Claimed) via CAS (pd_try_claim).

Arguments

pd : loc
the PendingDispatch state location.
the current state (0 = waiting, 1 = claimed, 2 = closed).
Theorem pending_dispatch_claim_spec (pd : loc) (st : pd_state) :
  {{{ pd ↦ #(Z.of_nat st) }}}
    pd_try_claim_code #pd
  {{{ RET #(snd (pd_try_claim st)); pd ↦ #(Z.of_nat (fst (pd_try_claim st))) }}}.

Arguments

pd : loc
the PendingDispatch state location.
the current state (0 = waiting, 1 = claimed, 2 = closed).
Qed
Proof.
  iIntros (Φ) "Hpd HΦ".
  rewrite /pd_try_claim_code /pd_try_claim.
  wp_lam.
  destruct (decide (st = 0)) as [->|Hne].
  - wp_cmpxchg_suc; [done|]. wp_pures. iModIntro. by iApply "HΦ".
  - wp_cmpxchg_fail; [intros Heq; injection Heq as Heq; apply Hne; lia|].
    wp_pures. iModIntro. by iApply "HΦ".
Qed.
References

Uses

✓ No axioms

pd_try_close_code atomically transitions a PendingDispatch cell from 0 (Waiting) to 2 (Closed) via CAS (pd_try_close).

Arguments

pd : loc
the PendingDispatch state location.
the current state (0 = waiting, 1 = claimed, 2 = closed).
Theorem pending_dispatch_close_spec (pd : loc) (st : pd_state) :
  {{{ pd ↦ #(Z.of_nat st) }}}
    pd_try_close_code #pd
  {{{ RET #(snd (pd_try_close st)); pd ↦ #(Z.of_nat (fst (pd_try_close st))) }}}.

Arguments

pd : loc
the PendingDispatch state location.
the current state (0 = waiting, 1 = claimed, 2 = closed).
Qed
Proof.
  iIntros (Φ) "Hpd HΦ".
  rewrite /pd_try_close_code /pd_try_close.
  wp_lam.
  destruct (decide (st = 0)) as [->|Hne].
  - wp_cmpxchg_suc; [done|]. wp_pures. iModIntro. by iApply "HΦ".
  - wp_cmpxchg_fail; [intros Heq; injection Heq as Heq; apply Hne; lia|].
    wp_pures. iModIntro. by iApply "HΦ".
Qed.
References

Uses

Hot/cold fast-path specifications for Memo.Value and Signal.Value

Lemma aff_enter_spec (cs_loc : loc) (me ctx : nat) (cs : concurrency_state) :
  {{{ is_concurrency_state cs_loc cs }}}
    aff_enter_code #cs_loc #(Z.of_nat me) #(Z.of_nat ctx)
  {{{ RET enter_res_val (graph_enter me ctx cs);
      is_concurrency_state cs_loc
        (match graph_enter me ctx cs with Some (cs', _) => cs' | None => cs end) }}}.
Qed
Proof.
  iIntros (Φ) "(H0 & H1 & H2 & H3 & H4 & H5) HΦ".
  rewrite /aff_enter_code. wp_lam. wp_pures. wp_load. wp_pures.
  destruct (cs_affinity cs) eqn:Haff; simpl; wp_pures.
  - wp_apply (guarded_enter_spec cs_loc me ctx cs Haff with "[H0 H1 H2 H3 H4 H5]").
    + rewrite /is_concurrency_state Haff /=. iFrame.
    + iIntros "Hcs".
      destruct (guarded_enter_iff_owner me ctx cs Haff) as [[_ Hsome] [Hne _]].
      destruct (decide (me = cs_owner_thread cs)) as [Heq|Hneq].
      * assert (Hge : graph_enter me ctx cs = Some (cs, false)) by (by apply Hsome).
        rewrite Hge. by iApply "HΦ".
      * assert (Hge : graph_enter me ctx cs = None) by (by apply Hne).
        rewrite Hge. by iApply "HΦ".
  - rewrite /graph_enter Haff /=. iModIntro.
    iApply "HΦ". rewrite /is_concurrency_state Haff /=. iFrame.
  - wp_apply (serialised_acquire_spec cs_loc me ctx cs Haff with "[H0 H1 H2 H3 H4 H5]").
    + rewrite /is_concurrency_state Haff /=. iFrame.
    + done.
Qed.
References
✓ No axioms

read_memo_fast_code inspects freshness and status &&& 3 inline to track and return clean cached memo values directly while matching read_code across all states.

Arguments

γ : iprop.gname
the ghost name.
g : lang.heap_lang.val
the graph value.
the target node id.
the initial pure state.
fuel : nat
the pure step budget.
the final pure state.
the read outcome.
Theorem read_memo_fast_spec γ g (n : node_id) (s : State.state) (fuel : nat) (s' : State.state) (r : outcome) :
  read fuel n true s = Some (s', r) ->
  {{{ is_graph γ g s }}}
    read_memo_fast_code g #(Z.of_nat n)
  {{{ RET outcome_val r; is_graph γ g s' }}}.

Arguments

γ : iprop.gname
the ghost name.
g : lang.heap_lang.val
the graph value.
the target node id.
the initial pure state.
fuel : nat
the pure step budget.
the final pure state.
the read outcome.
Qed
Proof.
  intros Hread. iIntros (Φ) "Hg HΦ".
  rewrite /read_memo_fast_code. wp_lam. wp_pures.
  wp_apply (graph_get_node_spec with "Hg"). iIntros "Hg".
  destruct (g_nodes s !! n) as [d|] eqn:Hnode; simpl.
  - rewrite /node_val /node_val_a /node_val_b /node_val_c /node_val_d.
    wp_pures.
    destruct (n_kind d) eqn:Hk; simpl; wp_pures.
    + wp_apply (read_spec _ _ n true s fuel s' r Hread with "Hg"). iExact "HΦ".
    + destruct (n_freshness d) eqn:Hfr, (n_status d) eqn:Hst; simpl; wp_pures.
      1: {
        destruct fuel as [|fuel']; [done|].
        destruct fuel' as [|f]; [done|].
        pose proof (proj2 (memo_read_fast_split_eq f n s)) as Hsplit.
        rewrite Hsplit /read_memo_fast in Hread.
        unfold m_bind, m_return, get_node in Hread. simpl in Hread.
        rewrite Hnode Hk Hfr Hst in Hread.
        assert (Hc : is_clean_and_ready_bits Clean StNone false = true) by reflexivity.
        rewrite Hc in Hread.
        destruct (track n s) as [[s1 []]|] eqn:Htr; [|discriminate Hread].
        injection Hread as -> <-.
        assert (Hndom : n ∈ dom (g_nodes s)) by (by apply elem_of_dom_2 in Hnode).
        wp_apply (track_spec _ _ n s s' Hndom Htr with "Hg"). iIntros "Hg".
        wp_pures. by iApply "HΦ".
      }
      all: wp_apply (read_spec _ _ n true s fuel s' r Hread with "Hg"); iExact "HΦ".
    + wp_apply (read_spec _ _ n true s fuel s' r Hread with "Hg"). iExact "HΦ".
  - wp_pures.
    wp_apply (read_spec _ _ n true s fuel s' r Hread with "Hg"). iExact "HΦ".
Qed.
References
✓ No axioms

signal_set_fast_split_code enters affinity first (aff_enter_code), short-circuits unjournaled equal writes without write_signal_code or serialised_release_code when must_rel = false, and releases acquired Serialised locks cleanly.

Arguments

γ : iprop.gname
the ghost name.
g : lang.heap_lang.val
the graph value.
cs_loc : loc
the concurrency control block location.
me : nat
the calling thread id.
ctx : nat
the calling thread's SynchronizationContext id.
f : nat
inner pure step budget for write_signal (S f).
the signal node id.
v : Z
the written integer value.
the initial concurrency state.
the initial pure graph state.
the pure state produced by write_signal (S f) n v s.
d : node
the signal node record at n.
Theorem signal_set_fast_split_spec
    γ g (cs_loc : loc) (me ctx f : nat) (n : node_id) (v : Z)
    (cs : concurrency_state) (s s_w : State.state) (d : node) :
  g_nodes s !! n = Some d ->
  n_kind d = KSignal ->
  write_signal (S f) n v s = Some (s_w, tt) ->
  {{{ is_concurrency_state cs_loc cs ∗ is_graph γ g s }}}
    signal_set_fast_split_code #cs_loc #(Z.of_nat me) #(Z.of_nat ctx) g #(Z.of_nat n) #v
  {{{ RET match signal_set_fast_split me ctx (S f) n v cs None s with
          | Some _ => SOMEV #()
          | None => NONEV
          end;
      match signal_set_fast_split me ctx (S f) n v cs None s with
      | Some (cs', _, s') => is_concurrency_state cs_loc cs' ∗ is_graph γ g s'
      | None => is_concurrency_state cs_loc cs ∗ is_graph γ g s
      end }}}.

Arguments

γ : iprop.gname
the ghost name.
g : lang.heap_lang.val
the graph value.
cs_loc : loc
the concurrency control block location.
me : nat
the calling thread id.
ctx : nat
the calling thread's SynchronizationContext id.
f : nat
inner pure step budget for write_signal (S f).
the signal node id.
v : Z
the written integer value.
the initial concurrency state.
the initial pure graph state.
the pure state produced by write_signal (S f) n v s.
d : node
the signal node record at n.
Qed
Proof.
  intros Hnode Hk Hws.
  iIntros (Φ) "(Hcs & Hg) HΦ".
  rewrite /signal_set_fast_split_code. wp_lam. wp_pures.
  wp_apply (aff_enter_spec cs_loc me ctx cs with "Hcs"). iIntros "Hcs".
  destruct (graph_enter me ctx cs) as [[cs1 []]|] eqn:Hge; simpl; wp_pures.
  - rewrite /signal_set_after_acquire_code. wp_lam. wp_pures.
    wp_apply (graph_get_node_spec with "Hg"). iIntros "Hg".
    rewrite Hnode /node_val /node_val_a /node_val_b /node_val_c /node_val_d /=.
    wp_pures.
    destruct (decide (n_value d = v)) as [Heq|Hne].
    + rewrite Heq bool_decide_eq_true_2 //. wp_pures.
      wp_apply (serialised_release_spec cs_loc cs1 with "Hcs"). iIntros "Hcs".
      wp_pures. iModIntro.
      rewrite /signal_set_fast_split /signal_set_after_acquire Hge Hnode Hk decide_True //.
      iApply "HΦ". iFrame.
    + rewrite bool_decide_eq_false_2; [|intros Hc; injection Hc as Hc; apply Hne; lia].
      wp_pures.
      wp_apply (write_signal_spec γ g n v s (S f) s_w Hws with "Hg"). iIntros "Hg".
      wp_pures.
      wp_apply (serialised_release_spec cs_loc cs1 with "Hcs"). iIntros "Hcs".
      wp_pures. iModIntro.
      rewrite /signal_set_fast_split /signal_set_after_acquire Hge Hnode Hk decide_False // Hws /=.
      iApply "HΦ". iFrame.
  - wp_apply (graph_get_node_spec with "Hg"). iIntros "Hg".
    rewrite Hnode /node_val /node_val_a /node_val_b /node_val_c /node_val_d /=.
    wp_pures.
    destruct (decide (n_value d = v)) as [Heq|Hne].
    + rewrite Heq bool_decide_eq_true_2 //. wp_pures. iModIntro.
      rewrite /signal_set_fast_split Hge Hnode Hk decide_True //.
      iApply "HΦ". iFrame.
    + rewrite bool_decide_eq_false_2; [|intros Hc; injection Hc as Hc; apply Hne; lia].
      wp_pures.
      wp_apply (write_signal_spec γ g n v s (S f) s_w Hws with "Hg"). iIntros "Hg".
      wp_pures. iModIntro.
      rewrite /signal_set_fast_split Hge Hnode Hk decide_False // Hws /=.
      iApply "HΦ". iFrame.
  - rewrite /signal_set_fast_split Hge.
    iModIntro. iApply "HΦ". iFrame.
Qed.
References
Definition cmp_outcome_val (out : cmp_outcome) : val :=
  match out with
  | CmpBool b => InjLV #b
  | CmpNullRef => InjRV (#0, #0)
  | CmpThrow err => InjRV (#1, #err)
  end.
References

Uses

Definition eq_prim_code : val :=
  λ: "x" "y", InjL ("x" = "y").
References

Uses

  • heap_lang.InjL
  • heap_lang.val
Definition nullable_comparer_args (c : nullable_comparer) (fv : val) : val * val * val :=
  match c with
  | CmpNullInput => (#false, #false, NONEV)
  | CmpValDefault => (#true, #false, SOMEV eq_prim_code)
  | CmpRefIdentity => (#false, #true, SOMEV eq_prim_code)
  | CmpCustomInput _ => (#false, #false, SOMEV fv)
  end.
References
✓ No axioms

encode_comparer_code followed by eval_comparer_sentinel_code encodes value-type EqualityComparer<'T>.Default and reference-type ReferenceComparer<'T> as NONE (null) evaluated via primitive =, raises CmpNullRef via null_comparer_code when the input comparer was NONE, and invokes other comparers unchanged.

Arguments

is_val_type : bool
true when typeof<'T>.IsValueType, false otherwise.
fv : val
the HeapLang closure representing a custom comparer when c = CmpCustomInput f.
a : Z
the previous integer value.
b : Z
the candidate integer value.
Theorem default_comparer_null_sentinel_spec
    (is_val_type : bool) (c : nullable_comparer) (fv : val) (a b : Z) :
  (forall f, c = CmpCustomInput f ->
    {{{ True }}} fv #a #b {{{ RET cmp_outcome_val (f a b); True }}}) ->
  let '(is_val_def, is_ref_id, opt_cmp) := nullable_comparer_args c fv in
  {{{ True }}}
    let: "enc" := encode_comparer_code #is_val_type is_val_def is_ref_id opt_cmp in
    eval_comparer_sentinel_code #is_val_type "enc" #a #b
  {{{ RET cmp_outcome_val (eval_nullable_comparer Z.eqb Z.eqb c a b); True }}}.

Arguments

is_val_type : bool
true when typeof<'T>.IsValueType, false otherwise.
fv : val
the HeapLang closure representing a custom comparer when c = CmpCustomInput f.
a : Z
the previous integer value.
b : Z
the candidate integer value.
Qed
Proof.
  intros Hcustom.
  destruct is_val_type, c as [| | |f]; simpl; iIntros (Φ) "_ HΦ".
  - rewrite /encode_comparer_code /eval_comparer_sentinel_code /null_comparer_code.
    wp_pures. iModIntro. by iApply "HΦ".
  - rewrite /encode_comparer_code /eval_comparer_sentinel_code.
    wp_pures. iModIntro.
    replace (bool_decide (#a = #b)) with (Z.eqb a b).
    + by iApply "HΦ".
    + symmetry. destruct (Z.eqb_spec a b) as [->|Hne].
      * by rewrite bool_decide_eq_true_2.
      * rewrite bool_decide_eq_false_2 //; intros [= H]; exact (Hne H).
  - rewrite /encode_comparer_code /eval_comparer_sentinel_code /eq_prim_code.
    wp_pures. iModIntro.
    replace (bool_decide (#a = #b)) with (Z.eqb a b).
    + by iApply "HΦ".
    + symmetry. destruct (Z.eqb_spec a b) as [->|Hne].
      * by rewrite bool_decide_eq_true_2.
      * rewrite bool_decide_eq_false_2 //; intros [= H]; exact (Hne H).
  - rewrite /encode_comparer_code /eval_comparer_sentinel_code.
    wp_pures.
    wp_apply (Hcustom f eq_refl with "[//]").
    iIntros "_". by iApply "HΦ".
  - rewrite /encode_comparer_code /eval_comparer_sentinel_code /null_comparer_code.
    wp_pures. iModIntro. by iApply "HΦ".
  - rewrite /encode_comparer_code /eval_comparer_sentinel_code /eq_prim_code.
    wp_pures. iModIntro.
    replace (bool_decide (#a = #b)) with (Z.eqb a b).
    + by iApply "HΦ".
    + symmetry. destruct (Z.eqb_spec a b) as [->|Hne].
      * by rewrite bool_decide_eq_true_2.
      * rewrite bool_decide_eq_false_2 //; intros [= H]; exact (Hne H).
  - rewrite /encode_comparer_code /eval_comparer_sentinel_code.
    wp_pures. iModIntro.
    replace (bool_decide (#a = #b)) with (Z.eqb a b).
    + by iApply "HΦ".
    + symmetry. destruct (Z.eqb_spec a b) as [->|Hne].
      * by rewrite bool_decide_eq_true_2.
      * rewrite bool_decide_eq_false_2 //; intros [= H]; exact (Hne H).
  - rewrite /encode_comparer_code /eval_comparer_sentinel_code.
    wp_pures.
    wp_apply (Hcustom f eq_refl with "[//]").
    iIntros "_". by iApply "HΦ".
Qed.
References
Lemma check_fast_write_token_spec (tok_loc : loc) (tok caller_tok : Z) :
  {{{ tok_loc ↦ #tok }}}
    check_fast_write_token_code #tok_loc #caller_tok
  {{{ RET #(check_fast_write_token tok caller_tok); tok_loc ↦ #tok }}}.
Qed
Proof.
  iIntros (Φ) "Htok HΦ".
  rewrite /check_fast_write_token_code /check_fast_write_token.
  wp_lam. wp_pures. wp_load. wp_pures.
  destruct (decide (0 <= tok)%Z) as [Hnn|Hneg].
  - rewrite !(bool_decide_eq_true_2 (0 <= tok)%Z) //=.
    wp_pures.
    destruct (decide (tok = 0%Z)) as [->|Hnt0].
    + rewrite (bool_decide_eq_true_2 (0%Z = 0%Z)) //=.
      wp_pures. iModIntro. by iApply "HΦ".
    + rewrite (bool_decide_eq_false_2 (tok = 0%Z)) //=.
      rewrite (bool_decide_eq_false_2 (#tok = #0)); [|intros [= H]; exact (Hnt0 H)].
      wp_pures.
      destruct (decide (tok = caller_tok)) as [->|Hntc].
      * rewrite (bool_decide_eq_true_2 (caller_tok = caller_tok)) //.
        rewrite (bool_decide_eq_true_2 (#caller_tok = #caller_tok)) //.
        iModIntro. by iApply "HΦ".
      * rewrite (bool_decide_eq_false_2 (tok = caller_tok)) //.
        rewrite (bool_decide_eq_false_2 (#tok = #caller_tok)); [|intros [= H]; exact (Hntc H)].
        iModIntro. by iApply "HΦ".
  - rewrite !(bool_decide_eq_false_2 (0 <= tok)%Z) //=.
    wp_pures. iModIntro. by iApply "HΦ".
Qed.
References

Uses

✓ No axioms

signal_set_token_fast_code loads fastWriteToken (tok_loc) and compares against 0 or caller_tok to bypass aff_enter_code on unjournaled Unchecked and owner-thread Guarded writes while refining signal_set_entered identically across all affinities and threads.

Arguments

γ : iprop.gname
the ghost name.
g : lang.heap_lang.val
the graph value.
tok_loc : loc
the immutable fastWriteToken cell location.
cs_loc : loc
the concurrency control block location.
owner_tok : Z
the positive token (>= 1) assigned to cs_owner_thread cs.
caller_tok : Z
OwnerThreadToken.Current on the calling thread (>= 0).
me : nat
the calling thread id.
ctx : nat
the calling thread's SynchronizationContext id.
f : nat
inner pure step budget for write_signal (S f).
the signal node id.
v : Z
the written integer value.
the initial concurrency state.
the initial pure graph state.
the pure state produced by write_signal (S f) n v s.
d : node
the signal node record at n.
Theorem fast_write_token_spec
    γ g (tok_loc cs_loc : loc) (owner_tok caller_tok : Z)
    (me ctx f : nat) (n : node_id) (v : Z)
    (cs : concurrency_state) (s s_w : State.state) (d : node) :
  (1 <= owner_tok)%Z ->
  (0 <= caller_tok)%Z ->
  (caller_tok = owner_tok <-> me = cs_owner_thread cs) ->
  g_nodes s !! n = Some d ->
  n_kind d = KSignal ->
  write_signal (S f) n v s = Some (s_w, tt) ->
  let tok := compute_fast_write_token (cs_affinity cs) false owner_tok in
  {{{ tok_loc ↦ #tok ∗ is_concurrency_state cs_loc cs ∗ is_graph γ g s }}}
    signal_set_token_fast_code #tok_loc #caller_tok #cs_loc #(Z.of_nat me) #(Z.of_nat ctx) g #(Z.of_nat n) #v
  {{{ RET match signal_set_entered me ctx (S f) n v cs None s with
          | Some _ => SOMEV #()
          | None => NONEV
          end;
      tok_loc ↦ #tok ∗
      match signal_set_entered me ctx (S f) n v cs None s with
      | Some (cs', _, s') => is_concurrency_state cs_loc cs' ∗ is_graph γ g s'
      | None => is_concurrency_state cs_loc cs ∗ is_graph γ g s
      end }}}.

Arguments

γ : iprop.gname
the ghost name.
g : lang.heap_lang.val
the graph value.
tok_loc : loc
the immutable fastWriteToken cell location.
cs_loc : loc
the concurrency control block location.
owner_tok : Z
the positive token (>= 1) assigned to cs_owner_thread cs.
caller_tok : Z
OwnerThreadToken.Current on the calling thread (>= 0).
me : nat
the calling thread id.
ctx : nat
the calling thread's SynchronizationContext id.
f : nat
inner pure step budget for write_signal (S f).
the signal node id.
v : Z
the written integer value.
the initial concurrency state.
the initial pure graph state.
the pure state produced by write_signal (S f) n v s.
d : node
the signal node record at n.
Qed
Proof.
  intros Hown Hcaller Htok_eq Hnode Hk Hws. cbv zeta.
  set (tok := compute_fast_write_token (cs_affinity cs) false owner_tok).
  destruct (fast_write_token_iff owner_tok caller_tok me ctx f n v cs None s Hown Hcaller Htok_eq)
    as (_ & _ & Href).
  assert (Hbody :
    signal_set_entered me ctx (S f) n v cs None s =
    if check_fast_write_token tok caller_tok then
      if decide (n_value d = v) then Some (cs, None, s) else Some (cs, None, s_w)
    else
      signal_set_fast_split me ctx (S f) n v cs None s).
  { rewrite <- Href. unfold signal_set_token_fast.
    rewrite Hnode. cbv beta iota zeta.
    rewrite Hk Hws. reflexivity. }
  rewrite Hbody.
  iIntros (Φ) "(Htok & Hcs & Hg) HΦ".
  rewrite /signal_set_token_fast_code. wp_lam. wp_pures.
  wp_apply (check_fast_write_token_spec tok_loc tok caller_tok with "Htok").
  iIntros "Htok".
  destruct (check_fast_write_token tok caller_tok) eqn:Hchk; wp_pures.
  - wp_apply (graph_get_node_spec with "Hg"). iIntros "Hg".
    rewrite Hnode /node_val /node_val_a /node_val_b /node_val_c /node_val_d /=.
    wp_pures.
    destruct (decide (n_value d = v)) as [Heq|Hne].
    + rewrite Heq bool_decide_eq_true_2 //. wp_pures. iModIntro.
      iApply "HΦ". iFrame.
    + rewrite bool_decide_eq_false_2; [|intros [= Hc]; exact (Hne Hc)].
      wp_pures.
      wp_apply (write_signal_spec γ g n v s (S f) s_w Hws with "Hg"). iIntros "Hg".
      wp_pures. iModIntro.
      iApply "HΦ". iFrame.
  - wp_apply (signal_set_fast_split_spec γ g cs_loc me ctx f n v cs s s_w d Hnode Hk Hws with "[$Hcs $Hg]").
    iIntros "Hpost".
    iApply "HΦ". iFrame.
Qed.
References

Coalescing SignalPort HeapLang specifications

Heap layout of a 4-cell SignalPort block port_loc:

  • port_loc +ₗ 0: disposed (boolean)
  • port_loc +ₗ 1: inInbox (boolean)
  • port_loc +ₗ 2: hasPending (boolean)
  • port_loc +ₗ 3: pendingVal (integer value 'T)
Definition signal_port_post_off_code : val :=
  λ: "port" "cs" "drain_id" "v",
    if: !"port" then #()
    else
      ("port" +ₗ #3) <- "v";;
      ("port" +ₗ #2) <- #true;;
      if: !("port" +ₗ #1) then #()
      else
        ("port" +ₗ #1) <- #true;;
        post_queued_code "cs" "drain_id".
References

Uses

Definition signal_port_post_inline_code : val :=
  λ: "port" "g" "n" "v",
    if: !"port" then #()
    else
      (if: !("port" +ₗ #2) then
         ("port" +ₗ #2) <- #false;;
         ("port" +ₗ #3) <- #0
       else #());;
      write_signal_code "g" "n" "v";;
      #().
References

Uses

Definition signal_port_drain_code : val :=
  λ: "port" "g" "n",
    if: !("port" +ₗ #1) then
      let: "take" := !("port" +ₗ #2) in
      let: "v" := !("port" +ₗ #3) in
      ("port" +ₗ #1) <- #false;;
      ("port" +ₗ #2) <- #false;;
      ("port" +ₗ #3) <- #0;;
      if: !"port" then #()
      else if: "take" then
        write_signal_code "g" "n" "v";;
        #()
      else #()
    else #().
References

Uses

Definition is_signal_port (port_loc : loc) (st : signal_port_state) : iProp Σ :=
  port_loc ↦ #(sp_disposed st) ∗
  (port_loc +ₗ 1) ↦ #(sp_in_inbox st) ∗
  (port_loc +ₗ 2) ↦ #(sp_has_pending st) ∗
  (port_loc +ₗ 3) ↦ #(sp_pending_val st).
References

Uses

✓ No axioms

signal_port_post_off_code coalesces off-thread posts in port_loc, enqueuing drain_id via post_queued_code iff sp_in_inbox = false, and drops posts silently when sp_disposed = true.

Arguments

port_loc : loc
the 4-cell SignalPort heap block.
cs_loc : loc
the target graph's concurrency control block.
drain_id : nat
the work-item id representing this port's pre-allocated drain callback.
v : Z
the posted integer value.
the target graph's concurrency_state.
Theorem signal_port_post_off_spec
    (port_loc cs_loc : loc) (drain_id : nat) (v : Z)
    (st : signal_port_state) (cs : concurrency_state) :
  sp_pending_attr st = None ->
  {{{ is_signal_port port_loc st ∗ is_concurrency_state cs_loc cs }}}
    signal_port_post_off_code #port_loc #cs_loc #(Z.of_nat drain_id) #v
  {{{ RET #();
      is_signal_port port_loc (sp_post_off v None st) ∗
      is_concurrency_state cs_loc
        (if sp_disposed st || sp_in_inbox st then cs else post_queued drain_id cs) }}}.

Arguments

port_loc : loc
the 4-cell SignalPort heap block.
cs_loc : loc
the target graph's concurrency control block.
drain_id : nat
the work-item id representing this port's pre-allocated drain callback.
v : Z
the posted integer value.
the target graph's concurrency_state.
Qed
Proof.
  intros Hattr.
  iIntros (Φ) "((Hd & Hin & Hp & Hv) & Hcs) HΦ".
  rewrite /signal_port_post_off_code /sp_post_off.
  wp_lam. wp_pures. wp_load.
  destruct (sp_disposed st) eqn:Edisp; wp_pures.
  - iModIntro. iApply "HΦ". rewrite /is_signal_port Edisp /=. iFrame.
  - wp_store. wp_store. wp_load.
    destruct (sp_in_inbox st) eqn:Ein; simpl; wp_pures.
    + iModIntro. iApply "HΦ". rewrite /is_signal_port /=. iFrame.
    + wp_store.
      wp_apply (post_queued_spec cs_loc drain_id cs with "Hcs").
      iIntros "[_ Hcs]".
      iApply "HΦ". rewrite /is_signal_port /=. iFrame.
Qed.
References
✓ No axioms

signal_port_post_inline_code clears any pending off-thread value (hasPending := false, pendingVal := 0) before applying write_signal_code inline on the owning thread, and drops silently when sp_disposed = true.

Arguments

γ : iprop.gname
the ghost name.
g : lang.heap_lang.val
the target graph value.
port_loc : loc
the 4-cell SignalPort heap block.
f : nat
inner pure step budget for write_signal (S f).
the target signal node id.
v : Z
the posted value.
the signal_port_state before inline Post.
the pure graph state before inline Post.
the pure graph state after write_signal (S f) n v s.
Theorem signal_port_post_inline_spec
    γ g (port_loc : loc) (f : nat) (n : node_id) (v : Z)
    (st : signal_port_state) (s s_w : State.state) :
  write_signal (S f) n v s = Some (s_w, tt) ->
  {{{ is_signal_port port_loc st ∗ is_graph γ g s }}}
    signal_port_post_inline_code #port_loc g #(Z.of_nat n) #v
  {{{ RET #();
      is_signal_port port_loc (sp_post_inline v st) ∗
      is_graph γ g (if sp_disposed st then s else s_w) }}}.

Arguments

γ : iprop.gname
the ghost name.
g : lang.heap_lang.val
the target graph value.
port_loc : loc
the 4-cell SignalPort heap block.
f : nat
inner pure step budget for write_signal (S f).
the target signal node id.
v : Z
the posted value.
the signal_port_state before inline Post.
the pure graph state before inline Post.
the pure graph state after write_signal (S f) n v s.
Qed
Proof.
  intros Hws.
  iIntros (Φ) "((Hd & Hin & Hp & Hv) & Hg) HΦ".
  rewrite /signal_port_post_inline_code /sp_post_inline.
  wp_lam. wp_pures. wp_load.
  destruct (sp_disposed st) eqn:Edisp; wp_pures.
  - iModIntro. iApply "HΦ". rewrite /is_signal_port Edisp /=. iFrame.
  - wp_load.
    destruct (sp_has_pending st) eqn:Epend; wp_pures.
    + wp_store. wp_store.
      wp_apply (write_signal_spec γ g n v s (S f) s_w Hws with "Hg").
      iIntros "Hg". wp_pures. iModIntro.
      iApply "HΦ". rewrite /is_signal_port /=. iFrame.
    + wp_apply (write_signal_spec γ g n v s (S f) s_w Hws with "Hg").
      iIntros "Hg". wp_pures. iModIntro.
      iApply "HΦ". rewrite /is_signal_port /=. iFrame.
Qed.
References
✓ No axioms

signal_port_drain_code resets inInbox, hasPending, and pendingVal to idle, applies write_signal_code with sp_pending_val iff sp_in_inbox = true, sp_disposed = false, and sp_has_pending = true, and skips superseded or disposed drains cleanly.

Arguments

γ : iprop.gname
the ghost name.
g : lang.heap_lang.val
the target graph value.
port_loc : loc
the 4-cell SignalPort heap block.
f : nat
inner pure step budget for write_signal (S f).
the target signal node id.
the signal_port_state before Drain.
the pure graph state before Drain.
the pure graph state after write_signal (S f) n (sp_pending_val st) s.
Theorem signal_port_drain_spec
    γ g (port_loc : loc) (f : nat) (n : node_id)
    (st : signal_port_state) (s s_w : State.state) :
  write_signal (S f) n (sp_pending_val st) s = Some (s_w, tt) ->
  {{{ is_signal_port port_loc st ∗ is_graph γ g s }}}
    signal_port_drain_code #port_loc g #(Z.of_nat n)
  {{{ RET #();
      is_signal_port port_loc (sp_drain st) ∗
      is_graph γ g
        (if sp_in_inbox st && negb (sp_disposed st) && sp_has_pending st then s_w else s) }}}.

Arguments

γ : iprop.gname
the ghost name.
g : lang.heap_lang.val
the target graph value.
port_loc : loc
the 4-cell SignalPort heap block.
f : nat
inner pure step budget for write_signal (S f).
the target signal node id.
the signal_port_state before Drain.
the pure graph state before Drain.
the pure graph state after write_signal (S f) n (sp_pending_val st) s.
Qed
Proof.
  intros Hws.
  iIntros (Φ) "((Hd & Hin & Hp & Hv) & Hg) HΦ".
  rewrite /signal_port_drain_code /sp_drain.
  wp_lam. wp_pures. wp_load.
  destruct (sp_in_inbox st) eqn:Ein; simpl; wp_pures.
  - wp_load. wp_pures. wp_load. wp_pures.
    wp_store. wp_store. wp_store. wp_load.
    destruct (sp_disposed st) eqn:Edisp; simpl; wp_pures.
    + iModIntro. iApply "HΦ". rewrite /is_signal_port /=. iFrame.
    + destruct (sp_has_pending st) eqn:Epend; simpl; wp_pures.
      * wp_apply (write_signal_spec γ g n (sp_pending_val st) s (S f) s_w Hws with "Hg").
        iIntros "Hg". wp_pures. iModIntro.
        iApply "HΦ". rewrite /is_signal_port /=. iFrame.
      * iModIntro. iApply "HΦ". rewrite /is_signal_port /=. iFrame.
  - iModIntro. iApply "HΦ". rewrite /is_signal_port Ein /=. iFrame.
Qed.
References
End heap_concurrency.