Struct sero::LockStore

source ·
pub struct LockStore<K>where
    K: Hash + Eq + Clone + Send + Sync + 'static,
{ /* private fields */ }
Expand description

A LockStore for sharing and managing a set of locks.

To lock a specific key with the store use LockStore::lock(key), any subsequent calls for that key will remain locked until the current holder is released (the returned LockGuard is dropped). When there are no longer any guards (when nothing is locking the lock) and there are no waiters left (no subsequent calls are waiting for the lock) then the lock will be placed into an unused locks queue and reused for a different key in the future.

To configure the number of locks kept in the queue, the LockStore can be created with LockStore::with_custom_unused_locks. The default value for LockStore::new() is 100 locks.

Example

use sero::LockStore;

let store = LockStore::with_custom_unused_locks(1000);
let waiter = store.lock("test");
let guard = waiter.wait();
// the lock is released here
drop(guard);

Implementations§

Create a new LockStore.

Example
use sero::LockStore;

let store = LockStore::new();

let guard = store.lock("test").wait();
Examples found in repository?
src/lib.rs (line 136)
135
136
137
    fn default() -> Self {
        Self::new()
    }

Provide a custom number of unused locks to keep in the internal queue rather than recreating locks.

The default value when this function is not used is 100 locks.

Example
use sero::LockStore;

// now the store will keep up to 1000 unused locks in the queue to prevent reallocating them.
let store: LockStore<String> = LockStore::with_custom_unused_locks(1000);
Examples found in repository?
src/lib.rs (line 73)
72
73
74
    pub fn new() -> Self {
        Self::with_custom_unused_locks(100)
    }

Lock a specific key and get the relevant LockWaiter. To actually acquire the lock either use the wait() method to lock synchronously or .await to lock asynchronously.

Example
let store = LockStore::new();
// acquire a lock
let guard = store.lock("test").wait();

// to acquire the lock asynchronously use
let guard = store.lock("test").await;

// the lock is released here
drop(guard);

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.