Skip to content

Commit

Permalink
Tracking allocator: mark Spinlock::unlock() as unsafe and provide a…
Browse files Browse the repository at this point in the history
… safety contract (#2156)
  • Loading branch information
s0me0ne-unkn0wn authored Nov 5, 2023
1 parent 0c39cf0 commit c46a7db
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions polkadot/node/tracking-allocator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ impl<T> Spinlock<T> {
}
}

// SAFETY: It should be only called from the guard's destructor. Calling it explicitly while
// the guard is alive is undefined behavior, as it breaks the security contract of `Deref` and
// `DerefMut`, which implies that lock is held at the moment of dereferencing.
#[inline]
fn unlock(&self) {
unsafe fn unlock(&self) {
self.lock.store(false, Ordering::Release);
}
}
Expand All @@ -97,7 +100,9 @@ impl<T> DerefMut for SpinlockGuard<'_, T> {

impl<T> Drop for SpinlockGuard<'_, T> {
fn drop(&mut self) {
self.lock.unlock();
// SAFETY: Calling `unlock` is only safe when it's guaranteed no guard outlives the
// unlocking point; here, the guard is dropped, so it is safe.
unsafe { self.lock.unlock() }
}
}

Expand Down

0 comments on commit c46a7db

Please sign in to comment.