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