forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#115401 - Zoxc:freeze, r=oli-obk
Add `FreezeLock` type and use it to store `Definitions` This adds a `FreezeLock` type which allows mutation using a lock until the value is frozen where it can be accessed lock-free. It's used to store `Definitions` in `Untracked` instead of a `RwLock`. Unlike the current scheme of leaking read guards this doesn't deadlock if definitions is written to after no mutation are expected.
- Loading branch information
Showing
9 changed files
with
148 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
use crate::sync::{AtomicBool, ReadGuard, RwLock, WriteGuard}; | ||
#[cfg(parallel_compiler)] | ||
use crate::sync::{DynSend, DynSync}; | ||
use std::{ | ||
cell::UnsafeCell, | ||
marker::PhantomData, | ||
ops::{Deref, DerefMut}, | ||
sync::atomic::Ordering, | ||
}; | ||
|
||
/// A type which allows mutation using a lock until | ||
/// the value is frozen and can be accessed lock-free. | ||
/// | ||
/// Unlike `RwLock`, it can be used to prevent mutation past a point. | ||
#[derive(Default)] | ||
pub struct FreezeLock<T> { | ||
data: UnsafeCell<T>, | ||
frozen: AtomicBool, | ||
|
||
/// This lock protects writes to the `data` and `frozen` fields. | ||
lock: RwLock<()>, | ||
} | ||
|
||
#[cfg(parallel_compiler)] | ||
unsafe impl<T: DynSync + DynSend> DynSync for FreezeLock<T> {} | ||
|
||
impl<T> FreezeLock<T> { | ||
#[inline] | ||
pub fn new(value: T) -> Self { | ||
Self { data: UnsafeCell::new(value), frozen: AtomicBool::new(false), lock: RwLock::new(()) } | ||
} | ||
|
||
#[inline] | ||
pub fn read(&self) -> FreezeReadGuard<'_, T> { | ||
FreezeReadGuard { | ||
_lock_guard: if self.frozen.load(Ordering::Acquire) { | ||
None | ||
} else { | ||
Some(self.lock.read()) | ||
}, | ||
lock: self, | ||
} | ||
} | ||
|
||
#[inline] | ||
#[track_caller] | ||
pub fn write(&self) -> FreezeWriteGuard<'_, T> { | ||
let _lock_guard = self.lock.write(); | ||
// Use relaxed ordering since we're in the write lock. | ||
assert!(!self.frozen.load(Ordering::Relaxed), "still mutable"); | ||
FreezeWriteGuard { _lock_guard, lock: self, marker: PhantomData } | ||
} | ||
|
||
#[inline] | ||
pub fn freeze(&self) -> &T { | ||
if !self.frozen.load(Ordering::Acquire) { | ||
// Get the lock to ensure no concurrent writes and that we release the latest write. | ||
let _lock = self.lock.write(); | ||
self.frozen.store(true, Ordering::Release); | ||
} | ||
|
||
// SAFETY: This is frozen so the data cannot be modified and shared access is sound. | ||
unsafe { &*self.data.get() } | ||
} | ||
} | ||
|
||
/// A guard holding shared access to a `FreezeLock` which is in a locked state or frozen. | ||
#[must_use = "if unused the FreezeLock may immediately unlock"] | ||
pub struct FreezeReadGuard<'a, T> { | ||
_lock_guard: Option<ReadGuard<'a, ()>>, | ||
lock: &'a FreezeLock<T>, | ||
} | ||
|
||
impl<'a, T: 'a> Deref for FreezeReadGuard<'a, T> { | ||
type Target = T; | ||
#[inline] | ||
fn deref(&self) -> &T { | ||
// SAFETY: If `lock` is not frozen, `_lock_guard` holds the lock to the `UnsafeCell` so | ||
// this has shared access until the `FreezeReadGuard` is dropped. If `lock` is frozen, | ||
// the data cannot be modified and shared access is sound. | ||
unsafe { &*self.lock.data.get() } | ||
} | ||
} | ||
|
||
/// A guard holding mutable access to a `FreezeLock` which is in a locked state or frozen. | ||
#[must_use = "if unused the FreezeLock may immediately unlock"] | ||
pub struct FreezeWriteGuard<'a, T> { | ||
_lock_guard: WriteGuard<'a, ()>, | ||
lock: &'a FreezeLock<T>, | ||
marker: PhantomData<&'a mut T>, | ||
} | ||
|
||
impl<'a, T: 'a> Deref for FreezeWriteGuard<'a, T> { | ||
type Target = T; | ||
#[inline] | ||
fn deref(&self) -> &T { | ||
// SAFETY: `self._lock_guard` holds the lock to the `UnsafeCell` so this has shared access. | ||
unsafe { &*self.lock.data.get() } | ||
} | ||
} | ||
|
||
impl<'a, T: 'a> DerefMut for FreezeWriteGuard<'a, T> { | ||
#[inline] | ||
fn deref_mut(&mut self) -> &mut T { | ||
// SAFETY: `self._lock_guard` holds the lock to the `UnsafeCell` so this has mutable access. | ||
unsafe { &mut *self.lock.data.get() } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters