pub unsafe trait Lock<I: LockInfo = WriteLock> {
type Inner: ?Sized;
type GuardContext;
fn lock_noguard(&self) -> Self::GuardContext;
unsafe fn unlock(&self, context: &mut Self::GuardContext);
fn locked_data(&self) -> &UnsafeCell<Self::Inner>;
fn relock(&self, ctx: &mut Self::GuardContext) { ... }
}
Expand description
A generic mutual exclusion primitive.
Guard
is written such that any mutual exclusion primitive that can implement this trait can
also benefit from having an automatic way to unlock itself.
Safety
- Implementers of this trait with the
WriteLock
marker must ensure that only one thread/CPU may access the protected data once the lock is held, that is, between calls tolock_noguard
andunlock
. - Implementers of all other markers must ensure that a mutable reference to the protected data
is not active in any thread/CPU because at least one shared reference is active between calls
to
lock_noguard
andunlock
.
Required Associated Types
sourcetype GuardContext
type GuardContext
The type of context, if any, that needs to be stored in the guard.
Required Methods
sourcefn lock_noguard(&self) -> Self::GuardContext
fn lock_noguard(&self) -> Self::GuardContext
Acquires the lock, making the caller its owner.
sourceunsafe fn unlock(&self, context: &mut Self::GuardContext)
unsafe fn unlock(&self, context: &mut Self::GuardContext)
Releases the lock, giving up ownership of the lock.
Safety
It must only be called by the current owner of the lock.
sourcefn locked_data(&self) -> &UnsafeCell<Self::Inner>
fn locked_data(&self) -> &UnsafeCell<Self::Inner>
Returns the data protected by the lock.
Provided Methods
sourcefn relock(&self, ctx: &mut Self::GuardContext)
fn relock(&self, ctx: &mut Self::GuardContext)
Reacquires the lock, making the caller its owner.
The guard context before the last unlock is passed in.
Locks that don’t require this state on relock can simply use the default implementation
that calls Lock::lock_noguard
.