ordofp_core 0.1.0

OrdoFP core provides developers with HList, Disiunctio, NominataUniversalis, Universalis, and functional type classes
Documentation
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
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
//! Concurrent Primitives - Cats Effect-style Synchronization
//!
//! > *"Concurrere est simul currere"*
//! > — To concur is to run together. (Latin)
//!
//! This module provides concurrent synchronization primitives inspired by
//! Cats Effect 3's concurrent data structures.
//!
//! # These primitives BLOCK the calling thread
//!
//! Despite the fiber-flavoured naming, every waiting operation here is
//! implemented with `std::sync::Mutex`/`Condvar` — waits **block the OS
//! thread**, they do not yield to an async executor. Calling them from
//! inside an async task can stall or deadlock single-threaded executors.
//! Use them between OS threads (or dedicated blocking pools), not as
//! await-points inside futures.
//!
//! # Overview
//!
//! These primitives enable safe communication and synchronization between
//! concurrent threads:
//!
//! - `Dilatum` - Single-assignment promise (Deferred)
//! - `Referentia` - Atomic mutable reference (Ref)
//! - `Semaphorum` - Counting semaphore
//! - `MVarSync` - Synchronized mutable variable
//! - `CaudaBackpressure` - Bounded queue with backpressure
//!
//! # Scholastic Naming
//!
//! | English | Latin | Etymology |
//! |---------|-------|-----------|
//! | Deferred | Dilatum | *dilatum* = postponed, deferred |
//! | Ref | Referentia | *referentia* = reference |
//! | Semaphore | Semaphorum | *semaphorum* = signal bearer |
//! | Queue | Cauda | *cauda* = tail (FIFO order) |
//! | Permit | Permissum | *permissum* = permission |

extern crate alloc;

use alloc::collections::VecDeque;
use alloc::vec::Vec;

#[cfg(feature = "std")]
use std::sync::{Condvar, Mutex};

// =============================================================================
// Dilatum - Single-Assignment Deferred Value
// =============================================================================

/// Internal completion state of a [`Dilatum`].
///
/// Both the "is it done" question and the stored value live behind the
/// *same* mutex, so a completion check-and-set is a single critical
/// section — there is no window where one thread can observe "completed"
/// while another hasn't yet published the value (the TOCTOU that existed
/// when the flag and the value were two separate synchronization
/// primitives).
#[cfg(feature = "std")]
enum Status<A> {
    /// Not yet completed.
    Vacuus,
    /// Completed with a value.
    Perfectus(A),
}

/// A single-assignment concurrent variable.
///
/// `Dilatum<A>` can be completed exactly once with a value, after which
/// all waiters receive that value.
///
/// # Latin Etymology
/// *Dilatum* = participle of *differre*, to postpone or defer.
///
/// # Example
///
/// ```rust
/// use ordofp_core::async_core::concurrent::Dilatum;
///
/// let deferred = Dilatum::<i32>::new();
///
/// // Complete once, from anywhere.
/// assert!(deferred.complete(42));
///
/// // Any waiter blocks until completion; already complete here, so this
/// // returns immediately with the value.
/// let value = deferred.get_blocking();
/// assert_eq!(value, 42);
/// ```
#[cfg(feature = "std")]
pub struct Dilatum<A> {
    /// Completion state (vacant or holding the value), guarded by a single
    /// mutex so check-and-set is one critical section — see [`Status`].
    state: Mutex<Status<A>>,
    /// Condition variable for waiters.
    cond: Condvar,
}

#[cfg(feature = "std")]
impl<A: Clone> Dilatum<A> {
    /// Create a new empty deferred value.
    #[inline]
    pub fn new() -> Self {
        Dilatum {
            state: Mutex::new(Status::Vacuus),
            cond: Condvar::new(),
        }
    }

    /// Try to complete with a value.
    ///
    /// Returns `true` if this call completed the deferred,
    /// `false` if it was already completed.
    ///
    /// The completion check and the value write happen under one lock
    /// acquisition, so concurrent callers race for a single winner: exactly
    /// one `complete` call ever returns `true`, and no other thread can
    /// ever observe the deferred as completed without its value already
    /// being in place.
    ///
    /// # Panics
    ///
    /// Panics if the state mutex is poisoned. The only user code that ever
    /// runs while this lock is held is `A::clone` (inside
    /// [`try_get`](Self::try_get)/[`get_blocking`](Self::get_blocking)), so
    /// absent a panicking `Clone` implementation, poisoning indicates a bug
    /// in this crate.
    #[inline]
    pub fn complete(&self, a: A) -> bool {
        let mut guard = self
            .state
            .lock()
            .expect("Dilatum::complete: state mutex poisoned — library invariant violated");
        match &*guard {
            Status::Perfectus(_) => false,
            Status::Vacuus => {
                *guard = Status::Perfectus(a);
                self.cond.notify_all();
                true
            }
        }
    }

    /// Check if the deferred has been completed.
    ///
    /// # Panics
    ///
    /// Panics if the state mutex is poisoned, which can only happen if
    /// `A::clone` panicked inside a concurrent
    /// [`try_get`](Self::try_get)/[`get_blocking`](Self::get_blocking) call;
    /// otherwise it indicates a bug in this crate.
    #[inline]
    pub fn is_completed(&self) -> bool {
        let guard = self
            .state
            .lock()
            .expect("Dilatum::is_completed: state mutex poisoned — library invariant violated");
        matches!(&*guard, Status::Perfectus(_))
    }

