use std::fmt;
use std::future::Future;
use std::pin::pin;
use std::time::Duration;
use crate::kv::locks::{Ends, Held};
use crate::locks::Fence;
use crate::{Error, Key, Result};
#[must_use = "a lock opens with open()"]
pub struct LockBuilder {
builder: crate::locks::LockBuilder,
}
#[must_use = "a semaphore opens with open()"]
pub struct SemaphoreBuilder {
builder: crate::locks::SemaphoreBuilder,
}
impl super::Store {
pub fn lock(&self, name: &str) -> LockBuilder {
LockBuilder { builder: self.blocking().lock(name) }
}
pub fn semaphore(&self, name: &str) -> SemaphoreBuilder {
SemaphoreBuilder { builder: self.blocking().semaphore(name) }
}
}
impl LockBuilder {
pub fn max_hold(self, bound: Duration) -> Self {
LockBuilder { builder: self.builder.max_hold(bound) }
}
pub fn open(self) -> Result<Lock> {
Ok(Lock { lock: self.builder.open()? })
}
}
impl SemaphoreBuilder {
pub fn limit(self, limit: u32) -> Self {
SemaphoreBuilder { builder: self.builder.limit(limit) }
}
pub fn max_hold(self, bound: Duration) -> Self {
SemaphoreBuilder { builder: self.builder.max_hold(bound) }
}
pub fn open(self) -> Result<Semaphore> {
Ok(Semaphore { semaphore: self.builder.open()? })
}
}
#[derive(Clone)]
pub struct Lock {
lock: crate::locks::Lock,
}
#[derive(Clone)]
pub struct Semaphore {
semaphore: crate::locks::Semaphore,
}
impl Lock {
pub fn hold<'w, T, E, F>(
&self,
key: impl Key,
work: impl FnOnce(Lease) -> F + Send + 'w,
) -> impl Future<Output = Result<T, E>> + Send + 'w
where
F: Future<Output = Result<T, E>> + Send + 'w,
T: Send + 'w,
E: From<Error> + Send + 'w,
{
self.key(key).hold(work)
}
pub fn try_hold<'w, T, E, F>(
&self,
key: impl Key,
work: impl FnOnce(Lease) -> F + Send + 'w,
) -> impl Future<Output = Result<Option<T>, E>> + Send + 'w
where
F: Future<Output = Result<T, E>> + Send + 'w,
T: Send + 'w,
E: From<Error> + Send + 'w,
{
self.key(key).try_hold(work)
}
pub fn key(&self, key: impl Key) -> LockKey<'_> {
LockKey { key: self.lock.key(key) }
}
pub fn blocking(&self) -> &crate::locks::Lock {
&self.lock
}
}
impl From<crate::locks::Lock> for Lock {
fn from(lock: crate::locks::Lock) -> Lock {
Lock { lock }
}
}
impl Semaphore {
pub fn hold<'w, T, E, F>(
&self,
key: impl Key,
work: impl FnOnce(Permit) -> F + Send + 'w,
) -> impl Future<Output = Result<T, E>> + Send + 'w
where
F: Future<Output = Result<T, E>> + Send + 'w,
T: Send + 'w,
E: From<Error> + Send + 'w,
{
self.key(key).hold(work)
}
pub fn try_hold<'w, T, E, F>(
&self,
key: impl Key,
work: impl FnOnce(Permit) -> F + Send + 'w,
) -> impl Future<Output = Result<Option<T>, E>> + Send + 'w
where
F: Future<Output = Result<T, E>> + Send + 'w,
T: Send + 'w,
E: From<Error> + Send + 'w,
{
self.key(key).try_hold(work)
}
pub fn key(&self, key: impl Key) -> SemaphoreKey<'_> {
SemaphoreKey { key: self.semaphore.key(key) }
}
pub fn blocking(&self) -> &crate::locks::Semaphore {
&self.semaphore
}
}
impl From<crate::locks::Semaphore> for Semaphore {
fn from(semaphore: crate::locks::Semaphore) -> Semaphore {
Semaphore { semaphore }
}
}
#[must_use = "a key is held with hold() or try_hold()"]
pub struct LockKey<'a> {
key: crate::locks::LockKey<'a>,
}
#[must_use = "a key is held with hold() or try_hold()"]
pub struct SemaphoreKey<'a> {
key: crate::locks::SemaphoreKey<'a>,
}
impl LockKey<'_> {
pub fn wait(self, wait: Duration) -> Self {
LockKey { key: self.key.wait(wait) }
}
pub fn hold<'w, T, E, F>(
self,
work: impl FnOnce(Lease) -> F + Send + 'w,
) -> impl Future<Output = Result<T, E>> + Send + 'w
where
F: Future<Output = Result<T, E>> + Send + 'w,
T: Send + 'w,
E: From<Error> + Send + 'w,
{
let awaited = self.key.awaited();
async move {
let held = awaited.hold().await?;
let lease = Lease { fence: held.fence(), waited: held.waited(), ends: held.ends() };
holding(held, |held| held.marked(|| work(lease))).await
}
}
pub fn try_hold<'w, T, E, F>(
self,
work: impl FnOnce(Lease) -> F + Send + 'w,
) -> impl Future<Output = Result<Option<T>, E>> + Send + 'w
where
F: Future<Output = Result<T, E>> + Send + 'w,
T: Send + 'w,
E: From<Error> + Send + 'w,
{
let awaited = self.key.awaited();
async move {
let Some(held) = awaited.try_hold().await? else { return Ok(None) };
let lease = Lease { fence: held.fence(), waited: held.waited(), ends: held.ends() };
holding(held, |held| held.marked(|| work(lease))).await.map(Some)
}
}
}
impl SemaphoreKey<'_> {
pub fn wait(self, wait: Duration) -> Self {
SemaphoreKey { key: self.key.wait(wait) }
}
pub fn hold<'w, T, E, F>(
self,
work: impl FnOnce(Permit) -> F + Send + 'w,
) -> impl Future<Output = Result<T, E>> + Send + 'w
where
F: Future<Output = Result<T, E>> + Send + 'w,
T: Send + 'w,
E: From<Error> + Send + 'w,
{
let awaited = self.key.awaited();
async move {
let held = awaited.hold().await?;
let permit = Permit { waited: held.waited(), ends: held.ends() };
holding(held, |held| held.marked(|| work(permit))).await
}
}
pub fn try_hold<'w, T, E, F>(
self,
work: impl FnOnce(Permit) -> F + Send + 'w,
) -> impl Future<Output = Result<Option<T>, E>> + Send + 'w
where
F: Future<Output = Result<T, E>> + Send + 'w,
T: Send + 'w,
E: From<Error> + Send + 'w,
{
let awaited = self.key.awaited();
async move {
let Some(held) = awaited.try_hold().await? else { return Ok(None) };
let permit = Permit { waited: held.waited(), ends: held.ends() };
holding(held, |held| held.marked(|| work(permit))).await.map(Some)
}
}
}
async fn holding<T, E, F>(held: Held, start: impl FnOnce(&Held) -> F) -> Result<T, E>
where
F: Future<Output = Result<T, E>>,
E: From<Error>,
{
let mut work = pin!(start(&held));
let value = std::future::poll_fn(|context| held.marked(|| work.as_mut().poll(context))).await?;
held.kept(value)
}
pub struct Lease {
fence: Fence,
waited: Duration,
ends: Ends,
}
impl Lease {
pub fn fence(&self) -> Fence {
self.fence
}
pub fn lost(&self) -> bool {
self.ends.lost()
}
pub fn waited(&self) -> Duration {
self.waited
}
}
pub struct Permit {
waited: Duration,
ends: Ends,
}
impl Permit {
pub fn lost(&self) -> bool {
self.ends.lost()
}
pub fn waited(&self) -> Duration {
self.waited
}
}
impl fmt::Debug for LockBuilder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.builder, f)
}
}
impl fmt::Debug for SemaphoreBuilder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.builder, f)
}
}
impl fmt::Debug for Lock {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.lock, f)
}
}
impl fmt::Debug for Semaphore {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.semaphore, f)
}
}
impl fmt::Debug for LockKey<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.key, f)
}
}
impl fmt::Debug for SemaphoreKey<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.key, f)
}
}
impl fmt::Debug for Lease {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Lease")
.field("fence", &self.fence)
.field("waited", &self.waited)
.field("lost", &self.lost())
.finish()
}
}
impl fmt::Debug for Permit {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Permit").field("waited", &self.waited).field("lost", &self.lost()).finish()
}
}
#[cfg(test)]
#[path = "locks_tests.rs"]
mod tests;