Source file rwlock.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
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
open! Base
open! Import
module Capsule = Capsule.Prim

(** See [Adaptive_backoff.once] *)
let log_scale = 10

module Prim = struct
  module State : sig @@ portable
    (** Implements primitive read-write lock state manipulation logic.

        The state has several main features:

        - a [shared] count,

        - a (very large) threshold [shared] count that indicates that the rwlock is
          permanently shared,

        - a bit indicating that the lock is held [exclusive],

        - a (very large) threshold indicating that the lock is [exclusive] permanently,

        - a bit indicating that there are potentially readers awaiting, and

        - a bit indicating that there are potentially writers awaiting.

        State values are allowed to be momentarily imprecise:

        - The bits that indicate that there are readers / writers awaiting are allowed to
          momentarily say that there are no awaiters when, in fact, there are awaiters and
          also the opposite that there are awaiters when, in fact, there is none.

        - The [shared] count may be non-zero while the [exclusive] bit is set.

        To make this possible, state changes are expressed as incremental updates to
        current state that retain information from the previous state and actively
        contending operations work cooperatively on the state:

        - Both [acquire_shared] and [release_shared] may need to undo their increment or
          decrement, respectively, of the [shared] count.

        - The responsibility to signal awaiters, and update the state to indicate that
          there are no awaiters, is transferred to whichever thread is the last to release
          the mutex while there potentially are awaiters.

        - Awoken awaiters must actively re-establish that some thread might be awaiting to
          read or write. *)

    type t : immediate

    val no_awaiters : t

    (* *)

    val incr_shared : t Awaitable.t @ local -> t
    val decr_shared : t Awaitable.t @ local -> t

    (* *)

    val is_unlocked : t -> bool
    val is_unlocked_and_has_awaiters : t -> bool
    val is_permanently : t -> bool
    val is_shared_permanently : t -> bool
    val is_exclusive : t -> bool
    val is_exclusive_permanently : t -> bool
    val has_awaiters : t -> bool
    val has_readers : t -> bool
    val is_shared_at_most_once_and_has_awaiters : t -> bool

    (* *)

    val and_shared_permanently : t -> t
    val and_exclusive : t -> t
    val and_exclusive_permanently : t -> t
    val and_not_exclusive : t -> t
    val and_readers : t -> t
    val and_no_readers : t -> t
    val and_writers : t -> t
    val and_no_awaiters : t -> t
    val and_incr_shared : t -> t
    val and_decr_shared : t -> t
    val and_downgrade : t -> t
  end = struct
    (* The state looks like this:

       {v
         Bit: [      0     |      1     | 2 to n-7 | n-6 |   n-5  |   n-4     | n-3 |   n-2     | n-1 ]
         Use: [ no_writers ! no_readers |  shared  |     ! shared | exclusive |     ! exclusive |  0  ]
              [       no_awaiters       |  count   |        perm. |           |         perm.   |     ]
       v}

       Above, [n] is the number of bits in an [int] and bit at [n-1] is the sign bit,
       which should normally always be [0].

       The [n-3] bit allows the [exclusive_permanently] bit to be temporarily [0] due to
       an optimistic decrement of the [shared] count by [release_shared].

       The number of bits for [shared] count should be enough that it is (practically)
       impossible to overflow on a 64-bit system even with a silly program that just
       repeatedly calls [acquire_shared]. *)

    type t = int

    let no_writers = 1 lsl 0
    let no_readers = 1 lsl 1
    let no_awaiters = no_readers lor no_writers
    let shared = 1 lsl 2
    let shared_permanently = 1 lsl (Int.num_bits - 5)
    let exclusive = 1 lsl (Int.num_bits - 4)
    let exclusive_permanently = 1 lsl (Int.num_bits - 2)

    (* *)

    let[@inline] incr_shared t = Awaitable.fetch_and_add t shared
    let[@inline] decr_shared t = Awaitable.fetch_and_add t (-shared)

    (* *)

    let[@inline] is_unlocked state = state < shared
    let[@inline] is_unlocked_and_has_awaiters state = state < no_awaiters

    let[@inline] is_permanently state =
      shared_permanently / 2 <= state land lnot exclusive
    ;;

    let[@inline] is_shared_permanently state = shared_permanently / 2 <= state
    let[@inline] is_exclusive_permanently state = exclusive_permanently / 2 <= state
    let[@inline] is_exclusive state = exclusive <= state
    let[@inline] has_awaiters state = state land no_awaiters <> no_awaiters
    let[@inline] has_readers state = state land no_readers = 0

    let[@inline] is_shared_at_most_once_and_has_awaiters state =
      state < shared lor no_awaiters
    ;;

    (* *)

    let[@inline] and_shared_permanently state = state lor shared_permanently
    let[@inline] and_exclusive state = state lor exclusive
    let[@inline] and_exclusive_permanently state = state lor exclusive_permanently
    let[@inline] and_not_exclusive state = state land lnot exclusive
    let[@inline] and_readers state = state land lnot no_readers
    let[@inline] and_no_readers state = state lor no_readers
    let[@inline] and_writers state = state land lnot no_writers
    let[@inline] and_no_awaiters state = state lor no_awaiters
    let[@inline] and_incr_shared state = state + shared
    let[@inline] and_decr_shared state = state - shared
    let[@inline] and_downgrade state = state |> and_not_exclusive |> and_incr_shared
  end

  type t = State.t Awaitable.t

  type ('a, _, 'w) result =
    | Value : ('a, 'a, Await.t) result
    | Or_canceled : ('a, 'a Or_canceled.t, Await.t) result
    | Or_would_block : ('a, 'a Or_would_block.t, unit) result

  let[@inline never] acquire_as (type r w) (w : w) c t (r : (unit, r, w) result) : r =
    let[@inline] completed () : r =
      match r with
      | Value -> ()
      | Or_canceled -> Completed ()
      | Or_would_block -> Acquired ()
    in
    (* This is intentionally optimized optimistically for low contention. *)
    let assumption = State.no_awaiters in
    let after = State.no_awaiters |> State.and_exclusive in
    let before =
      Awaitable.compare_exchange t ~if_phys_equal_to:assumption ~replace_with:after
    in
    if phys_equal before assumption
    then completed ()
    else (
      let[@inline] rec acquire_awaiting (w : w) c t before =
        if State.is_unlocked before
        then (
          let after = before |> State.and_writers |> State.and_exclusive in
          match
            Awaitable.compare_and_set t ~if_phys_equal_to:before ~replace_with:after
          with
          | Set_here -> completed ()
          | Compare_failed ->
            Adaptive_backoff.once ~random_key:(Awaitable.random_key t) ~log_scale;
            acquire_awaiting w c t (Awaitable.get t))
        else if State.is_permanently before
        then raise (if State.is_exclusive before then Poisoned else Frozen)
        else (
          let after = before |> State.and_writers in
          if phys_equal before after
             ||
             match
               Awaitable.compare_and_set t ~if_phys_equal_to:before ~replace_with:after
             with
             | Set_here -> true
             | Compare_failed -> false
          then (
            match r with
            | Value ->
              (match Awaitable.await w t ~until_phys_unequal_to:after with
               | Signaled -> acquire_awaiting w c t (Awaitable.get t)
               | Terminated -> raise Await.Terminated)
            | Or_canceled ->
              (match Awaitable.await_or_cancel w c t ~until_phys_unequal_to:after with
               | Signaled -> acquire_awaiting w c t (Awaitable.get t)
               | Terminated -> raise Await.Terminated
               | Canceled -> Or_canceled.Canceled)
            | Or_would_block -> Or_would_block.Would_block)
          else (
            Adaptive_backoff.once ~random_key:(Awaitable.random_key t) ~log_scale;
            acquire_awaiting w c t (Awaitable.get t)))
      in
      acquire_awaiting w c t before)
  ;;

  let acquire_or_cancel w c t = acquire_as w c t Or_canceled
  let acquire w t = acquire_as w Cancellation.never t Value
  let try_acquire t = acquire_as () Cancellation.never t Or_would_block

  let[@inline never] release t =
    (* This is intentionally optimized optimistically for low contention. *)
    let assumption = State.no_awaiters |> State.and_exclusive in
    let after = State.no_awaiters in
    let before =
      Awaitable.compare_exchange t ~if_phys_equal_to:assumption ~replace_with:after
    in
    if not (phys_equal before assumption)
    then (
      let[@inline] rec release_contended t before =
        if not (State.is_permanently before)
        then (
          let after = before |> State.and_not_exclusive |> State.and_no_awaiters in
          match
            Awaitable.compare_and_set t ~if_phys_equal_to:before ~replace_with:after
          with
          | Set_here ->
            if State.has_awaiters before
            then
              if State.has_readers before
              then Awaitable.broadcast t
              else Awaitable.signal t
          | Compare_failed ->
            Adaptive_backoff.once ~random_key:(Awaitable.random_key t) ~log_scale;
            release_contended t (Awaitable.get t))
      in
      release_contended t before)
  ;;

  let[@inline never] release_and_reraise exn t =
    let bt = Backtrace.Exn.most_recent () in
    release t;
    Exn.raise_with_original_backtrace exn bt
  ;;

  let[@inline never] acquire_shared_as (type r w) (w : w) c t (r : (unit, r, w) result)
    : r
    =
    let[@inline] completed () : r =
      match r with
      | Value -> ()
      | Or_canceled -> Completed ()
      | Or_would_block -> Acquired ()
    in
    let prior = State.incr_shared t in
    if not (State.is_exclusive prior)
    then completed ()
    else (
      let[@inline] rec acquire_shared_awaiting (w : w) c t =
        let before = Awaitable.get t in
        if not (State.is_exclusive before)
        then (
          let after = before |> State.and_incr_shared |> State.and_readers in
          match
            Awaitable.compare_and_set t ~if_phys_equal_to:before ~replace_with:after
          with
          | Set_here -> completed ()
          | Compare_failed ->
            Adaptive_backoff.once ~random_key:(Awaitable.random_key t) ~log_scale;
            acquire_shared_awaiting w c t)
        else if State.is_exclusive_permanently before
        then raise Poisoned
        else (
          let after = before |> State.and_readers in
          if phys_equal before after
             ||
             match
               Awaitable.compare_and_set t ~if_phys_equal_to:before ~replace_with:after
             with
             | Set_here -> true
             | Compare_failed -> false
          then (
            match r with
            | Value ->
              (match Awaitable.await w t ~until_phys_unequal_to:after with
               | Signaled -> acquire_shared_awaiting w c t
               | Terminated -> raise Await.Terminated)
            | Or_canceled ->
              (match Awaitable.await_or_cancel w c t ~until_phys_unequal_to:after with
               | Signaled -> acquire_shared_awaiting w c t
               | Terminated -> raise Await.Terminated
               | Canceled -> Or_canceled.Canceled)
            | Or_would_block -> Or_would_block.Would_block)
          else (
            Adaptive_backoff.once ~random_key:(Awaitable.random_key t) ~log_scale;
            acquire_shared_awaiting w c t))
      in
      let[@inline] rec acquire_shared_contended w c t =
        let before = Awaitable.get t in
        if not (State.is_exclusive before)
        then completed ()
        else (
          let after = before |> State.and_decr_shared |> State.and_readers in
          match
            Awaitable.compare_and_set t ~if_phys_equal_to:before ~replace_with:after
          with
          | Set_here -> acquire_shared_awaiting w c t
          | Compare_failed ->
            Adaptive_backoff.once ~random_key:(Awaitable.random_key t) ~log_scale;
            acquire_shared_contended w c t)
      in
      acquire_shared_contended w c t)
  ;;

  let acquire_shared_or_cancel w c t = acquire_shared_as w c t Or_canceled
  let acquire_shared w t = acquire_shared_as w Cancellation.never t Value
  let try_acquire_shared t = acquire_shared_as () Cancellation.never t Or_would_block

  let[@inline never] release_shared t =
    let prior = State.decr_shared t in
    if State.is_shared_at_most_once_and_has_awaiters prior
    then (
      let[@inline] rec signal_awaiters t =
        let before = Awaitable.get t in
        if State.is_unlocked_and_has_awaiters before
        then (
          match
            Awaitable.compare_and_set
              t
              ~if_phys_equal_to:before
              ~replace_with:State.no_awaiters
          with
          | Set_here ->
            if State.has_readers before then Awaitable.broadcast t else Awaitable.signal t
          | Compare_failed ->
            (* Backoff is probably not worth the trouble here as contending state changes
               potentially change the state such that we no longer need to signal. *)
            signal_awaiters t)
      in
      signal_awaiters t)
    else if State.is_exclusive_permanently prior
    then (
      let[@inline] undo_release_shared t =
        let _ : State.t = State.incr_shared t in
        ()
      in
      undo_release_shared t)
  ;;

  let[@inline never] release_shared_and_reraise exn t =
    let bt = Backtrace.Exn.most_recent () in
    release_shared t;
    Exn.raise_with_original_backtrace exn bt
  ;;

  let[@inline never] rec poison t =
    let before = Awaitable.get t in
    (* Unfortunately we cannot check ownership at this point. *)
    if not (State.is_exclusive_permanently before)
    then (
      let after = State.no_awaiters |> State.and_exclusive_permanently in
      match Awaitable.compare_and_set t ~if_phys_equal_to:before ~replace_with:after with
      | Set_here -> Awaitable.broadcast t
      | Compare_failed ->
        (* Backoff is probably not worth the trouble. *)
        poison t)
  ;;

  let[@inline never] poison_and_reraise exn t =
    let bt = Backtrace.Exn.most_recent () in
    poison t;
    Exn.raise_with_original_backtrace exn bt
  ;;

  (** Note that [Prim.freeze] is idempotent and you typically need to
      [Prim.release_shared] the lock after calling [Prim.freeze] to avoid having the
      shared count grow without bound.

      The idea behind having to [Prim.release_shared] even after freezing a lock is that
      this allows [Prim.acquire_shared] and [Prim.release_shared] to be as fast as
      possible, i.e. just a single atomic [fetch_and_add] touching the lock word, in the
      ordinary cases. *)
  let[@inline never] freeze t =
    let before = ref (Awaitable.get t : State.t) in
    assert%debug ((not (State.is_unlocked !before)) && not (State.is_exclusive !before));
    while
      (not (State.is_shared_permanently !before))
      &&
      let after = !before |> State.and_shared_permanently |> State.and_no_awaiters in
      (* This leaves the rwlock as read locked. *)
      match Awaitable.compare_and_set t ~if_phys_equal_to:!before ~replace_with:after with
      | Set_here -> false
      | Compare_failed -> true
    do
      (* Backoff is probably not worth the trouble. *)
      before := Awaitable.get t
    done;
    (* We must wake up any writers waiting to obtain the lock. *)
    if State.has_awaiters !before then Awaitable.broadcast t
  ;;

  let[@inline never] freeze_release_shared_and_reraise exn t =
    let bt = Backtrace.Exn.most_recent () in
    freeze t;
    release_shared t;
    Exn.raise_with_original_backtrace exn bt
  ;;

  let downgrade t =
    (* This is intentionally optimized optimistically for low contention. *)
    let assumption = State.no_awaiters |> State.and_exclusive in
    let after = State.no_awaiters |> State.and_incr_shared in
    let before =
      Awaitable.compare_exchange t ~if_phys_equal_to:assumption ~replace_with:after
    in
    if not (phys_equal before assumption)
    then (
      assert%debug (State.is_exclusive before && not (State.is_permanently before));
      let rec downgrade_contended t before =
        let after = before |> State.and_downgrade |> State.and_no_readers in
        match
          Awaitable.compare_and_set t ~if_phys_equal_to:before ~replace_with:after
        with
        | Set_here -> if State.has_readers before then Awaitable.broadcast t
        | Compare_failed ->
          Adaptive_backoff.once ~random_key:(Awaitable.random_key t) ~log_scale;
          downgrade_contended t (Awaitable.get t)
      in
      downgrade_contended t before)
  ;;

  let[@inline] create ?padded () = Awaitable.make ?padded State.no_awaiters
