pub struct LockStore<K>{ /* 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§
Source§impl<K> LockStore<K>
impl<K> LockStore<K>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new LockStore.
§Example
use sero::LockStore;
let store = LockStore::new();
let guard = store.lock("test").wait();
Sourcepub fn with_custom_unused_locks(keep_unused_locks: usize) -> Self
pub fn with_custom_unused_locks(keep_unused_locks: usize) -> Self
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);
Sourcepub fn lock(&self, key: K) -> LockWaiter<K> ⓘ
pub fn lock(&self, key: K) -> LockWaiter<K> ⓘ
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);