-
Notifications
You must be signed in to change notification settings - Fork 0
/
code_ebr.v
139 lines (124 loc) · 4.25 KB
/
code_ebr.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
From smr.lang Require Import notation.
From iris.prelude Require Import options.
From smr Require Import code_slot_bag_onat code_retired_list.
(** RCUDomain *)
Notation domSize := 3 (only parsing).
Notation domLBag := 0 (only parsing).
Notation domRSet := 1 (only parsing).
Notation domGEpoch := 2 (only parsing).
(** Guard *)
Notation guardSize := 2%nat (only parsing).
Notation guardSlot := 0 (only parsing).
Notation guardDom := 1 (only parsing).
(** RCUDomain *)
Definition ebr_domain_new : val := λ: <>,
let: "dom" := AllocN #domSize #0 in
"dom" +ₗ #domLBag <- slot_bag_new #();;
"dom" +ₗ #domRSet <- retired_list_new #();;
"dom" +ₗ #domGEpoch <- #3%nat;;
"dom".
Definition may_advance_loop : val :=
rec: "loop" "slot" "g_epoch" :=
if: "slot" = #NULL then
#true
else
let: "continue" := (λ: <>,
let: "next" := !("slot" +ₗ #slotNext) in
"loop" "next" "g_epoch"
) in
let: "active" := !("slot" +ₗ #slotActive) in
if: "active" then
let: "value" := !("slot" +ₗ #slotValue) in
if: #0%nat ≤ "value" then (* activated ("pinned") epoch *)
if: "value" ≠ "g_epoch" then
#false
else
"continue" #()
else
"continue" #()
else
"continue" #().
(* If there exists a slot with active epoch not equal to [g_epoch], return [false]. *)
Definition may_advance : val :=
λ: "LBag" "g_epoch",
let: "head" := !("LBag" +ₗ #slotBagHead) in
may_advance_loop "head" "g_epoch".
Definition try_advance : val :=
λ: "dom",
let: "g_epoch" := !("dom" +ₗ #domGEpoch) in
(* SC fence *)
let: "LBag" := !("dom" +ₗ #domLBag) in
if: may_advance "LBag" "g_epoch" then
let: "new_epoch" := "g_epoch" + #1%nat in
let: "res" := CmpXchg ("dom" +ₗ #domGEpoch) "g_epoch" "new_epoch" in
if: Snd "res" then
"new_epoch"
else
Fst "res"
else
"g_epoch".
Definition ebr_domain_do_reclamation_loop : val :=
rec: "loop" "rSet" "rNode" "g_epoch" :=
if: "rNode" = #NULL then
#()
else
let: "ptr" := retired_node_ptr "rNode" in
let: "epoch" := retired_node_epoch "rNode" in
let: "next" := retired_node_next "rNode" in
( if: "g_epoch" < "epoch" + #3%nat then
retired_list_push "rSet" "rNode"
else
let: "size" := retired_node_size "rNode" in
Free "size" "ptr";;
retired_node_drop "rNode");;
"loop" "rSet" "next" "g_epoch".
Definition ebr_domain_do_reclamation : val :=
λ: "dom",
let: "g_epoch" := try_advance "dom" in
let: "rSet" := !("dom" +ₗ #domRSet) in
let: "rNode" := retired_list_pop_all "rSet" in
ebr_domain_do_reclamation_loop "rSet" "rNode" "g_epoch".
(** guard *)
Definition ebr_guard_new : val :=
λ: "dom",
let: "bag" := !("dom" +ₗ #domLBag) in
let: "slot" := slot_bag_acquire_slot "bag" in
let: "guard" := AllocN #guardSize #0 in
"guard" +ₗ #guardSlot <- "slot";;
"guard" +ₗ #guardDom <- "dom";;
"guard".
Definition activate_loop : val :=
rec: "loop" "slot" "dom" "epoch" :=
slot_set "slot" "epoch";;
(* SC fence before validating *)
let: "val_epoch" := !("dom" +ₗ #domGEpoch) in
if: "val_epoch" = "epoch" then
#()
else
"loop" "slot" "dom" "val_epoch".
Definition ebr_guard_activate : val :=
λ: "guard",
let: "slot" := !("guard" +ₗ #guardSlot) in
let: "dom" := !("guard" +ₗ #guardDom) in
let: "epoch" := !("dom" +ₗ #domGEpoch) in
activate_loop "slot" "dom" "epoch".
Definition ebr_guard_deactivate : val :=
λ: "guard",
let: "slot" := !("guard" +ₗ #guardSlot) in
"slot" +ₗ #slotValue <- #(-1).
Definition ebr_guard_drop : val :=
λ: "guard",
let: "slot" := !("guard" +ₗ #guardSlot) in
"slot" +ₗ #slotValue <- #(-1);;
slot_drop "slot";;
Free #guardSize "guard".
Definition ebr_domain_retire : val :=
λ: "dom" "ptr" "size",
let: "guard" := ebr_guard_new "dom" in
ebr_guard_activate "guard";;
let: "rSet" := !("dom" +ₗ #domRSet) in
let: "slot" := !("guard" +ₗ #guardSlot) in
let: "epoch" := !("slot" +ₗ #slotValue) in
let: "rNode" := retired_node_new "ptr" "size" "epoch" in
retired_list_push "rSet" "rNode";;
ebr_guard_drop "guard".