|
| 1 | +use crate::sync::{AtomicBool, ReadGuard, RwLock, WriteGuard}; |
| 2 | +#[cfg(parallel_compiler)] |
| 3 | +use crate::sync::{DynSend, DynSync}; |
| 4 | +use std::{ |
| 5 | + cell::UnsafeCell, |
| 6 | + marker::PhantomData, |
| 7 | + ops::{Deref, DerefMut}, |
| 8 | + sync::atomic::Ordering, |
| 9 | +}; |
| 10 | + |
| 11 | +/// A type which allows mutation using a lock until |
| 12 | +/// the value is frozen and can be accessed lock-free. |
| 13 | +/// |
| 14 | +/// Unlike `RwLock`, it can be used to prevent mutation past a point. |
| 15 | +#[derive(Default)] |
| 16 | +pub struct FreezeLock<T> { |
| 17 | + data: UnsafeCell<T>, |
| 18 | + frozen: AtomicBool, |
| 19 | + |
| 20 | + /// This lock protects writes to the `data` and `frozen` fields. |
| 21 | + lock: RwLock<()>, |
| 22 | +} |
| 23 | + |
| 24 | +#[cfg(parallel_compiler)] |
| 25 | +unsafe impl<T: DynSync + DynSend> DynSync for FreezeLock<T> {} |
| 26 | + |
| 27 | +impl<T> FreezeLock<T> { |
| 28 | + #[inline] |
| 29 | + pub fn new(value: T) -> Self { |
| 30 | + Self { data: UnsafeCell::new(value), frozen: AtomicBool::new(false), lock: RwLock::new(()) } |
| 31 | + } |
| 32 | + |
| 33 | + #[inline] |
| 34 | + pub fn read(&self) -> FreezeReadGuard<'_, T> { |
| 35 | + FreezeReadGuard { |
| 36 | + _lock_guard: if self.frozen.load(Ordering::Acquire) { |
| 37 | + None |
| 38 | + } else { |
| 39 | + Some(self.lock.read()) |
| 40 | + }, |
| 41 | + lock: self, |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + #[inline] |
| 46 | + #[track_caller] |
| 47 | + pub fn write(&self) -> FreezeWriteGuard<'_, T> { |
| 48 | + let _lock_guard = self.lock.write(); |
| 49 | + // Use relaxed ordering since we're in the write lock. |
| 50 | + assert!(!self.frozen.load(Ordering::Relaxed), "still mutable"); |
| 51 | + FreezeWriteGuard { _lock_guard, lock: self, marker: PhantomData } |
| 52 | + } |
| 53 | + |
| 54 | + #[inline] |
| 55 | + pub fn freeze(&self) -> &T { |
| 56 | + if !self.frozen.load(Ordering::Acquire) { |
| 57 | + // Get the lock to ensure no concurrent writes and that we release the latest write. |
| 58 | + let _lock = self.lock.write(); |
| 59 | + self.frozen.store(true, Ordering::Release); |
| 60 | + } |
| 61 | + |
| 62 | + // SAFETY: This is frozen so the data cannot be modified and shared access is sound. |
| 63 | + unsafe { &*self.data.get() } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/// A guard holding shared access to a `FreezeLock` which is in a locked state or frozen. |
| 68 | +#[must_use = "if unused the FreezeLock may immediately unlock"] |
| 69 | +pub struct FreezeReadGuard<'a, T> { |
| 70 | + _lock_guard: Option<ReadGuard<'a, ()>>, |
| 71 | + lock: &'a FreezeLock<T>, |
| 72 | +} |
| 73 | + |
| 74 | +impl<'a, T: 'a> Deref for FreezeReadGuard<'a, T> { |
| 75 | + type Target = T; |
| 76 | + #[inline] |
| 77 | + fn deref(&self) -> &T { |
| 78 | + // SAFETY: If `lock` is not frozen, `_lock_guard` holds the lock to the `UnsafeCell` so |
| 79 | + // this has shared access until the `FreezeReadGuard` is dropped. If `lock` is frozen, |
| 80 | + // the data cannot be modified and shared access is sound. |
| 81 | + unsafe { &*self.lock.data.get() } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/// A guard holding mutable access to a `FreezeLock` which is in a locked state or frozen. |
| 86 | +#[must_use = "if unused the FreezeLock may immediately unlock"] |
| 87 | +pub struct FreezeWriteGuard<'a, T> { |
| 88 | + _lock_guard: WriteGuard<'a, ()>, |
| 89 | + lock: &'a FreezeLock<T>, |
| 90 | + marker: PhantomData<&'a mut T>, |
| 91 | +} |
| 92 | + |
| 93 | +impl<'a, T: 'a> Deref for FreezeWriteGuard<'a, T> { |
| 94 | + type Target = T; |
| 95 | + #[inline] |
| 96 | + fn deref(&self) -> &T { |
| 97 | + // SAFETY: `self._lock_guard` holds the lock to the `UnsafeCell` so this has shared access. |
| 98 | + unsafe { &*self.lock.data.get() } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +impl<'a, T: 'a> DerefMut for FreezeWriteGuard<'a, T> { |
| 103 | + #[inline] |
| 104 | + fn deref_mut(&mut self) -> &mut T { |
| 105 | + // SAFETY: `self._lock_guard` holds the lock to the `UnsafeCell` so this has mutable access. |
| 106 | + unsafe { &mut *self.lock.data.get() } |
| 107 | + } |
| 108 | +} |
0 commit comments