pub fn try_lock_scope<F, Ret>(f: F) -> Option<Ret>Expand description
Try to enter an ordered lock acquisition scope.
Only available with the std feature (enabled by default). On
no_std, use KeyHandle::claim
and KeyHandle::scope instead.
scope(&mut self) provides static nesting prevention via the
borrow checker, without requiring thread_local!.
Returns Some(result) if a scope was entered successfully, or
None if a scope is already active on the current thread.
Use lock_scope for a panicking convenience wrapper.
The for<'scope> bound makes 'scope universally quantified:
the closure must work for any 'scope, so Ret cannot name
'scope. This prevents keys and guards from escaping the closure.
Same technique as std::thread::scope.
ยงExamples
use surelock::{key::try_lock_scope, mutex::Mutex};
let counter: Mutex<u32> = Mutex::new(0);
let result = try_lock_scope(|key| {
let (mut guard, _key) = key.lock(&counter);
*guard += 1;
});
match result {
Some(()) => { /* success */ }
None => { /* already inside a scope */ }
}