Source file bus.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
open! Core
module State = struct
type t =
| Closed
| Write_in_progress
| Ok_to_write
[@@deriving sexp_of]
let is_closed = function
| Closed -> true
| Write_in_progress -> false
| Ok_to_write -> false
;;
end
module Callback_arity = struct
type _ t =
| Arity1 : ('a -> unit) t
| Arity1_local : ('a -> unit) t
| Arity2 : ('a -> 'b -> unit) t
| Arity3 : ('a -> 'b -> 'c -> unit) t
| Arity4 : ('a -> 'b -> 'c -> 'd -> unit) t
| Arity5 : ('a -> 'b -> 'c -> 'd -> 'e -> unit) t
[@@deriving sexp_of]
let uses_local_args : type a. a t -> bool = function
| Arity1 -> false
| Arity1_local -> true
| Arity2 -> false
| Arity3 -> false
| Arity4 -> false
| Arity5 -> false
;;
end
module Last_value : sig
type 'callback t
val create_exn : 'callback Callback_arity.t -> 'callback t
val set1 : ('a -> unit) t -> 'a -> unit
val set2 : ('a -> 'b -> unit) t -> 'a -> 'b -> unit
val set3 : ('a -> 'b -> 'c -> unit) t -> 'a -> 'b -> 'c -> unit
val set4 : ('a -> 'b -> 'c -> 'd -> unit) t -> 'a -> 'b -> 'c -> 'd -> unit
val set5 : ('a -> 'b -> 'c -> 'd -> 'e -> unit) t -> 'a -> 'b -> 'c -> 'd -> 'e -> unit
val send : 'callback t -> 'callback -> unit
end = struct
type _ tuple =
| Tuple1 : { mutable arg1 : 'a } -> ('a -> unit) tuple
| Tuple2 :
{ mutable arg1 : 'a
; mutable arg2 : 'b
}
-> ('a -> 'b -> unit) tuple
| Tuple3 :
{ mutable arg1 : 'a
; mutable arg2 : 'b
; mutable arg3 : 'c
}
-> ('a -> 'b -> 'c -> unit) tuple
| Tuple4 :
{ mutable arg1 : 'a
; mutable arg2 : 'b
; mutable arg3 : 'c
; mutable arg4 : 'd
}
-> ('a -> 'b -> 'c -> 'd -> unit) tuple
| Tuple5 :
{ mutable arg1 : 'a
; mutable arg2 : 'b
; mutable arg3 : 'c
; mutable arg4 : 'd
; mutable arg5 : 'e
}
-> ('a -> 'b -> 'c -> 'd -> 'e -> unit) tuple
type 'callback t = 'callback tuple option ref
let create_exn (type callback) (arity : callback Callback_arity.t) : callback t =
if Callback_arity.uses_local_args arity
then
raise_s
[%message
"Cannot save last value when using local args" (arity : _ Callback_arity.t)];
ref None
;;
let set1 t a =
match !t with
| None -> t := Some (Tuple1 { arg1 = a })
| Some (Tuple1 args) -> args.arg1 <- a
;;
let set2 t a b =
match !t with
| None -> t := Some (Tuple2 { arg1 = a; arg2 = b })
| Some (Tuple2 args) ->
args.arg1 <- a;
args.arg2 <- b
;;
let set3 t a b c =
match !t with
| None -> t := Some (Tuple3 { arg1 = a; arg2 = b; arg3 = c })
| Some (Tuple3 args) ->
args.arg1 <- a;
args.arg2 <- b;
args.arg3 <- c
;;
let set4 t a b c d =
match !t with
| None -> t := Some (Tuple4 { arg1 = a; arg2 = b; arg3 = c; arg4 = d })
| Some (Tuple4 args) ->
args.arg1 <- a;
args.arg2 <- b;
args.arg3 <- c;
args.arg4 <- d
;;
let set5 t arg1 arg2 arg3 arg4 arg5 =
match !t with
| None -> t := Some (Tuple5 { arg1; arg2; arg3; arg4; arg5 })
| Some (Tuple5 args) ->
args.arg1 <- arg1;
args.arg2 <- arg2;
args.arg3 <- arg3;
args.arg4 <- arg4;
args.arg5 <- arg5
;;
let send (type callback) (t : callback t) (callback : callback) : unit =
match !t with
| None -> ()
| Some (Tuple1 { arg1 }) -> callback arg1
| Some (Tuple2 { arg1; arg2 }) -> callback arg1 arg2
| Some (Tuple3 { arg1; arg2; arg3 }) -> callback arg1 arg2 arg3
| Some (Tuple4 { arg1; arg2; arg3; arg4 }) -> callback arg1 arg2 arg3 arg4
| Some (Tuple5 { arg1; arg2; arg3; arg4; arg5 }) -> callback arg1 arg2 arg3 arg4 arg5
;;
end
module On_subscription_after_first_write = struct
type t =
| Allow
| Allow_and_send_last_value
| Raise
[@@deriving enumerate, sexp_of]
let allow_subscription_after_first_write = function
| Allow -> true
| Allow_and_send_last_value -> true
| Raise -> false
;;
let save_last_value_exn t callback_arity =
match t with
| Allow_and_send_last_value -> Some (Last_value.create_exn callback_arity)
| Allow -> None
| Raise -> None
;;
end
module Bus_id = Unique_id.Int63 ()
module Subscriber = struct
type 'callback =
{ bus_id : Bus_id.t
; callback : 'callback
; : bool
;
mutable subscribers_index : int
; on_callback_raise : (Error.t -> unit) option
; on_close : (unit -> unit) option
; subscribed_from : Source_code_position.t
}
[@@deriving fields ~iterators:iter]
let is_subscribed t ~to_ = t.subscribers_index >= 0 && Bus_id.equal t.bus_id to_
let sexp_of_t
_
{ callback = _
; bus_id = _
;
; subscribers_index
; on_callback_raise
; on_close = _
; subscribed_from
}
: Sexp.t
=
List
[ Atom "Bus.Subscriber.t"
; [%message
""
~subscribers_index:
(if Ppx_inline_test_lib.am_running then None else Some subscribers_index
: (int option[@sexp.option]))
(on_callback_raise : ((Error.t -> unit) option[@sexp.option]))
~extract_exn:
(if extract_exn then Some true else None : (bool option[@sexp.option]))
(subscribed_from : Source_code_position.t)]
]
;;
let invariant invariant_a t =
Invariant.invariant [%here] t [%sexp_of: _ t] (fun () ->
let check f = Invariant.check_field t f in
Fields.iter
~bus_id:ignore
~callback:(check invariant_a)
~extract_exn:ignore
~subscribers_index:ignore
~on_callback_raise:ignore
~on_close:ignore
~subscribed_from:ignore)
;;
let create
subscribed_from
~callback
~bus_id
~
~subscribers_index
~on_callback_raise
~on_close
=
{ bus_id
; callback
; extract_exn
; subscribers_index
; on_callback_raise
; on_close
; subscribed_from
}
;;
end
type ('callback, 'phantom) t =
{ bus_id : Bus_id.t
; name : Info.t option
; callback_arity : 'callback Callback_arity.t
; created_from : Source_code_position.t
; on_subscription_after_first_write : On_subscription_after_first_write.t
; on_callback_raise : Error.t -> unit
; last_value : 'callback Last_value.t option
; mutable state : State.t
; mutable write_ever_called : bool
; mutable num_subscribers : int
;
mutable subscribers : 'callback Subscriber.t Option_array.t
;
mutable callbacks : 'callback Option_array.t
; mutable unsubscribes_during_write : 'callback Subscriber.t list
}
[@@deriving fields ~getters ~iterators:iter]
let sexp_of_t
_
_
{ bus_id = _
; callback_arity
; callbacks = _
; created_from
; last_value = _
; name
; num_subscribers
; on_subscription_after_first_write
; on_callback_raise = _
; state
; subscribers
; write_ever_called
; unsubscribes_during_write = _
}
=
let subscribers =
Array.init num_subscribers ~f:(fun i -> Option_array.get_some_exn subscribers i)
in
[%message
""
(name : (Info.t option[@sexp.option]))
(callback_arity : _ Callback_arity.t)
(created_from : Source_code_position.t)
(on_subscription_after_first_write : On_subscription_after_first_write.t)
(state : State.t)
(write_ever_called : bool)
(subscribers : _ Subscriber.t Array.t)]
;;
type ('callback, 'phantom) bus = ('callback, 'phantom) t [@@deriving sexp_of]
let read_only t = (t :> (_, read) t)
let invariant invariant_a _ t =
Invariant.invariant [%here] t [%sexp_of: (_, _) t] (fun () ->
let check f = Invariant.check_field t f in
Fields.iter
~bus_id:ignore
~name:ignore
~callbacks:
(check (fun callbacks ->
assert (Option_array.length callbacks = Option_array.length t.subscribers);
for i = 0 to Option_array.length callbacks - 1 do
if i < t.num_subscribers
then invariant_a (Option_array.get_some_exn callbacks i)
else assert (Option_array.is_none callbacks i)
done))
~callback_arity:ignore
~created_from:ignore
~num_subscribers:(check (fun num_subscribers -> assert (num_subscribers >= 0)))
~on_subscription_after_first_write:ignore
~on_callback_raise:ignore
~last_value:ignore
~state:ignore
~write_ever_called:ignore
~subscribers:
(check (fun subscribers ->
for i = 0 to Option_array.length subscribers - 1 do
if i < t.num_subscribers
then (
let subscriber = Option_array.get_some_exn subscribers i in
Subscriber.invariant invariant_a subscriber;
assert (i = subscriber.subscribers_index))
else assert (Option_array.is_none subscribers i)
done))
~unsubscribes_during_write:ignore)
;;
let is_closed t = State.is_closed t.state
module Read_write = struct
type 'callback t = ('callback, read_write) bus [@@deriving sexp_of]
let invariant invariant_a t = invariant invariant_a ignore t
end
module Read_only = struct
type 'callback t = ('callback, read) bus [@@deriving sexp_of]
let invariant invariant_a t = invariant invariant_a ignore t
end
let[@cold] start_write_failing t =
match t.state with
| Closed ->
failwiths ~here:[%here] "[Bus.write] called on closed bus" t [%sexp_of: (_, _) t]
| Write_in_progress ->
failwiths
~here:[%here]
"[Bus.write] called from callback on the same bus"
t
[%sexp_of: (_, _) t]
| Ok_to_write -> assert false
;;
let capacity t = Option_array.length t.subscribers
let maybe_shrink_capacity t =
if t.num_subscribers * 4 <= capacity t
then (
let desired_capacity = t.num_subscribers in
let copy_and_shrink array =
let new_array = Option_array.create ~len:desired_capacity in
Option_array.blit
~src:array
~src_pos:0
~dst:new_array
~dst_pos:0
~len:t.num_subscribers;
new_array
in
t.subscribers <- copy_and_shrink t.subscribers;
t.callbacks <- copy_and_shrink t.callbacks)
;;
let add_subscriber t (subscriber : _ Subscriber.t) ~at_subscribers_index =
subscriber.subscribers_index <- at_subscribers_index;
Option_array.set_some t.subscribers at_subscribers_index subscriber;
Option_array.set_some t.callbacks at_subscribers_index subscriber.callback
;;
let remove_subscriber t (subscriber : _ Subscriber.t) =
let subscribers_index = subscriber.subscribers_index in
subscriber.subscribers_index <- -1;
Option_array.set_none t.subscribers subscribers_index;
Option_array.set_none t.callbacks subscribers_index
;;
let unsubscribe_assuming_valid_subscriber t (subscriber : _ Subscriber.t) =
let subscriber_index = subscriber.subscribers_index in
let last_subscriber_index = t.num_subscribers - 1 in
remove_subscriber t subscriber;
if subscriber_index < last_subscriber_index
then (
let last_subscriber = Option_array.get_some_exn t.subscribers last_subscriber_index in
remove_subscriber t last_subscriber;
add_subscriber t last_subscriber ~at_subscribers_index:subscriber_index);
t.num_subscribers <- t.num_subscribers - 1;
maybe_shrink_capacity t
;;
let unsubscribe t subscriber =
if Subscriber.is_subscribed subscriber ~to_:t.bus_id
then (
match t.state with
| Write_in_progress ->
t.unsubscribes_during_write <- subscriber :: t.unsubscribes_during_write
| Closed ->
()
| Ok_to_write -> unsubscribe_assuming_valid_subscriber t subscriber)
;;
let[@cold] unsubscribe_after_finish_write t =
List.iter t.unsubscribes_during_write ~f:(unsubscribe_assuming_valid_subscriber t);
t.unsubscribes_during_write <- []
;;
let[@cold] unsubscribe_all t =
assert (is_closed t);
for i = 0 to t.num_subscribers - 1 do
let subscriber = Option_array.get_some_exn t.subscribers i in
Option.iter subscriber.on_close ~f:(fun on_close -> on_close ());
remove_subscriber t subscriber
done;
t.num_subscribers <- 0;
maybe_shrink_capacity t
;;
let[@inline always] finish_write t =
if not (List.is_empty t.unsubscribes_during_write) then unsubscribe_after_finish_write t;
match t.state with
| Closed -> unsubscribe_all t
| Ok_to_write -> assert false
| Write_in_progress -> t.state <- Ok_to_write
;;
let[@cold] close t =
match t.state with
| Closed -> ()
| Write_in_progress -> t.state <- Closed
| Ok_to_write ->
t.state <- Closed;
unsubscribe_all t
;;
let call_on_callback_raise t error =
try t.on_callback_raise error with
| exn ->
close t;
raise exn
;;
let callback_raised t i exn =
let subscriber = Option_array.get_some_exn t.subscribers (i - 1) in
let error =
match subscriber.extract_exn with
| true -> Error.of_exn exn
| false ->
let backtrace = Backtrace.Exn.most_recent () in
[%message
"Bus subscriber raised"
(exn : exn)
(backtrace : Backtrace.t)
(subscriber : _ Subscriber.t)]
|> [%of_sexp: Error.t]
in
match subscriber.on_callback_raise with
| None -> call_on_callback_raise t error
| Some f ->
(try f error with
| exn ->
let backtrace = Backtrace.Exn.most_recent () in
call_on_callback_raise
t
(let original_error = error in
[%message
"Bus subscriber's [on_callback_raise] raised"
(exn : exn)
(backtrace : Backtrace.t)
(original_error : Error.t)]
|> [%of_sexp: Error.t]))
;;
let[@inline always] unsafe_get_callback a i =
Option_array.unsafe_get_some_assuming_some a i
;;
let write_non_optimized t callbacks a1 =
let len = t.num_subscribers in
let i = ref 0 in
while !i < len do
try
let callback = unsafe_get_callback callbacks !i in
incr i;
callback a1
with
| exn -> callback_raised t !i exn
done;
finish_write t
;;
let write_local_non_optimized t callbacks a1 =
let len = t.num_subscribers in
let i = ref 0 in
while !i < len do
try
let callback = unsafe_get_callback callbacks !i in
incr i;
callback a1
with
| exn -> callback_raised t !i exn
done;
finish_write t
;;
let write2_non_optimized t callbacks a1 a2 =
let len = t.num_subscribers in
let i = ref 0 in
while !i < len do
try
let callback = unsafe_get_callback callbacks !i in
incr i;
callback a1 a2
with
| exn -> callback_raised t !i exn
done;
finish_write t
;;
let write3_non_optimized t callbacks a1 a2 a3 =
let len = t.num_subscribers in
let i = ref 0 in
while !i < len do
try
let callback = unsafe_get_callback callbacks !i in
incr i;
callback a1 a2 a3
with
| exn -> callback_raised t !i exn
done;
finish_write t
;;
let write4_non_optimized t callbacks a1 a2 a3 a4 =
let len = t.num_subscribers in
let i = ref 0 in
while !i < len do
try
let callback = unsafe_get_callback callbacks !i in
incr i;
callback a1 a2 a3 a4
with
| exn -> callback_raised t !i exn
done;
finish_write t
;;
let write5_non_optimized t callbacks a1 a2 a3 a4 a5 =
let len = t.num_subscribers in
let i = ref 0 in
while !i < len do
try
let callback = unsafe_get_callback callbacks !i in
incr i;
callback a1 a2 a3 a4 a5
with
| exn -> callback_raised t !i exn
done;
finish_write t
;;
let[@inline always] write t a1 =
let callbacks = t.callbacks in
t.write_ever_called <- true;
match t.state with
| Closed | Write_in_progress -> start_write_failing t
| Ok_to_write ->
(match t.last_value with
| None -> ()
| Some last_value -> Last_value.set1 last_value a1);
if t.num_subscribers > 0
then (
t.state <- Write_in_progress;
if t.num_subscribers = 1
then (
(try (unsafe_get_callback callbacks 0) a1 with
| exn -> callback_raised t 1 exn);
finish_write t)
else (write_non_optimized [@inlined never]) t callbacks a1)
;;
let[@inline always] write_local t a1 =
let callbacks = t.callbacks in
t.write_ever_called <- true;
match t.state with
| Closed | Write_in_progress -> start_write_failing t
| Ok_to_write ->
if t.num_subscribers > 0
then (
t.state <- Write_in_progress;
if t.num_subscribers = 1
then (
(try (unsafe_get_callback callbacks 0) a1 with
| exn -> callback_raised t 1 exn);
finish_write t)
else (write_local_non_optimized [@inlined never]) t callbacks a1)
;;
let[@inline always] write2 t a1 a2 =
let callbacks = t.callbacks in
t.write_ever_called <- true;
match t.state with
| Closed | Write_in_progress -> start_write_failing t
| Ok_to_write ->
(match t.last_value with
| None -> ()
| Some last_value -> Last_value.set2 last_value a1 a2);
if t.num_subscribers > 0
then (
t.state <- Write_in_progress;
if t.num_subscribers = 1
then (
(try (unsafe_get_callback callbacks 0) a1 a2 with
| exn -> callback_raised t 1 exn);
finish_write t)
else (write2_non_optimized [@inlined never]) t callbacks a1 a2)
;;
let[@inline always] write3 t a1 a2 a3 =
let callbacks = t.callbacks in
t.write_ever_called <- true;
match t.state with
| Closed | Write_in_progress -> start_write_failing t
| Ok_to_write ->
(match t.last_value with
| None -> ()
| Some last_value -> Last_value.set3 last_value a1 a2 a3);
if t.num_subscribers > 0
then (
t.state <- Write_in_progress;
if t.num_subscribers = 1
then (
(try (unsafe_get_callback callbacks 0) a1 a2 a3 with
| exn -> callback_raised t 1 exn);
finish_write t)
else (write3_non_optimized [@inlined never]) t callbacks a1 a2 a3)
;;
let[@inline always] write4 t a1 a2 a3 a4 =
let callbacks = t.callbacks in
t.write_ever_called <- true;
match t.state with
| Closed | Write_in_progress -> start_write_failing t
| Ok_to_write ->
(match t.last_value with
| None -> ()
| Some last_value -> Last_value.set4 last_value a1 a2 a3 a4);
if t.num_subscribers > 0
then (
t.state <- Write_in_progress;
if t.num_subscribers = 1
then (
(try (unsafe_get_callback callbacks 0) a1 a2 a3 a4 with
| exn -> callback_raised t 1 exn);
finish_write t)
else (write4_non_optimized [@inlined never]) t callbacks a1 a2 a3 a4)
;;
let[@inline always] write5 t a1 a2 a3 a4 a5 =
let callbacks = t.callbacks in
t.write_ever_called <- true;
match t.state with
| Closed | Write_in_progress -> start_write_failing t
| Ok_to_write ->
(match t.last_value with
| None -> ()
| Some last_value -> Last_value.set5 last_value a1 a2 a3 a4 a5);
if t.num_subscribers > 0
then (
t.state <- Write_in_progress;
if t.num_subscribers = 1
then (
(try (unsafe_get_callback callbacks 0) a1 a2 a3 a4 a5 with
| exn -> callback_raised t 1 exn);
finish_write t)
else (write5_non_optimized [@inlined never]) t callbacks a1 a2 a3 a4 a5)
;;
let allow_subscription_after_first_write t =
On_subscription_after_first_write.allow_subscription_after_first_write
t.on_subscription_after_first_write
;;
let create_exn
?name
created_from
callback_arity
~(on_subscription_after_first_write : On_subscription_after_first_write.t)
~on_callback_raise
=
let last_value =
On_subscription_after_first_write.save_last_value_exn
on_subscription_after_first_write
callback_arity
in
{ bus_id = Bus_id.create ()
; name
; callback_arity
; created_from
; num_subscribers = 0
; on_subscription_after_first_write
; on_callback_raise
; last_value
; subscribers = Option_array.create ~len:0
; callbacks = Option_array.create ~len:0
; state = Ok_to_write
; write_ever_called = false
; unsubscribes_during_write = []
}
;;
let can_subscribe t = allow_subscription_after_first_write t || not t.write_ever_called
let enlarge_capacity t =
let capacity = capacity t in
let new_capacity = Int.max 1 (capacity * 2) in
let copy_and_double array =
let new_array = Option_array.create ~len:new_capacity in
Option_array.blit ~src:array ~src_pos:0 ~dst:new_array ~dst_pos:0 ~len:capacity;
new_array
in
t.subscribers <- copy_and_double t.subscribers;
t.callbacks <- copy_and_double t.callbacks
;;
let subscribe_exn
?( = false)
?on_callback_raise
?on_close
t
subscribed_from
~f:callback
=
if not (can_subscribe t)
then
failwiths
~here:[%here]
"Bus.subscribe_exn called after first write"
[%sexp ~~(subscribed_from : Source_code_position.t), { bus = (t : (_, _) t) }]
[%sexp_of: Sexp.t];
match t.state with
| Closed ->
Subscriber.create
subscribed_from
~bus_id:t.bus_id
~callback
~extract_exn
~subscribers_index:(-1)
~on_callback_raise
~on_close
| Ok_to_write | Write_in_progress ->
let subscriber =
Subscriber.create
subscribed_from
~bus_id:t.bus_id
~callback
~extract_exn
~subscribers_index:t.num_subscribers
~on_callback_raise
~on_close
in
if capacity t = t.num_subscribers then enlarge_capacity t;
add_subscriber t subscriber ~at_subscribers_index:t.num_subscribers;
t.num_subscribers <- t.num_subscribers + 1;
(match t.last_value with
| None -> ()
| Some last_value -> Last_value.send last_value callback);
subscriber
;;
let iter_exn ? t subscribed_from ~f =
if not (can_subscribe t)
then
failwiths ~here:[%here] "Bus.iter_exn called after first write" t [%sexp_of: (_, _) t];
ignore (subscribe_exn ?extract_exn t subscribed_from ~f : _ Subscriber.t)
;;
module Fold_arity = struct
type (_, _, _) t =
| Arity1 : ('a -> unit, 's -> 'a -> 's, 's) t
| Arity2 : ('a -> 'b -> unit, 's -> 'a -> 'b -> 's, 's) t
| Arity3 : ('a -> 'b -> 'c -> unit, 's -> 'a -> 'b -> 'c -> 's, 's) t
| Arity4 : ('a -> 'b -> 'c -> 'd -> unit, 's -> 'a -> 'b -> 'c -> 'd -> 's, 's) t
| Arity5
: ( 'a -> 'b -> 'c -> 'd -> 'e -> unit
, 's -> 'a -> 'b -> 'c -> 'd -> 'e -> 's
, 's )
t
[@@deriving sexp_of]
end
let fold_exn
?
(type c f s)
(t : (c, _) t)
subscribed_from
(fold_arity : (c, f, s) Fold_arity.t)
~(init : s)
~(f : f)
=
let state = ref init in
if not (can_subscribe t)
then
failwiths ~here:[%here] "Bus.fold_exn called after first write" t [%sexp_of: (_, _) t];
iter_exn
?extract_exn
t
subscribed_from
~f:
(match fold_arity with
| Arity1 -> fun a1 -> state := f !state a1
| Arity2 -> fun a1 a2 -> state := f !state a1 a2
| Arity3 -> fun a1 a2 a3 -> state := f !state a1 a2 a3
| Arity4 -> fun a1 a2 a3 a4 -> state := f !state a1 a2 a3 a4
| Arity5 -> fun a1 a2 a3 a4 a5 -> state := f !state a1 a2 a3 a4 a5)
;;
let%test_module _ =
(module struct
let assert_no_allocation bus callback write =
let bus_r = read_only bus in
ignore (subscribe_exn bus_r [%here] ~f:callback : _ Subscriber.t);
let starting_minor_words = Gc.minor_words () in
let starting_major_words = Gc.major_words () in
write ();
let ending_minor_words = Gc.minor_words () in
let ending_major_words = Gc.major_words () in
[%test_result: int] (ending_minor_words - starting_minor_words) ~expect:0;
[%test_result: int] (ending_major_words - starting_major_words) ~expect:0
;;
let%test_unit "write doesn't allocate when inlined" =
let create created_from arity =
create_exn
created_from
arity
~on_subscription_after_first_write:Raise
~on_callback_raise:Error.raise
in
let bus1 = create [%here] Arity1 in
let bus2 = create [%here] Arity2 in
let bus3 = create [%here] Arity3 in
let bus4 = create [%here] Arity4 in
let bus5 = create [%here] Arity5 in
assert_no_allocation bus1 (fun () -> ()) (fun () -> write bus1 ());
assert_no_allocation bus2 (fun () () -> ()) (fun () -> write2 bus2 () ());
assert_no_allocation bus3 (fun () () () -> ()) (fun () -> write3 bus3 () () ());
assert_no_allocation
bus4
(fun () () () () -> ())
(fun () -> write4 bus4 () () () ());
assert_no_allocation
bus5
(fun () () () () () -> ())
(fun () -> write5 bus5 () () () () ())
;;
end)
;;