val acquire_core (#p:vprop) (#u:inames) (r:ref bool) (i:inv (lockinv p r))
  : SteelAtomicT bool u
    (lockinv p r `star` emp)
    (fun b -> lockinv p r  `star` (if b then p else emp))
let acquire_core #p #u r i =
  let ghost = witness_exists () in

  let res = cas_pt_bool r ghost available locked in

  (* Not sure we can avoid calling an SMT here. Better force the manual call? *)
  rewrite_slprop (if (Ghost.reveal ghost) then emp else p) (if res then p else emp)
    (fun _ -> ());
  rewrite_slprop (if res then pts_to r full_perm (Ghost.hide locked) else pts_to r full_perm ghost) (pts_to r full_perm locked) (fun _ -> ());

  intro_lockinv_locked p r;
  return res
val acquire_core (#p:vprop) (#u:inames) (r:ref bool) (i:inv (lockinv p r))
  : SteelAtomicT bool u
    (lockinv p r `star` emp)
    (fun b -> lockinv p r  `star` (if b then p else emp))
let acquire_core #p #u r i =
let ghost = witness_exists () in
let res = cas_pt_bool r ghost available locked in
rewrite_slprop (if (Ghost.reveal ghost) then emp else p) (if res then p else emp) (fun _ -> ());
rewrite_slprop (if res then pts_to r full_perm (Ghost.hide locked) else pts_to r full_perm ghost)
  (pts_to r full_perm locked)
  (fun _ -> ());
intro_lockinv_locked p r;
return res
Steel.SpinLock.fst.checked
Steel.Reference.fsti.checked Steel.FractionalPermission.fst.checked Steel.Effect.Atomic.fsti.checked Steel.Effect.fsti.checked prims.fst.checked FStar.Pervasives.fsti.checked FStar.Ghost.fsti.checked
Steel.SpinLock.fst
Steel.Effect.Common.vprop Steel.Memory.inames Steel.Reference.ref Prims.bool Steel.Effect.Common.inv Steel.SpinLock.lockinv Steel.Effect.Atomic.return Steel.Effect.Common.VStar Steel.Effect.Common.emp Prims.unit Steel.SpinLock.intro_lockinv_locked Steel.Effect.Atomic.rewrite_slprop Steel.Reference.pts_to Steel.FractionalPermission.full_perm Steel.SpinLock.locked FStar.Ghost.reveal Steel.Memory.mem Prims.l_iff Prims.b2t Prims.eq2 Steel.SpinLock.available Steel.Reference.cas_pt_bool FStar.Ghost.erased Steel.Effect.Atomic.witness_exists Steel.Effect.Common.star
(*
   Copyright 2020 Microsoft Research

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*)

module Steel.SpinLock

open FStar.Ghost
open Steel.Effect.Atomic
open Steel.Effect
open Steel.Reference
open Steel.FractionalPermission

#set-options "--ide_id_info_off --fuel 0 --ifuel 0"

let available = false
let locked = true

let lockinv (p:vprop) (r:ref bool) : vprop =
  h_exists (fun b -> pts_to r full_perm b `star` (if b then emp else p))

noeq
type lock (p:vprop) = | Lock: r: ref bool -> i: inv (lockinv p r) -> lock p

val intro_lockinv_available (#uses:inames) (p:vprop) (r:ref bool)
  : SteelGhostT unit uses (pts_to r full_perm available `star` p) (fun _ -> lockinv p r)

val intro_lockinv_locked (#uses:inames) (p:vprop) (r:ref bool)
  : SteelGhostT unit uses (pts_to r full_perm locked) (fun _ -> lockinv p r)

let intro_lockinv_available #uses p r =
  intro_exists false
    (fun (b: bool) ->
      pts_to r full_perm b `star`
        (if b then emp else p)
    )

let intro_lockinv_locked #uses p r =
  intro_exists true
    (fun b -> pts_to r full_perm b `star`
          (if b then emp else p))

let new_lock (p:vprop)
  : SteelT (lock p) p (fun _ -> emp) =
  let r = alloc_pt available in
  intro_lockinv_available p r;
  let i:inv (lockinv p r) = new_invariant (lockinv p r) in
  return (Lock r i)

val acquire_core (#p:vprop) (#u:inames) (r:ref bool) (i:inv (lockinv p r))
  : SteelAtomicT bool u
    (lockinv p r `star` emp)
    (fun b -> lockinv p r  `star` (if b then p else emp))
val acquire_core (#p:vprop) (#u:inames) (r:ref bool) (i:inv (lockinv p r))
  : SteelAtomicT bool u
    (lockinv p r `star` emp)
    (fun b -> lockinv p r  `star` (if b then p else emp))
r: Steel.Reference.ref Prims.bool -> i: Steel.Effect.Common.inv (Steel.SpinLock.lockinv p r)
  -> Steel.Effect.Atomic.SteelAtomicT Prims.bool