(** * Lemmas about the Dry Machine*)
From mathcomp.ssreflect Require Import ssreflect ssrbool ssrnat seq ssrfun eqtype fintype finfun.
Set Implicit Arguments.
(*NOTE: because of redefinition of [val], these imports must appear after Ssreflect eqtype.*)
(* Require Import compcert.common.Events. *)
Global Notation "a # b" := (Maps.PMap.get b a) (at level 1).
(** This file holds various results about the dry machine*)
(* Find other lemmas in dry_machine_step_lemmas.v        *)
Module ThreadPoolWF.
Section ThreadPoolWF.
Context {Sem : Semantics}.
Existing Instance DryHybridMachine.dryResources.
Existing Instance OrdinalPool.OrdinalThreadPool.
(** Take an instance of the Dry Machine *)
Existing Instance DryHybridMachine.DryHybridMachineSig.

(* Specifications: *)
Lemma unlift_m_inv :
forall tp tid (Htid : tid < (OrdinalPool.num_threads tp).+1) ord
(Hunlift: unlift (ordinal_pos_incr (OrdinalPool.num_threads tp))
(Ordinal (n:=(OrdinalPool.num_threads tp).+1)
(m:=tid) Htid)=Some ord),
nat_of_ord ord = tid.
Lemma updThread_inv: forall ds i (cnt: containsThread ds i) c pmap,
invariant ds ->
(forall j (cnt: containsThread ds j),
i<>j -> permMapsDisjoint pmap.1 (getThreadR cnt).1 /\
permMapsDisjoint pmap.2 (getThreadR cnt).2) ->
(forall j (cnt: containsThread ds j),
i<>j ->
permMapCoherence (getThreadR cnt).1 pmap.2)->
(forall j (cnt: containsThread ds j),
i<>j ->
permMapCoherence pmap.1 (getThreadR cnt).2)->
(forall l pmap0, lockRes ds l = Some pmap0 ->
permMapsDisjoint pmap0.1 pmap.1 /\
permMapsDisjoint pmap0.2 pmap.2  ) ->
(forall l pmap0, lockRes ds l = Some pmap0 ->
permMapCoherence pmap0.1 pmap.2 /\
permMapCoherence pmap.1 pmap0.2) ->
(permMapCoherence pmap#1 pmap#2) ->
invariant (updThread cnt c pmap).
Lemma invariant_decr:
forall tp c pmap i (cnti: containsThread tp i)
(Hinv: invariant tp)
(Hdecr1: forall b ofs,
Mem.perm_order'' ((getThreadR cnti).1 # b ofs)
(pmap.1 # b ofs))
(Hdecr2: forall b ofs,
Mem.perm_order'' ((getThreadR cnti).2 # b ofs)
(pmap.2 # b ofs)),
invariant (updThread cnti c pmap).
(** [invariant] is preserved by [updThreadC]*)
Lemma updThreadC_invariant:
forall (tp : t) i c
(ctn : containsThread tp i)
(Hinv : invariant tp),
invariant (updThreadC ctn c).
Lemma updLock_inv:
forall tp b ofs rmap
(Hinv: invariant tp)
(Hdisjoint_res: forall laddr rmap',
laddr <> (b, ofs) ->
lockRes tp laddr = Some rmap' ->
permMapsDisjoint2 rmap rmap')
(Hdisjoint_thr: forall i (cnti: containsThread tp i),
permMapsDisjoint2 (getThreadR cnti) rmap)
(Hcoh_res: forall laddr rmap',
laddr <> (b, ofs) ->
lockRes tp laddr = Some rmap' ->
permMapCoherence rmap.1 rmap'.2 /\
permMapCoherence rmap'.1 rmap.2)
(Hcoh_rmap: permMapCoherence rmap.1 rmap.2)
(Hcoh_thr: forall i (cnti: containsThread tp i),
permMapCoherence rmap.1 (getThreadR cnti).2 /\
permMapCoherence (getThreadR cnti).1 rmap.2)
(Hvalid1: forall ofs0,
(ofs < ofs0 < ofs + lksize.LKSIZE)%Z -> lockRes tp (b, ofs0) = None)
(Hvalid2: forall ofs0 : Z,
(ofs0 < ofs < ofs0 + lksize.LKSIZE)%Z -> lockRes tp (b, ofs0) = None),
invariant (updLockSet tp (b, ofs) rmap).
(** [mem_compatible] is preserved by [addThread]*)
(* NOTE: Strange statement, updThread doesn't need to be here probably*)
Lemma mem_compatible_add:
forall tp i (cnti: containsThread tp i) c pmap vf arg pmap2 m
(Hcomp: mem_compatible
(addThread
(updThread cnti c pmap) vf arg pmap2) m),
mem_compatible (updThread cnti c pmap) m.
(** [mem_compatible] is preserved by [remLockSet] *)
Lemma mem_compatible_remlock:
forall tp m addr
(Hinv: lr_valid (lockRes tp))
(Hcomp: mem_compatible tp m),
mem_compatible (remLockSet tp addr) m.
(** [invariant] is preserved by [remLockSet]*)
Lemma remLock_inv: forall ds a,
invariant ds ->
invariant (remLockSet ds a).
Lemma invariant_add:
forall tp i (cnti: containsThread tp i) c pmap1 pmap2 vf arg
(Hinv: invariant
(addThread
(updThread cnti c pmap1)
vf arg pmap2)),
invariant (updThread cnti c pmap1).
Lemma invariant_not_freeable:
forall tp
(Hinv: invariant tp),
forall b ofs,
(forall i (cnti: containsThread tp i), (getThreadR cnti).2 # b ofs <> Some Freeable) /\
(forall laddr rmap (Hres: lockRes tp laddr = Some rmap), rmap.2 # b ofs <> Some Freeable).
Lemma invariant_freeable_empty_threads:
forall tp i (cnti: containsThread tp i) b ofs
(Hinv: invariant tp)
(Hfreeable: (getThreadR cnti).1 !! b ofs = Some Freeable),
forall j (cntj: containsThread tp j),
(getThreadR cntj).2 !! b ofs = None /\
(i <> j -> (getThreadR cntj).1 !! b ofs = None).
Lemma invariant_freeable_empty_locks:
forall tp i (cnti: containsThread tp i) b ofs
(Hinv: invariant tp)
(Hfreeable: (getThreadR cnti).1 !! b ofs = Some Freeable),
forall laddr rmap,
lockRes tp laddr = Some rmap ->
rmap.1 !! b ofs = None /\
rmap.2 !! b ofs = None.
Lemma mem_compatible_invalid_block:
forall tp m b ofs
(Hcomp: mem_compatible tp m)
(Hinvalid: ~ Mem.valid_block m b),
(forall i (cnti: containsThread tp i),
(getThreadR cnti).1 !! b ofs = None /\
(getThreadR cnti).2 !! b ofs = None) /\
(forall laddr rmap,
lockRes tp laddr = Some rmap ->
rmap.1 !! b ofs = None /\
rmap.2 !! b ofs = None).