Skip to content

Commit bf04343

Browse files
committed
add MappedWriteGuard
1 parent 7a875d4 commit bf04343

File tree

1 file changed

+67
-21
lines changed
  • compiler/rustc_data_structures/src

1 file changed

+67
-21
lines changed

compiler/rustc_data_structures/src/sync.rs

Lines changed: 67 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -779,18 +779,15 @@ unsafe impl<T: ?Sized + Sync> std::marker::Send for MappedReadGuard<'_, T> {}
779779
unsafe impl<T: ?Sized + Sync> std::marker::Sync for MappedReadGuard<'_, T> {}
780780

781781
impl<'a, T: 'a + ?Sized> MappedReadGuard<'a, T> {
782+
#[inline]
782783
pub fn map<U: ?Sized, F>(s: Self, f: F) -> MappedReadGuard<'a, U>
783-
where
784-
F: FnOnce(&T) -> &U,
784+
where
785+
F: FnOnce(&T) -> &U,
785786
{
786787
let raw = s.raw;
787788
let data = f(unsafe { &*s.data });
788789
std::mem::forget(s);
789-
MappedReadGuard {
790-
raw,
791-
data,
792-
marker: PhantomData,
793-
}
790+
MappedReadGuard { raw, data, marker: PhantomData }
794791
}
795792
}
796793

@@ -818,9 +815,55 @@ impl<'a, T: 'a + ?Sized> Drop for MappedReadGuard<'a, T> {
818815
}
819816
}
820817

821-
//pub use std::cell::RefMut;
822-
//pub use parking_lot::MappedRwLockReadGuard as MappedReadGuard;
823-
//pub use parking_lot::MappedRwLockWriteGuard as MappedWriteGuard;
818+
pub struct MappedWriteGuard<'a, T: ?Sized> {
819+
raw: &'a RwLockRaw,
820+
data: *mut T,
821+
marker: PhantomData<&'a mut T>,
822+
}
823+
824+
unsafe impl<T: ?Sized + Sync> std::marker::Send for MappedWriteGuard<'_, T> {}
825+
826+
impl<'a, T: 'a + ?Sized> MappedWriteGuard<'a, T> {
827+
#[inline]
828+
pub fn map<U: ?Sized, F>(s: Self, f: F) -> MappedWriteGuard<'a, U>
829+
where
830+
F: FnOnce(&mut T) -> &mut U,
831+
{
832+
let raw = s.raw;
833+
let data = f(unsafe { &mut *s.data });
834+
std::mem::forget(s);
835+
MappedWriteGuard { raw, data, marker: PhantomData }
836+
}
837+
}
838+
839+
impl<'a, T: 'a + ?Sized> Deref for MappedWriteGuard<'a, T> {
840+
type Target = T;
841+
#[inline]
842+
fn deref(&self) -> &T {
843+
unsafe { &*self.data }
844+
}
845+
}
846+
847+
impl<'a, T: 'a + ?Sized> DerefMut for MappedWriteGuard<'a, T> {
848+
#[inline]
849+
fn deref_mut(&mut self) -> &mut T {
850+
unsafe { &mut *self.data }
851+
}
852+
}
853+
854+
impl<'a, T: 'a + ?Sized> Drop for MappedWriteGuard<'a, T> {
855+
#[inline]
856+
fn drop(&mut self) {
857+
if likely(self.raw.single_thread) {
858+
assert_eq!(self.raw.borrow.replace(0), -1);
859+
} else {
860+
// Safety: An RwLockReadGuard always holds a shared lock.
861+
unsafe {
862+
self.raw.raw.unlock_exclusive();
863+
}
864+
}
865+
}
866+
}
824867

825868
pub struct ReadGuard<'a, T> {
826869
rwlock: &'a RwLock<T>,
@@ -868,6 +911,18 @@ pub struct WriteGuard<'a, T> {
868911
marker: PhantomData<&'a mut T>,
869912
}
870913

914+
impl<'a, T: 'a> WriteGuard<'a, T> {
915+
pub fn map<U: ?Sized, F>(s: Self, f: F) -> MappedWriteGuard<'a, U>
916+
where
917+
F: FnOnce(&mut T) -> &mut U,
918+
{
919+
let raw = &s.rwlock.raw;
920+
let data = f(unsafe { &mut *s.rwlock.data.get() });
921+
std::mem::forget(s);
922+
MappedWriteGuard { raw, data, marker: PhantomData }
923+
}
924+
}
925+
871926
impl<'a, T: 'a> Deref for WriteGuard<'a, T> {
872927
type Target = T;
873928
#[inline]
@@ -917,11 +972,7 @@ impl<T: Debug> Debug for RwLock<T> {
917972
impl<T: Default> Default for RwLock<T> {
918973
fn default() -> Self {
919974
RwLock {
920-
raw: RwLockRaw {
921-
single_thread: !active(),
922-
borrow: Cell::new(0),
923-
raw: RawRwLock::INIT,
924-
},
975+
raw: RwLockRaw { single_thread: !active(), borrow: Cell::new(0), raw: RawRwLock::INIT },
925976

926977
data: UnsafeCell::new(T::default()),
927978
}
@@ -932,14 +983,9 @@ impl<T> RwLock<T> {
932983
#[inline(always)]
933984
pub fn new(inner: T) -> Self {
934985
RwLock {
935-
raw: RwLockRaw {
936-
single_thread: !active(),
937-
borrow: Cell::new(0),
938-
raw: RawRwLock::INIT,
939-
},
986+
raw: RwLockRaw { single_thread: !active(), borrow: Cell::new(0), raw: RawRwLock::INIT },
940987

941988
data: UnsafeCell::new(inner),
942-
943989
}
944990
}
945991

0 commit comments

Comments
 (0)