Library zoo_parabs.waiter__code

Require Import zoo.prelude.
Require Import zoo.language.typeclasses.
Require Import zoo.language.notations.
Require Import zoo_std.condition.
Require Import zoo_std.mutex.
Require Import zoo.options.

Notation "'waiterู mutex'" := (
  in_type "zoo_parabs.waiter.t" 0
)(in custom zoo_field
).
Notation "'waiterู condition'" := (
  in_type "zoo_parabs.waiter.t" 1
)(in custom zoo_field
).
Notation "'waiterู flag'" := (
  in_type "zoo_parabs.waiter.t" 2
)(in custom zoo_field
).

Definition waiterู create : val :=
  ๐—ณ๐˜‚๐—ป โŽฝ โ†’
    { mutexู create (), conditionู create (), false }.

Definition waiterู notify : val :=
  ๐—ณ๐˜‚๐—ป "t" โ†’
    mutexู lock "t".{waiterู mutex} โฎ
    ๐—ถ๐—ณ "t".{waiterู flag} ๐˜๐—ต๐—ฒ๐—ป (
      mutexู unlock "t".{waiterู mutex} โฎ
      false
    ) ๐—ฒ๐—น๐˜€๐—ฒ (
      "t" <-{waiterู flag} true โฎ
      mutexู unlock "t".{waiterู mutex} โฎ
      conditionู notify "t".{waiterู condition} โฎ
      true
    ).

Definition waiterู prepare_wait : val :=
  ๐—ณ๐˜‚๐—ป "t" โ†’
    mutexู protect "t".{waiterู mutex}
      (๐—ณ๐˜‚๐—ป โŽฝ โ†’ "t" <-{waiterู flag} false).

Definition waiterู cancel_wait : val :=
  ๐—ณ๐˜‚๐—ป "t" โ†’
    mutexู protect "t".{waiterู mutex}
      (๐—ณ๐˜‚๐—ป โŽฝ โ†’
         ๐—ถ๐—ณ "t".{waiterู flag} ๐˜๐—ต๐—ฒ๐—ป (
           false
         ) ๐—ฒ๐—น๐˜€๐—ฒ (
           "t" <-{waiterู flag} true โฎ
           true
         )).

Definition waiterู commit_wait : val :=
  ๐—ณ๐˜‚๐—ป "t" โ†’
    mutexู protect "t".{waiterู mutex}
      (๐—ณ๐˜‚๐—ป โŽฝ โ†’
         conditionู wait_until
           "t".{waiterู condition}
           "t".{waiterู mutex}
           (๐—ณ๐˜‚๐—ป โŽฝ โ†’ "t".{waiterู flag})).