From 50f0d666d3308f0b0a45842cbe725fcb5b3c3861 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20K=C3=A5re=20Alsaker?= Date: Sat, 2 Sep 2023 01:22:22 +0200 Subject: [PATCH] Add some comments --- compiler/rustc_data_structures/src/sync/freeze.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_data_structures/src/sync/freeze.rs b/compiler/rustc_data_structures/src/sync/freeze.rs index 21bbc35e317..e9ad6ee8394 100644 --- a/compiler/rustc_data_structures/src/sync/freeze.rs +++ b/compiler/rustc_data_structures/src/sync/freeze.rs @@ -9,6 +9,8 @@ use std::{ /// 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 Freeze { data: UnsafeCell, @@ -46,6 +48,7 @@ impl Freeze { #[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, @@ -58,7 +61,7 @@ impl Freeze { #[inline] pub fn freeze(&self) -> &T { if !self.frozen.load(Ordering::Acquire) { - // Get the lock to ensure no concurrent writes. + // 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); }