    /// Try to get the value without blocking.
    ///
    /// Returns `Some` with a clone of the value if the deferred has been
    /// completed, `None` otherwise.
    ///
    /// # Panics
    ///
    /// Panics if the state mutex is poisoned. `A::clone` runs while the lock
    /// is held, so a panicking `Clone` implementation poisons the lock for
    /// all later callers; absent that, poisoning indicates a bug in this
    /// crate.
    #[inline]
    pub fn try_get(&self) -> Option<A> {
        let guard = self
            .state
            .lock()
            .expect("Dilatum::try_get: state mutex poisoned — library invariant violated");
        match &*guard {
            Status::Perfectus(a) => Some(a.clone()),
            Status::Vacuus => None,
        }
    }

    /// Block until the value is available.
    ///
    /// Blocks the calling OS thread (this is a `Condvar` wait, not an async
    /// await-point) until some thread calls [`complete`](Self::complete),
    /// then returns a clone of the completed value. Returns immediately if
    /// already completed. There is no timeout.
    ///
    /// # Panics
    ///
    /// Panics if the state mutex or the condition-variable wait is poisoned.
    /// `A::clone` runs while the lock is held, so a panicking `Clone`
    /// implementation poisons the lock for all later callers; absent that,
    /// poisoning indicates a bug in this crate.
    pub fn get_blocking(&self) -> A {
        let mut guard = self
            .state
            .lock()
            .expect("Dilatum::get_blocking: state mutex poisoned — library invariant violated");
        loop {
            match &*guard {
                Status::Perfectus(a) => return a.clone(),
                Status::Vacuus => {
                    guard = self.cond.wait(guard).expect(
                        "Dilatum::get_blocking: condvar wait poisoned — library invariant violated",
                    );
                }
            }
        }
    }
}

#[cfg(feature = "std")]
impl<A: Clone> Default for Dilatum<A> {
    fn default() -> Self {
        Self::new()
    }
}

// =============================================================================
// Referentia - Atomic Mutable Reference
// =============================================================================

/// An atomic mutable reference.
///
/// `Referentia<A>` provides atomic read-modify-write operations for
/// concurrent access to mutable state.
///
/// # Latin Etymology
/// *Referentia* = the act of referring, reference.
///
/// # Example
///
/// ```rust
/// use ordofp_core::async_core::concurrent::Referentia;
///
/// let ref_cell = Referentia::new(0);
///
/// // Atomically increment
/// ref_cell.update(|x| x + 1);
///
/// // Get current value
/// let value = ref_cell.get();
/// assert_eq!(value, 1);
/// ```
#[cfg(feature = "std")]
pub struct Referentia<A> {
    /// The mutable value protected by a mutex.
    value: Mutex<A>,
}

#[cfg(feature = "std")]
impl<A: Clone> Referentia<A> {
    /// Create a new reference with initial value.
    #[inline]
    pub fn new(a: A) -> Self {
        Referentia {
            value: Mutex::new(a),
        }
    }

    /// Get the current value.
    ///
    /// # Poison recovery
    /// No user closure runs here, so there is nothing that could leave the
    /// value torn; a poisoned mutex still holds a fully-formed `A`.
    #[inline]
    pub fn get(&self) -> A {
        let guard = self
            .value
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        guard.clone()
    }

    /// Set the value, returning the old value.
    ///
    /// # Poison recovery
    /// No user closure runs here either — `mem::replace` is a single atomic
    /// swap, so there is no torn state to observe.
    #[inline]
    pub fn set(&self, a: A) -> A {
        let mut guard = self
            .value
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        core::mem::replace(&mut *guard, a)
    }

    /// Update the value with a function.
    ///
    /// # Poison recovery
    /// `f(guard.clone())` runs and completes into the local `new_value`
    /// BEFORE the single `*guard = new_value` assignment. A panic inside `f`
    /// unwinds before that assignment executes, leaving the previous
    /// (fully-consistent) value in place — there is no torn state to
    /// observe, so recovering from poisoning here is sound.
    #[inline]
    pub fn update<F>(&self, f: F)
    where
        F: FnOnce(A) -> A,
    {
        let mut guard = self
            .value
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let new_value = f(guard.clone());
        *guard = new_value;
    }

    /// Update and return the old value.
    ///
    /// # Poison recovery
    /// `f(old.clone())` runs and completes BEFORE the single
    /// `*guard = ...` assignment. A panic inside `f` leaves `*guard`
    /// untouched — no torn state.
    #[inline]
    pub fn get_and_update<F>(&self, f: F) -> A
    where
        F: FnOnce(A) -> A,
    {
        let mut guard = self
            .value
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let old = guard.clone();
        *guard = f(old.clone());
        old
    }

    /// Update and return the new value.
    ///
    /// # Poison recovery
    /// `f(guard.clone())` runs into the local `new_value` BEFORE the single
    /// `*guard = new_value.clone()` assignment. A panic inside `f` leaves
    /// `*guard` untouched — no torn state.
    #[inline]
    pub fn update_and_get<F>(&self, f: F) -> A
    where
        F: FnOnce(A) -> A,
    {
        let mut guard = self
            .value
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let new_value = f(guard.clone());
        *guard = new_value.clone();
        new_value
    }

