Skip to content

Commit

Permalink
Provide safety contract for unlock
Browse files Browse the repository at this point in the history
  • Loading branch information
s0me0ne-unkn0wn committed Nov 5, 2023
1 parent f84b897 commit bc9f073
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 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,10 @@ 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.
#[inline]
fn unlock(&self) {
unsafe fn unlock(&self) {
self.lock.store(false, Ordering::Release);
}
}
Expand All @@ -97,7 +99,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 bc9f073

Please sign in to comment.