@@ -364,61 +364,63 @@ module Storage_map =
364
364
end )
365
365
type storage = (unit -> unit ) Storage_map .t
366
366
367
+ module Multidomain_sync = struct
368
+
369
+ (* callback_exchange is a domain-indexed map for storing callbacks that
370
+ different domains should execute. This is used when a domain d1 resolves a
371
+ promise on which a different domain d2 has attached callbacks (implicitely
372
+ via bind etc. or explicitly via on_success etc.). When this happens, the
373
+ domain resolving the promise calls its local callbacks and sends the other
374
+ domains' callbacks into the callback exchange *)
375
+ let callback_exchange = Domain_map. create_protected_map ()
376
+
377
+ (* notification_map is a domain-indexed map for waking sleeping domains. each
378
+ (should) domain registers a notification (see Lwt_unix) into the map when it
379
+ starts its scheduler. other domains can wake the domain up to indicate that
380
+ callbacks are available to be called *)
381
+ let notification_map = Domain_map. create_protected_map ()
382
+
383
+ (* send_callback d cb adds the callback cb into the callback_exchange and pings
384
+ the domain d via the notification_map *)
385
+ let send_callback d cb =
386
+ Domain_map. update
387
+ callback_exchange
388
+ d
389
+ (function
390
+ | None ->
391
+ let cbs = Lwt_sequence. create () in
392
+ let _ : (unit -> unit) Lwt_sequence.node = Lwt_sequence. add_l cb cbs in
393
+ Some cbs
394
+ | Some cbs ->
395
+ let _ : (unit -> unit) Lwt_sequence.node = Lwt_sequence. add_l cb cbs in
396
+ Some cbs);
397
+ begin match Domain_map. find notification_map d with
398
+ | None ->
399
+ failwith " ERROR: domain didn't register at startup"
400
+ | Some n ->
401
+ n ()
402
+ end
367
403
368
- (* callback_exchange is a domain-indexed map for storing callbacks that
369
- different domains should execute. This is used when a domain d1 resolves a
370
- promise on which a different domain d2 has attached callbacks (implicitely
371
- via bind etc. or explicitly via on_success etc.). When this happens, the
372
- domain resolving the promise calls its local callbacks and sends the other
373
- domains' callbacks into the callback exchange *)
374
- let callback_exchange = Domain_map. create_protected_map ()
375
-
376
- (* notification_map is a domain-indexed map for waking sleeping domains. each
377
- (should) domain registers a notification (see Lwt_unix) into the map when it
378
- starts its scheduler. other domains can wake the domain up to indicate that
379
- callbacks are available to be called *)
380
- let notification_map = Domain_map. create_protected_map ()
381
-
382
- (* send_callback d cb adds the callback cb into the callback_exchange and pings
383
- the domain d via the notification_map *)
384
- let send_callback d cb =
385
- Domain_map. update
386
- callback_exchange
387
- d
388
- (function
389
- | None ->
390
- let cbs = Lwt_sequence. create () in
391
- let _ : (unit -> unit) Lwt_sequence.node = Lwt_sequence. add_l cb cbs in
392
- Some cbs
393
- | Some cbs ->
394
- let _ : (unit -> unit) Lwt_sequence.node = Lwt_sequence. add_l cb cbs in
395
- Some cbs);
396
- begin match Domain_map. find notification_map d with
397
- | None ->
398
- failwith " ERROR: domain didn't register at startup"
399
- | Some n ->
400
- n ()
401
- end
402
-
403
- (* get_sent_callbacks gets a domain's own callback from the callbasck exchange,
404
- this is so that the notification handler installed by main.run can obtain the
405
- callbacks that have been sent its way *)
406
- let get_sent_callbacks domain_id =
407
- match Domain_map. extract callback_exchange domain_id with
408
- | None -> Lwt_sequence. create ()
409
- | Some cbs -> cbs
410
-
411
- (* register_notification adds a domain's own notification (see Lwt_unix) into
412
- the notification map *)
413
- let register_notification d n =
414
- Domain_map. update notification_map d (function
415
- | None -> Some n
416
- | Some _ -> failwith " already registered!!" )
417
-
418
- let is_alredy_registered d =
419
- match Domain_map. find notification_map d with
420
- | Some _ -> true
421
- | None -> false
404
+ (* get_sent_callbacks gets a domain's own callback from the callbasck exchange,
405
+ this is so that the notification handler installed by main.run can obtain the
406
+ callbacks that have been sent its way *)
407
+ let get_sent_callbacks domain_id =
408
+ match Domain_map. extract callback_exchange domain_id with
409
+ | None -> Lwt_sequence. create ()
410
+ | Some cbs -> cbs
411
+
412
+ (* register_notification adds a domain's own notification (see Lwt_unix) into
413
+ the notification map *)
414
+ let register_notification d n =
415
+ Domain_map. update notification_map d (function
416
+ | None -> Some n
417
+ | Some _ -> failwith " already registered!!" )
418
+
419
+ let is_alredy_registered d =
420
+ match Domain_map. find notification_map d with
421
+ | Some _ -> true
422
+ | None -> false
423
+ end
422
424
423
425
module Main_internal_types =
424
426
struct
@@ -1230,7 +1232,7 @@ struct
1230
1232
Domain.DLS. set current_storage storage;
1231
1233
handle_with_async_exception_hook f ()
1232
1234
end else
1233
- send_callback domain (fun () ->
1235
+ Multidomain_sync. send_callback domain (fun () ->
1234
1236
Domain.DLS. set current_storage storage;
1235
1237
handle_with_async_exception_hook f ()
1236
1238
)
@@ -1240,7 +1242,7 @@ struct
1240
1242
begin if domain = Domain. self () then
1241
1243
Lwt_sequence. remove node
1242
1244
else
1243
- send_callback domain (fun () -> Lwt_sequence. remove node)
1245
+ Multidomain_sync. send_callback domain (fun () -> Lwt_sequence. remove node)
1244
1246
end ;
1245
1247
iter_list rest
1246
1248
| Cancel_callback_list_concat (fs , fs' ) ->
@@ -1265,7 +1267,7 @@ struct
1265
1267
begin if domain = Domain. self () then
1266
1268
f result
1267
1269
else
1268
- send_callback domain (fun () -> f result)
1270
+ Multidomain_sync. send_callback domain (fun () -> f result)
1269
1271
end ;
1270
1272
iter_list rest
1271
1273
| Regular_callback_list_explicitly_removable_callback (_ , {contents = None } ) ->
@@ -1274,7 +1276,7 @@ struct
1274
1276
begin if domain = Domain. self () then
1275
1277
f result
1276
1278
else
1277
- send_callback domain (fun () -> f result)
1279
+ Multidomain_sync. send_callback domain (fun () -> f result)
1278
1280
end ;
1279
1281
iter_list rest
1280
1282
| Regular_callback_list_concat (fs , fs' ) ->
@@ -3308,4 +3310,5 @@ end
3308
3310
module Private = struct
3309
3311
type nonrec storage = storage
3310
3312
module Sequence_associated_storage = Sequence_associated_storage
3313
+ module Multidomain_sync = Multidomain_sync
3311
3314
end
0 commit comments