    /// Modify with a function that returns both the new value and a result.
    ///
    /// # Poison recovery
    /// `f(guard.clone())` runs and is destructured into the local
    /// `(new_value, result)` BEFORE the single `*guard = new_value`
    /// assignment. A panic inside `f` leaves `*guard` untouched — no torn
    /// state.
    #[inline]
    pub fn modify<B, F>(&self, f: F) -> B
    where
        F: FnOnce(A) -> (A, B),
    {
        let mut guard = self
            .value
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let (new_value, result) = f(guard.clone());
        *guard = new_value;
        result
    }
}

// =============================================================================
// Semaphorum - Counting Semaphore
// =============================================================================

/// A counting semaphore.
///
/// `Semaphorum` maintains a count of available permits. Acquiring
/// a permit decrements the count, releasing increments it.
///
/// # Latin Etymology
/// *Semaphorum* = signal bearer (from Greek σῆμα + φέρειν).
///
/// # Example
///
/// ```rust
/// use ordofp_core::async_core::concurrent::{Permissum, Semaphorum};
///
/// let sem = Semaphorum::new(3); // 3 permits
///
/// // Acquire a permit (blocks until one is available)
/// let permit = Permissum::new(&sem);
/// assert_eq!(sem.available(), 2);
///
/// // Release when done (also happens automatically on drop)
/// permit.release();
/// assert_eq!(sem.available(), 3);
/// ```
#[cfg(feature = "std")]
pub struct Semaphorum {
    /// Number of available permits.
    permits: Mutex<usize>,
    /// Condition for waiting acquires.
    cond: Condvar,
}

#[cfg(feature = "std")]
impl Semaphorum {
    /// Create a semaphore with initial permits.
    #[inline]
    pub fn new(permits: usize) -> Self {
        Semaphorum {
            permits: Mutex::new(permits),
            cond: Condvar::new(),
        }
    }

    /// Get the current number of available permits.
    ///
    /// The count is a snapshot: by the time the caller inspects it, other
    /// threads may already have acquired or released permits.
    ///
    /// # Panics
    ///
    /// Panics only if the permits mutex is poisoned. No user code ever runs
    /// while this lock is held, so poisoning indicates a bug in this crate.
    #[inline]
    pub fn available(&self) -> usize {
        *self
            .permits
            .lock()
            .expect("Semaphorum::available: permits mutex poisoned — library invariant violated")
    }

    /// Try to acquire a permit without blocking.
    ///
    /// Returns `true` if a permit was available and has now been taken,
    /// `false` if the count was zero (nothing is taken in that case).
    ///
    /// # Panics
    ///
    /// Panics only if the permits mutex is poisoned. No user code ever runs
    /// while this lock is held, so poisoning indicates a bug in this crate.
    #[inline]
    pub fn try_acquire(&self) -> bool {
        let mut guard = self
            .permits
            .lock()
            .expect("Semaphorum::try_acquire: permits mutex poisoned — library invariant violated");
        if *guard > 0 {
            *guard -= 1;
            true
        } else {
            false
        }
    }

    /// Block until a permit is available.
    ///
    /// Blocks the calling OS thread (a `Condvar` wait, not an async
    /// await-point) until the permit count is non-zero, then takes one
    /// permit. There is no timeout and no fairness guarantee: waiters race
    /// on wake-up, so a late arrival may acquire before a longer-waiting
    /// thread.
    ///
    /// # Panics
    ///
    /// Panics only if the permits mutex or the condition-variable wait is
    /// poisoned. No user code ever runs while this lock is held, so
    /// poisoning indicates a bug in this crate.
    pub fn acquire_blocking(&self) {
        let mut guard = self.permits.lock().expect(
            "Semaphorum::acquire_blocking: permits mutex poisoned — library invariant violated",
        );
        while *guard == 0 {
            guard = self.cond.wait(guard).expect(
                "Semaphorum::acquire_blocking: condvar wait poisoned — library invariant violated",
            );
        }
        *guard -= 1;
    }

    /// Try to acquire multiple permits.
    ///
    /// All-or-nothing: returns `true` and takes exactly `n` permits if at
    /// least `n` are available, otherwise returns `false` and takes none.
    ///
    /// # Panics
    ///
    /// Panics only if the permits mutex is poisoned. No user code ever runs
    /// while this lock is held, so poisoning indicates a bug in this crate.
    #[inline]
    pub fn try_acquire_n(&self, n: usize) -> bool {
        let mut guard = self.permits.lock().expect(
            "Semaphorum::try_acquire_n: permits mutex poisoned — library invariant violated",
        );
        if *guard >= n {
            *guard -= n;
            true
        } else {
            false
        }
    }

    /// Release a permit.
    ///
    /// Increments the permit count and wakes one blocked
    /// [`acquire_blocking`](Self::acquire_blocking) waiter, if any. The
    /// semaphore does not track ownership: releasing without a matching
    /// acquire inflates the count.
    ///
    /// # Panics
    ///
    /// Panics only if the permits mutex is poisoned. No user code ever runs
    /// while this lock is held, so poisoning indicates a bug in this crate.
    #[inline]
    pub fn release(&self) {
        let mut guard = self
            .permits
            .lock()
            .expect("Semaphorum::release: permits mutex poisoned — library invariant violated");
        *guard += 1;
        self.cond.notify_one();
    }

