use std::fmt;
use std::sync::{Arc, OnceLock};
use crate::error::LimitError;
use crate::limits::{Charge, GrantScope, Meter};
pub fn get_or_publish<T>(cell: &OnceLock<T>, compute: impl FnOnce() -> T) -> &T {
if let Some(v) = cell.get() {
return v;
}
let v = compute();
cell.get_or_init(move || v)
}
pub trait CellError: Send + Sync {
fn is_transient(&self) -> bool;
fn is_meter_refusal(&self) -> bool;
fn heap_bytes(&self) -> u64;
fn work_refused(limit: LimitError) -> Self
where
Self: Sized;
}
pub struct KeptError<E> {
error: E,
charge: Charge,
}
impl<E> KeptError<E> {
pub(crate) fn into_parts(self) -> (E, Charge) {
(self.error, self.charge)
}
pub fn error(&self) -> &E {
&self.error
}
}
impl<E: fmt::Debug> fmt::Debug for KeptError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.error.fmt(f)
}
}
pub type ResultCell<T, E> = OnceLock<Result<T, KeptError<E>>>;
pub fn get_or_publish_result<'a, T, E: CellError + Clone>(
cell: &'a ResultCell<T, E>,
meter: &Arc<Meter>,
compute: impl FnOnce() -> Result<T, E>,
) -> Result<&'a T, E> {
let published = |r: &'a Result<T, KeptError<E>>| r.as_ref().map_err(|k| k.error.clone());
if let Some(r) = cell.get() {
return published(r);
}
if let Some(used_up) = meter.work_refused() {
return keep(cell, meter, E::work_refused(used_up));
}
let scope = GrantScope::enter(meter);
let got = compute();
let own = scope.finish(meter);
match got {
Ok(v) => published(cell.get_or_init(move || Ok(v))),
Err(e) if e.is_transient() => {
if e.is_meter_refusal() {
meter.add_work(own);
}
Err(e)
}
Err(e) => keep(cell, meter, e),
}
}
fn keep<'a, T, E: CellError + Clone>(
cell: &'a ResultCell<T, E>,
meter: &Arc<Meter>,
e: E,
) -> Result<&'a T, E> {
match meter.charge(e.heap_bytes()) {
Ok(charge) => {
let kept = KeptError { error: e, charge };
cell.get_or_init(move || Err(kept))
.as_ref()
.map_err(|k| k.error.clone())
}
Err(_) => Err(e),
}
}
#[cfg(test)]
mod tests {
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, OnceLock};
use super::get_or_publish;
fn finishes<T: Send + 'static>(f: impl FnOnce() -> T + Send + 'static) -> T {
unityrs_testkit::guard::finishes("the compute (running it inside the cell deadlocks it)", f)
}
#[test]
fn a_compute_that_re_enters_its_cell_does_not_deadlock() {
let got = finishes(|| {
let cell = OnceLock::new();
let v = *get_or_publish(&cell, || *get_or_publish(&cell, || 2) + 1);
(v, cell.get().copied())
});
assert_eq!(got, (2, Some(2)));
}
#[test]
fn a_thread_holding_a_lock_the_compute_needs_does_not_wait_on_the_cell() {
let got = finishes(|| {
let cell = Arc::new(OnceLock::new());
let gil = Arc::new(std::sync::Mutex::new(()));
let started = Arc::new(std::sync::Barrier::new(2));
let a = {
let (cell, gil, started) = (cell.clone(), gil.clone(), started.clone());
std::thread::spawn(move || {
*get_or_publish(&cell, || {
started.wait();
let _held = gil.lock().unwrap();
1
})
})
};
let held = gil.lock().unwrap();
started.wait();
let b = *get_or_publish(&cell, || 2);
drop(held);
let a = a.join().unwrap();
(a, b)
});
assert_eq!(got.0, got.1, "both see the one published value");
}
#[derive(Clone, Debug, PartialEq, Eq)]
struct Failed {
transient: bool,
heap: u64,
}
impl super::CellError for Failed {
fn is_transient(&self) -> bool {
self.transient
}
fn is_meter_refusal(&self) -> bool {
false
}
fn heap_bytes(&self) -> u64 {
self.heap
}
fn work_refused(_: crate::error::LimitError) -> Self {
Self {
transient: false,
heap: 0,
}
}
}
#[test]
fn a_refusal_is_not_published_and_the_next_ask_computes_again() {
use crate::budget::{Counting, Reservation};
use crate::error::Error;
use crate::limits::Meter;
let pool = Arc::new(Counting::new(100));
let dep = Meter::new(u64::MAX, pool.clone(), Reservation::Dependency);
let other = dep.charge(90).unwrap();
let cell: super::ResultCell<(u32, crate::limits::Charge), Error> = OnceLock::new();
let compute = || dep.charge(20).map(|c| (7, c)).map_err(Error::from);
let first = super::get_or_publish_result(&cell, &dep, compute).map(|v| v.0);
assert!(matches!(first, Err(Error::Budget(_))), "{first:?}");
assert!(cell.get().is_none(), "a refusal is not published");
drop(other);
let second = super::get_or_publish_result(&cell, &dep, compute).map(|v| v.0);
assert_eq!(second.ok(), Some(7));
assert_eq!(pool.used(), 20, "the published value keeps its charge");
}
#[test]
fn a_compute_refused_on_its_own_stops_at_the_work_bound() {
use crate::budget::{Reservation, Unbounded};
use crate::error::{Error, LimitKind};
use crate::limits::{Limits, Meter};
let limits = Limits {
max_input_memory: 500,
max_decode_work: 1000,
..Limits::default()
};
let m = Meter::for_input(&limits, Arc::new(Unbounded), Reservation::Input);
let cell: super::ResultCell<u32, Error> = OnceLock::new();
let runs = AtomicUsize::new(0);
let compute = || {
runs.fetch_add(1, Ordering::Relaxed);
let _a = m.charge(300)?;
let _b = m.charge(300)?;
Ok(7)
};
let got: Vec<_> = (0..50)
.map(|_| super::get_or_publish_result(&cell, &m, compute).copied())
.collect();
assert_eq!(runs.load(Ordering::Relaxed), 4);
assert!(
got[..4]
.iter()
.all(|r| matches!(r, Err(Error::Limit(l)) if l.kind() == LimitKind::InputMemory))
);
assert!(
got[4..]
.iter()
.all(|r| matches!(r, Err(Error::Limit(l)) if l.kind() == LimitKind::DecodeWork))
);
assert!(cell.get().is_some(), "the work refusal is kept");
assert_eq!((m.decoded(), m.used()), (1200, 0));
}
#[test]
fn budget_refusals_count_no_work_and_the_read_succeeds_once_the_pool_empties() {
use crate::budget::{Counting, Reservation};
use crate::error::Error;
use crate::limits::{Limits, Meter};
let limits = Limits {
max_decode_work: 1000,
..Limits::default()
};
let pool = Arc::new(Counting::new(1000));
let dep = Meter::for_input(&limits, pool.clone(), Reservation::Dependency);
let other = Meter::new(u64::MAX, pool.clone(), Reservation::Input);
let held = other.charge(600).unwrap();
let cell: super::ResultCell<u32, Error> = OnceLock::new();
let compute = || {
let _a = dep.charge(300)?;
let _b = dep.charge(300)?;
Ok(7)
};
for _ in 0..50 {
let got = super::get_or_publish_result(&cell, &dep, compute).copied();
assert!(matches!(got, Err(Error::Budget(_))), "{got:?}");
}
assert_eq!(dep.decoded(), 0, "contention is not the input's work");
drop(held);
let got = super::get_or_publish_result(&cell, &dep, compute).copied();
assert_eq!(got.ok(), Some(7));
}
#[test]
fn another_threads_grants_are_not_a_failing_computations_work() {
use crate::budget::{Reservation, Unbounded};
use crate::error::{Error, LimitKind};
use crate::limits::{Limits, Meter};
let limits = Limits {
max_decode_work: 4000,
..Limits::default()
};
let m = Meter::for_input(&limits, Arc::new(Unbounded), Reservation::Input);
let cell: super::ResultCell<u32, Error> = OnceLock::new();
let mut kept = Vec::new();
for _ in 0..10 {
let got = super::get_or_publish_result(&cell, &m, || {
let m = m.clone();
kept.push(
std::thread::spawn(move || m.charge(1000).unwrap())
.join()
.unwrap(),
);
Err(Error::Limit(crate::error::LimitError::refused(
LimitKind::InputMemory,
1,
0,
)))
});
assert!(
matches!(got, Err(Error::Limit(l)) if l.kind() == LimitKind::InputMemory),
"{got:?}"
);
}
assert_eq!(m.decoded(), 0, "the other thread's grants are its own");
assert!(cell.get().is_none(), "nothing kept");
}
#[test]
fn an_input_error_is_published_with_a_charge_for_its_heap() {
use crate::budget::{Reservation, Unbounded};
use crate::limits::Meter;
let m = Meter::new(1000, Arc::new(Unbounded), Reservation::Input);
let cell: super::ResultCell<u32, Failed> = OnceLock::new();
let bad = Failed {
transient: false,
heap: 48,
};
let runs = AtomicUsize::new(0);
for _ in 0..3 {
let got = super::get_or_publish_result(&cell, &m, || {
runs.fetch_add(1, Ordering::Relaxed);
Err(bad.clone())
});
assert_eq!(got, Err(bad.clone()));
}
assert_eq!(runs.load(Ordering::Relaxed), 1, "computed once");
assert_eq!(m.used(), 48, "the kept error is charged");
drop(cell);
assert_eq!(m.used(), 0);
let full = Meter::new(10, Arc::new(Unbounded), Reservation::Input);
let cell: super::ResultCell<u32, Failed> = OnceLock::new();
assert!(super::get_or_publish_result(&cell, &full, || Err(bad.clone())).is_err());
assert!(cell.get().is_none());
}
#[test]
fn the_first_published_value_wins_and_later_computes_never_run() {
let runs = AtomicUsize::new(0);
let cell = OnceLock::new();
let a = *get_or_publish(&cell, || {
runs.fetch_add(1, Ordering::Relaxed);
7
});
let b = *get_or_publish(&cell, || {
runs.fetch_add(1, Ordering::Relaxed);
8
});
assert_eq!((a, b, runs.load(Ordering::Relaxed)), (7, 7, 1));
}
}