#[cfg(feature = "async")]
mod awaited;
mod engine;
mod lease;
mod state;
use std::cell::RefCell;
use std::fmt;
use std::sync::mpsc;
use std::sync::{Arc, Weak};
use std::time::Duration;
#[cfg(feature = "async")]
pub(crate) use self::awaited::{Ends, Held};
pub(crate) use self::engine::Locks;
pub use self::lease::Fence;
pub(crate) use self::lease::{Answer, Granted};
use self::lease::{Ask, Ending, Kind, Place};
use super::scope::check_name;
use crate::{Error, ErrorKind, Key, Result, Store, sqlite};
const WAIT: Duration = Duration::from_secs(10);
const MOST_KEY: usize = 1024;
const MOST_LIMIT: u32 = 1000;
#[must_use = "a lock opens with open()"]
pub struct LockBuilder {
store: Store,
name: String,
max_hold: Option<Duration>,
}
#[must_use = "a semaphore opens with open()"]
pub struct SemaphoreBuilder {
store: Store,
name: String,
limit: Option<u32>,
max_hold: Option<Duration>,
}
impl Store {
pub fn lock(&self, name: &str) -> LockBuilder {
LockBuilder { store: self.clone(), name: name.to_owned(), max_hold: None }
}
pub fn semaphore(&self, name: &str) -> SemaphoreBuilder {
SemaphoreBuilder { store: self.clone(), name: name.to_owned(), limit: None, max_hold: None }
}
}
impl LockBuilder {
pub fn max_hold(mut self, bound: Duration) -> Self {
self.max_hold = Some(bound);
self
}
pub fn open(self) -> Result<Lock> {
Ok(Lock { opened: Opened::open(&self.store, Kind::Lock, &self.name, 1, self.max_hold)? })
}
}
impl SemaphoreBuilder {
pub fn limit(mut self, limit: u32) -> Self {
self.limit = Some(limit);
self
}
pub fn max_hold(mut self, bound: Duration) -> Self {
self.max_hold = Some(bound);
self
}
pub fn open(self) -> Result<Semaphore> {
let shown = || format!("semaphore {}", self.name);
let limit = self.limit.ok_or_else(|| Error::invalid("its limit is given with limit()").within(shown()))?;
if !(1..=MOST_LIMIT).contains(&limit) {
return Err(Error::invalid(format!("a limit of {limit}, not 1 to {MOST_LIMIT}")).within(shown()));
}
Ok(Semaphore { opened: Opened::open(&self.store, Kind::Semaphore, &self.name, limit, self.max_hold)? })
}
}
#[derive(Clone)]
pub struct Lock {
opened: Opened,
}
#[derive(Clone)]
pub struct Semaphore {
opened: Opened,
}
impl Lock {
pub fn hold<T, E: From<Error>>(&self, key: impl Key, work: impl FnOnce(&Lease) -> Result<T, E>) -> Result<T, E> {
self.key(key).hold(work)
}
pub fn try_hold<T, E: From<Error>>(
&self,
key: impl Key,
work: impl FnOnce(&Lease) -> Result<T, E>,
) -> Result<Option<T>, E> {
self.key(key).try_hold(work)
}
pub fn key(&self, key: impl Key) -> LockKey<'_> {
LockKey { opened: &self.opened, key: key.text().into_owned(), wait: WAIT }
}
}
impl Semaphore {
pub fn hold<T, E: From<Error>>(&self, key: impl Key, work: impl FnOnce(&Permit) -> Result<T, E>) -> Result<T, E> {
self.key(key).hold(work)
}
pub fn try_hold<T, E: From<Error>>(
&self,
key: impl Key,
work: impl FnOnce(&Permit) -> Result<T, E>,
) -> Result<Option<T>, E> {
self.key(key).try_hold(work)
}
pub fn key(&self, key: impl Key) -> SemaphoreKey<'_> {
SemaphoreKey { opened: &self.opened, key: key.text().into_owned(), wait: WAIT }
}
}
#[must_use = "a key is held with hold() or try_hold()"]
pub struct LockKey<'a> {
opened: &'a Opened,
key: String,
wait: Duration,
}
#[must_use = "a key is held with hold() or try_hold()"]
pub struct SemaphoreKey<'a> {
opened: &'a Opened,
key: String,
wait: Duration,
}
impl LockKey<'_> {
pub fn wait(mut self, wait: Duration) -> Self {
self.wait = wait;
self
}
pub fn hold<T, E: From<Error>>(self, work: impl FnOnce(&Lease) -> Result<T, E>) -> Result<T, E> {
let holding = self.opened.hold(&self.key, self.wait)?;
let lease = Lease { fence: holding.granted.fence, holding: &holding };
holding.run(|| work(&lease))
}
pub fn try_hold<T, E: From<Error>>(self, work: impl FnOnce(&Lease) -> Result<T, E>) -> Result<Option<T>, E> {
let Some(holding) = self.opened.try_hold(&self.key)? else { return Ok(None) };
let lease = Lease { fence: holding.granted.fence, holding: &holding };
holding.run(|| work(&lease)).map(Some)
}
}
impl SemaphoreKey<'_> {
pub fn wait(mut self, wait: Duration) -> Self {
self.wait = wait;
self
}
pub fn hold<T, E: From<Error>>(self, work: impl FnOnce(&Permit) -> Result<T, E>) -> Result<T, E> {
let holding = self.opened.hold(&self.key, self.wait)?;
holding.run(|| work(&Permit { holding: &holding }))
}
pub fn try_hold<T, E: From<Error>>(self, work: impl FnOnce(&Permit) -> Result<T, E>) -> Result<Option<T>, E> {
let Some(holding) = self.opened.try_hold(&self.key)? else { return Ok(None) };
holding.run(|| work(&Permit { holding: &holding })).map(Some)
}
}
pub struct Lease<'a> {
fence: Fence,
holding: &'a Holding,
}
impl Lease<'_> {
pub fn fence(&self) -> Fence {
self.fence
}
pub fn lost(&self) -> bool {
self.holding.ended()
}
}
pub struct Permit<'a> {
holding: &'a Holding,
}
impl Permit<'_> {
pub fn lost(&self) -> bool {
self.holding.ended()
}
}
#[derive(Clone)]
struct Opened {
locks: Arc<Locks>,
kind: Kind,
name: Arc<str>,
limit: u32,
max_hold: Option<Duration>,
}
impl Opened {
fn open(store: &Store, kind: Kind, name: &str, limit: u32, max_hold: Option<Duration>) -> Result<Opened> {
let shown = || format!("{} {name}", if kind == Kind::Lock { "lock" } else { "semaphore" });
check_name(name).map_err(|error| error.within(shown()))?;
if max_hold.is_some_and(|bound| bound.is_zero()) {
return Err(Error::invalid("a max_hold of zero").within(shown()));
}
let locks = Locks::of(store)?;
Ok(Opened { locks, kind, name: Arc::from(name), limit, max_hold })
}
fn place(&self, key: &str) -> Result<Place> {
let place = Place::new(self.kind, Arc::clone(&self.name), key.to_owned());
if key.is_empty() || key.len() > MOST_KEY {
return Err(Error::invalid(format!("a key is 1 to {MOST_KEY} bytes")).within(place));
}
Ok(place)
}
fn hold(&self, key: &str, wait: Duration) -> Result<Holding> {
let place = self.place(key)?;
match self.ask(&place, wait)? {
Some(holding) => Ok(holding),
None => Err(Error::new(ErrorKind::InUse, format!("held past a wait of {wait:?}")).within(place)),
}
}
fn try_hold(&self, key: &str) -> Result<Option<Holding>> {
let place = self.place(key)?;
self.ask(&place, Duration::ZERO)
}
fn ask(&self, place: &Place, wait: Duration) -> Result<Option<Holding>> {
crate::polled::may_wait(|| format!("{place}: a hold"))?;
refuse_where_a_hold_would_wait_for_itself(&self.locks, place)?;
let mark = Mark::enter(&self.locks, place);
let ask =
Ask { place: place.clone(), limit: self.limit, ttl: None, max_hold: self.max_hold, wait, request: None };
let (answered, answer) = mpsc::sync_channel(1);
self.locks.ask(ask, Box::new(move |answer| drop(answered.send(answer))));
let answer = answer.recv().map_err(|_| Error::internal("a lock's answer was dropped").within(place))?;
Ok(match answer? {
Answer::Granted(granted) => {
Some(Holding { locks: Arc::downgrade(&self.locks), place: place.clone(), granted, _mark: Some(mark) })
}
Answer::Busy => None,
})
}
}
#[cfg(feature = "wire")]
pub(crate) struct Remote<'a> {
pub(crate) kind: &'a str,
pub(crate) name: &'a str,
pub(crate) key: &'a str,
pub(crate) limit: u32,
pub(crate) ttl: Duration,
pub(crate) max_hold: Option<Duration>,
pub(crate) wait: Duration,
pub(crate) request: [u8; 16],
}
#[cfg(feature = "wire")]
pub(crate) fn remote_ask(store: &Store, remote: &Remote<'_>) -> Result<(Arc<Locks>, Ask)> {
let kind = match remote.kind {
"lock" if remote.limit == 1 => Kind::Lock,
"semaphore" if (1..=MOST_LIMIT).contains(&remote.limit) => Kind::Semaphore,
other => return Err(Error::invalid(format!("a {other} of limit {}", remote.limit))),
};
let opened = Opened::open(store, kind, remote.name, remote.limit, remote.max_hold)?;
let place = opened.place(remote.key)?;
if !(Duration::from_secs(1)..=Duration::from_secs(60 * 60)).contains(&remote.ttl) {
return Err(Error::invalid(format!("a ttl of {:?}, not 1 s to 1 hour", remote.ttl)).within(place));
}
let ask = Ask {
place,
limit: remote.limit,
ttl: Some(remote.ttl),
max_hold: remote.max_hold,
wait: remote.wait,
request: Some(remote.request),
};
Ok((opened.locks, ask))
}
struct Holding {
locks: Weak<Locks>,
place: Place,
granted: Granted,
_mark: Option<Mark>,
}
impl Holding {
fn ended(&self) -> bool {
self.granted.watch.ending() != Ending::Live
}
fn run<T, E: From<Error>>(&self, work: impl FnOnce() -> Result<T, E>) -> Result<T, E> {
let value = work()?;
self.kept(value)
}
fn kept<T, E: From<Error>>(&self, value: T) -> Result<T, E> {
match self.granted.watch.ending() {
Ending::Live => Ok(value),
Ending::Lost => {
let lost = "the lease ended while its function ran, revoked at its max_hold";
Err(E::from(Error::new(ErrorKind::Lost, lost).within(&self.place)))
}
Ending::Closed => Err(E::from(Error::closed(self.place.to_string()))),
}
}
}
impl Drop for Holding {
fn drop(&mut self) {
if let Some(locks) = self.locks.upgrade() {
locks.release(&self.granted.lease);
}
}
}
impl fmt::Debug for Lock {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Lock({})", self.opened.name)
}
}
impl fmt::Debug for Semaphore {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Semaphore({}, {})", self.opened.name, self.opened.limit)
}
}
impl fmt::Debug for LockBuilder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "LockBuilder({})", self.name)
}
}
impl fmt::Debug for SemaphoreBuilder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "SemaphoreBuilder({})", self.name)
}
}
impl fmt::Debug for LockKey<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "LockKey({}: {:?})", self.opened.name, self.key)
}
}
impl fmt::Debug for SemaphoreKey<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "SemaphoreKey({}: {:?})", self.opened.name, self.key)
}
}
impl fmt::Debug for Lease<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Lease({}, {:?})", self.holding.place, self.fence)
}
}
impl fmt::Debug for Permit<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Permit({})", self.holding.place)
}
}
thread_local! {
static HOLDING: RefCell<Vec<(usize, Place)>> = const { RefCell::new(Vec::new()) };
}
fn refuse_where_a_hold_would_wait_for_itself(locks: &Arc<Locks>, place: &Place) -> Result<()> {
if sqlite::in_a_transaction() {
let why = "a hold inside a transaction would keep its writer while the key is waited for: hold first";
return Err(Error::invalid(why).within(place));
}
let address = Arc::as_ptr(locks) as usize;
if HOLDING.with(|holding| holding.borrow().iter().any(|(at, held)| *at == address && held == place)) {
return Err(Error::invalid("a hold of a key inside its own hold would wait for itself").within(place));
}
Ok(())
}
struct Mark(usize, Place);
impl Mark {
fn enter(locks: &Arc<Locks>, place: &Place) -> Mark {
Mark::at(Arc::as_ptr(locks) as usize, place)
}
fn at(address: usize, place: &Place) -> Mark {
HOLDING.with(|holding| holding.borrow_mut().push((address, place.clone())));
Mark(address, place.clone())
}
}
impl Drop for Mark {
fn drop(&mut self) {
HOLDING.with(|holding| {
let mut holding = holding.borrow_mut();
if let Some(at) = holding.iter().rposition(|(address, place)| *address == self.0 && place == &self.1) {
holding.remove(at);
}
});
}
}
#[cfg(test)]
#[path = "locks_tests.rs"]
mod tests;