    /// Release multiple permits.
    ///
    /// Increments the permit count by `n` and wakes all blocked waiters
    /// (each re-checks the count, so at most `n` of them proceed). Like
    /// [`release`](Self::release), ownership is not tracked.
    ///
    /// # Panics
    ///
    /// Panics only if the permits mutex is poisoned. No user code ever runs
    /// while this lock is held, so poisoning indicates a bug in this crate.
    #[inline]
    pub fn release_n(&self, n: usize) {
        let mut guard = self
            .permits
            .lock()
            .expect("Semaphorum::release_n: permits mutex poisoned — library invariant violated");
        *guard += n;
        self.cond.notify_all();
    }
}

/// A permit acquired from a semaphore.
///
/// Automatically releases when dropped.
#[cfg(feature = "std")]
pub struct Permissum<'a> {
    semaphore: &'a Semaphorum,
}

#[cfg(feature = "std")]
impl<'a> Permissum<'a> {
    /// Acquire a permit from `semaphore`, blocking until one is available.
    ///
    /// The returned guard holds exactly the one permit it acquired; it is
    /// returned to the semaphore when the guard is dropped (or via
    /// [`release`](Self::release)). Previously this constructor did not
    /// acquire anything — only `Drop` released — so every `Permissum`
    /// created and dropped inflated the semaphore's permit count by one.
    #[inline]
    pub fn new(semaphore: &'a Semaphorum) -> Self {
        semaphore.acquire_blocking();
        Permissum { semaphore }
    }

    /// Manually release the permit.
    #[inline]
    pub fn release(self) {
        // Drop will handle release
    }
}

#[cfg(feature = "std")]
impl Drop for Permissum<'_> {
    fn drop(&mut self) {
        self.semaphore.release();
    }
}

// =============================================================================
// MVarSync - Synchronized Mutable Variable
// =============================================================================

/// A synchronized mutable variable.
///
/// `MVarSync<A>` is a mutable location that can be empty or full.
/// Taking from an empty `MVar` blocks; putting to a full `MVar` blocks.
///
/// # Latin Etymology
/// *Varia Mutabilis Synchrona* = synchronized mutable variable.
///
/// # Example
///
/// ```rust
/// use ordofp_core::async_core::concurrent::MVarSync;
///
/// let mvar = MVarSync::<i32>::new_empty();
///
/// // Put a value (blocks if already full).
/// mvar.put_blocking(42);
///
/// // Take the value (blocks if empty).
/// let value = mvar.take_blocking();
/// assert_eq!(value, 42);
/// ```
#[cfg(feature = "std")]
pub struct MVarSync<A> {
    /// The stored value.
    value: Mutex<Option<A>>,
    /// Condition for waiting operations.
    cond: Condvar,
}

#[cfg(feature = "std")]
impl<A> MVarSync<A> {
    /// Create a new empty `MVar`.
    #[inline]
    pub fn new_empty() -> Self {
        MVarSync {
            value: Mutex::new(None),
            cond: Condvar::new(),
        }
    }

    /// Create a new `MVar` with an initial value.
    #[inline]
    pub fn new(a: A) -> Self {
        MVarSync {
            value: Mutex::new(Some(a)),
            cond: Condvar::new(),
        }
    }

