-
Notifications
You must be signed in to change notification settings - Fork 0
/
code_hazptr.v
100 lines (86 loc) · 2.96 KB
/
code_hazptr.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
From smr.lang Require Import notation.
From iris.prelude Require Import options.
From smr Require Import code_slot_bag_oloc code_retired_list.
(** Implements a minimized version of Folly Hazptr.h.
Folly's optimizations:
- Efficient node reuse
- Folly collects the unreclaimable ptrs and pushes them with single CAS
- many more ..
*)
(** HazardDomain *)
Notation domSize := 2%nat (only parsing).
Notation domHBag := 0 (only parsing).
Notation domRSet := 1 (only parsing).
(** Shield *)
Notation shieldSize := 1%nat (only parsing).
Notation shieldSlot := 0 (only parsing).
(** HazardDomain *)
Definition hazard_domain_new : val := λ: <>,
let: "dom" := AllocN #domSize #0 in
"dom" +ₗ #domHBag <- slot_bag_new #();;
"dom" +ₗ #domRSet <- retired_list_new #();;
"dom".
Definition hazard_domain_do_reclamation_loop : val :=
rec: "loop" "rSet" "rNode" "hazards" :=
if: "rNode" = #NULL then
#()
else
let: "ptr" := retired_node_ptr "rNode" in
let: "next" := retired_node_next "rNode" in
( if: seq_bag_contains "hazards" "ptr" then
retired_list_push "rSet" "rNode"
else
let: "size" := retired_node_size "rNode" in
Free "size" "ptr";;
retired_node_drop "rNode");;
"loop" "rSet" "next" "hazards".
Definition hazard_domain_do_reclamation : val :=
λ: "dom",
let: "rSet" := !("dom" +ₗ #domRSet) in
let: "rNode" := retired_list_pop_all "rSet" in
(* SC fence *)
let: "hBag" := !("dom" +ₗ #domHBag) in
let: "hazards" := slot_bag_snapshot "hBag" in
hazard_domain_do_reclamation_loop "rSet" "rNode" "hazards".
Definition hazard_domain_retire : val :=
λ: "dom" "ptr" "size",
let: "rSet" := !("dom" +ₗ #domRSet) in
(* We give [#()] for epoch of retired node in hazard pointers *)
let: "rNode" := retired_node_new "ptr" "size" #0%nat in
retired_list_push "rSet" "rNode".
(** Shield *)
Definition shield_new : val :=
λ: "dom",
let: "bag" := !("dom" +ₗ #domHBag) in
let: "slot" := slot_bag_acquire_slot "bag" in
let: "shield" := AllocN #shieldSize #0 in
"shield" +ₗ #shieldSlot <- "slot";;
"shield".
Definition shield_set : val :=
λ: "shield" "ptr",
let: "slot" := !("shield" +ₗ #shieldSlot) in
slot_set "slot" "ptr".
(* SC fence before validating *)
Definition shield_protect_loop : val :=
rec: "loop" "shield" "atomic" "ptr" :=
shield_set "shield" "ptr";;
(* SC fence *)
let: "ptr'" := !"atomic" in
if: "ptr" = "ptr'" then
"ptr'"
else
"loop" "shield" "atomic" "ptr'".
Definition shield_protect : val :=
λ: "shield" "atomic",
let: "ptr" := !"atomic" in
shield_protect_loop "shield" "atomic" "ptr".
Definition shield_unset : val :=
λ: "shield",
let: "slot" := !("shield" +ₗ #shieldSlot) in
slot_unset "slot".
Definition shield_drop : val :=
λ: "shield",
let: "slot" := !("shield" +ₗ #shieldSlot) in
slot_unset "slot";;
slot_drop "slot";;
Free #shieldSize "shield".