use std::any::Any;
use std::collections::HashMap;
use std::collections::hash_map::Entry;
use std::future::Future;
use std::hash::Hash;
use std::pin::Pin;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, MutexGuard, OnceLock, PoisonError};
use std::task::{Context, Poll, Waker};
use id_effect::{Effect, from_async};
fn lock<T: ?Sized>(m: &Mutex<T>) -> MutexGuard<'_, T> {
m.lock().unwrap_or_else(PoisonError::into_inner)
}
static STM_COMMIT_LOCK: OnceLock<Mutex<()>> = OnceLock::new();
fn commit_lock() -> &'static Mutex<()> {
STM_COMMIT_LOCK.get_or_init(|| Mutex::new(()))
}
static NEXT_WAITER: AtomicU64 = AtomicU64::new(0);
trait Shadow: Send {
fn clone_box(&self) -> Box<dyn Shadow>;
fn as_any(&self) -> &dyn Any;
fn into_any(self: Box<Self>) -> Box<dyn Any>;
}
impl<T: Any + Clone + Send> Shadow for T {
fn clone_box(&self) -> Box<dyn Shadow> {
Box::new(self.clone())
}
fn as_any(&self) -> &dyn Any {
self
}
fn into_any(self: Box<Self>) -> Box<dyn Any> {
self
}
}
trait TCell: Send + Sync {
fn version(&self) -> u64;
fn apply(&self, value: Box<dyn Shadow>) -> Vec<Waker>;
fn watch(&self, id: u64, waker: &Waker, seen: u64) -> bool;
fn unwatch(&self, id: u64);
}
struct Inner<A> {
ver: AtomicU64,
data: Mutex<A>,
watchers: Mutex<HashMap<u64, Waker>>,
}
impl<A: Clone + Send + Sync + 'static> TCell for Inner<A> {
fn version(&self) -> u64 {
self.ver.load(Ordering::Acquire)
}
fn apply(&self, value: Box<dyn Shadow>) -> Vec<Waker> {
let value = *value
.into_any()
.downcast::<A>()
.unwrap_or_else(|_| unreachable!("stm: shadow type mismatch"));
{
let mut g = lock(&self.data);
*g = value;
self.ver.fetch_add(1, Ordering::Release);
}
lock(&self.watchers).drain().map(|(_, w)| w).collect()
}
fn watch(&self, id: u64, waker: &Waker, seen: u64) -> bool {
let mut ws = lock(&self.watchers);
if self.version() != seen {
return false;
}
match ws.entry(id) {
Entry::Occupied(mut e) => {
if !e.get().will_wake(waker) {
e.insert(waker.clone());
}
}
Entry::Vacant(e) => {
e.insert(waker.clone());
}
}
true
}
fn unwatch(&self, id: u64) {
lock(&self.watchers).remove(&id);
}
}
pub struct Txn {
shadow: HashMap<usize, Box<dyn Shadow>>,
reads: HashMap<usize, (Arc<dyn TCell>, u64)>,
writes: HashMap<usize, Arc<dyn TCell>>,
}
struct Checkpoint {
shadow: HashMap<usize, Box<dyn Shadow>>,
writes: HashMap<usize, Arc<dyn TCell>>,
}
impl Txn {
fn new() -> Self {
Self {
shadow: HashMap::new(),
reads: HashMap::new(),
writes: HashMap::new(),
}
}
fn checkpoint(&self) -> Checkpoint {
Checkpoint {
shadow: self
.shadow
.iter()
.map(|(id, v)| (*id, v.clone_box()))
.collect(),
writes: self.writes.clone(),
}
}
fn rollback(&mut self, cp: Checkpoint) {
self.shadow = cp.shadow;
self.writes = cp.writes;
}
fn is_consistent(&self) -> bool {
self.reads.values().all(|(c, ver)| c.version() == *ver)
}
fn into_wait(self) -> RetryWait {
RetryWait {
id: NEXT_WAITER.fetch_add(1, Ordering::Relaxed),
cells: self.reads.into_values().collect(),
}
}
}
fn try_commit(mut txn: Txn) -> bool {
let wakers = {
let _guard = lock(commit_lock());
if !txn.is_consistent() {
return false;
}
let mut ids: Vec<usize> = txn.writes.keys().copied().collect();
ids.sort_unstable();
let mut wakers = Vec::new();
for id in ids {
let cell = txn
.writes
.remove(&id)
.expect("stm: writes must contain sorted ids");
let value = txn
.shadow
.remove(&id)
.expect("stm: a written cell has a shadow value");
wakers.extend(cell.apply(value));
}
wakers
};
for w in wakers {
w.wake();
}
true
}
struct RetryWait {
id: u64,
cells: Vec<(Arc<dyn TCell>, u64)>,
}
impl Future for RetryWait {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
let this = self.get_mut();
for (cell, seen) in &this.cells {
if !cell.watch(this.id, cx.waker(), *seen) {
return Poll::Ready(());
}
}
Poll::Pending
}
}
impl Drop for RetryWait {
fn drop(&mut self) {
for (cell, _) in &self.cells {
cell.unwatch(self.id);
}
}
}
pub enum Outcome<A, E> {
Done(A),
Fail(E),
Retry,
}
type StmRun<A, E> = dyn Fn(&mut Txn) -> Outcome<A, E> + Send + Sync;
pub struct Stm<A, E>
where
A: Send + 'static,
E: Send + 'static,
{
run: Arc<StmRun<A, E>>,
}
impl<A, E> Clone for Stm<A, E>
where
A: Send + 'static,
E: Send + 'static,
{
fn clone(&self) -> Self {
Self {
run: Arc::clone(&self.run),
}
}
}
impl<A, E> Stm<A, E>
where
A: Send + 'static,
E: Send + 'static,
{
pub fn succeed(a: A) -> Self
where
A: Clone + Send + Sync + 'static,
{
Self::from_fn(move |_| Outcome::Done(a.clone()))
}
pub fn fail(e: E) -> Self
where
E: Clone + Send + Sync + 'static,
{
Self::from_fn(move |_| Outcome::Fail(e.clone()))
}
pub fn retry() -> Self {
Self::from_fn(|_| Outcome::Retry)
}
pub fn check(cond: bool) -> Stm<(), E> {
if cond { Stm::succeed(()) } else { Stm::retry() }
}
pub fn from_fn(f: impl Fn(&mut Txn) -> Outcome<A, E> + Send + Sync + 'static) -> Self {
Self { run: Arc::new(f) }
}
pub fn map<B, F>(self, f: F) -> Stm<B, E>
where
B: Send + 'static,
F: Fn(A) -> B + Send + Sync + 'static,
{
let left = self.run.clone();
Stm::<B, E>::from_fn(move |txn| match left(txn) {
Outcome::Done(a) => Outcome::Done(f(a)),
Outcome::Fail(e) => Outcome::Fail(e),
Outcome::Retry => Outcome::Retry,
})
}
pub fn flat_map<B, F>(self, f: F) -> Stm<B, E>
where
B: Send + 'static,
F: Fn(A) -> Stm<B, E> + Send + Sync + 'static,
{
let left = self.run.clone();
Stm::<B, E>::from_fn(move |txn| match left(txn) {
Outcome::Done(a) => f(a).run_on(txn),
Outcome::Fail(e) => Outcome::Fail(e),
Outcome::Retry => Outcome::Retry,
})
}
pub fn or_else(self, that: Stm<A, E>) -> Stm<A, E> {
let r1 = self.run.clone();
let r2 = that.run.clone();
Self::from_fn(move |txn| {
let cp = txn.checkpoint();
match r1(txn) {
Outcome::Retry => {
txn.rollback(cp);
r2(txn)
}
o => o,
}
})
}
pub fn run_on(&self, txn: &mut Txn) -> Outcome<A, E> {
(self.run)(txn)
}
}
#[derive(Clone)]
pub struct TRef<A> {
inner: Arc<Inner<A>>,
}
impl<A> TRef<A>
where
A: Clone + Send + Sync + 'static,
{
fn id(&self) -> usize {
Arc::as_ptr(&self.inner) as usize
}
fn erased(&self) -> Arc<dyn TCell> {
self.inner.clone()
}
fn track(&self, txn: &mut Txn) {
if let Entry::Vacant(e) = txn.reads.entry(self.id()) {
let ver = self.inner.version();
e.insert((self.erased(), ver));
}
}
pub fn make(initial: A) -> Stm<TRef<A>, ()>
where
A: Clone,
{
Stm::from_fn(move |_| {
Outcome::Done(TRef {
inner: Arc::new(Inner {
ver: AtomicU64::new(0),
data: Mutex::new(initial.clone()),
watchers: Mutex::new(HashMap::new()),
}),
})
})
}
pub fn read_stm<E: Send + 'static>(&self) -> Stm<A, E> {
let r = self.clone();
Stm::from_fn(move |txn| Outcome::Done(r.read_txn(txn)))
}
pub fn write_stm<E: Send + 'static>(&self, value: A) -> Stm<(), E> {
let r = self.clone();
Stm::from_fn(move |txn| {
r.set_txn(txn, value.clone());
Outcome::Done(())
})
}
pub fn update_stm<E, F>(&self, f: F) -> Stm<(), E>
where
F: Fn(A) -> A + Send + Sync + Clone + 'static,
E: Send + 'static,
{
let r = self.clone();
Stm::from_fn(move |txn| {
let cur = r.read_txn(txn);
r.set_txn(txn, f(cur));
Outcome::Done(())
})
}
pub fn modify_stm<B, E, F>(&self, f: F) -> Stm<B, E>
where
B: Send + 'static,
F: Fn(A) -> (B, A) + Send + Sync + Clone + 'static,
E: Send + 'static,
{
let r = self.clone();
Stm::from_fn(move |txn| {
let cur = r.read_txn(txn);
let (out, next) = f(cur);
r.set_txn(txn, next);
Outcome::Done(out)
})
}
fn read_txn(&self, txn: &mut Txn) -> A {
let id = self.id();
if let Some(v) = txn.shadow.get(&id) {
return v
.as_any()
.downcast_ref::<A>()
.expect("stm: shadow type mismatch")
.clone();
}
let (val, ver) = {
let g = lock(&self.inner.data);
(g.clone(), self.inner.version())
};
txn.reads.entry(id).or_insert_with(|| (self.erased(), ver));
txn.shadow.insert(id, Box::new(val.clone()));
val
}
fn set_txn(&self, txn: &mut Txn, value: A) {
self.track(txn);
let id = self.id();
txn.shadow.insert(id, Box::new(value));
txn.writes.insert(id, self.erased());
}
#[cfg(test)]
fn watcher_count(&self) -> usize {
lock(&self.inner.watchers).len()
}
}
#[derive(Clone, Default)]
struct QueueState<A> {
buf: std::collections::VecDeque<A>,
cap: Option<usize>,
}
#[derive(Clone)]
pub struct TQueue<A> {
inner: TRef<QueueState<A>>,
}
impl<A> TQueue<A>
where
A: Clone + Send + Sync + 'static,
{
pub fn bounded(capacity: usize) -> Stm<TQueue<A>, ()> {
TRef::make(QueueState {
buf: std::collections::VecDeque::new(),
cap: Some(capacity),
})
.map(|inner| TQueue { inner })
}
pub fn unbounded() -> Stm<TQueue<A>, ()> {
TRef::make(QueueState {
buf: std::collections::VecDeque::new(),
cap: None,
})
.map(|inner| TQueue { inner })
}
pub fn offer<E>(&self, value: A) -> Stm<bool, E>
where
E: Send + 'static,
{
let q = self.clone();
Stm::from_fn(move |txn| {
let mut st = q.inner.read_txn(txn);
if let Some(cap) = st.cap
&& st.buf.len() >= cap
{
return Outcome::Done(false);
}
st.buf.push_back(value.clone());
q.inner.set_txn(txn, st);
Outcome::Done(true)
})
}
pub fn take<E>(&self) -> Stm<A, E>
where
E: Send + 'static,
{
let q = self.clone();
Stm::from_fn(move |txn| {
let mut st = q.inner.read_txn(txn);
if let Some(x) = st.buf.pop_front() {
q.inner.set_txn(txn, st);
Outcome::Done(x)
} else {
Outcome::Retry
}
})
}
}
#[derive(Clone)]
pub struct TMap<K, V> {
inner: TRef<HashMap<K, V>>,
}
impl<K, V> TMap<K, V>
where
K: Clone + Eq + Hash + Send + Sync + 'static,
V: Clone + Send + Sync + 'static,
{
pub fn make() -> Stm<TMap<K, V>, ()> {
TRef::make(HashMap::new()).map(|inner| TMap { inner })
}
pub fn get<E>(&self, key: &K) -> Stm<Option<V>, E>
where
E: Send + 'static,
{
let m = self.clone();
let key = key.clone();
Stm::from_fn(move |txn| {
let map = m.inner.read_txn(txn);
Outcome::Done(map.get(&key).cloned())
})
}
pub fn set<E>(&self, key: K, value: V) -> Stm<(), E>
where
E: Send + 'static,
{
let m = self.clone();
Stm::from_fn(move |txn| {
let mut map = m.inner.read_txn(txn);
map.insert(key.clone(), value.clone());
m.inner.set_txn(txn, map);
Outcome::Done(())
})
}
pub fn delete<E>(&self, key: &K) -> Stm<(), E>
where
E: Send + 'static,
{
let m = self.clone();
let key = key.clone();
Stm::from_fn(move |txn| {
let mut map = m.inner.read_txn(txn);
map.remove(&key);
m.inner.set_txn(txn, map);
Outcome::Done(())
})
}
}
#[derive(Clone)]
pub struct TSemaphore {
inner: TRef<usize>,
}
impl TSemaphore {
pub fn make(permits: usize) -> Stm<TSemaphore, ()> {
TRef::make(permits).map(|inner| TSemaphore { inner })
}
pub fn acquire<E>(&self) -> Stm<(), E>
where
E: Send + 'static,
{
let s = self.clone();
Stm::from_fn(move |txn| {
let n = s.inner.read_txn(txn);
if n == 0 {
return Outcome::Retry;
}
s.inner.set_txn(txn, n - 1);
Outcome::Done(())
})
}
pub fn release<E>(&self) -> Stm<(), E>
where
E: Send + 'static,
{
let s = self.clone();
Stm::from_fn(move |txn| {
let n = s.inner.read_txn(txn);
s.inner.set_txn(txn, n + 1);
Outcome::Done(())
})
}
}
pub fn commit<A, E, R>(stm: Stm<A, E>) -> Effect<A, E, R>
where
A: Send + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
{
from_async(move || async move {
loop {
let mut txn = Txn::new();
match stm.run_on(&mut txn) {
Outcome::Fail(e) => {
if txn.is_consistent() {
return Err(e);
}
}
Outcome::Retry => txn.into_wait().await,
Outcome::Done(a) => {
if try_commit(txn) {
return Ok(a);
}
}
}
}
})
}
pub fn atomically<A, E, R>(stm: Stm<A, E>) -> Effect<A, E, R>
where
A: Send + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
{
commit(stm)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_support::run;
use id_effect::{FiberHandle, HNil, TestExecutor};
use std::thread;
use std::time::Duration;
fn run_stm<A: Send + 'static, E: Send + 'static>(stm: Stm<A, E>) -> Result<A, E> {
run(commit(stm))
}
fn cell<A: Clone + Send + Sync + 'static>(a: A) -> TRef<A> {
run_stm(TRef::make(a)).expect("cell")
}
fn spawn_on<A: Send + 'static>(exec: &TestExecutor, stm: Stm<A, ()>) -> FiberHandle<A, ()> {
let h = commit(stm).fork_root(Arc::new(exec.clone()));
exec.run_until_stalled();
h
}
fn wait_nonzero(c: &TRef<u32>) -> Stm<u32, ()> {
let c = c.clone();
Stm::from_fn(move |txn| {
let n = c.read_txn(txn);
if n == 0 {
Outcome::Retry
} else {
Outcome::Done(n)
}
})
}
mod stm_constructors {
use super::*;
#[test]
fn succeed_returns_value_immediately() {
assert_eq!(run_stm(Stm::<i32, ()>::succeed(42)), Ok(42));
}
#[test]
fn fail_returns_error_immediately() {
assert_eq!(run_stm(Stm::<i32, &str>::fail("boom")), Err("boom"));
}
#[test]
fn check_true_returns_unit_success() {
assert_eq!(run_stm(Stm::<(), ()>::check(true)), Ok(()));
}
}
mod stm_functor {
use super::*;
#[test]
fn map_transforms_success_value() {
let stm = Stm::<i32, ()>::succeed(3).map(|n| n * 2);
assert_eq!(run_stm(stm), Ok(6));
}
#[test]
fn map_preserves_failure() {
let stm = Stm::<i32, &str>::fail("err").map(|n| n + 1);
assert_eq!(run_stm(stm), Err("err"));
}
}
mod stm_monad {
use super::*;
#[test]
fn flat_map_sequences_two_succeed_values() {
let stm = Stm::<i32, ()>::succeed(5).flat_map(|n| Stm::succeed(n + 1));
assert_eq!(run_stm(stm), Ok(6));
}
#[test]
fn flat_map_propagates_first_failure() {
let stm = Stm::<i32, &str>::fail("e").flat_map(|_| Stm::succeed(0));
assert_eq!(run_stm(stm), Err("e"));
}
#[test]
fn or_else_picks_second_when_first_retries() {
let stm = Stm::<i32, ()>::retry().or_else(Stm::succeed(7));
assert_eq!(run_stm(stm), Ok(7));
}
#[test]
fn or_else_preserves_first_success() {
let stm = Stm::<i32, ()>::succeed(1).or_else(Stm::succeed(99));
assert_eq!(run_stm(stm), Ok(1));
}
#[test]
fn or_else_propagates_failure_without_trying_second() {
let stm = Stm::<i32, &str>::fail("e").or_else(Stm::succeed(0));
assert_eq!(run_stm(stm), Err("e"));
}
#[test]
fn or_else_rolls_back_the_writes_of_a_retrying_branch() {
let c = cell(1i32);
let (c1, c2) = (c.clone(), c.clone());
let left = c
.write_stm::<()>(100)
.flat_map(|()| Stm::<i32, ()>::retry());
let right = Stm::from_fn(move |txn| Outcome::Done(c1.read_txn(txn)));
assert_eq!(
run_stm(left.or_else(right)),
Ok(1),
"right sees the original value"
);
assert_eq!(
run_stm(c2.read_stm::<()>()),
Ok(1),
"left's write is not committed"
);
}
}
mod tref {
use super::*;
#[test]
fn make_creates_tref_with_initial_value() {
let got = run_stm(TRef::make(10i32).flat_map(|r| r.read_stm::<()>()));
assert_eq!(got, Ok(10));
}
#[test]
fn write_then_read_returns_new_value() {
let got = run_stm(TRef::make(0i32).flat_map(|r| {
let r2 = r.clone();
r.write_stm::<()>(42).flat_map(move |_| r2.read_stm::<()>())
}));
assert_eq!(got, Ok(42));
}
#[test]
fn update_applies_function_to_value() {
let got = run_stm(TRef::make(3i32).flat_map(|r| {
let r2 = r.clone();
r.update_stm::<(), _>(|n| n * 2)
.flat_map(move |_| r2.read_stm::<()>())
}));
assert_eq!(got, Ok(6));
}
#[test]
fn modify_applies_and_returns_derived_value() {
let got = run_stm(TRef::make(5i32).flat_map(|r| r.modify_stm::<i32, (), _>(|n| (n + 1, n))));
assert_eq!(got, Ok(6));
}
#[test]
fn tref_update_visible_after_commit() {
let c = cell(0i32);
run_stm(c.write_stm::<()>(42)).expect("write");
assert_eq!(run_stm(c.read_stm::<()>()), Ok(42));
}
#[test]
fn failed_transaction_writes_nothing() {
let c = cell(1i32);
let stm = c
.write_stm::<&str>(9)
.flat_map(|()| Stm::<(), &str>::fail("no"));
assert_eq!(run_stm(stm), Err("no"));
assert_eq!(run_stm(c.read_stm::<()>()), Ok(1));
}
}
mod retry_parks {
use super::*;
#[test]
fn stm_retry_waits_for_tref_change_across_threads() {
let c = cell(0u32);
let c_w = c.clone();
let waiter = thread::spawn(move || run_stm(wait_nonzero(&c_w)).expect("waiter"));
thread::sleep(Duration::from_millis(20));
run_stm(c.write_stm::<()>(1)).expect("set");
assert_eq!(waiter.join().expect("join"), 1);
}
#[test]
fn retry_parks_the_fiber_and_registers_one_waker() {
let exec = TestExecutor::new();
let c = cell(0u32);
let h = spawn_on(&exec, wait_nonzero(&c));
assert!(!h.is_done(), "parked, not spinning");
assert_eq!(c.watcher_count(), 1);
assert_eq!(exec.queued_tasks(), 0, "nothing is re-queued while parked");
}
#[test]
fn commit_to_a_read_cell_wakes_the_parked_transaction() {
let exec = TestExecutor::new();
let c = cell(0u32);
let h = spawn_on(&exec, wait_nonzero(&c));
let w = spawn_on(&exec, c.write_stm::<()>(5));
assert!(w.is_done());
assert!(h.is_done(), "woken by the commit and re-run");
assert_eq!(c.watcher_count(), 0);
assert_eq!(run(h.join::<HNil>()), Ok(5));
}
#[test]
fn commit_to_an_unrelated_cell_does_not_wake() {
let exec = TestExecutor::new();
let c = cell(0u32);
let other = cell(0u32);
let h = spawn_on(&exec, wait_nonzero(&c));
spawn_on(&exec, other.write_stm::<()>(1));
assert!(!h.is_done());
assert_eq!(c.watcher_count(), 1);
assert_eq!(other.watcher_count(), 0);
}
#[test]
fn retry_watches_every_cell_read_by_both_or_else_branches() {
let exec = TestExecutor::new();
let a = cell(0u32);
let b = cell(0u32);
let h = spawn_on(&exec, wait_nonzero(&a).or_else(wait_nonzero(&b)));
assert_eq!((a.watcher_count(), b.watcher_count()), (1, 1));
spawn_on(&exec, b.write_stm::<()>(3));
assert!(h.is_done());
assert_eq!((a.watcher_count(), b.watcher_count()), (0, 0));
}
#[test]
fn interrupting_a_parked_transaction_deregisters_it() {
let exec = TestExecutor::new();
let a = cell(0u32);
let b = cell(0u32);
let h = spawn_on(&exec, wait_nonzero(&a).or_else(wait_nonzero(&b)));
assert_eq!((a.watcher_count(), b.watcher_count()), (1, 1));
h.interrupt_now();
exec.run_until_stalled();
assert!(h.is_done());
assert_eq!((a.watcher_count(), b.watcher_count()), (0, 0));
spawn_on(&exec, a.write_stm::<()>(1));
assert_eq!(a.watcher_count(), 0);
}
#[test]
fn a_change_between_read_and_park_is_not_lost() {
let c = cell(0u32);
let mut txn = Txn::new();
assert_eq!(c.read_txn(&mut txn), 0);
run_stm(c.write_stm::<()>(1)).expect("write");
let mut wait = std::pin::pin!(txn.into_wait());
let mut cx = Context::from_waker(Waker::noop());
assert!(wait.as_mut().poll(&mut cx).is_ready());
}
}
mod tmap {
use super::*;
#[test]
fn set_then_get_returns_inserted_value() {
let got = run_stm(TMap::<&str, i32>::make().flat_map(|m| {
let m2 = m.clone();
m.set::<()>("k", 42).flat_map(move |_| m2.get::<()>(&"k"))
}));
assert_eq!(got, Ok(Some(42)));
}
#[test]
fn get_absent_key_returns_none() {
let got = run_stm(TMap::<&str, i32>::make().flat_map(|m| m.get::<()>(&"missing")));
assert_eq!(got, Ok(None));
}
#[test]
fn delete_removes_key() {
let got = run_stm(TMap::<&str, i32>::make().flat_map(|m| {
let m2 = m.clone();
let m3 = m.clone();
m.set::<()>("k", 1)
.flat_map(move |_| m2.delete::<()>(&"k"))
.flat_map(move |_| m3.get::<()>(&"k"))
}));
assert_eq!(got, Ok(None));
}
}
mod tqueue {
use super::*;
#[test]
fn offer_then_take_returns_value() {
let got = run_stm(TQueue::<i32>::unbounded().flat_map(|q| {
let q2 = q.clone();
q.offer::<()>(99).flat_map(move |_| q2.take::<()>())
}));
assert_eq!(got, Ok(99));
}
#[test]
fn tqueue_take_parks_until_offer() {
let exec = TestExecutor::new();
let q: TQueue<i32> = run_stm(TQueue::unbounded()).expect("make queue");
let take = spawn_on(&exec, q.take::<()>());
assert!(!take.is_done());
spawn_on(&exec, q.offer::<()>(7));
assert!(take.is_done());
assert_eq!(run(take.join::<HNil>()), Ok(7));
}
#[test]
fn bounded_queue_offer_returns_true_when_capacity_available() {
let got = run_stm(TQueue::<i32>::bounded(2).flat_map(|q| q.offer::<()>(1)));
assert_eq!(got, Ok(true));
}
}
mod tsemaphore {
use super::*;
#[test]
fn acquire_then_release_increments_permits_back() {
let got = run_stm(TSemaphore::make(1).flat_map(|s| {
let s2 = s.clone();
s.acquire::<()>().flat_map(move |_| s2.release::<()>())
}));
assert_eq!(got, Ok(()));
}
#[test]
fn tsemaphore_acquire_blocks_when_zero() {
let sem = run_stm(TSemaphore::make(0usize)).expect("make sem");
let sem_c = sem.clone();
let acq = thread::spawn(move || {
run_stm(sem_c.acquire::<()>()).expect("acquire");
});
thread::sleep(Duration::from_millis(20));
run_stm(sem.release::<()>()).expect("release");
acq.join().expect("join");
}
}
mod atomically {
use super::*;
#[test]
fn atomically_is_alias_for_commit() {
assert_eq!(run(atomically(Stm::<i32, ()>::succeed(5))), Ok(5));
}
}
}