    /// Check if the `MVar` is empty.
    ///
    /// The answer is a snapshot: another thread may put or take between
    /// this check and any subsequent operation.
    ///
    /// # Panics
    ///
    /// Panics if the value mutex is poisoned, which can only happen if
    /// `A::clone` panicked inside a concurrent
    /// [`read_blocking`](Self::read_blocking) call; otherwise it indicates
    /// a bug in this crate.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.value
            .lock()
            .expect("MVarSync::is_empty: value mutex poisoned — library invariant violated")
            .is_none()
    }

    /// Try to take a value without blocking.
    ///
    /// Returns `Some(value)` and leaves the `MVar` empty if it was full,
    /// `None` if it was already empty. On success, one blocked
    /// [`put_blocking`](Self::put_blocking) waiter is woken.
    ///
    /// # Panics
    ///
    /// Panics if the value mutex is poisoned, which can only happen if
    /// `A::clone` panicked inside a concurrent
    /// [`read_blocking`](Self::read_blocking) call; otherwise it indicates
    /// a bug in this crate.
    #[inline]
    pub fn try_take(&self) -> Option<A> {
        let mut guard = self
            .value
            .lock()
            .expect("MVarSync::try_take: value mutex poisoned — library invariant violated");
        let result = guard.take();
        if result.is_some() {
            self.cond.notify_one();
        }
        result
    }

    /// Block until a value is available and take it.
    ///
    /// Blocks the calling OS thread (a `Condvar` wait, not an async
    /// await-point) until the `MVar` is full, removes the value (leaving
    /// the `MVar` empty), and wakes one blocked putter. There is no
    /// timeout and no FIFO fairness among competing takers.
    ///
    /// # Panics
    ///
    /// Panics if the value mutex or the condition-variable wait is
    /// poisoned — reachable only via `A::clone` panicking inside a
    /// concurrent [`read_blocking`](Self::read_blocking) call — or if the
    /// internal "full after wait" invariant is violated, which indicates a
    /// bug in this crate.
    pub fn take_blocking(&self) -> A {
        let mut guard = self
            .value
            .lock()
            .expect("MVarSync::take_blocking: value mutex poisoned — library invariant violated");
        while guard.is_none() {
            guard = self.cond.wait(guard).expect(
                "MVarSync::take_blocking: condvar wait poisoned — library invariant violated",
            );
        }
        let result = guard
            .take()
            .expect("MVarSync::take_blocking: value must be Some after wait loop — invariant bug");
        self.cond.notify_one();
        result
    }

    /// Try to put a value without blocking.
    ///
    /// Returns `true` and stores `a` if the `MVar` was empty (waking one
    /// blocked taker), `false` if it was already full — in which case `a`
    /// is dropped.
    ///
    /// # Panics
    ///
    /// Panics if the value mutex is poisoned, which can only happen if
    /// `A::clone` panicked inside a concurrent
    /// [`read_blocking`](Self::read_blocking) call; otherwise it indicates
    /// a bug in this crate.
    #[inline]
    pub fn try_put(&self, a: A) -> bool {
        let mut guard = self
            .value
            .lock()
            .expect("MVarSync::try_put: value mutex poisoned — library invariant violated");
        if guard.is_none() {
            *guard = Some(a);
            self.cond.notify_one();
            true
        } else {
            false
        }
    }

    /// Block until the `MVar` is empty and put a value.
    ///
    /// Blocks the calling OS thread (a `Condvar` wait, not an async
    /// await-point) until the `MVar` is empty, stores `a`, and wakes one
    /// blocked taker/reader. There is no timeout and no FIFO fairness
    /// among competing putters.
    ///
    /// # Panics
    ///
    /// Panics if the value mutex or the condition-variable wait is
    /// poisoned, which can only happen if `A::clone` panicked inside a
    /// concurrent [`read_blocking`](Self::read_blocking) call; otherwise
    /// it indicates a bug in this crate.
    pub fn put_blocking(&self, a: A) {
        let mut guard = self
            .value
            .lock()
            .expect("MVarSync::put_blocking: value mutex poisoned — library invariant violated");
        while guard.is_some() {
            guard = self.cond.wait(guard).expect(
                "MVarSync::put_blocking: condvar wait poisoned — library invariant violated",
            );
        }
        *guard = Some(a);
        self.cond.notify_one();
    }

    /// Read the value without taking it.
    ///
    /// Blocks the calling OS thread until the `MVar` is full, then returns
    /// a clone of the value, leaving the `MVar` still full. There is no
    /// timeout.
    ///
    /// # Panics
    ///
    /// Panics if the value mutex or the condition-variable wait is
    /// poisoned. `A::clone` runs while the lock is held here, so a
    /// panicking `Clone` implementation poisons the `MVar` for all later
    /// callers; absent that, poisoning (or the internal "full after wait"
    /// invariant failing) indicates a bug in this crate.
    pub fn read_blocking(&self) -> A
    where
        A: Clone,
    {
        let mut guard = self
            .value
            .lock()
            .expect("MVarSync::read_blocking: value mutex poisoned — library invariant violated");
        while guard.is_none() {
            guard = self.cond.wait(guard).expect(
                "MVarSync::read_blocking: condvar wait poisoned — library invariant violated",
            );
        }
        guard
            .clone()
            .expect("MVarSync::read_blocking: value must be Some after wait loop — invariant bug")
    }

    /// Swap the value atomically.
    ///
    /// Blocks the calling OS thread until the `MVar` is full, then replaces
    /// the stored value with `a` and returns the old value. The take and
    /// put happen in one critical section, so no other thread can observe
    /// the `MVar` empty in between.
    ///
    /// # Panics
    ///
    /// Panics if the value mutex or the condition-variable wait is
    /// poisoned — reachable only via `A::clone` panicking inside a
    /// concurrent [`read_blocking`](Self::read_blocking) call — or if the
    /// internal "full after wait" invariant is violated, which indicates a
    /// bug in this crate.
    pub fn swap_blocking(&self, a: A) -> A {
        let mut guard = self
            .value
            .lock()
            .expect("MVarSync::swap_blocking: value mutex poisoned — library invariant violated");
        while guard.is_none() {
            guard = self.cond.wait(guard).expect(
                "MVarSync::swap_blocking: condvar wait poisoned — library invariant violated",
            );
        }
        let old = guard
            .take()
            .expect("MVarSync::swap_blocking: value must be Some after wait loop — invariant bug");
        *guard = Some(a);
        self.cond.notify_one();
        old
    }
}

// =============================================================================
// CaudaBackpressure - Bounded Queue with Backpressure
// =============================================================================

/// A bounded queue with backpressure.
///
/// `CaudaBackpressure<A>` provides a FIFO queue with a maximum capacity.
/// Offering to a full queue blocks; taking from an empty queue blocks.
///
/// # Latin Etymology
/// *Cauda* = tail (referring to FIFO order).
///
/// # Example
///
/// ```rust
/// use ordofp_core::async_core::concurrent::CaudaBackpressure;
///
/// let queue = CaudaBackpressure::<i32>::new(10); // capacity 10
///
/// // Producer
/// queue.offer_blocking(42);
///
/// // Consumer
/// let item = queue.take_blocking();
/// assert_eq!(item, 42);
/// ```
#[cfg(feature = "std")]
pub struct CaudaBackpressure<A> {
    /// Maximum capacity.
    capacity: usize,
    /// The buffer.
    buffer: Mutex<VecDeque<A>>,
    /// Condition for consumers.
    not_empty: Condvar,
    /// Condition for producers.
    not_full: Condvar,
}