end

module [@inline] Make (C : Lock_common.Capability_with_of_await) = struct
  type 'k t = Prim.t

  let create ?padded _key = Prim.create ?padded ()

  module Shared_guard = struct
    type 'k rwlock = 'k t

    type 'k inner =
      { rwlock : 'k rwlock @@ many
      ; mutable should_freeze : bool [@atomic]
      }

    type 'k t = { inner : 'k inner @@ aliased contended portable } [@@unboxed]

    let freeze_if_locked ({ rwlock; _ } as inner) =
      if Atomic.Loc.get [%atomic.loc inner.should_freeze]
      then (
        Prim.freeze rwlock;
        Prim.release_shared rwlock)
    ;;

    let create : 'k rwlock -> 'k t @ unique =
      fun rwlock ->
      let t = { rwlock; should_freeze = true } in
      Stdlib.Gc.Safe.finalise freeze_if_locked t;
      { inner = t }
    ;;

    let with_key
      :  'k t @ unique -> f:('k Capsule.Key.t -> 'a @ once unique) @ local once
      -> 'a * 'k t @ once unique
      =
      fun t ~f ->
      match f (Capsule.Key.unsafe_mk ()) with
      | res -> res, t
      | exception exn ->
        let bt = Backtrace.Exn.most_recent () in
        Prim.freeze t.inner.rwlock;
        Exn.raise_with_original_backtrace exn bt
    ;;

    let with_password t ~f =
      let { many = a }, t =
        with_key t ~f:(fun key ->
          Capsule.Key.with_password_shared key ~f:(fun password -> { many = f password })
          [@nontail])
      in
      a, t
    ;;

    let access t ~f =
      let { contended = { portable = a } }, t =
        with_key t ~f:(fun key ->
          { contended =
              Capsule.Key.access_shared key ~f:(fun access -> { portable = f access })
          })
      in
      a, t
    ;;

    let release { inner = { rwlock; _ } as inner } =
      Atomic.Loc.set [%atomic.loc inner.should_freeze] false;
      (* Make sure the stack root for [inner] stays alive at least this long, to make sure
         that if its finalizer runs, it sees [should_poison] set to [false]. *)
      let _ : _ = (Sys.opaque_identity [@mode contended]) inner in
      Prim.release_shared rwlock
    ;;

    let freeze : 'k t @ unique -> 'k Capsule.Key.t =
      fun { inner = { rwlock; _ } as inner } ->
      (* Not technically releasing, but setting this value to [false] prevents calling
         [freeze] again in the finalizer, which is moderately more efficient. *)
      Atomic.Loc.set [%atomic.loc inner.should_freeze] false;
      Prim.freeze rwlock;
      Prim.release_shared rwlock;
      Capsule.Key.unsafe_mk ()
    ;;
  end

  module Guard = struct
    type 'k rwlock = 'k t

    type 'k inner =
      { rwlock : 'k rwlock @@ many
      ; mutable should_poison : bool [@atomic]
      }

    type 'k t = { inner : 'k inner @@ aliased contended portable } [@@unboxed]

    let poison_if_locked ({ rwlock; _ } as inner) =
      if Atomic.Loc.get [%atomic.loc inner.should_poison] then Prim.poison rwlock
    ;;

    let create : 'k rwlock -> 'k t @ unique =
      fun rwlock ->
      let t = { rwlock; should_poison = true } in
      Stdlib.Gc.Safe.finalise poison_if_locked t;
      { inner = t }
    ;;

    let with_key
      :  'k t @ unique
      -> f:('k Capsule.Key.t @ unique -> #('a * 'k Capsule.Key.t) @ once unique)
         @ local once
      -> 'a * 'k t @ once unique
      =
      fun t ~f ->
      match f (Capsule.Key.unsafe_mk ()) with
      | #(res, _key) -> res, t
      | exception exn -> Prim.poison_and_reraise exn t.inner.rwlock
    ;;

    let with_password
      :  'k t @ unique -> f:('k Capsule.Password.t @ local -> 'a @ unique) @ local once
      -> 'a * 'k t @ unique
      =
      fun t ~f ->
      let { many = a }, t =
        with_key
          t
          ~f:
            (Capsule.Key.with_password ~f:(fun password -> { many = f password })
            [@nontail])
      in
      a, t
    ;;

    let access t ~f =
      let { contended = { portable = a } }, t =
        with_key t ~f:(fun key ->
          let #(a, key) =
            Capsule.Key.access key ~f:(fun access -> { portable = f access })
          in
          #({ contended = a }, key))
      in
      a, t
    ;;

    let release { inner = { rwlock; _ } as inner } =
      Atomic.Loc.set [%atomic.loc inner.should_poison] false;
      (* Make sure the stack root for [inner] stays alive at least this long, to make sure
         that if its finalizer runs, it sees [should_poison] set to [false]. *)
      let _ : _ = (Sys.opaque_identity [@mode contended]) inner in
      Prim.release rwlock
    ;;

    let poison { inner = { rwlock; _ } as inner } =
      (* Not technically releasing, but setting this value to [false] prevents calling
         [poison] again in the finalizer, which is moderately more efficient. *)
      Atomic.Loc.set [%atomic.loc inner.should_poison] false;
      Prim.poison rwlock;
      Capsule.Key.unsafe_mk ()
    ;;

    let downgrade { inner = { rwlock; _ } as inner } : _ Shared_guard.t =
      Prim.downgrade rwlock;
      Atomic.Loc.set [%atomic.loc inner.should_poison] false;
      (* Make sure the stack root for [inner] stays alive at least this long, to make sure
         that if its finalizer runs, it sees [should_poison] set to [false]. *)
      let _ : _ = (Sys.opaque_identity [@mode contended]) inner in
      Shared_guard.create rwlock
    ;;
  end

  module Unsafe = struct
    (* SAFETY:

       Since these functions unlock the mutex on uncaught exceptions rather than
       poisoning, they are unsound: it's possible to stash the provided key in a raised
       exception, which leaks the key. We define them internally to implement functions
       like [with_access] and [with_password], but intentionally don't expose them in the
       interface.
    *)

    let[@inline] with_key
      : ('a : value_or_null) 'k.
      C.t @ local
      -> 'k t @ local
      -> f:('k Capsule.Key.t @ unique -> #('a * 'k Capsule.Key.t) @ once unique)
         @ local once
      -> 'a @ once unique
      =
      fun w t ~f ->
      Prim.acquire (C.unsafe_to_await w) t;
      match f (Capsule.Key.unsafe_mk ()) with
      | #(res, _key) ->
        Prim.release t;
        res
      | exception exn -> Prim.release_and_reraise exn t
    ;;

    let[@inline] try_with_key
      : ('a : value_or_null) 'k.
      'k t @ local
      -> f:('k Capsule.Key.t @ unique -> #('a * 'k Capsule.Key.t) @ once unique)
         @ local once
      -> 'a Or_would_block.t @ once unique
      =
      fun t ~f ->
      match Prim.try_acquire t with
      | Would_block -> Would_block
      | Acquired () ->
        (match f (Capsule.Key.unsafe_mk ()) with
         | #(res, _key) ->
           Prim.release t;
           Acquired res
         | exception exn -> Prim.release_and_reraise exn t)
    ;;

    let[@inline] with_key_or_cancel
      : ('a : value_or_null) 'k.
      C.t @ local
      -> Cancellation.t @ local
      -> 'k t @ local
      -> f:('k Capsule.Key.t @ unique -> #('a * 'k Capsule.Key.t) @ once unique)
         @ local once
      -> 'a Or_canceled.t @ once unique
      =
      fun w c t ~f ->
      match Prim.acquire_or_cancel (C.unsafe_to_await w) c t with
      | Canceled -> Canceled
      | Completed () ->
        (match f (Capsule.Key.unsafe_mk ()) with
         | #(res, _key) ->
           Prim.release t;
           Completed res
         | exception exn -> Prim.release_and_reraise exn t)
    ;;

    let[@inline] with_key_shared
      : ('a : value_or_null) 'k.
      C.t @ local
      -> 'k t @ local
      -> f:('k Capsule.Key.t -> 'a @ once unique) @ local once
      -> 'a @ once unique
      =
      fun w t ~f ->
      Prim.acquire_shared (C.unsafe_to_await w) t;
      match f (Capsule.Key.unsafe_mk ()) with
      | res ->
        Prim.release_shared t;
        res
      | exception exn -> Prim.release_shared_and_reraise exn t
    ;;

    let[@inline] with_key_shared_freezing
      : ('a : value_or_null) 'k.
      C.t @ local
      -> 'k t @ local
      -> f:('k Capsule.Key.t -> 'a @ once unique) @ local once
      -> 'a @ once unique
      =
      fun w t ~f ->
      Prim.acquire_shared (C.unsafe_to_await w) t;
      match f (Capsule.Key.unsafe_mk ()) with
      | res ->
        Prim.release_shared t;
        res
      | exception exn -> Prim.freeze_release_shared_and_reraise exn t
    ;;

    let[@inline] try_with_key_shared
      : ('a : value_or_null) 'k.
      'k t @ local
      -> f:('k Capsule.Key.t -> 'a @ once unique) @ local once
      -> 'a Or_would_block.t @ once unique
      =
      fun t ~f ->
      match Prim.try_acquire_shared t with
      | Would_block -> Would_block
      | Acquired () ->
        (match f (Capsule.Key.unsafe_mk ()) with
         | res ->
           Prim.release_shared t;
           Acquired res
         | exception exn -> Prim.release_shared_and_reraise exn t)
    ;;

    let[@inline] try_with_key_shared_freezing
      :  'k t @ local -> f:('k Capsule.Key.t -> 'a @ once unique) @ local once
      -> 'a Or_would_block.t @ once unique
      =
      fun t ~f ->
      match Prim.try_acquire_shared t with
      | Would_block -> Would_block
      | Acquired () ->
        (match f (Capsule.Key.unsafe_mk ()) with
         | res ->
           Prim.release_shared t;
           Acquired res
         | exception exn -> Prim.freeze_release_shared_and_reraise exn t)
    ;;

    let[@inline] with_key_shared_or_cancel
      : ('a : value_or_null) 'k.
      C.t @ local
      -> Cancellation.t @ local
      -> 'k t @ local
      -> f:('k Capsule.Key.t -> 'a @ once unique) @ local once
      -> 'a Or_canceled.t @ once unique
      =
      fun w c t ~f ->
      match Prim.acquire_shared_or_cancel (C.unsafe_to_await w) c t with
      | Canceled -> Canceled
      | Completed () ->
        (match f (Capsule.Key.unsafe_mk ()) with
         | res ->
           Prim.release_shared t;
           Completed res
         | exception exn -> Prim.release_shared_and_reraise exn t)
    ;;

    let[@inline] with_key_shared_or_cancel_freezing
      :  C.t @ local -> Cancellation.t @ local -> 'k t @ local
      -> f:('k Capsule.Key.t -> 'a @ once unique) @ local once
      -> 'a Or_canceled.t @ once unique
      =
      fun w c t ~f ->
      match Prim.acquire_shared_or_cancel (C.unsafe_to_await w) c t with
      | Canceled -> Canceled
      | Completed () ->
        (match f (Capsule.Key.unsafe_mk ()) with
         | res ->
           Prim.release_shared t;
           Completed res
         | exception exn -> Prim.freeze_release_shared_and_reraise exn t)
    ;;
  end

  [%%template
  [@@@mode.default l = (global, local)]

  let[@inline] with_key_poisoning
    : ('a : value_or_null) 'k.
    C.t @ local
    -> 'k t @ local
    -> f:('k Capsule.Key.t @ unique -> #('a * 'k Capsule.Key.t) @ l once unique)
       @ local once
    -> 'a @ l once unique
    =
    fun w t ~f ->
    (Prim.acquire (C.unsafe_to_await w) t;
     match f (Capsule.Key.unsafe_mk ()) with
     | #(res, _key) ->
       Prim.release t;
       res
     | exception exn -> Prim.poison_and_reraise exn t)
    [@exclave_if_local l ~reasons:[ May_return_local ]]
  ;;

  let[@inline] with_key_or_cancel_poisoning
    : ('a : value_or_null) 'k.
    C.t @ local
    -> Cancellation.t @ local
    -> 'k t @ local
    -> f:('k Capsule.Key.t @ unique -> #('a * 'k Capsule.Key.t) @ l once unique)
       @ local once
    -> 'a Or_canceled.t @ l once unique
    =
    fun w c t ~f ->
    match[@exclave_if_local l ~reasons:[ May_return_local ]]
      Prim.acquire_or_cancel (C.unsafe_to_await w) c t
    with
    | Canceled -> Canceled
    | Completed () ->
      (match f (Capsule.Key.unsafe_mk ()) with
       | #(res, _key) ->
         Prim.release t;
         Completed res
       | exception exn -> Prim.poison_and_reraise exn t)
  ;;

  let[@inline] try_with_key_poisoning
    : ('a : value_or_null) 'k.
    'k t @ local
    -> f:('k Capsule.Key.t @ unique -> #('a * 'k Capsule.Key.t) @ l once unique)
       @ local once
    -> 'a Or_would_block.t @ l once unique
    =
    fun t ~f ->
    match[@exclave_if_local l ~reasons:[ May_return_local ]] Prim.try_acquire t with
    | Would_block -> Would_block
    | Acquired () ->
      (match f (Capsule.Key.unsafe_mk ()) with
       | #(res, _key) ->
         Prim.release t;
         Acquired res
       | exception exn -> Prim.poison_and_reraise exn t)
  ;;]

  let acquire w t =
    Prim.acquire w t;
    Guard.create t
  ;;

  let try_acquire t : _ Or_would_block.t =
    match Prim.try_acquire t with
    | Would_block -> Would_block
    | Acquired () -> Acquired (Guard.create t)
  ;;

  let acquire_or_cancel w c t : _ Or_canceled.t =
    match Prim.acquire_or_cancel w c t with
    | Canceled -> Canceled
    | Completed () -> Completed (Guard.create t)
  ;;

  let acquire_shared w t =
    Prim.acquire_shared w t;
    Shared_guard.create t
  ;;

  let try_acquire_shared t : _ Or_would_block.t =
    match Prim.try_acquire_shared t with
    | Would_block -> Would_block
    | Acquired () -> Acquired (Shared_guard.create t)
  ;;

  let acquire_shared_or_cancel w c t : _ Or_canceled.t =
    match Prim.acquire_shared_or_cancel w c t with
    | Canceled -> Canceled
    | Completed () -> Completed (Shared_guard.create t)
  ;;

  let with_access w t ~f =
    (Unsafe.with_key w t ~f:(fun key ->
       let #(a, key) =
         Capsule.Key.access key ~f:(fun access -> { portable = f access })
       in
       #({ contended = a }, key)))
      .contended
      .portable
  ;;

  let with_access_poisoning w t ~f =
    (with_key_poisoning w t ~f:(fun key ->
       let #(a, key) =
         Capsule.Key.access key ~f:(fun access -> { portable = f access })
       in
       #({ contended = a }, key)))
      .contended
      .portable
  ;;

  let try_with_access t ~f : _ Or_would_block.t =
    match
      Unsafe.try_with_key t ~f:(fun key ->
        let #(a, key) =
          Capsule.Key.access key ~f:(fun access -> { portable = f access })
        in
        #({ contended = a }, key))
    with
    | Would_block -> Would_block
    | Acquired { contended = { portable = a } } -> Acquired a
  ;;

  let try_with_access_poisoning t ~f : _ Or_would_block.t =
    match
      try_with_key_poisoning t ~f:(fun key ->
        let #(a, key) =
          Capsule.Key.access key ~f:(fun access -> { portable = f access })
        in
        #({ contended = a }, key))
    with
    | Would_block -> Would_block
    | Acquired { contended = { portable = a } } -> Acquired a
  ;;

  let with_access_or_cancel w c t ~f : _ Or_canceled.t =
    match
      Unsafe.with_key_or_cancel w c t ~f:(fun key ->
        let #(a, key) =
          Capsule.Key.access key ~f:(fun access -> { portable = f access })
        in
        #({ contended = a }, key))
    with
    | Canceled -> Canceled
    | Completed { contended = { portable = a } } -> Completed a
  ;;

  let with_access_or_cancel_poisoning w c t ~f : _ Or_canceled.t =
    match
      with_key_or_cancel_poisoning w c t ~f:(fun key ->
        let #(a, key) =
          Capsule.Key.access key ~f:(fun access -> { portable = f access })
        in
        #({ contended = a }, key))
    with
    | Canceled -> Canceled
    | Completed { contended = { portable = a } } -> Completed a
  ;;

  let with_access_shared w t ~f =
    (Unsafe.with_key_shared w t ~f:(fun key ->
       { contended =
           Capsule.Key.access_shared key ~f:(fun access -> { portable = f access })
       }))
      .contended
      .portable
  ;;

  let with_access_shared_freezing w t ~f =
    (Unsafe.with_key_shared_freezing w t ~f:(fun key ->
       { contended =
           Capsule.Key.access_shared key ~f:(fun access -> { portable = f access })
       }))
      .contended
      .portable
  ;;

  let try_with_access_shared t ~f : _ Or_would_block.t =
    match
      Unsafe.try_with_key_shared t ~f:(fun key ->
        { contended =
            Capsule.Key.access_shared key ~f:(fun access -> { portable = f access })
        })
    with
    | Would_block -> Would_block
    | Acquired { contended = { portable = a } } -> Acquired a
  ;;

  let try_with_access_shared_freezing t ~f : _ Or_would_block.t =
    match
      Unsafe.try_with_key_shared_freezing t ~f:(fun key ->
        { contended =
            Capsule.Key.access_shared key ~f:(fun access -> { portable = f access })
        })
    with
    | Would_block -> Would_block
    | Acquired { contended = { portable = a } } -> Acquired a
  ;;

  let with_access_shared_or_cancel w c t ~f : _ Or_canceled.t =
    match
      Unsafe.with_key_shared_or_cancel w c t ~f:(fun key ->
        { contended =
            Capsule.Key.access_shared key ~f:(fun access -> { portable = f access })
        })
    with
    | Canceled -> Canceled
    | Completed { contended = { portable = a } } -> Completed a
  ;;

  let with_access_shared_or_cancel_freezing w c t ~f : _ Or_canceled.t =
    match
      Unsafe.with_key_shared_or_cancel_freezing w c t ~f:(fun key ->
        { contended =
            Capsule.Key.access_shared key ~f:(fun access -> { portable = f access })
        })
    with
    | Canceled -> Canceled
    | Completed { contended = { portable = a } } -> Completed a
  ;;

  let with_password w t ~f =
    (Unsafe.with_key w t ~f:(fun key ->
       Capsule.Key.with_password key ~f:(fun password -> { many = f password }) [@nontail]))
      .many
  ;;

  let with_password_poisoning w t ~f =
    (with_key_poisoning w t ~f:(fun key ->
       Capsule.Key.with_password key ~f:(fun password -> { many = f password }) [@nontail]))
      .many
  ;;

  let try_with_password t ~f : _ Or_would_block.t =
    match
      Unsafe.try_with_key t ~f:(fun key ->
        Capsule.Key.with_password key ~f:(fun password -> { many = f password })
        [@nontail])
    with
    | Would_block -> Would_block
    | Acquired { many = a } -> Acquired a
  ;;

  let try_with_password_poisoning t ~f : _ Or_would_block.t =
    match
      try_with_key_poisoning t ~f:(fun key ->
        Capsule.Key.with_password key ~f:(fun password -> { many = f password })
        [@nontail])
    with
    | Would_block -> Would_block
    | Acquired { many = a } -> Acquired a
  ;;

  let with_password_or_cancel w c t ~f : _ Or_canceled.t =
    match
      Unsafe.with_key_or_cancel w c t ~f:(fun key ->
        Capsule.Key.with_password key ~f:(fun password -> { many = f password })
        [@nontail])
    with
    | Canceled -> Canceled
    | Completed { many = a } -> Completed a
  ;;

  let with_password_or_cancel_poisoning w c t ~f : _ Or_canceled.t =
    match
      with_key_or_cancel_poisoning w c t ~f:(fun key ->
        Capsule.Key.with_password key ~f:(fun password -> { many = f password })
        [@nontail])
    with
    | Canceled -> Canceled
    | Completed { many = a } -> Completed a
  ;;

  let with_password_shared w t ~f =
    (Unsafe.with_key_shared w t ~f:(fun key ->
       Capsule.Key.with_password_shared key ~f:(fun password -> { many = f password })
       [@nontail]))
      .many
  ;;

  let with_password_shared_freezing w t ~f =
    (Unsafe.with_key_shared_freezing w t ~f:(fun key ->
       Capsule.Key.with_password_shared key ~f:(fun password -> { many = f password })
       [@nontail]))
      .many
  ;;

  let try_with_password_shared t ~f : _ Or_would_block.t =
    match
      Unsafe.try_with_key_shared t ~f:(fun key ->
        Capsule.Key.with_password_shared key ~f:(fun password -> { many = f password })
        [@nontail])
    with
    | Would_block -> Would_block
    | Acquired { many = a } -> Acquired a
  ;;

  let try_with_password_shared_freezing t ~f : _ Or_would_block.t =
    match
      Unsafe.try_with_key_shared_freezing t ~f:(fun key ->
        Capsule.Key.with_password_shared key ~f:(fun password -> { many = f password })
        [@nontail])
    with
    | Would_block -> Would_block
    | Acquired { many = a } -> Acquired a
  ;;

  let with_password_shared_or_cancel w c t ~f : _ Or_canceled.t =
    match
      Unsafe.with_key_shared_or_cancel w c t ~f:(fun key ->
        Capsule.Key.with_password_shared key ~f:(fun password -> { many = f password })
        [@nontail])
    with
    | Canceled -> Canceled
    | Completed { many = a } -> Completed a
  ;;

  let with_password_shared_or_cancel_freezing w c t ~f : _ Or_canceled.t =
    match
      Unsafe.with_key_shared_or_cancel_freezing w c t ~f:(fun key ->
        Capsule.Key.with_password_shared key ~f:(fun password -> { many = f password })
        [@nontail])
    with
    | Canceled -> Canceled
    | Completed { many = a } -> Completed a
  ;;

  let is_poisoned t = Prim.State.is_exclusive_permanently (Awaitable.get t)
  let is_frozen t = Prim.State.is_shared_permanently (Awaitable.get t)

  let poison t key =
    Prim.poison t;
    key
  ;;

  module Condition = Condition.Make (struct
      type nonrec 'k t = 'k t

      let unsafe_acquire w t = Prim.acquire w t
      let unsafe_release t = Prim.release t
    end)

  [%%template
  [@@@mode.default l = (global, local)]

  let[@inline] with_key_and_condition_wait_poisoning
    : type (a : value_or_null) k.
      Await.t @ local
      -> k t @ local
      -> f:
           (k Condition.Wait.t @ local
            -> k Capsule.Key.t @ unique
            -> #(a * k Capsule.Key.t) @ l once unique)
         @ local once
      -> a @ l once unique
    =
    fun w t ~f ->
    (Prim.acquire w t;
     (Condition.with_wait [@mode l])
       w
       t
       (Capsule.Key.unsafe_mk () : k Capsule.Key.t)
       (fun cw key ->
         match[@exclave_if_local l ~reasons:[ May_return_local ]] f cw key with
         | #(res, _key) ->
           (* If the caller has been able to give a key back to us, the lock must be held *)
           assert%debug (Condition.lock_is_held cw);
           Prim.release t;
           res
         | exception exn ->
           if Condition.lock_is_held cw
           then Prim.poison_and_reraise exn t
           else (
             let bt = Backtrace.Exn.most_recent () in
             Exn.raise_with_original_backtrace exn bt)) [@nontail])
    [@exclave_if_local l ~reasons:[ May_return_local ]]
  ;;

  let with_key_and_condition_wait_or_cancel_poisoning
    : type (a : value_or_null) k.
      Await.t @ local
      -> Cancellation.t @ local
      -> k t @ local
      -> f:
           (k Condition.Wait.t @ local
            -> k Capsule.Key.t @ unique
            -> #(a * k Capsule.Key.t) @ l once unique)
         @ local once
      -> a Or_canceled.t @ l once unique
    =
    fun w c t ~f ->
    match[@exclave_if_local l ~reasons:[ May_return_local ]]
      Prim.acquire_or_cancel w c t
    with
    | Canceled -> Canceled
    | Completed () ->
      (Condition.with_wait [@mode l])
        w
        t
        (Capsule.Key.unsafe_mk () : k Capsule.Key.t)
        (fun cw key ->
          match[@exclave_if_local l ~reasons:[ May_return_local ]] f cw key with
          | #(res, _key) ->
            (* If the caller has been able to give a key back to us, the lock must be held *)
            assert%debug (Condition.lock_is_held cw);
            Prim.release t;
            Or_canceled.Completed res
          | exception exn ->
            if Condition.lock_is_held cw
            then Prim.poison_and_reraise exn t
            else (
              let bt = Backtrace.Exn.most_recent () in
              Exn.raise_with_original_backtrace exn bt)) [@nontail]
  ;;]

  module For_testing = struct
    let is_exclusive t = Prim.State.is_exclusive (Awaitable.get t)

    let is_shared t =
      let state = Awaitable.get t in
      (not (Prim.State.is_unlocked state)) && not (Prim.State.is_exclusive state)
    ;;

    include Awaitable.For_testing
  end
end

module Sync = struct
  include Make (Lock_common.Sync)

  let[@inline] with_access s t ~f =
    with_access s t ~f:(fun access -> f s access [@nontail]) [@nontail]
  ;;

  let[@inline] with_access_poisoning s t ~f =
    with_access_poisoning s t ~f:(fun access -> f s access [@nontail]) [@nontail]
  ;;

  let[@inline] with_access_or_cancel s c t ~f =
    with_access_or_cancel s c t ~f:(fun access -> f s access [@nontail]) [@nontail]
  ;;

  let[@inline] with_access_or_cancel_poisoning s c t ~f =
    with_access_or_cancel_poisoning s c t ~f:(fun access -> f s access [@nontail])
    [@nontail]
  ;;

  let[@inline] with_access_shared s t ~f =
    with_access_shared s t ~f:(fun access -> f s access [@nontail]) [@nontail]
  ;;

  let[@inline] with_access_shared_freezing s t ~f =
    with_access_shared_freezing s t ~f:(fun access -> f s access [@nontail]) [@nontail]
  ;;

  let[@inline] with_access_shared_or_cancel s c t ~f =
    with_access_shared_or_cancel s c t ~f:(fun access -> f s access [@nontail]) [@nontail]
  ;;

  let[@inline] with_access_shared_or_cancel_freezing s c t ~f =
    with_access_shared_or_cancel_freezing s c t ~f:(fun access -> f s access [@nontail])
    [@nontail]
  ;;

  let[@inline] with_password s t ~f =
    with_password s t ~f:(fun password -> f s password [@nontail]) [@nontail]
  ;;

  let[@inline] with_password_poisoning s t ~f =
    with_password_poisoning s t ~f:(fun password -> f s password [@nontail]) [@nontail]
  ;;

  let[@inline] with_password_or_cancel s c t ~f =
    with_password_or_cancel s c t ~f:(fun password -> f s password [@nontail]) [@nontail]
  ;;

  let[@inline] with_password_or_cancel_poisoning s c t ~f =
    with_password_or_cancel_poisoning s c t ~f:(fun password -> f s password [@nontail])
    [@nontail]
  ;;

  let[@inline] with_password_shared s t ~f =
    with_password_shared s t ~f:(fun password -> f s password [@nontail]) [@nontail]
  ;;

  let[@inline] with_password_shared_freezing s t ~f =
    with_password_shared_freezing s t ~f:(fun password -> f s password [@nontail])
    [@nontail]
  ;;

  let[@inline] with_password_shared_or_cancel s c t ~f =
    with_password_shared_or_cancel s c t ~f:(fun password -> f s password [@nontail])
    [@nontail]
  ;;

  let[@inline] with_password_shared_or_cancel_freezing s c t ~f =
    with_password_shared_or_cancel_freezing s c t ~f:(fun password ->
      f s password [@nontail])
    [@nontail]
  ;;

  [%%template
  [@@@mode.default l = (global, local)]

  let[@inline] with_key_poisoning s t ~f =
    (with_key_poisoning [@mode l]) s t ~f:(fun key ->
      f s key [@nontail] [@exclave_if_local l ~reasons:[ May_return_local ]])
    [@nontail] [@exclave_if_local l ~reasons:[ May_return_local ]]
  ;;

  let[@inline] try_with_key_poisoning t ~f =
    (try_with_key_poisoning [@mode l]) t ~f:(fun key ->
      f key [@nontail] [@exclave_if_local l ~reasons:[ May_return_local ]])
    [@nontail] [@exclave_if_local l ~reasons:[ May_return_local ]]
  ;;

  let[@inline] with_key_or_cancel_poisoning s c t ~f =
    (with_key_or_cancel_poisoning [@mode l]) s c t ~f:(fun key ->
      f s key [@nontail] [@exclave_if_local l ~reasons:[ May_return_local ]])
    [@nontail] [@exclave_if_local l ~reasons:[ May_return_local ]]
  ;;

  let[@inline] with_key_and_condition_wait_poisoning w t ~f =
    (with_key_and_condition_wait_poisoning [@mode l]) w t ~f:(fun cw key ->
      f
        (Await.sync w)
        cw
        key [@nontail] [@exclave_if_local l ~reasons:[ May_return_local ]])
    [@nontail] [@exclave_if_local l ~reasons:[ May_return_local ]]
  ;;

  let[@inline] with_key_and_condition_wait_or_cancel_poisoning w c t ~f =
    (with_key_and_condition_wait_or_cancel_poisoning [@mode l]) w c t ~f:(fun cw key ->
      f
        (Await.sync w)
        cw
        key [@nontail] [@exclave_if_local l ~reasons:[ May_return_local ]])
    [@nontail] [@exclave_if_local l ~reasons:[ May_return_local ]]
  ;;]
end

module Await = Make (Lock_common.Await)