Library zoo_std.semaphore__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 "'semaphoreู mutex'" := (
in_type "zoo_std.semaphore.t" 0
)(in custom zoo_field
).
Notation "'semaphoreู condition'" := (
in_type "zoo_std.semaphore.t" 1
)(in custom zoo_field
).
Notation "'semaphoreู count'" := (
in_type "zoo_std.semaphore.t" 2
)(in custom zoo_field
).
Definition semaphoreู create : val :=
๐ณ๐๐ป "cap" โ
{ mutexู create (), conditionู create (), "cap" - 1 }.
Definition semaphoreู try_lock : val :=
๐ณ๐๐ป "t" โ
mutexู protect "t".{semaphoreู mutex}
(๐ณ๐๐ป โฝ โ
๐น๐ฒ๐ "cnt" = "t".{semaphoreู count} ๐ถ๐ป
๐ถ๐ณ 0 < "cnt" ๐๐ต๐ฒ๐ป (
"t" <-{semaphoreู count} "cnt" - 1 โฎ
true
) ๐ฒ๐น๐๐ฒ (
false
)).
Definition semaphoreู lock : val :=
๐ณ๐๐ป "t" โ
mutexู protect "t".{semaphoreู mutex}
(๐ณ๐๐ป โฝ โ
conditionู wait_until
"t".{semaphoreู condition}
"t".{semaphoreู mutex}
(๐ณ๐๐ป โฝ โ 0 < "t".{semaphoreู count}) โฎ
"t" <-{semaphoreู count} "t".{semaphoreู count} - 1).
Definition semaphoreู unlock : val :=
๐ณ๐๐ป "t" โ
mutexู protect
"t".{semaphoreู mutex}
(๐ณ๐๐ป โฝ โ
"t" <-{semaphoreู count} "t".{semaphoreู count} + 1) โฎ
conditionู notify "t".{semaphoreู condition}.
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 "'semaphoreู mutex'" := (
in_type "zoo_std.semaphore.t" 0
)(in custom zoo_field
).
Notation "'semaphoreู condition'" := (
in_type "zoo_std.semaphore.t" 1
)(in custom zoo_field
).
Notation "'semaphoreู count'" := (
in_type "zoo_std.semaphore.t" 2
)(in custom zoo_field
).
Definition semaphoreู create : val :=
๐ณ๐๐ป "cap" โ
{ mutexู create (), conditionู create (), "cap" - 1 }.
Definition semaphoreู try_lock : val :=
๐ณ๐๐ป "t" โ
mutexู protect "t".{semaphoreู mutex}
(๐ณ๐๐ป โฝ โ
๐น๐ฒ๐ "cnt" = "t".{semaphoreู count} ๐ถ๐ป
๐ถ๐ณ 0 < "cnt" ๐๐ต๐ฒ๐ป (
"t" <-{semaphoreู count} "cnt" - 1 โฎ
true
) ๐ฒ๐น๐๐ฒ (
false
)).
Definition semaphoreู lock : val :=
๐ณ๐๐ป "t" โ
mutexู protect "t".{semaphoreู mutex}
(๐ณ๐๐ป โฝ โ
conditionู wait_until
"t".{semaphoreู condition}
"t".{semaphoreู mutex}
(๐ณ๐๐ป โฝ โ 0 < "t".{semaphoreู count}) โฎ
"t" <-{semaphoreู count} "t".{semaphoreู count} - 1).
Definition semaphoreู unlock : val :=
๐ณ๐๐ป "t" โ
mutexู protect
"t".{semaphoreู mutex}
(๐ณ๐๐ป โฝ โ
"t" <-{semaphoreู count} "t".{semaphoreู count} + 1) โฎ
conditionู notify "t".{semaphoreู condition}.