pub mod mutex;
pub mod rwlock;
pub use mutex::Mutex;
pub use rwlock::RwLock;
#[cfg(all(feature = "native", not(feature = "async-tokio")))]
pub use std::sync::{Barrier, Condvar};
#[cfg(feature = "wasm")]
pub use wasm_fallback::{Barrier, Condvar};
#[cfg(feature = "wasm")]
mod wasm_fallback {
use std::sync::Mutex as StdMutex;
use std::sync::Condvar as StdCondvar;
pub struct Barrier {
count: StdMutex<usize>,
condvar: StdCondvar,
expected: usize,
}
impl Barrier {
pub fn new(n: usize) -> Self {
Self {
count: StdMutex::new(0),
condvar: StdCondvar::new(),
expected: n,
}
}
pub fn wait(&self) {
let mut count = self.count.lock().unwrap();
*count += 1;
if *count >= self.expected {
*count = 0;
self.condvar.notify_all();
} else {
drop(count);
let _ = self.condvar.wait_while(
self.count.lock().unwrap(),
|c| *c < self.expected,
);
}
}
}
pub type Condvar = StdCondvar;
}
#[cfg(feature = "async-tokio")]
pub mod async_wrappers {
pub use tokio::sync::{Mutex as AsyncMutex, RwLock as AsyncRwLock};
}