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
open Base
open Await
module Atomic = Portable.Atomic
module Scheduler = Parallel_kernel.For_scheduler
module Mpmc : sig @@ portable
type t : value mod contended portable
val create : unit -> t
val push : t -> (unit -> unit) @ once portable -> unit
val pop : t -> (unit -> unit) or_null @ once portable
end = struct
type t = (unit -> unit) aliased Portable_mpmc_queue.t
let create () = Portable_mpmc_queue.create ~padded:true ()
let[@inline] push t f = Portable_mpmc_queue.push t { aliased = f }
let[@inline] pop t =
match Portable_mpmc_queue.pop t with
| This { aliased } -> This aliased
| Null -> Null
;;
end
module Wsdeq : sig @@ portable
type t : value mod portable
val create : unit -> t
val push : t -> (unit -> unit) @ once portable -> unit
val pop : t -> (unit -> unit) or_null @ once portable
val steal : t @ contended -> (unit -> unit) or_null @ once portable
end = struct
type t = (unit -> unit) aliased Portable_ws_deque.t
let create = Portable_ws_deque.create
let[@inline] push t f =
Scheduler.without_heartbeat (fun [@inline] () ->
Portable_ws_deque.push t { aliased = f })
[@nontail]
;;
let[@inline] pop t =
match
Scheduler.without_heartbeat (fun [@inline] () ->
{ portended = Portable_ws_deque.pop t })
with
| { portended = This { aliased } } -> This aliased
| { portended = Null } -> Null
;;
let[@inline] steal t =
match Portable_ws_deque.steal t with
| This { aliased } -> This aliased
| Null -> Null
;;
end
type worker =
| P :
{ queue : Wsdeq.t @@ contended
; sleepy : bool Await.Awaitable.t
; mutex : 'k Sync.Mutex.t
}
-> worker
type t =
{ global_queue : Mpmc.t
; workers : worker Iarray.t
; sleepers : int Atomic.t
}
let create_worker () =
let queue = Wsdeq.create () in
let sleepy = Await.Awaitable.make ~padded:true false in
let (P key) = Capsule.Prim.create () in
let mutex = Sync.Mutex.create key in
P { queue; sleepy; mutex }
;;
let create ~workers =
let global_queue = Mpmc.create () in
let workers = Iarray.init workers ~f:(fun _ -> create_worker ()) in
let sleepers = Atomic.make ~padded:true 0 in
{ global_queue; workers; sleepers }
;;
let length t = Iarray.length t.workers
let[@inline] wake { workers; _ } ~idx =
let (P { mutex; sleepy; _ }) = Iarray.get workers idx in
Sync.Mutex.with_access Sync.spinning mutex ~f:(fun _ _ : bool ->
let wake = Await.Awaitable.get sleepy in
if wake && Await.Awaitable.exchange sleepy false then Await.Awaitable.signal sleepy;
wake)
;;
let[@inline] wake_one t =
let len = Iarray.length t.workers in
let start = Random.int len in
let rec aux i =
if i < len
then (
let idx = start + i in
let idx = Bool.select (idx >= len) (idx - len) idx in
if not (wake t ~idx) then aux (i + 1))
in
aux 0
;;
let[@inline] try_wake { workers; sleepers; _ } ~n =
let s = Atomic.get sleepers in
if s > 0
then (
let n = Int.min n s in
let len = Iarray.length workers in
let start = Random.int len in
let rec find i ~n =
if i < len && n > 0
then (
let j = start + i in
let j = Bool.select (j >= len) (j - len) j in
let (P { mutex; sleepy; _ }) = Iarray.unsafe_get workers j in
if Await.Awaitable.get sleepy
then (
if Await.Awaitable.exchange sleepy false
then
Sync.Mutex.with_access Sync.spinning mutex ~f:(fun _ _ ->
Await.Awaitable.signal sleepy);
find (i + 1) ~n:(n - 1))
else find (i + 1) ~n)
in
find 0 ~n [@nontail])
;;
let steal workers ~idx =
let n = Iarray.length workers in
let start = Random.int n in
let rec aux i =
if i < n
then (
let j = start + i in
let j = Bool.select (j >= n) (j - n) j in
if j = idx
then aux (i + 1)
else (
let (P { queue; _ }) = Iarray.unsafe_get workers j in
match Wsdeq.steal queue with
| This _ as task -> task
| Null -> aux (i + 1)))
else Null
in
aux 0 [@nontail]
;;
let inject t f =
Mpmc.push t.global_queue f;
wake_one t
;;
let push { workers; _ } f =
let idx = Multicore.current_domain () in
let (P { queue = self; _ }) =
assert%debug (idx < Iarray.length workers);
Iarray.unsafe_get workers idx
in
Wsdeq.push (Obj.magic_uncontended self) f
;;
let work { global_queue; workers; sleepers } ~await ~cancellation =
let idx = Multicore.current_domain () in
let (P { queue = self; sleepy; mutex }) = Iarray.get workers idx in
let sync = Obj.magic_unyielding (Await.sync await) in
let rec go spin cw key =
let task =
match
Wsdeq.pop
(Obj.magic_uncontended self)
with
| This task -> This task
| Null ->
(match Mpmc.pop global_queue with
| This task -> This task
| Null ->
(match steal workers ~idx with
| This task -> This task
| Null -> Null))
in
match task with
| This task ->
let #(_, key) =
Sync.Mutex.Condition.Wait.release_temporarily cw key ~f:(fun _ ->
task ();
Sync.yield sync)
in
go spin cw key
| Null ->
Atomic.incr sleepers;
Await.Awaitable.set sleepy true;
let #(outcome, key) =
Sync.Mutex.Condition.Wait.release_temporarily cw key ~f:(fun await ->
let await = (Await.Expert.with_sync [@alloc stack]) await sync in
Await.Awaitable.await_or_cancel
await
cancellation
sleepy
~until_phys_unequal_to:true [@nontail])
in
Atomic.decr sleepers;
(match outcome with
| Canceled -> #((), key)
| Signaled -> go spin cw key
| Terminated ->
(match (raise Await.Terminated : Nothing.t) with
| _ -> .))
in
let await = (Await.Expert.with_sync [@alloc stack]) await Sync.spinning in
Sync.Mutex.with_key_and_condition_wait_poisoning await mutex ~f:go [@nontail]
;;