Skip to content

Commit 3a40e21

Browse files
committed
Auto merge of rust-lang#136772 - safinaskar:parallel-2025-02-08-11-55, r=<try>
parallel: rustc_data_structures: sync: implement RwLock as enum { Sync(parking_lot::RwLock), NoSync(RefCell) } parallel: rustc_data_structures: sync: implement `RwLock` as `enum { Sync(parking_lot::RwLock), NoSync(RefCell) }` Hopefully this will make single threaded front end faster. I carefully split changes into commits. Commit messages are self-explanatory. Squashing is not recommended. cc "Parallel Rustc Front-end" rust-lang#113349 r? SparrowLii `@rustbot` label: +WG-compiler-parallel
2 parents 124cc92 + 5397b39 commit 3a40e21

File tree

3 files changed

+320
-94
lines changed

3 files changed

+320
-94
lines changed

Diff for: compiler/rustc_data_structures/src/sync.rs

+5-93
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,7 @@ pub use std::sync::atomic::{AtomicBool, AtomicU32, AtomicUsize};
111111
pub use std::sync::{OnceLock, Weak};
112112

113113
pub use mode::{is_dyn_thread_safe, set_dyn_thread_safe_mode};
114-
pub use parking_lot::{
115-
MappedMutexGuard as MappedLockGuard, MappedRwLockReadGuard as MappedReadGuard,
116-
MappedRwLockWriteGuard as MappedWriteGuard, RwLockReadGuard as ReadGuard,
117-
RwLockWriteGuard as WriteGuard,
118-
};
114+
pub use parking_lot::MappedMutexGuard as MappedLockGuard;
119115
#[cfg(not(target_has_atomic = "64"))]
120116
pub use portable_atomic::AtomicU64;
121117

@@ -151,12 +147,6 @@ impl<T> MTLock<T> {
151147
}
152148
}
153149

154-
use parking_lot::RwLock as InnerRwLock;
155-
156-
/// This makes locks panic if they are already held.
157-
/// It is only useful when you are running in a single thread
158-
const ERROR_CHECKING: bool = false;
159-
160150
pub type MTLockRef<'a, T> = LRef<'a, MTLock<T>>;
161151

162152
#[derive(Default)]
@@ -175,85 +165,7 @@ impl<K: Eq + Hash, V: Eq, S: BuildHasher> HashMapExt<K, V> for HashMap<K, V, S>
175165
}
176166
}
177167

178-
#[derive(Debug, Default)]
179-
pub struct RwLock<T>(InnerRwLock<T>);
180-
181-
impl<T> RwLock<T> {
182-
#[inline(always)]
183-
pub fn new(inner: T) -> Self {
184-
RwLock(InnerRwLock::new(inner))
185-
}
186-
187-
#[inline(always)]
188-
pub fn into_inner(self) -> T {
189-
self.0.into_inner()
190-
}
191-
192-
#[inline(always)]
193-
pub fn get_mut(&mut self) -> &mut T {
194-
self.0.get_mut()
195-
}
196-
197-
#[inline(always)]
198-
pub fn read(&self) -> ReadGuard<'_, T> {
199-
if ERROR_CHECKING {
200-
self.0.try_read().expect("lock was already held")
201-
} else {
202-
self.0.read()
203-
}
204-
}
205-
206-
#[inline(always)]
207-
#[track_caller]
208-
pub fn with_read_lock<F: FnOnce(&T) -> R, R>(&self, f: F) -> R {
209-
f(&*self.read())
210-
}
211-
212-
#[inline(always)]
213-
pub fn try_write(&self) -> Result<WriteGuard<'_, T>, ()> {
214-
self.0.try_write().ok_or(())
215-
}
216-
217-
#[inline(always)]
218-
pub fn write(&self) -> WriteGuard<'_, T> {
219-
if ERROR_CHECKING {
220-
self.0.try_write().expect("lock was already held")
221-
} else {
222-
self.0.write()
223-
}
224-
}
225-
226-
#[inline(always)]
227-
#[track_caller]
228-
pub fn with_write_lock<F: FnOnce(&mut T) -> R, R>(&self, f: F) -> R {
229-
f(&mut *self.write())
230-
}
231-
232-
#[inline(always)]
233-
#[track_caller]
234-
pub fn borrow(&self) -> ReadGuard<'_, T> {
235-
self.read()
236-
}
237-
238-
#[inline(always)]
239-
#[track_caller]
240-
pub fn borrow_mut(&self) -> WriteGuard<'_, T> {
241-
self.write()
242-
}
243-
244-
#[inline(always)]
245-
pub fn leak(&self) -> &T {
246-
let guard = self.read();
247-
let ret = unsafe { &*(&raw const *guard) };
248-
std::mem::forget(guard);
249-
ret
250-
}
251-
}
252-
253-
// FIXME: Probably a bad idea
254-
impl<T: Clone> Clone for RwLock<T> {
255-
#[inline]
256-
fn clone(&self) -> Self {
257-
RwLock::new(self.borrow().clone())
258-
}
259-
}
168+
mod rwlock;
169+
pub use rwlock::{
170+
MappedReadGuard, MappedWriteGuard, ReadError, ReadGuard, RwLock, WriteError, WriteGuard,
171+
};

0 commit comments

Comments
 (0)