#[cfg(all(feature = "std", not(target_family = "wasm")))]
mod example {
#![allow(dead_code)]
use std::ops::{Deref, DerefMut};
use std::sync::{mpsc, Arc};
use std::thread;
use std::time::{Duration, Instant};
use event_listener::{listener, Event, Listener};
use try_lock::{Locked, TryLock};
struct Mutex<T> {
lock_ops: Event,
data: TryLock<T>,
}
unsafe impl<T: Send> Send for Mutex<T> {}
unsafe impl<T: Send> Sync for Mutex<T> {}
impl<T> Mutex<T> {
fn new(t: T) -> Mutex<T> {
Mutex {
lock_ops: Event::new(),
data: TryLock::new(t),
}
}
fn try_lock(&self) -> Option<MutexGuard<'_, T>> {
self.data.try_lock().map(|l| MutexGuard {
lock_ops: &self.lock_ops,
locked: Some(l),
})
}
fn lock(&self) -> MutexGuard<'_, T> {
loop {
if let Some(guard) = self.try_lock() {
return guard;
}
listener!(self.lock_ops => listener);
if let Some(guard) = self.try_lock() {
return guard;
}
listener.wait();
}
}
fn lock_timeout(&self, timeout: Duration) -> Option<MutexGuard<'_, T>> {
let deadline = Instant::now() + timeout;
loop {
if let Some(guard) = self.try_lock() {
return Some(guard);
}
listener!(self.lock_ops => listener);
if let Some(guard) = self.try_lock() {
return Some(guard);
}
listener.wait_deadline(deadline)?;
}
}
async fn lock_async(&self) -> MutexGuard<'_, T> {
loop {
if let Some(guard) = self.try_lock() {
return guard;
}
listener!(self.lock_ops => listener);
if let Some(guard) = self.try_lock() {
return guard;
}
listener.await;
}
}
}
struct MutexGuard<'a, T> {
lock_ops: &'a Event,
locked: Option<Locked<'a, T>>,
}
impl<T> Deref for MutexGuard<'_, T> {
type Target = T;
fn deref(&self) -> &T {
self.locked.as_deref().unwrap()
}
}
impl<T> DerefMut for MutexGuard<'_, T> {
fn deref_mut(&mut self) -> &mut T {
self.locked.as_deref_mut().unwrap()
}
}
impl<T> Drop for MutexGuard<'_, T> {
fn drop(&mut self) {
self.locked = None;
self.lock_ops.notify(1);
}
}
pub(super) fn entry() {
const N: usize = 10;
let counter = Arc::new(Mutex::new(0));
let (tx, rx) = mpsc::channel();
for _ in 0..N {
let counter = counter.clone();
let tx = tx.clone();
thread::spawn(move || {
let mut counter = counter.lock();
*counter += 1;
if *counter == N {
tx.send(()).unwrap();
}
});
}
rx.recv().unwrap();
assert_eq!(*counter.lock(), N);
println!("Done!");
}
}
#[cfg(any(target_family = "wasm", not(feature = "std")))]
mod example {
pub(super) fn entry() {
println!("This example is not supported on wasm yet.");
}
}
fn main() {
example::entry();
}