-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add FreezeLock
type and use it to store Definitions
#115401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0c96a92
Add `Freeze` type and use it to store `Definitions`
Zoxc 6881eed
Don't hold the definitions' lock across `index_hir`
Zoxc ac54d49
Freeze `Definitions` earlier
Zoxc 25884ec
Use `RwLock` for `Freeze`
Zoxc 50f0d66
Add some comments
Zoxc a3ad045
Rename `Freeze` to `FreezeLock`
Zoxc 35e8b67
Use a reference to the lock in the guards
Zoxc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.