val acquire (#v:vprop) (c:cancellable_lock v)
  : STT bool emp (fun b -> maybe_acquired b c)
let acquire #v c =
  acquire c.llock;
  let b_erased = elim_exists () in
  let b = read c.lref in
  if b
  then begin
    rewrite (if b_erased then v else emp) v;
    rewrite (v `star` can_release c)
            (maybe_acquired b c)
  end
  else begin
    intro_exists (G.reveal b_erased) (lock_inv_pred c.lref v);
    release c.llock;
    rewrite emp (maybe_acquired b c)
  end;
  return b
val acquire (#v:vprop) (c:cancellable_lock v)
  : STT bool emp (fun b -> maybe_acquired b c)
let acquire #v c =
acquire c.llock;
let b_erased = elim_exists () in
let b = read c.lref in
if b
then
  (rewrite (if b_erased then v else emp) v;
    rewrite (v `star` (can_release c)) (maybe_acquired b c))
else
  (intro_exists (G.reveal b_erased) (lock_inv_pred c.lref v);
    release c.llock;
    rewrite emp (maybe_acquired b c));
return b
Steel.ST.CancellableSpinLock.fst.checked
Steel.ST.Util.fsti.checked Steel.ST.SpinLock.fsti.checked Steel.ST.Reference.fsti.checked Steel.ST.Effect.fsti.checked Steel.FractionalPermission.fst.checked prims.fst.checked FStar.Pervasives.fsti.checked FStar.Ghost.fsti.checked
Steel.ST.CancellableSpinLock.fst
Steel.Effect.Common.vprop Steel.ST.CancellableSpinLock.cancellable_lock Steel.ST.Util.return Prims.bool FStar.Ghost.hide FStar.Set.set Steel.Memory.iname FStar.Set.empty Steel.ST.CancellableSpinLock.maybe_acquired Prims.unit Steel.ST.Util.rewrite Steel.Effect.Common.star Steel.ST.CancellableSpinLock.can_release FStar.Ghost.reveal Steel.Effect.Common.emp Steel.ST.SpinLock.release Steel.ST.CancellableSpinLock.lock_inv Steel.ST.CancellableSpinLock.__proj__Mkcancellable_lock__item__lref Steel.ST.CancellableSpinLock.__proj__Mkcancellable_lock__item__llock Steel.ST.Util.intro_exists Steel.ST.CancellableSpinLock.lock_inv_pred Steel.ST.Reference.read Steel.FractionalPermission.full_perm FStar.Ghost.erased Steel.ST.Util.elim_exists Steel.Effect.Common.VStar Steel.ST.Reference.pts_to Steel.ST.SpinLock.acquire
(*
   Copyright 2021 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.

   Author: Aseem Rastogi
*)

module Steel.ST.CancellableSpinLock

open Steel.ST.Effect
open Steel.ST.Util
open Steel.FractionalPermission
open Steel.ST.Reference
open Steel.ST.SpinLock

module G = FStar.Ghost

[@@__reduce__]
let lock_inv_pred (r:ref bool) (v:vprop) : bool -> vprop =
  fun b -> pts_to r full_perm b `star` (if b then v else emp)  

[@@__reduce__]
let lock_inv (r:ref bool) (v:vprop) : vprop = exists_ (lock_inv_pred r v)

noeq
type cancellable_lock (v:vprop) = {
  lref   : ref bool;
  llock  : lock (lock_inv lref v)
}

let new_cancellable_lock v =
  let r = alloc true in
  intro_exists true (lock_inv_pred r v);
  let l = new_lock (lock_inv r v) in
  return ({lref = r; llock = l})

[@__reduce__]
let can_release #v c = pts_to c.lref full_perm true
val acquire (#v:vprop) (c:cancellable_lock v)
  : STT bool emp (fun b -> maybe_acquired b c)
c: Steel.ST.CancellableSpinLock.cancellable_lock v -> Steel.ST.Effect.STT Prims.bool