#[cfg(feature = "std")]
impl<A> CaudaBackpressure<A> {
    /// Create a new bounded queue.
    #[inline]
    pub fn new(capacity: usize) -> Self {
        CaudaBackpressure {
            capacity,
            buffer: Mutex::new(VecDeque::with_capacity(capacity)),
            not_empty: Condvar::new(),
            not_full: Condvar::new(),
        }
    }

    /// Get the capacity.
    #[inline]
    pub fn capacity(&self) -> usize {
        self.capacity
    }

    /// Get the current size.
    ///
    /// The count is a snapshot: producers and consumers may change it
    /// before the caller acts on the answer.
    ///
    /// # Panics
    ///
    /// Panics if the buffer mutex is poisoned, which can only happen if
    /// `A::clone` panicked inside a concurrent [`peek`](Self::peek) call;
    /// otherwise it indicates a bug in this crate.
    #[inline]
    pub fn size(&self) -> usize {
        self.buffer
            .lock()
            .expect("CaudaBackpressure::size: buffer mutex poisoned — library invariant violated")
            .len()
    }

    /// Check if empty.
    ///
    /// A snapshot only — see [`size`](Self::size).
    ///
    /// # Panics
    ///
    /// Panics if the buffer mutex is poisoned, which can only happen if
    /// `A::clone` panicked inside a concurrent [`peek`](Self::peek) call;
    /// otherwise it indicates a bug in this crate.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.buffer
            .lock()
            .expect(
                "CaudaBackpressure::is_empty: buffer mutex poisoned — library invariant violated",
            )
            .is_empty()
    }

    /// Check if full (size has reached capacity).
    ///
    /// A snapshot only — see [`size`](Self::size).
    ///
    /// # Panics
    ///
    /// Panics if the buffer mutex is poisoned, which can only happen if
    /// `A::clone` panicked inside a concurrent [`peek`](Self::peek) call;
    /// otherwise it indicates a bug in this crate.
    #[inline]
    pub fn is_full(&self) -> bool {
        self.buffer
            .lock()
            .expect(
                "CaudaBackpressure::is_full: buffer mutex poisoned — library invariant violated",
            )
            .len()
            >= self.capacity
    }

    /// Try to offer without blocking.
    ///
    /// Returns `true` and enqueues `a` at the tail (waking one blocked
    /// consumer) if the queue is below capacity, `false` if it is full —
    /// in which case `a` is dropped.
    ///
    /// # Panics
    ///
    /// Panics if the buffer mutex is poisoned, which can only happen if
    /// `A::clone` panicked inside a concurrent [`peek`](Self::peek) call;
    /// otherwise it indicates a bug in this crate.
    #[inline]
    pub fn try_offer(&self, a: A) -> bool {
        let mut guard = self.buffer.lock().expect(
            "CaudaBackpressure::try_offer: buffer mutex poisoned — library invariant violated",
        );
        if guard.len() < self.capacity {
            guard.push_back(a);
            self.not_empty.notify_one();
            true
        } else {
            false
        }
    }

    /// Block until space is available and offer.
    ///
    /// This is the backpressure edge: it blocks the calling OS thread (a
    /// `Condvar` wait, not an async await-point) while the queue is at
    /// capacity, then enqueues `a` at the tail and wakes one blocked
    /// consumer. There is no timeout and no FIFO fairness among competing
    /// producers.
    ///
    /// # Panics
    ///
    /// Panics if the buffer mutex or the condition-variable wait is
    /// poisoned, which can only happen if `A::clone` panicked inside a
    /// concurrent [`peek`](Self::peek) call; otherwise it indicates a bug
    /// in this crate.
    pub fn offer_blocking(&self, a: A) {
        let mut guard = self.buffer.lock().expect(
            "CaudaBackpressure::offer_blocking: buffer mutex poisoned — library invariant violated",
        );
        while guard.len() >= self.capacity {
            guard = self.not_full.wait(guard).expect(
                "CaudaBackpressure::offer_blocking: not_full condvar poisoned — library invariant violated",
            );
        }
        guard.push_back(a);
        self.not_empty.notify_one();
    }

    /// Try to take without blocking.
    ///
    /// Returns `Some` with the head (oldest) item if the queue is
    /// non-empty, waking one blocked producer; `None` if it is empty.
    ///
    /// # Panics
    ///
    /// Panics if the buffer mutex is poisoned, which can only happen if
    /// `A::clone` panicked inside a concurrent [`peek`](Self::peek) call;
    /// otherwise it indicates a bug in this crate.
    #[inline]
    pub fn try_take(&self) -> Option<A> {
        let mut guard = self.buffer.lock().expect(
            "CaudaBackpressure::try_take: buffer mutex poisoned — library invariant violated",
        );
        let result = guard.pop_front();
        if result.is_some() {
            self.not_full.notify_one();
        }
        result
    }

    /// Block until an item is available and take it.
    ///
    /// Blocks the calling OS thread (a `Condvar` wait, not an async
    /// await-point) while the queue is empty, then removes and returns the
    /// head (oldest) item and wakes one blocked producer. There is no
    /// timeout and no FIFO fairness among competing consumers.
    ///
    /// # Panics
    ///
    /// Panics if the buffer mutex or the condition-variable wait is
    /// poisoned — reachable only via `A::clone` panicking inside a
    /// concurrent [`peek`](Self::peek) call — or if the internal
    /// "non-empty after wait" invariant is violated, which indicates a bug
    /// in this crate.
    pub fn take_blocking(&self) -> A {
        let mut guard = self.buffer.lock().expect(
            "CaudaBackpressure::take_blocking: buffer mutex poisoned — library invariant violated",
        );
        while guard.is_empty() {
            guard = self.not_empty.wait(guard).expect(
                "CaudaBackpressure::take_blocking: not_empty condvar poisoned — library invariant violated",
            );
        }
        let result = guard.pop_front().expect(
            "CaudaBackpressure::take_blocking: buffer must be non-empty after wait loop — invariant bug",
        );
        self.not_full.notify_one();
        result
    }

    /// Peek at the front without removing.
    ///
    /// Returns a clone of the head (oldest) item, or `None` if the queue
    /// is empty. The item stays in the queue, so another consumer may
    /// still take it.
    ///
    /// # Panics
    ///
    /// Panics if the buffer mutex is poisoned. `A::clone` runs while the
    /// lock is held here, so a panicking `Clone` implementation poisons
    /// the queue for all later callers; absent that, poisoning indicates
    /// a bug in this crate.
    #[inline]
    pub fn peek(&self) -> Option<A>
    where
        A: Clone,
    {
        let guard = self
            .buffer
            .lock()
            .expect("CaudaBackpressure::peek: buffer mutex poisoned — library invariant violated");
        guard.front().cloned()
    }

    /// Drain all items.
    ///
    /// Atomically removes every queued item and returns them in FIFO
    /// order, leaving the queue empty and waking all blocked producers.
    ///
    /// # Panics
    ///
    /// Panics if the buffer mutex is poisoned, which can only happen if
    /// `A::clone` panicked inside a concurrent [`peek`](Self::peek) call;
    /// otherwise it indicates a bug in this crate.
    #[inline]
    pub fn drain(&self) -> Vec<A> {
        let mut guard = self
            .buffer
            .lock()
            .expect("CaudaBackpressure::drain: buffer mutex poisoned — library invariant violated");
        let mut result = Vec::with_capacity(guard.len());
        result.extend(guard.drain(..));
        self.not_full.notify_all();
        result
    }
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(all(test, feature = "std"))]
mod tests {
    use super::*;
    use alloc::vec;

