This crate has been deprecated in favour of pin-list
!
If you want to get the crate name for something else, please do not hesitate to [contact me] or
email help@crates.io.
Original readme below:
This crate provides WaitList
, the most fundamental type for async synchronization. WaitList
is implemented as an intrusive linked list of futures.
std
: Implements the Lock
traits on locks from the standard library.lock_api_04
: Implements the Lock
traits on locks from [lock_api
] v0.4. This enables
integration of crates like [parking_lot
], [spin
] and [usync
].loom_05
: Implements the Lock
traits on locks from [loom
] v0.5.A thread-safe unfair async mutex.
```rust use pinprojectlite::pinproject; use std::cell::UnsafeCell; use std::future::Future; use std::ops::Deref; use std::ops::DerefMut; use std::pin::Pin; use std::task; use std::task::Poll; use waitlist::WaitList;
pub struct Mutex
unsafe impl
impl
pinproject! {
pub struct Lock<'mutex, T> {
mutex: &'mutex Mutex
impl<'mutex, T> Future for Lock<'mutex, T> {
type Output = Guard<'mutex, T>;
fn poll(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll
let mut waiters = if this.inner.as_ref().is_completed() {
// If we haven't initialized the future yet, lock the mutex for the first time
this.mutex.waiters.lock_exclusive()
} else {
// Otherwise, wait for us to be woken
match this.inner.as_mut().poll(cx) {
Poll::Ready((waiters, ())) => waiters,
Poll::Pending => return Poll::Pending,
}
};
// If the mutex is unlocked, mark it as locked and return the guard
if !*waiters.guard {
*waiters.guard = true;
return Poll::Ready(Guard { mutex: this.mutex });
}
// Otherwise, re-register ourselves to be woken when the mutex is unlocked again
this.inner.init(cx.waker().clone(), &mut waiters, (), TryForward);
Poll::Pending
}
}
/// When the future is cancelled before the mutex guard can be taken, wake up the next waiter.
struct TryForward;
impl<'waitlist> waitlist::CancelCallback<'waitlist, std::sync::Mutex
pub struct Guard<'mutex, T> {
mutex: &'mutex Mutex
impl
impl
License: MIT