Source file bounded_spsc_queue.ml
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
open! Base
open Await_kernel
open Portable_kernel
type 'a inner =
{
mutable rd_index : int [@atomic]
; mutable wr_index_cache : int
; rd_contents : 'a portended or_null array
; rd_capacity : int
; mutable wr_trigger : Trigger.Source.t [@atomic]
; _pad06 : int
; _pad07 : int
;
mutable wr_index : int [@atomic]
; wr_capacity : int
; mutable rd_index_cache : int
; wr_contents : 'a portended or_null array
; mutable rd_trigger : Trigger.Source.t [@atomic]
; _pad13 : int
; _pad14 : int
; _pad15 : int
}
type ('a : value mod non_float, 'phantom) t = { inner : 'a inner @@ aliased contended }
[@@unboxed]
type ('a : value mod non_float, 'phantom) queue = ('a, 'phantom) t
module Producer = struct
type 'a t = ('a, [ `Producer ]) queue
end
module Consumer = struct
type 'a t = ('a, [ `Consumer ]) queue
external magic_of_producer
: 'a Producer.t Capsule.Owned.t @ local
-> 'a t Capsule.Owned.t @ unique
@@ portable
= "%identity"
end
let create ~(capacity : int) =
if capacity <= 0
then invalid_arg "[Bounded_spsc_queue.create]: capacity must be strictly positive";
let producer =
Capsule.Owned.create (fun () ->
let contents = Array.create ~len:(Int.ceil_pow2 capacity) Null in
{ inner =
{ rd_index = 0
; wr_index_cache = 0
; wr_trigger = Trigger.(source (create ()))
; rd_contents = contents
; rd_capacity = capacity
; _pad06 = 0
; _pad07 = 0
; wr_index = 0
; rd_index_cache = 0
; rd_trigger = Trigger.(source (create ()))
; wr_contents = contents
; wr_capacity = capacity
; _pad13 = 0
; _pad14 = 0
; _pad15 = 0
}
})
in
let consumer = Consumer.magic_of_producer (borrow_ producer) in
#(producer, consumer)
;;
let%test_unit "['a t] is 15 words (16 including the header)" =
let #(producer, _consumer) = create ~capacity:1 in
let producer = Capsule.Owned.unwrap producer in
[%test_eq: int] 15 (Obj.size (Obj.repr producer))
;;
let wr_mask t = Array.length t.inner.wr_contents - 1
let rd_mask t = Array.length t.inner.rd_contents - 1
type 'a pushed_or_full =
| Pushed
| Full of 'a
[@@or_null]
(** Try to push a value to the tail (producer end) of the queue. Returns [Pushed] if the
value was pushed, or [Full value] if the queue was full, containing the value that was
passed *)
let try_push t a =
let current_wr_index = Atomic.Loc.get [%atomic.loc t.inner.wr_index] in
if current_wr_index
= (Obj.magic_uncontended t.inner).rd_index_cache + t.inner.wr_capacity
&&
let head = Atomic.Loc.get [%atomic.loc t.inner.rd_index] in
(Obj.magic_uncontended t.inner).rd_index_cache <- head;
current_wr_index = head + t.inner.wr_capacity
then Full a
else (
let idx = current_wr_index land wr_mask t in
let arr = (Obj.magic_uncontended t.inner).wr_contents in
[%debug assert (idx < Array.length arr)];
Array.unsafe_set arr idx (This { portended = a });
Atomic.Loc.set [%atomic.loc t.inner.wr_index] (current_wr_index + 1);
Pushed)
;;
type ('a, 'r) res =
| Terminated : ('a, 'a) res
| Or_canceled : ('a, 'a Or_canceled.t) res
let[@inline] push_success (type r) t (res : (unit, r) res) : r =
Trigger.Source.signal (Atomic.Loc.get [%atomic.loc t.inner.rd_trigger]);
match res with
| Or_canceled -> Completed ()
| Terminated -> ()
;;
let[@inline never] rec push_slow_path
: type (a : value mod non_float) r.
Await.t @ local
-> Cancellation.t @ local
-> (a, _) t @ local
-> a @ contended once portable unique
-> (unit, r) res
-> r
=
fun w cancellation t a res ->
let a =
(Obj.magic_many [@mode portable contended unique]) a
in
let trigger = Trigger.create () in
Atomic.Loc.set [%atomic.loc t.inner.wr_trigger] (Trigger.source trigger);
match try_push t a with
| Pushed ->
push_success t res [@nontail]
| Full a ->
Await.await_until_terminated_or_canceled w cancellation trigger;
if Await.is_terminated w
then raise Await.Terminated
else (
match res with
| Or_canceled ->
if Cancellation.Expert.is_canceled_ignore_termination cancellation
then Canceled
else
push_slow_path w cancellation t a res
| Terminated ->
push_slow_path w cancellation t a res)
;;
let[@inline] push_fast_path w cancellation t a res =
let a =
(Obj.magic_many [@mode portable contended unique]) a
in
match try_push t a with
| Pushed -> push_success t res [@nontail]
| Full a -> push_slow_path w cancellation t a res
;;
let push w t a = push_fast_path w Cancellation.never t a Terminated
let push_or_cancel w c t a = push_fast_path w c t a Or_canceled
(** Try to pop a value off the head (consumer end) of the queue. Returns [This value] if a
value was popped, or [Null] if the queue was empty *)
let try_pop t =
let current_rd_index = Atomic.Loc.get [%atomic.loc t.inner.rd_index] in
if (Obj.magic_uncontended t.inner).wr_index_cache = current_rd_index
&&
let tail = Atomic.Loc.get [%atomic.loc t.inner.wr_index] in
(Obj.magic_uncontended t.inner).wr_index_cache <- tail;
tail = current_rd_index
then Null
else (
let index = current_rd_index land rd_mask t in
let value = (Obj.magic_uncontended t.inner.rd_contents).(index) in
(Obj.magic_uncontended t.inner.rd_contents).(index) <- Null;
Atomic.Loc.set [%atomic.loc t.inner.rd_index] (current_rd_index + 1);
value)
;;
let[@inline] pop_success (type a r) t (value : a @ contended portable) (res : (a, r) res)
: r @ contended once portable unique
=
Trigger.Source.signal (Atomic.Loc.get [%atomic.loc t.inner.wr_trigger]);
let value =
(Obj.magic_unique [@mode contended portable]) value
in
match res with
| Or_canceled -> Completed value
| Terminated -> value
;;
let[@inline never] rec pop_slow_path
: type (a : value mod non_float) r.
_ @ local
-> _ @ local
-> (a, _) t @ local
-> (a, r) res
-> r @ contended once portable unique
=
fun w cancellation t res ->
let trigger = Trigger.create () in
Atomic.Loc.set [%atomic.loc t.inner.rd_trigger] (Trigger.source trigger);
match try_pop t with
| This { portended = value } ->
pop_success t value res [@nontail]
| Null ->
Await.await_until_terminated_or_canceled w cancellation trigger;
if Await.is_terminated w
then raise Await.Terminated
else (
match res with
| Or_canceled ->
if Cancellation.Expert.is_canceled_ignore_termination cancellation
then Canceled
else
pop_slow_path w cancellation t res
| Terminated ->
pop_slow_path w cancellation t res)
;;
let[@inline] pop_fast_path w cancellation t res =
match try_pop t with
| This { portended = value } -> pop_success t value res [@nontail]
| Null -> pop_slow_path w cancellation t res
;;
let pop w t = pop_fast_path w Cancellation.never t Terminated
let pop_or_cancel w c t = pop_fast_path w c t Or_canceled
let capacity t = t.inner.rd_capacity
let equal a b = phys_equal a.inner b.inner