    #[test]
    fn test_referentia_new() {
        let r = Referentia::new(42);
        assert_eq!(r.get(), 42);
    }

    #[test]
    fn test_referentia_set() {
        let r = Referentia::new(0);
        let old = r.set(42);
        assert_eq!(old, 0);
        assert_eq!(r.get(), 42);
    }

    #[test]
    fn test_referentia_update() {
        let r = Referentia::new(21);
        r.update(|x| x * 2);
        assert_eq!(r.get(), 42);
    }

    #[test]
    fn test_referentia_modify() {
        let r = Referentia::new(21);
        let result = r.modify(|x| (x * 2, "done"));
        assert_eq!(result, "done");
        assert_eq!(r.get(), 42);
    }

    #[test]
    fn panicking_update_does_not_poison_forever() {
        let r = Referentia::new(1i32);
        let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            r.update(|_| -> i32 { panic!("boom") });
        }));
        // Pre-fix: this second call panics "mutex poisoned".
        assert_eq!(r.get(), 1);
    }

    #[test]
    fn test_semaphorum_new() {
        let sem = Semaphorum::new(3);
        assert_eq!(sem.available(), 3);
    }

    #[test]
    fn test_semaphorum_try_acquire() {
        let sem = Semaphorum::new(1);
        assert!(sem.try_acquire());
        assert!(!sem.try_acquire());
        sem.release();
        assert!(sem.try_acquire());
    }

    #[test]
    fn test_semaphorum_release_n() {
        let sem = Semaphorum::new(0);
        sem.release_n(5);
        assert_eq!(sem.available(), 5);
    }

    #[test]
    fn test_mvar_sync_new_empty() {
        let mvar: MVarSync<i32> = MVarSync::new_empty();
        assert!(mvar.is_empty());
    }

    #[test]
    fn test_mvar_sync_new() {
        let mvar = MVarSync::new(42);
        assert!(!mvar.is_empty());
    }

    #[test]
    fn test_mvar_sync_try_put_take() {
        let mvar: MVarSync<i32> = MVarSync::new_empty();
        assert!(mvar.try_put(42));
        assert!(!mvar.try_put(43)); // Should fail, already full
        assert_eq!(mvar.try_take(), Some(42));
        assert!(mvar.is_empty());
    }

    #[test]
    fn test_cauda_new() {
        let queue: CaudaBackpressure<i32> = CaudaBackpressure::new(10);
        assert_eq!(queue.capacity(), 10);
        assert!(queue.is_empty());
    }

    #[test]
    fn test_cauda_try_offer_take() {
        let queue = CaudaBackpressure::new(2);
        assert!(queue.try_offer(1));
        assert!(queue.try_offer(2));
        assert!(!queue.try_offer(3)); // Full

        assert_eq!(queue.try_take(), Some(1));
        assert_eq!(queue.try_take(), Some(2));
        assert_eq!(queue.try_take(), None);
    }

    #[test]
    fn test_cauda_drain() {
        let queue = CaudaBackpressure::new(10);
        queue.try_offer(1);
        queue.try_offer(2);
        queue.try_offer(3);

        let items = queue.drain();
        assert_eq!(items, vec![1, 2, 3]);
        assert!(queue.is_empty());
    }

    #[test]
    fn test_dilatum_complete() {
        let deferred = Dilatum::<i32>::new();
        assert!(!deferred.is_completed());

        assert!(deferred.complete(42));
        assert!(deferred.is_completed());

        assert!(!deferred.complete(43)); // Already completed
        assert_eq!(deferred.try_get(), Some(42));
    }

    #[test]
    fn test_dilatum_concurrent_complete_runs_exactly_once() {
        use alloc::sync::Arc;
        use core::sync::atomic::{AtomicU32, Ordering};
        use std::thread;

        // Regression test for the completed-flag/value TOCTOU: with the old
        // AtomicBool + separate Mutex<Option<A>> representation, the flag
        // could flip to "completed" before the value was written, letting a
        // racing reader observe a torn state. Racing many threads through
        // `complete` must still yield exactly one winner and a consistent
        // final value under the new single-mutex `Status<A>`.
        let deferred = Arc::new(Dilatum::<u32>::new());
        let counter = Arc::new(AtomicU32::new(0));

        let handles: Vec<_> = (0..8)
            .map(|_| {
                let deferred = Arc::clone(&deferred);
                let counter = Arc::clone(&counter);
                thread::spawn(move || {
                    let value = counter.fetch_add(1, Ordering::SeqCst) + 1;
                    deferred.complete(value)
                })
            })
            .collect();

        let wins = handles
            .into_iter()
            .map(|h| h.join().expect("thread panicked"))
            .filter(|&won| won)
            .count();

        assert_eq!(wins, 1, "exactly one complete() call must win the race");
        assert!(deferred.is_completed());
        assert!(
            deferred.try_get().is_some(),
            "completed deferred must never yield a torn (empty) read"
        );
    }

    #[test]
    fn test_dilatum_no_torn_read_under_race() {
        use alloc::sync::Arc;
        use std::thread;

        // Stress the is_completed()/try_get() pair concurrently with racing
        // completions: under the old flag+mutex split, a reader could see
        // is_completed() == true while try_get() still returned None.
        let deferred = Arc::new(Dilatum::<u32>::new());

        let writers: Vec<_> = (0..4)
            .map(|i| {
                let deferred = Arc::clone(&deferred);
                thread::spawn(move || {
                    deferred.complete(i);
                })
            })
            .collect();

        let reader_deferred = Arc::clone(&deferred);
        let reader = thread::spawn(move || {
            for _ in 0..5_000 {
                if reader_deferred.is_completed() {
                    assert!(
                        reader_deferred.try_get().is_some(),
                        "torn read: is_completed() true but try_get() returned None"
                    );
                }
            }
        });

        for h in writers {
            h.join().expect("writer thread panicked");
        }
        reader.join().expect("reader thread panicked");
    }

    #[test]
    fn test_permissum_new_consumes_exactly_one_permit() {
        // Regression test for permit inflation: `Permissum::new` used to
        // only store a reference without acquiring, while `Drop` still
        // released — so every construct+drop cycle added a phantom permit.
        let sem = Semaphorum::new(2);
        assert_eq!(sem.available(), 2);

        let permit = Permissum::new(&sem);
        assert_eq!(
            sem.available(),
            1,
            "Permissum::new must consume exactly one permit"
        );

        drop(permit);
        assert_eq!(
            sem.available(),
            2,
            "releasing the permit must restore the original count exactly, no inflation"
        );
    }

    #[test]
    fn test_permissum_new_admits_exactly_requested_concurrent_holders() {
        use alloc::sync::Arc;
        use core::sync::atomic::{AtomicI64, AtomicUsize, Ordering};
        use std::thread;
        use std::time::Duration;

        let sem = Arc::new(Semaphorum::new(2));
        let current_holders = Arc::new(AtomicI64::new(0));
        let max_holders = Arc::new(AtomicUsize::new(0));

        let handles: Vec<_> = (0..5)
            .map(|_| {
                let sem = Arc::clone(&sem);
                let current_holders = Arc::clone(&current_holders);
                let max_holders = Arc::clone(&max_holders);
                thread::spawn(move || {
                    let _permit = Permissum::new(&sem); // blocks until a permit frees up
                    let now = current_holders.fetch_add(1, Ordering::SeqCst) + 1;
                    max_holders.fetch_max(now as usize, Ordering::SeqCst);
                    thread::sleep(Duration::from_millis(20));
                    current_holders.fetch_sub(1, Ordering::SeqCst);
                })
            })
            .collect();

        for h in handles {
            h.join().expect("thread panicked");
        }

        assert_eq!(
            max_holders.load(Ordering::SeqCst),
            2,
            "Semaphorum::new(2) must admit exactly 2 concurrent Permissum holders"
        );
        assert_eq!(
            sem.available(),
            2,
            "all permits must be returned, no inflation"
        );
    }
}