-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode_slot_bag_onat.v
72 lines (61 loc) · 1.94 KB
/
code_slot_bag_onat.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
68
69
70
71
72
From smr.lang Require Import tactics notation.
From iris.prelude Require Import options.
(** Slot *)
Notation slotSize := 3%nat (only parsing).
Notation slotNext := 0 (only parsing).
Notation slotActive := 1 (only parsing).
Notation slotValue := 2 (only parsing).
Definition slot_new : val :=
λ: <>,
let: "slot" := AllocN #slotSize #0 in
"slot" +ₗ #slotNext <- #NULL;;
"slot" +ₗ #slotActive <- #true;;
"slot" +ₗ #slotValue <- #(-1);;
"slot".
Definition slot_set : val :=
λ: "slot" "epoch",
"slot" +ₗ #slotValue <- "epoch".
Definition slot_unset : val :=
λ: "slot",
"slot" +ₗ #slotValue <- #(-1).
Definition slot_drop : val :=
λ: "slot",
"slot" +ₗ #slotActive <- #false.
(** SlotBag *)
Notation slotBagSize := 1%nat (only parsing).
Notation slotBagHead := 0 (only parsing).
Definition slot_bag_new : val :=
λ: <>,
let: "slotBag" := AllocN #slotBagSize #0 in
"slotBag" +ₗ #slotBagHead <- #NULL;;
"slotBag".
Definition slot_try_acquire_inactive_slot_loop : val :=
rec: "loop" "slot" :=
if: "slot" = #NULL
then #NULL
else
let: "next" := !("slot" +ₗ #slotNext) in
if: CAS ("slot" +ₗ #slotActive) #false #true
then "slot"
else "loop" "next".
Definition slot_bag_try_acquire_inactive_slot : val :=
λ: "slotBag",
let: "head" := !("slotBag" +ₗ #slotBagHead) in
slot_try_acquire_inactive_slot_loop "head".
Definition slot_bag_push_slot_loop : val :=
rec: "loop" "slotBag" "new" :=
let: "head" := !("slotBag" +ₗ #slotBagHead) in
"new" +ₗ #slotNext <- "head";;
if: CAS ("slotBag" +ₗ #slotBagHead) "head" "new" then
#()
else
"loop" "slotBag" "new".
Definition slot_bag_acquire_slot : val :=
λ: "slotBag",
let: "slot" := slot_bag_try_acquire_inactive_slot "slotBag" in
if: "slot" ≠ #NULL
then "slot"
else
let: "new" := slot_new #() in
slot_bag_push_slot_loop "slotBag" "new";;
"new".