-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPhaser.v
67 lines (49 loc) · 1.64 KB
/
Phaser.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
Require Import Coq.Structures.OrderedType.
Require Import Coq.FSets.FMapAVL.
Require Import Coq.Structures.OrderedTypeEx.
Require Import Vars.
Import Map_TID.
(** * Phaser *)
(** A phaser is a map from tasks to phase numbers. *)
Definition phaser := t nat.
(** Operation advance increments the phase of the given task. *)
Definition advance (ph:phaser) (t:tid) : phaser :=
match find t ph with
| Some n => Map_TID.add t (n + 1) ph
| None => ph
end.
(** Operation await holds when every member of the phaser has
reached at least phase [n]. *)
Definition await (ph:phaser) (n:nat) : Prop :=
forall t n',
MapsTo t n' ph ->
n' <= n.
(** Operation [register] assigns a new member in the phaser. *)
Definition register (ph:phaser) (t:tid) (n:nat) : phaser :=
add t n ph.
(** Operation [deregister] revokes the membership of a task in the phaser. *)
Definition deregister (ph:phaser) (t:tid) : phaser :=
remove t ph.
(** Operation [phase] looks up the phase associated with a given task,
or yields [None] if the given task is unregistered. *)
Definition phase (ph:phaser) (t:tid) : option nat :=
find t ph.
(** Creates a phaser with a given task initialized at phase [0]. *)
Definition make (t:tid) : phaser :=
add t 0 (empty nat).
(** Defines the API of a phaser. *)
Inductive op :=
| ADV : op
| REG : tid -> op
| DEREG : op.
(** Applies a phaser operation according to the tag. *)
Definition eval (ph:phaser) (t:tid) (o:op) : phaser :=
match o with
| ADV => advance ph t
| REG t' =>
match phase ph t with
| Some n => register ph t' n
| None => ph
end
| DEREG => deregister ph t
end.