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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
module type S = sig
type 'a t
val create : unit -> 'a t
val push : 'a t -> 'a -> unit
val pop : 'a t -> 'a
val pop_opt : 'a t -> 'a option
val steal : 'a t -> 'a
val steal_opt : 'a t -> 'a option
end
module CArray = struct
type 'a t = 'a array
let rec log2 n = if n <= 1 then 0 else 1 + log2 (n asr 1)
let create sz v =
assert (0 < sz && sz = Int.shift_left 1 (log2 sz));
assert (Int.logand sz (sz - 1) == 0);
Array.make sz v
let size t = Array.length t [@@inline]
let mask t = size t - 1 [@@inline]
let index i t =
Int.logand i (mask t)
[@@inline]
let get t i = Array.unsafe_get t (index i t) [@@inline]
let put t i v = Array.unsafe_set t (index i t) v [@@inline]
let transfer src dst top num =
ArrayExtra.blit_circularly
src
(index top src)
dst
(index top dst)
num
[@@inline]
let grow t top bottom =
let sz = size t in
assert (bottom - top = sz);
let dst = create (2 * sz) (Obj.magic ()) in
transfer t dst top sz;
dst
let shrink t top bottom =
let sz = size t in
assert (bottom - top <= sz / 2);
let dst = create (sz / 2) (Obj.magic ()) in
transfer t dst top (bottom - top);
dst
end
module M : S = struct
let min_size = 32
let shrink_const = 3
type 'a t = {
top : int Atomic.t;
bottom : int Atomic.t;
tab : 'a ref CArray.t Atomic.t;
mutable next_shrink : int;
}
let create () =
{
top = Atomic.make 1;
bottom = Atomic.make 1;
tab = Atomic.make (CArray.create min_size (Obj.magic ()));
next_shrink = 0;
}
let set_next_shrink q =
let sz = CArray.size (Atomic.get q.tab) in
if sz <= min_size then q.next_shrink <- 0
else q.next_shrink <- sz / shrink_const
let grow q t b =
Atomic.set q.tab (CArray.grow (Atomic.get q.tab) t b);
set_next_shrink q
let size q =
let b = Atomic.get q.bottom in
let t = Atomic.get q.top in
b - t
let push q v =
let v' = ref v in
let b = Atomic.get q.bottom in
let t = Atomic.get q.top in
let a = Atomic.get q.tab in
let size = b - t in
let a =
if size = CArray.size a then (
grow q t b;
Atomic.get q.tab)
else a
in
CArray.put a b v';
Atomic.set q.bottom (b + 1)
let release ptr =
let res = !ptr in
ptr := Obj.magic ();
res
[@@inline]
let pop q =
if size q = 0 then raise Exit
else
let b = Atomic.get q.bottom - 1 in
Atomic.set q.bottom b;
let t = Atomic.get q.top in
let a = Atomic.get q.tab in
let size = b - t in
if size < 0 then (
Atomic.set q.bottom (b + 1);
raise Exit)
else
let out = CArray.get a b in
if b = t then
if Atomic.compare_and_set q.top t (t + 1) then (
Atomic.set q.bottom (b + 1);
release out)
else (
Atomic.set q.bottom (b + 1);
raise Exit)
else (
if q.next_shrink > size then (
Atomic.set q.tab (CArray.shrink a t b);
set_next_shrink q);
release out)
let pop_opt q = try Some (pop q) with Exit -> None
let rec steal backoff q =
let t = Atomic.get q.top in
let b = Atomic.get q.bottom in
let size = b - t in
if size <= 0 then raise Exit
else
let a = Atomic.get q.tab in
let out = CArray.get a t in
if Atomic.compare_and_set q.top t (t + 1) then release out
else steal (Backoff.once backoff) q
let steal q = steal Backoff.default q
let steal_opt q = try Some (steal q) with Exit -> None
end