Library zoo_parabs.ws_hub_hybrid
From zoo Require Import
prelude.
From zoo.common Require Import
countable
gmultiset
list.
From zoo.iris.bi Require Import
big_op.
From zoo.iris.base_logic Require Import
lib.ghost_list.
From zoo.language Require Import
notations.
From zoo.diaframe Require Import
diaframe.
From zoo_std Require Import
int
option
optional
array
random_round
domain.
From zoo_saturn Require Import
mpmc_queue_1.
From zoo_parabs Require Export
base
ws_hub_hybrid__code.
From zoo_parabs Require Import
ws_hub_hybrid__types
ws_bdeques_public
waiters.
From zoo Require Import
options.
Implicit Types b yield closed : bool.
Implicit Types num_active : Z.
Implicit Types ðĄ : location.
Implicit Types v t notification notify pred : val.
Implicit Types vs : gmultiset val.
Implicit Types ws us vs_queue : list val.
Implicit Types vss : list $ list val.
Implicit Types status : status.
Implicit Types empty : emptiness.
Class WsHubHybridG ÎĢ `{zoo_G : !ZooG ÎĢ} :=
{ #[local] ws_hub_hybrid_G_deques_G :: WsBdequesPublicG ÎĢ
; #[local] ws_hub_hybrid_G_queue_G :: MpmcQueue1G ÎĢ
; #[local] ws_hub_hybrid_G_waiters_G :: WaitersG ÎĢ
; #[local] ws_hub_hybrid_G_emptiness_G :: GhostListG ÎĢ emptiness
}.
Definition ws_hub_hybrid_ÎĢ :=
#[ws_bdeques_public_ÎĢ
; mpmc_queue_1_ÎĢ
; waiters_ÎĢ
; ghost_list_ÎĢ emptiness
].
#[global] Instance subG_ws_hub_hybrid_ÎĢ ÎĢ `{zoo_G : !ZooG ÎĢ} :
subG ws_hub_hybrid_ÎĢ ÎĢ â
WsHubHybridG ÎĢ.
Section consistent.
#[local] Definition consistent vs vss vs_queue :=
vs =
â+ (list_to_set_disj <$> vss) â
list_to_set_disj vs_queue.
#[local] Lemma consistent_alloc sz :
consistent â (replicate sz []) [].
#[local] Lemma consistent_empty vs vss vs_queue :
consistent vs vss vs_queue â
vs = â â
( â i us,
vss !! i = Some us â
us = []
) â§
vs_queue = [].
#[local] Lemma consistent_deque_push {vs vss vs_queue i us} v :
vss !! i = Some us â
consistent vs vss vs_queue â
consistent ({[+v+]} â vs) (<[i := us ++ [v]]> vss) vs_queue.
#[local] Lemma consistent_deque_remove {vs vss vs_queue i us} us1 v us2 :
vss !! i = Some us â
us = us1 ++ v :: us2 â
consistent vs vss vs_queue â
â vs',
vs = {[+v+]} â vs' â§
consistent vs' (<[i := us1 ++ us2]> vss) vs_queue.
#[local] Lemma consistent_deque_pop vs vss vs_queue i us v :
vss !! i = Some (us ++ [v]) â
consistent vs vss vs_queue â
â vs',
vs = {[+v+]} â vs' â§
consistent vs' (<[i := us]> vss) vs_queue.
#[local] Lemma consistent_deque_steal vs vss vs_queue i v us :
vss !! i = Some (v :: us) â
consistent vs vss vs_queue â
â vs',
vs = {[+v+]} â vs' â§
consistent vs' (<[i := us]> vss) vs_queue.
#[local] Lemma consistent_queue_push {vs vss vs_queue} v :
consistent vs vss vs_queue â
consistent ({[+v+]} â vs) vss (vs_queue ++ [v]).
#[local] Lemma consistent_queue_pop vs vss v vs_queue :
consistent vs vss (v :: vs_queue) â
â vs',
vs = {[+v+]} â vs' â§
consistent vs' vss vs_queue.
End consistent.
Opaque consistent.
Section ws_hub_hybrid_G.
Context `{ws_hub_hybrid_G : WsHubHybridG ÎĢ}.
Implicit Types P P_notification P_pred Q Q_pred : iProp ÎĢ.
Record metadata :=
{ metadata_size : nat
; metadata_deques : val
; metadata_rounds : val
; metadata_queue : val
; metadata_waiters : val
; metadata_emptiness : gname
}.
Implicit Types Îģ : metadata.
#[local] Instance metadata_eq_dec :
EqDecision metadata.
#[local] Instance metadata_countable :
Countable metadata.
#[local] Definition emptiness_auth' Îģ_emptiness sz vs_queue : iProp ÎĢ :=
â emptys,
ghost_list_auth Îģ_emptiness emptys â
âlength emptys = szâ â
â vs_queue = []
âĻ â i,
emptys !! i = Some Nonempty
â.
#[local] Definition emptiness_auth Îģ :=
emptiness_auth' Îģ.(metadata_emptiness) Îģ.(metadata_size).
#[local] Instance : CustomIpat "emptiness_auth" :=
" ( %emptys & Hauth & %Hemptys & %Hemptiness ) ".
#[local] Definition emptiness_at' Îģ_emptiness i :=
ghost_list_at Îģ_emptiness i (DfracOwn 1).
#[local] Definition emptiness_at Îģ :=
emptiness_at' Îģ.(metadata_emptiness).
#[local] Definition inv_inner ðĄ : iProp ÎĢ :=
â num_active,
ðĄ.[num_active] âĶ #num_active.
#[local] Instance : CustomIpat "inv_inner" :=
" ( %num_active & HðĄ_num_active ) ".
Definition ws_hub_hybrid_inv t Îđ sz : iProp ÎĢ :=
â ðĄ Îģ,
ât = #ðĄâ â
ðĄ ⊠Îģ â
âsz = Îģ.(metadata_size)â â
ðĄ.[deques] âĶ⥠Îģ.(metadata_deques) â
ðĄ.[rounds] âĶ⥠Îģ.(metadata_rounds) â
ðĄ.[queue] âĶ⥠Îģ.(metadata_queue) â
ðĄ.[waiters] âĶ⥠Îģ.(metadata_waiters) â
ws_bdeques_public_inv Îģ.(metadata_deques) Îđ Îģ.(metadata_size) â
array_inv Îģ.(metadata_rounds) Îģ.(metadata_size) â
mpmc_queue_1_inv Îģ.(metadata_queue) Îđ â
waiters_inv Îģ.(metadata_waiters) sz â
inv nroot (inv_inner ðĄ).
#[local] Instance : CustomIpat "inv" :=
" ( %ðĄ{} & %Îģ{} & {%Heq{};->} & #Hmeta{} & -> & #HðĄ{}_deques & #HðĄ{}_queue & #HðĄ{}_rounds & #HðĄ{}_waiters & #Hdeques{}_inv & #Hrounds{}_inv & #Hqueue{}_inv & #Hwaiters{}_inv & #Hinv{} ) ".
Definition ws_hub_hybrid_model t vs : iProp ÎĢ :=
â ðĄ Îģ vss vs_queue,
ât = #ðĄâ â
ðĄ ⊠Îģ â
ws_bdeques_public_model Îģ.(metadata_deques) vss â
mpmc_queue_1_model Îģ.(metadata_queue) vs_queue â
âconsistent vs vss vs_queueâ â
emptiness_auth Îģ vs_queue.
#[local] Instance : CustomIpat "model" :=
" ( %ðĄ_ & %Îģ_ & %vss & %vs_queue & %Heq & Hmeta_ & Hdeques_model & Hqueue_model & %Hconsistent & Hemptiness_auth ) ".
Definition ws_hub_hybrid_owner t i status empty : iProp ÎĢ :=
â ðĄ Îģ ws round n,
ât = #ðĄâ â
ðĄ ⊠Îģ â
ws_bdeques_public_owner Îģ.(metadata_deques) i status ws â
âempty = Empty â ws = []â â
array_slice Îģ.(metadata_rounds) i DfracDiscarded [round] â
random_round_model' round (Îģ.(metadata_size) - 1) n â
emptiness_at Îģ i empty.
#[local] Instance : CustomIpat "owner" :=
" ( %ðĄ{;_} & %Îģ{;_} & %ws{} & %round{} & %n{} & %Heq{} & Hmeta{;_} & Hdeques_owner{} & %Hempty{} & #Hrounds{} & Hround{} & Hemptiness_at{_{}} ) ".
#[global] Instance ws_hub_hybrid_model_timeless t vs :
Timeless (ws_hub_hybrid_model t vs).
#[global] Instance ws_hub_hybrid_inv_persistent t Îđ sz :
Persistent (ws_hub_hybrid_inv t Îđ sz).
#[local] Lemma emptiness_alloc sz :
âĒ |==>
â Îģ_emptiness,
emptiness_auth' Îģ_emptiness sz [] â
[â list] i â seq 0 sz,
emptiness_at' Îģ_emptiness i Empty.
#[local] Lemma emptiness_at_valid Îģ vs_queue i empty :
emptiness_auth Îģ vs_queue -â
emptiness_at Îģ i empty -â
âi < Îģ.(metadata_size)â.
#[local] Lemma emptiness_empty Îģ vs_queue :
emptiness_auth Îģ vs_queue -â
( [â list] i â seq 0 Îģ.(metadata_size),
emptiness_at Îģ i Empty
) -â
âvs_queue = []â.
#[local] Lemma emptiness_update_auth Îģ v vs_queue :
emptiness_auth Îģ (v :: vs_queue) âĒ
emptiness_auth Îģ vs_queue.
#[local] Lemma emptiness_update_Nonempty {Îģ vs_queue i empty} vs_queue' :
emptiness_auth Îģ vs_queue -â
emptiness_at Îģ i empty ==â
emptiness_auth Îģ vs_queue' â
emptiness_at Îģ i Nonempty.
#[local] Lemma emptiness_update_Empty Îģ i empty :
emptiness_auth Îģ [] -â
emptiness_at Îģ i empty ==â
emptiness_auth Îģ [] â
emptiness_at Îģ i Empty.
Opaque emptiness_auth'.
Lemma ws_hub_hybrid_inv_agree t Îđ sz1 sz2 :
ws_hub_hybrid_inv t Îđ sz1 -â
ws_hub_hybrid_inv t Îđ sz2 -â
âsz1 = sz2â.
Lemma ws_hub_hybrid_owner_exclusive t i status1 empty1 status2 empty2 :
ws_hub_hybrid_owner t i status1 empty1 -â
ws_hub_hybrid_owner t i status2 empty2 -â
False.
Lemma ws_hub_hybrid_inv_owner t Îđ sz i status empty :
ws_hub_hybrid_inv t Îđ sz -â
ws_hub_hybrid_owner t i status empty -â
âi < szâ.
Lemma ws_hub_hybrid_model_empty t Îđ sz vs :
ws_hub_hybrid_inv t Îđ sz -â
ws_hub_hybrid_model t vs -â
( [â list] i â seq 0 sz,
â status,
ws_hub_hybrid_owner t i status Empty
) -â
âvs = â â.
Lemma ws_hub_hybridŲ createðspec Îđ sz :
(0 âĪ sz)%Z â
{{{
True
}}}
ws_hub_hybridŲ create #sz
{{{
t
, RET t;
ws_hub_hybrid_inv t Îđ âsz â
ws_hub_hybrid_model t â â
[â list] i â seq 0 âsz,
ws_hub_hybrid_owner t i Nonblocked Empty
}}}.
Lemma ws_hub_hybridŲ sizeðspec t Îđ sz :
{{{
ws_hub_hybrid_inv t Îđ sz
}}}
ws_hub_hybridŲ size t
{{{
RET #sz;
True
}}}.
#[local] Lemma ws_hub_hybridŲ begin_inactiveðspec t Îđ sz :
{{{
ws_hub_hybrid_inv t Îđ sz
}}}
ws_hub_hybridŲ begin_inactive t
{{{
RET ();
True
}}}.
#[local] Lemma ws_hub_hybridŲ end_inactiveðspec t Îđ sz :
{{{
ws_hub_hybrid_inv t Îđ sz
}}}
ws_hub_hybridŲ end_inactive t
{{{
RET ();
True
}}}.
#[local] Lemma ws_hub_hybridŲ block_activeðspec t Îđ sz i i_ empty :
i = âši_ â
{{{
ws_hub_hybrid_inv t Îđ sz â
ws_hub_hybrid_owner t i_ Nonblocked empty
}}}
ws_hub_hybridŲ block_active t #i
{{{
RET ();
ws_hub_hybrid_owner t i_ Blocked empty
}}}.
#[local] Lemma ws_hub_hybridŲ unblock_activeðspec t Îđ sz i i_ empty :
i = âši_ â
{{{
ws_hub_hybrid_inv t Îđ sz â
ws_hub_hybrid_owner t i_ Blocked empty
}}}
ws_hub_hybridŲ unblock_active t #i
{{{
RET ();
ws_hub_hybrid_owner t i_ Nonblocked empty
}}}.
Lemma ws_hub_hybridŲ blockðspec t Îđ sz i i_ empty :
i = âši_ â
{{{
ws_hub_hybrid_inv t Îđ sz â
ws_hub_hybrid_owner t i_ Nonblocked empty
}}}
ws_hub_hybridŲ block t #i
{{{
RET ();
ws_hub_hybrid_owner t i_ Blocked empty
}}}.
Lemma ws_hub_hybridŲ unblockðspec t Îđ sz i i_ empty :
i = âši_ â
{{{
ws_hub_hybrid_inv t Îđ sz â
ws_hub_hybrid_owner t i_ Blocked empty
}}}
ws_hub_hybridŲ unblock t #i
{{{
RET ();
ws_hub_hybrid_owner t i_ Nonblocked empty
}}}.
Lemma ws_hub_hybridŲ closedðspec t Îđ sz :
{{{
ws_hub_hybrid_inv t Îđ sz
}}}
ws_hub_hybridŲ closed t
{{{
closed
, RET #closed;
True
}}}.
#[local] Lemma ws_hub_hybridŲ notifyðspec t Îđ sz :
{{{
ws_hub_hybrid_inv t Îđ sz
}}}
ws_hub_hybridŲ notify t
{{{
RET ();
True
}}}.
#[local] Lemma ws_hub_hybridŲ notify_allðspec t Îđ sz :
{{{
ws_hub_hybrid_inv t Îđ sz
}}}
ws_hub_hybridŲ notify_all t
{{{
RET ();
True
}}}.
Lemma ws_hub_hybridŲ pushðspec t Îđ sz i i_ empty v :
i = âši_ â
<<<
ws_hub_hybrid_inv t Îđ sz â
ws_hub_hybrid_owner t i_ Nonblocked empty
| ââ vs,
ws_hub_hybrid_model t vs
>>>
ws_hub_hybridŲ push t #i v @ âÎđ
<<<
ws_hub_hybrid_model t ({[+v+]} â vs)
| RET ();
ws_hub_hybrid_owner t i_ Nonblocked Nonempty
>>>.
Lemma ws_hub_hybridŲ popðspec t Îđ sz i i_ empty :
i = âši_ â
<<<
ws_hub_hybrid_inv t Îđ sz â
ws_hub_hybrid_owner t i_ Nonblocked empty
| ââ vs,
ws_hub_hybrid_model t vs
>>>
ws_hub_hybridŲ pop t #i @ âÎđ
<<<
ââ o,
match o with
| None â
ws_hub_hybrid_model t vs
| Some v â
â vs',
âvs = {[+v+]} â vs'â â
ws_hub_hybrid_model t vs'
end
| RET o;
ws_hub_hybrid_owner t i_ Nonblocked (if o then empty else Empty)
>>>.
#[local] Lemma ws_hub_hybridŲ try_steal_onceðspec t Îđ sz i i_ empty :
i = âši_ â
<<<
ws_hub_hybrid_inv t Îđ sz â
ws_hub_hybrid_owner t i_ Blocked empty
| ââ vs,
ws_hub_hybrid_model t vs
>>>
ws_hub_hybridŲ try_steal_once t #i @ âÎđ
<<<
ââ o,
match o with
| None â
ws_hub_hybrid_model t vs
| Some v â
â vs',
âvs = {[+v+]} â vs'â â
ws_hub_hybrid_model t vs'
end
| RET o;
ws_hub_hybrid_owner t i_ Blocked empty
>>>.
#[local] Lemma ws_hub_hybrid_try_stealâðspec P Q t Îđ sz i i_ empty yield max_round pred :
i = âši_ â
(0 âĪ max_round)%Z â
<<<
ws_hub_hybrid_inv t Îđ sz â
ws_hub_hybrid_owner t i_ Blocked empty â
P â
⥠(
P -â
WP pred () {{ res,
â b,
âres = #bâ â
if b then Q else P
}}
)
| ââ vs,
ws_hub_hybrid_model t vs
>>>
ws_hub_hybridŲ try_stealâ t #i #yield #max_round pred @ âÎđ
<<<
ââ o,
match o with
| Nothing
| Anything â
ws_hub_hybrid_model t vs
| Something v â
â vs',
âvs = {[+v+]} â vs'â â
ws_hub_hybrid_model t vs'
end
| RET o;
ws_hub_hybrid_owner t i_ Blocked empty â
if o is Anything then Q else P
>>>.
#[local] Lemma ws_hub_hybridŲ try_stealðspec P Q t Îđ sz i i_ empty max_round_noyield max_round_yield pred :
i = âši_ â
(0 âĪ max_round_noyield)%Z â
(0 âĪ max_round_yield)%Z â
<<<
ws_hub_hybrid_inv t Îđ sz â
ws_hub_hybrid_owner t i_ Blocked empty â
P â
⥠(
P -â
WP pred () {{ res,
â b,
âres = #bâ â
if b then Q else P
}}
)
| ââ vs,
ws_hub_hybrid_model t vs
>>>
ws_hub_hybridŲ try_steal t #i #max_round_noyield #max_round_yield pred @ âÎđ
<<<
ââ o,
match o with
| Nothing
| Anything â
ws_hub_hybrid_model t vs
| Something v â
â vs',
âvs = {[+v+]} â vs'â â
ws_hub_hybrid_model t vs'
end
| RET o;
ws_hub_hybrid_owner t i_ Blocked empty â
if o is Anything then Q else P
>>>.
#[local] Lemma ws_hub_hybridŲ steal_auxðspec P_notification P_pred Q_pred t Îđ sz i i_ empty max_round_noyield max_round_yield notification pred :
i = âši_ â
(0 âĪ max_round_noyield)%Z â
(0 âĪ max_round_yield)%Z â
<<<
ws_hub_hybrid_inv t Îđ sz â
ws_hub_hybrid_owner t i_ Blocked empty â
P_notification â
( â notify,
P_notification -â
WP notify () {{ itype_unit }} -â
WP notification notify {{ res,
âres = ()%Vâ â
P_notification
}}
) â
P_pred â
⥠(
P_pred -â
WP pred () {{ res,
â b,
âres = #bâ â
if b then Q_pred else P_pred
}}
)
| ââ vs,
ws_hub_hybrid_model t vs
>>>
ws_hub_hybridŲ steal_aux t #i #max_round_noyield #max_round_yield notification pred @ âÎđ
<<<
ââ o,
match o with
| None â
ws_hub_hybrid_model t vs
| Some v â
â vs',
âvs = {[+v+]} â vs'â â
ws_hub_hybrid_model t vs'
end
| RET o;
ws_hub_hybrid_owner t i_ (if o then Nonblocked else Blocked) empty â
P_notification â
if o then P_pred else Q_pred
>>>.
Lemma ws_hub_hybridŲ steal_untilðspec P_notification P_pred Q_pred t Îđ sz i i_ empty max_round_noyield max_round_yield notification pred :
i = âši_ â
(0 âĪ max_round_noyield)%Z â
(0 âĪ max_round_yield)%Z â
<<<
ws_hub_hybrid_inv t Îđ sz â
ws_hub_hybrid_owner t i_ Nonblocked empty â
P_notification â
( â notify,
P_notification -â
WP notify () {{ itype_unit }} -â
WP notification notify {{ res,
âres = ()%Vâ â
P_notification
}}
) â
P_pred â
⥠(
P_pred -â
WP pred () {{ res,
â b,
âres = #bâ â
if b then Q_pred else P_pred
}}
)
| ââ vs,
ws_hub_hybrid_model t vs
>>>
ws_hub_hybridŲ steal_until t #i #max_round_noyield #max_round_yield notification pred @ âÎđ
<<<
ââ o,
match o with
| None â
ws_hub_hybrid_model t vs
| Some v â
â vs',
âvs = {[+v+]} â vs'â â
ws_hub_hybrid_model t vs'
end
| RET o;
ws_hub_hybrid_owner t i_ Nonblocked empty â
P_notification â
if o then P_pred else Q_pred
>>>.
Lemma ws_hub_hybridŲ stealðspec t Îđ sz i i_ empty max_round_noyield max_round_yield :
i = âši_ â
(0 âĪ max_round_noyield)%Z â
(0 âĪ max_round_yield)%Z â
<<<
ws_hub_hybrid_inv t Îđ sz â
ws_hub_hybrid_owner t i_ Nonblocked empty
| ââ vs,
ws_hub_hybrid_model t vs
>>>
ws_hub_hybridŲ steal t #i #max_round_noyield #max_round_yield @ âÎđ
<<<
ââ o,
match o with
| None â
ws_hub_hybrid_model t vs
| Some v â
â vs',
âvs = {[+v+]} â vs'â â
ws_hub_hybrid_model t vs'
end
| RET o;
ws_hub_hybrid_owner t i_ (if o then Nonblocked else Blocked) empty
>>>.
Lemma ws_hub_hybridŲ closeðspec t Îđ sz :
{{{
ws_hub_hybrid_inv t Îđ sz
}}}
ws_hub_hybridŲ close t
{{{
RET ();
True
}}}.
End ws_hub_hybrid_G.
#[global] Opaque ws_hub_hybrid_inv.
#[global] Opaque ws_hub_hybrid_model.
#[global] Opaque ws_hub_hybrid_owner.
Section ws_hub_hybrid_G.
Context `{ws_hub_hybrid_G : WsHubHybridG ÎĢ}.
Implicit Types P P_notification P_pred Q Q_pred : iProp ÎĢ.
Lemma ws_hub_hybridŲ pop_steal_untilðspec P_notification P_pred Q_pred t Îđ sz i i_ empty max_round_noyield max_round_yield notification pred :
i = âši_ â
(0 âĪ max_round_noyield)%Z â
(0 âĪ max_round_yield)%Z â
<<<
ws_hub_hybrid_inv t Îđ sz â
ws_hub_hybrid_owner t i_ Nonblocked empty â
P_notification â
( â notify,
P_notification -â
WP notify () {{ itype_unit }} -â
WP notification notify {{ res,
âres = ()%Vâ â
P_notification
}}
) â
P_pred â
⥠(
P_pred -â
WP pred () {{ res,
â b,
âres = #bâ â
if b then Q_pred else P_pred
}}
)
| ââ vs,
ws_hub_hybrid_model t vs
>>>
ws_hub_hybridŲ pop_steal_until t #i #max_round_noyield #max_round_yield notification pred @ âÎđ
<<<
ââ o,
match o with
| None â
ws_hub_hybrid_model t vs
| Some v â
â vs',
âvs = {[+v+]} â vs'â â
ws_hub_hybrid_model t vs'
end
| empty,
RET o;
ws_hub_hybrid_owner t i_ Nonblocked empty â
P_notification â
if o then P_pred else Q_pred
>>>.
Lemma ws_hub_hybridŲ pop_stealðspec t Îđ sz i i_ empty max_round_noyield max_round_yield :
i = âši_ â
(0 âĪ max_round_noyield)%Z â
(0 âĪ max_round_yield)%Z â
<<<
ws_hub_hybrid_inv t Îđ sz â
ws_hub_hybrid_owner t i_ Nonblocked empty
| ââ vs,
ws_hub_hybrid_model t vs
>>>
ws_hub_hybridŲ pop_steal t #i #max_round_noyield #max_round_yield @ âÎđ
<<<
ââ o,
match o with
| None â
ws_hub_hybrid_model t vs
| Some v â
â vs',
âvs = {[+v+]} â vs'â â
ws_hub_hybrid_model t vs'
end
| empty,
RET o;
ws_hub_hybrid_owner t i_ (if o then Nonblocked else Blocked) empty â
if o then
True
else
âempty = Emptyâ
>>>.
End ws_hub_hybrid_G.
From zoo_parabs Require
ws_hub_hybrid__opaque.
prelude.
From zoo.common Require Import
countable
gmultiset
list.
From zoo.iris.bi Require Import
big_op.
From zoo.iris.base_logic Require Import
lib.ghost_list.
From zoo.language Require Import
notations.
From zoo.diaframe Require Import
diaframe.
From zoo_std Require Import
int
option
optional
array
random_round
domain.
From zoo_saturn Require Import
mpmc_queue_1.
From zoo_parabs Require Export
base
ws_hub_hybrid__code.
From zoo_parabs Require Import
ws_hub_hybrid__types
ws_bdeques_public
waiters.
From zoo Require Import
options.
Implicit Types b yield closed : bool.
Implicit Types num_active : Z.
Implicit Types ðĄ : location.
Implicit Types v t notification notify pred : val.
Implicit Types vs : gmultiset val.
Implicit Types ws us vs_queue : list val.
Implicit Types vss : list $ list val.
Implicit Types status : status.
Implicit Types empty : emptiness.
Class WsHubHybridG ÎĢ `{zoo_G : !ZooG ÎĢ} :=
{ #[local] ws_hub_hybrid_G_deques_G :: WsBdequesPublicG ÎĢ
; #[local] ws_hub_hybrid_G_queue_G :: MpmcQueue1G ÎĢ
; #[local] ws_hub_hybrid_G_waiters_G :: WaitersG ÎĢ
; #[local] ws_hub_hybrid_G_emptiness_G :: GhostListG ÎĢ emptiness
}.
Definition ws_hub_hybrid_ÎĢ :=
#[ws_bdeques_public_ÎĢ
; mpmc_queue_1_ÎĢ
; waiters_ÎĢ
; ghost_list_ÎĢ emptiness
].
#[global] Instance subG_ws_hub_hybrid_ÎĢ ÎĢ `{zoo_G : !ZooG ÎĢ} :
subG ws_hub_hybrid_ÎĢ ÎĢ â
WsHubHybridG ÎĢ.
Section consistent.
#[local] Definition consistent vs vss vs_queue :=
vs =
â+ (list_to_set_disj <$> vss) â
list_to_set_disj vs_queue.
#[local] Lemma consistent_alloc sz :
consistent â (replicate sz []) [].
#[local] Lemma consistent_empty vs vss vs_queue :
consistent vs vss vs_queue â
vs = â â
( â i us,
vss !! i = Some us â
us = []
) â§
vs_queue = [].
#[local] Lemma consistent_deque_push {vs vss vs_queue i us} v :
vss !! i = Some us â
consistent vs vss vs_queue â
consistent ({[+v+]} â vs) (<[i := us ++ [v]]> vss) vs_queue.
#[local] Lemma consistent_deque_remove {vs vss vs_queue i us} us1 v us2 :
vss !! i = Some us â
us = us1 ++ v :: us2 â
consistent vs vss vs_queue â
â vs',
vs = {[+v+]} â vs' â§
consistent vs' (<[i := us1 ++ us2]> vss) vs_queue.
#[local] Lemma consistent_deque_pop vs vss vs_queue i us v :
vss !! i = Some (us ++ [v]) â
consistent vs vss vs_queue â
â vs',
vs = {[+v+]} â vs' â§
consistent vs' (<[i := us]> vss) vs_queue.
#[local] Lemma consistent_deque_steal vs vss vs_queue i v us :
vss !! i = Some (v :: us) â
consistent vs vss vs_queue â
â vs',
vs = {[+v+]} â vs' â§
consistent vs' (<[i := us]> vss) vs_queue.
#[local] Lemma consistent_queue_push {vs vss vs_queue} v :
consistent vs vss vs_queue â
consistent ({[+v+]} â vs) vss (vs_queue ++ [v]).
#[local] Lemma consistent_queue_pop vs vss v vs_queue :
consistent vs vss (v :: vs_queue) â
â vs',
vs = {[+v+]} â vs' â§
consistent vs' vss vs_queue.
End consistent.
Opaque consistent.
Section ws_hub_hybrid_G.
Context `{ws_hub_hybrid_G : WsHubHybridG ÎĢ}.
Implicit Types P P_notification P_pred Q Q_pred : iProp ÎĢ.
Record metadata :=
{ metadata_size : nat
; metadata_deques : val
; metadata_rounds : val
; metadata_queue : val
; metadata_waiters : val
; metadata_emptiness : gname
}.
Implicit Types Îģ : metadata.
#[local] Instance metadata_eq_dec :
EqDecision metadata.
#[local] Instance metadata_countable :
Countable metadata.
#[local] Definition emptiness_auth' Îģ_emptiness sz vs_queue : iProp ÎĢ :=
â emptys,
ghost_list_auth Îģ_emptiness emptys â
âlength emptys = szâ â
â vs_queue = []
âĻ â i,
emptys !! i = Some Nonempty
â.
#[local] Definition emptiness_auth Îģ :=
emptiness_auth' Îģ.(metadata_emptiness) Îģ.(metadata_size).
#[local] Instance : CustomIpat "emptiness_auth" :=
" ( %emptys & Hauth & %Hemptys & %Hemptiness ) ".
#[local] Definition emptiness_at' Îģ_emptiness i :=
ghost_list_at Îģ_emptiness i (DfracOwn 1).
#[local] Definition emptiness_at Îģ :=
emptiness_at' Îģ.(metadata_emptiness).
#[local] Definition inv_inner ðĄ : iProp ÎĢ :=
â num_active,
ðĄ.[num_active] âĶ #num_active.
#[local] Instance : CustomIpat "inv_inner" :=
" ( %num_active & HðĄ_num_active ) ".
Definition ws_hub_hybrid_inv t Îđ sz : iProp ÎĢ :=
â ðĄ Îģ,
ât = #ðĄâ â
ðĄ ⊠Îģ â
âsz = Îģ.(metadata_size)â â
ðĄ.[deques] âĶ⥠Îģ.(metadata_deques) â
ðĄ.[rounds] âĶ⥠Îģ.(metadata_rounds) â
ðĄ.[queue] âĶ⥠Îģ.(metadata_queue) â
ðĄ.[waiters] âĶ⥠Îģ.(metadata_waiters) â
ws_bdeques_public_inv Îģ.(metadata_deques) Îđ Îģ.(metadata_size) â
array_inv Îģ.(metadata_rounds) Îģ.(metadata_size) â
mpmc_queue_1_inv Îģ.(metadata_queue) Îđ â
waiters_inv Îģ.(metadata_waiters) sz â
inv nroot (inv_inner ðĄ).
#[local] Instance : CustomIpat "inv" :=
" ( %ðĄ{} & %Îģ{} & {%Heq{};->} & #Hmeta{} & -> & #HðĄ{}_deques & #HðĄ{}_queue & #HðĄ{}_rounds & #HðĄ{}_waiters & #Hdeques{}_inv & #Hrounds{}_inv & #Hqueue{}_inv & #Hwaiters{}_inv & #Hinv{} ) ".
Definition ws_hub_hybrid_model t vs : iProp ÎĢ :=
â ðĄ Îģ vss vs_queue,
ât = #ðĄâ â
ðĄ ⊠Îģ â
ws_bdeques_public_model Îģ.(metadata_deques) vss â
mpmc_queue_1_model Îģ.(metadata_queue) vs_queue â
âconsistent vs vss vs_queueâ â
emptiness_auth Îģ vs_queue.
#[local] Instance : CustomIpat "model" :=
" ( %ðĄ_ & %Îģ_ & %vss & %vs_queue & %Heq & Hmeta_ & Hdeques_model & Hqueue_model & %Hconsistent & Hemptiness_auth ) ".
Definition ws_hub_hybrid_owner t i status empty : iProp ÎĢ :=
â ðĄ Îģ ws round n,
ât = #ðĄâ â
ðĄ ⊠Îģ â
ws_bdeques_public_owner Îģ.(metadata_deques) i status ws â
âempty = Empty â ws = []â â
array_slice Îģ.(metadata_rounds) i DfracDiscarded [round] â
random_round_model' round (Îģ.(metadata_size) - 1) n â
emptiness_at Îģ i empty.
#[local] Instance : CustomIpat "owner" :=
" ( %ðĄ{;_} & %Îģ{;_} & %ws{} & %round{} & %n{} & %Heq{} & Hmeta{;_} & Hdeques_owner{} & %Hempty{} & #Hrounds{} & Hround{} & Hemptiness_at{_{}} ) ".
#[global] Instance ws_hub_hybrid_model_timeless t vs :
Timeless (ws_hub_hybrid_model t vs).
#[global] Instance ws_hub_hybrid_inv_persistent t Îđ sz :
Persistent (ws_hub_hybrid_inv t Îđ sz).
#[local] Lemma emptiness_alloc sz :
âĒ |==>
â Îģ_emptiness,
emptiness_auth' Îģ_emptiness sz [] â
[â list] i â seq 0 sz,
emptiness_at' Îģ_emptiness i Empty.
#[local] Lemma emptiness_at_valid Îģ vs_queue i empty :
emptiness_auth Îģ vs_queue -â
emptiness_at Îģ i empty -â
âi < Îģ.(metadata_size)â.
#[local] Lemma emptiness_empty Îģ vs_queue :
emptiness_auth Îģ vs_queue -â
( [â list] i â seq 0 Îģ.(metadata_size),
emptiness_at Îģ i Empty
) -â
âvs_queue = []â.
#[local] Lemma emptiness_update_auth Îģ v vs_queue :
emptiness_auth Îģ (v :: vs_queue) âĒ
emptiness_auth Îģ vs_queue.
#[local] Lemma emptiness_update_Nonempty {Îģ vs_queue i empty} vs_queue' :
emptiness_auth Îģ vs_queue -â
emptiness_at Îģ i empty ==â
emptiness_auth Îģ vs_queue' â
emptiness_at Îģ i Nonempty.
#[local] Lemma emptiness_update_Empty Îģ i empty :
emptiness_auth Îģ [] -â
emptiness_at Îģ i empty ==â
emptiness_auth Îģ [] â
emptiness_at Îģ i Empty.
Opaque emptiness_auth'.
Lemma ws_hub_hybrid_inv_agree t Îđ sz1 sz2 :
ws_hub_hybrid_inv t Îđ sz1 -â
ws_hub_hybrid_inv t Îđ sz2 -â
âsz1 = sz2â.
Lemma ws_hub_hybrid_owner_exclusive t i status1 empty1 status2 empty2 :
ws_hub_hybrid_owner t i status1 empty1 -â
ws_hub_hybrid_owner t i status2 empty2 -â
False.
Lemma ws_hub_hybrid_inv_owner t Îđ sz i status empty :
ws_hub_hybrid_inv t Îđ sz -â
ws_hub_hybrid_owner t i status empty -â
âi < szâ.
Lemma ws_hub_hybrid_model_empty t Îđ sz vs :
ws_hub_hybrid_inv t Îđ sz -â
ws_hub_hybrid_model t vs -â
( [â list] i â seq 0 sz,
â status,
ws_hub_hybrid_owner t i status Empty
) -â
âvs = â â.
Lemma ws_hub_hybridŲ createðspec Îđ sz :
(0 âĪ sz)%Z â
{{{
True
}}}
ws_hub_hybridŲ create #sz
{{{
t
, RET t;
ws_hub_hybrid_inv t Îđ âsz â
ws_hub_hybrid_model t â â
[â list] i â seq 0 âsz,
ws_hub_hybrid_owner t i Nonblocked Empty
}}}.
Lemma ws_hub_hybridŲ sizeðspec t Îđ sz :
{{{
ws_hub_hybrid_inv t Îđ sz
}}}
ws_hub_hybridŲ size t
{{{
RET #sz;
True
}}}.
#[local] Lemma ws_hub_hybridŲ begin_inactiveðspec t Îđ sz :
{{{
ws_hub_hybrid_inv t Îđ sz
}}}
ws_hub_hybridŲ begin_inactive t
{{{
RET ();
True
}}}.
#[local] Lemma ws_hub_hybridŲ end_inactiveðspec t Îđ sz :
{{{
ws_hub_hybrid_inv t Îđ sz
}}}
ws_hub_hybridŲ end_inactive t
{{{
RET ();
True
}}}.
#[local] Lemma ws_hub_hybridŲ block_activeðspec t Îđ sz i i_ empty :
i = âši_ â
{{{
ws_hub_hybrid_inv t Îđ sz â
ws_hub_hybrid_owner t i_ Nonblocked empty
}}}
ws_hub_hybridŲ block_active t #i
{{{
RET ();
ws_hub_hybrid_owner t i_ Blocked empty
}}}.
#[local] Lemma ws_hub_hybridŲ unblock_activeðspec t Îđ sz i i_ empty :
i = âši_ â
{{{
ws_hub_hybrid_inv t Îđ sz â
ws_hub_hybrid_owner t i_ Blocked empty
}}}
ws_hub_hybridŲ unblock_active t #i
{{{
RET ();
ws_hub_hybrid_owner t i_ Nonblocked empty
}}}.
Lemma ws_hub_hybridŲ blockðspec t Îđ sz i i_ empty :
i = âši_ â
{{{
ws_hub_hybrid_inv t Îđ sz â
ws_hub_hybrid_owner t i_ Nonblocked empty
}}}
ws_hub_hybridŲ block t #i
{{{
RET ();
ws_hub_hybrid_owner t i_ Blocked empty
}}}.
Lemma ws_hub_hybridŲ unblockðspec t Îđ sz i i_ empty :
i = âši_ â
{{{
ws_hub_hybrid_inv t Îđ sz â
ws_hub_hybrid_owner t i_ Blocked empty
}}}
ws_hub_hybridŲ unblock t #i
{{{
RET ();
ws_hub_hybrid_owner t i_ Nonblocked empty
}}}.
Lemma ws_hub_hybridŲ closedðspec t Îđ sz :
{{{
ws_hub_hybrid_inv t Îđ sz
}}}
ws_hub_hybridŲ closed t
{{{
closed
, RET #closed;
True
}}}.
#[local] Lemma ws_hub_hybridŲ notifyðspec t Îđ sz :
{{{
ws_hub_hybrid_inv t Îđ sz
}}}
ws_hub_hybridŲ notify t
{{{
RET ();
True
}}}.
#[local] Lemma ws_hub_hybridŲ notify_allðspec t Îđ sz :
{{{
ws_hub_hybrid_inv t Îđ sz
}}}
ws_hub_hybridŲ notify_all t
{{{
RET ();
True
}}}.
Lemma ws_hub_hybridŲ pushðspec t Îđ sz i i_ empty v :
i = âši_ â
<<<
ws_hub_hybrid_inv t Îđ sz â
ws_hub_hybrid_owner t i_ Nonblocked empty
| ââ vs,
ws_hub_hybrid_model t vs
>>>
ws_hub_hybridŲ push t #i v @ âÎđ
<<<
ws_hub_hybrid_model t ({[+v+]} â vs)
| RET ();
ws_hub_hybrid_owner t i_ Nonblocked Nonempty
>>>.
Lemma ws_hub_hybridŲ popðspec t Îđ sz i i_ empty :
i = âši_ â
<<<
ws_hub_hybrid_inv t Îđ sz â
ws_hub_hybrid_owner t i_ Nonblocked empty
| ââ vs,
ws_hub_hybrid_model t vs
>>>
ws_hub_hybridŲ pop t #i @ âÎđ
<<<
ââ o,
match o with
| None â
ws_hub_hybrid_model t vs
| Some v â
â vs',
âvs = {[+v+]} â vs'â â
ws_hub_hybrid_model t vs'
end
| RET o;
ws_hub_hybrid_owner t i_ Nonblocked (if o then empty else Empty)
>>>.
#[local] Lemma ws_hub_hybridŲ try_steal_onceðspec t Îđ sz i i_ empty :
i = âši_ â
<<<
ws_hub_hybrid_inv t Îđ sz â
ws_hub_hybrid_owner t i_ Blocked empty
| ââ vs,
ws_hub_hybrid_model t vs
>>>
ws_hub_hybridŲ try_steal_once t #i @ âÎđ
<<<
ââ o,
match o with
| None â
ws_hub_hybrid_model t vs
| Some v â
â vs',
âvs = {[+v+]} â vs'â â
ws_hub_hybrid_model t vs'
end
| RET o;
ws_hub_hybrid_owner t i_ Blocked empty
>>>.
#[local] Lemma ws_hub_hybrid_try_stealâðspec P Q t Îđ sz i i_ empty yield max_round pred :
i = âši_ â
(0 âĪ max_round)%Z â
<<<
ws_hub_hybrid_inv t Îđ sz â
ws_hub_hybrid_owner t i_ Blocked empty â
P â
⥠(
P -â
WP pred () {{ res,
â b,
âres = #bâ â
if b then Q else P
}}
)
| ââ vs,
ws_hub_hybrid_model t vs
>>>
ws_hub_hybridŲ try_stealâ t #i #yield #max_round pred @ âÎđ
<<<
ââ o,
match o with
| Nothing
| Anything â
ws_hub_hybrid_model t vs
| Something v â
â vs',
âvs = {[+v+]} â vs'â â
ws_hub_hybrid_model t vs'
end
| RET o;
ws_hub_hybrid_owner t i_ Blocked empty â
if o is Anything then Q else P
>>>.
#[local] Lemma ws_hub_hybridŲ try_stealðspec P Q t Îđ sz i i_ empty max_round_noyield max_round_yield pred :
i = âši_ â
(0 âĪ max_round_noyield)%Z â
(0 âĪ max_round_yield)%Z â
<<<
ws_hub_hybrid_inv t Îđ sz â
ws_hub_hybrid_owner t i_ Blocked empty â
P â
⥠(
P -â
WP pred () {{ res,
â b,
âres = #bâ â
if b then Q else P
}}
)
| ââ vs,
ws_hub_hybrid_model t vs
>>>
ws_hub_hybridŲ try_steal t #i #max_round_noyield #max_round_yield pred @ âÎđ
<<<
ââ o,
match o with
| Nothing
| Anything â
ws_hub_hybrid_model t vs
| Something v â
â vs',
âvs = {[+v+]} â vs'â â
ws_hub_hybrid_model t vs'
end
| RET o;
ws_hub_hybrid_owner t i_ Blocked empty â
if o is Anything then Q else P
>>>.
#[local] Lemma ws_hub_hybridŲ steal_auxðspec P_notification P_pred Q_pred t Îđ sz i i_ empty max_round_noyield max_round_yield notification pred :
i = âši_ â
(0 âĪ max_round_noyield)%Z â
(0 âĪ max_round_yield)%Z â
<<<
ws_hub_hybrid_inv t Îđ sz â
ws_hub_hybrid_owner t i_ Blocked empty â
P_notification â
( â notify,
P_notification -â
WP notify () {{ itype_unit }} -â
WP notification notify {{ res,
âres = ()%Vâ â
P_notification
}}
) â
P_pred â
⥠(
P_pred -â
WP pred () {{ res,
â b,
âres = #bâ â
if b then Q_pred else P_pred
}}
)
| ââ vs,
ws_hub_hybrid_model t vs
>>>
ws_hub_hybridŲ steal_aux t #i #max_round_noyield #max_round_yield notification pred @ âÎđ
<<<
ââ o,
match o with
| None â
ws_hub_hybrid_model t vs
| Some v â
â vs',
âvs = {[+v+]} â vs'â â
ws_hub_hybrid_model t vs'
end
| RET o;
ws_hub_hybrid_owner t i_ (if o then Nonblocked else Blocked) empty â
P_notification â
if o then P_pred else Q_pred
>>>.
Lemma ws_hub_hybridŲ steal_untilðspec P_notification P_pred Q_pred t Îđ sz i i_ empty max_round_noyield max_round_yield notification pred :
i = âši_ â
(0 âĪ max_round_noyield)%Z â
(0 âĪ max_round_yield)%Z â
<<<
ws_hub_hybrid_inv t Îđ sz â
ws_hub_hybrid_owner t i_ Nonblocked empty â
P_notification â
( â notify,
P_notification -â
WP notify () {{ itype_unit }} -â
WP notification notify {{ res,
âres = ()%Vâ â
P_notification
}}
) â
P_pred â
⥠(
P_pred -â
WP pred () {{ res,
â b,
âres = #bâ â
if b then Q_pred else P_pred
}}
)
| ââ vs,
ws_hub_hybrid_model t vs
>>>
ws_hub_hybridŲ steal_until t #i #max_round_noyield #max_round_yield notification pred @ âÎđ
<<<
ââ o,
match o with
| None â
ws_hub_hybrid_model t vs
| Some v â
â vs',
âvs = {[+v+]} â vs'â â
ws_hub_hybrid_model t vs'
end
| RET o;
ws_hub_hybrid_owner t i_ Nonblocked empty â
P_notification â
if o then P_pred else Q_pred
>>>.
Lemma ws_hub_hybridŲ stealðspec t Îđ sz i i_ empty max_round_noyield max_round_yield :
i = âši_ â
(0 âĪ max_round_noyield)%Z â
(0 âĪ max_round_yield)%Z â
<<<
ws_hub_hybrid_inv t Îđ sz â
ws_hub_hybrid_owner t i_ Nonblocked empty
| ââ vs,
ws_hub_hybrid_model t vs
>>>
ws_hub_hybridŲ steal t #i #max_round_noyield #max_round_yield @ âÎđ
<<<
ââ o,
match o with
| None â
ws_hub_hybrid_model t vs
| Some v â
â vs',
âvs = {[+v+]} â vs'â â
ws_hub_hybrid_model t vs'
end
| RET o;
ws_hub_hybrid_owner t i_ (if o then Nonblocked else Blocked) empty
>>>.
Lemma ws_hub_hybridŲ closeðspec t Îđ sz :
{{{
ws_hub_hybrid_inv t Îđ sz
}}}
ws_hub_hybridŲ close t
{{{
RET ();
True
}}}.
End ws_hub_hybrid_G.
#[global] Opaque ws_hub_hybrid_inv.
#[global] Opaque ws_hub_hybrid_model.
#[global] Opaque ws_hub_hybrid_owner.
Section ws_hub_hybrid_G.
Context `{ws_hub_hybrid_G : WsHubHybridG ÎĢ}.
Implicit Types P P_notification P_pred Q Q_pred : iProp ÎĢ.
Lemma ws_hub_hybridŲ pop_steal_untilðspec P_notification P_pred Q_pred t Îđ sz i i_ empty max_round_noyield max_round_yield notification pred :
i = âši_ â
(0 âĪ max_round_noyield)%Z â
(0 âĪ max_round_yield)%Z â
<<<
ws_hub_hybrid_inv t Îđ sz â
ws_hub_hybrid_owner t i_ Nonblocked empty â
P_notification â
( â notify,
P_notification -â
WP notify () {{ itype_unit }} -â
WP notification notify {{ res,
âres = ()%Vâ â
P_notification
}}
) â
P_pred â
⥠(
P_pred -â
WP pred () {{ res,
â b,
âres = #bâ â
if b then Q_pred else P_pred
}}
)
| ââ vs,
ws_hub_hybrid_model t vs
>>>
ws_hub_hybridŲ pop_steal_until t #i #max_round_noyield #max_round_yield notification pred @ âÎđ
<<<
ââ o,
match o with
| None â
ws_hub_hybrid_model t vs
| Some v â
â vs',
âvs = {[+v+]} â vs'â â
ws_hub_hybrid_model t vs'
end
| empty,
RET o;
ws_hub_hybrid_owner t i_ Nonblocked empty â
P_notification â
if o then P_pred else Q_pred
>>>.
Lemma ws_hub_hybridŲ pop_stealðspec t Îđ sz i i_ empty max_round_noyield max_round_yield :
i = âši_ â
(0 âĪ max_round_noyield)%Z â
(0 âĪ max_round_yield)%Z â
<<<
ws_hub_hybrid_inv t Îđ sz â
ws_hub_hybrid_owner t i_ Nonblocked empty
| ââ vs,
ws_hub_hybrid_model t vs
>>>
ws_hub_hybridŲ pop_steal t #i #max_round_noyield #max_round_yield @ âÎđ
<<<
ââ o,
match o with
| None â
ws_hub_hybrid_model t vs
| Some v â
â vs',
âvs = {[+v+]} â vs'â â
ws_hub_hybrid_model t vs'
end
| empty,
RET o;
ws_hub_hybrid_owner t i_ (if o then Nonblocked else Blocked) empty â
if o then
True
else
âempty = Emptyâ
>>>.
End ws_hub_hybrid_G.
From zoo_parabs Require
ws_hub_hybrid__opaque.