Skip to content

Commit 2aa19fe

Browse files
committed
Add with_lock, with_read_lock and with_write_lock
1 parent 2789b06 commit 2aa19fe

File tree

1 file changed

+87
-39
lines changed
  • src/librustc_data_structures

1 file changed

+87
-39
lines changed

src/librustc_data_structures/sync.rs

Lines changed: 87 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ cfg_if! {
6262
pub use std::cell::RefMut as WriteGuard;
6363
pub use std::cell::RefMut as LockGuard;
6464

65-
pub use std::cell::RefCell as RwLock;
65+
use std::cell::RefCell as InnerRwLock;
6666
use std::cell::RefCell as InnerLock;
6767

6868
use std::cell::Cell;
@@ -159,13 +159,12 @@ cfg_if! {
159159

160160
pub use parking_lot::MutexGuard as LockGuard;
161161

162-
use parking_lot;
163-
164162
pub use std::sync::Arc as Lrc;
165163

166164
pub use self::Lock as MTLock;
167165

168166
use parking_lot::Mutex as InnerLock;
167+
use parking_lot::RwLock as InnerRwLock;
169168

170169
pub type MetadataRef = OwningRef<Box<Erased + Send + Sync>, [u8]>;
171170

@@ -222,42 +221,6 @@ cfg_if! {
222221
self.0.lock().take()
223222
}
224223
}
225-
226-
#[derive(Debug)]
227-
pub struct RwLock<T>(parking_lot::RwLock<T>);
228-
229-
impl<T> RwLock<T> {
230-
#[inline(always)]
231-
pub fn new(inner: T) -> Self {
232-
RwLock(parking_lot::RwLock::new(inner))
233-
}
234-
235-
#[inline(always)]
236-
pub fn borrow(&self) -> ReadGuard<T> {
237-
if ERROR_CHECKING {
238-
self.0.try_read().expect("lock was already held")
239-
} else {
240-
self.0.read()
241-
}
242-
}
243-
244-
#[inline(always)]
245-
pub fn borrow_mut(&self) -> WriteGuard<T> {
246-
if ERROR_CHECKING {
247-
self.0.try_write().expect("lock was already held")
248-
} else {
249-
self.0.write()
250-
}
251-
}
252-
}
253-
254-
// FIXME: Probably a bad idea
255-
impl<T: Clone> Clone for RwLock<T> {
256-
#[inline]
257-
fn clone(&self) -> Self {
258-
RwLock::new(self.borrow().clone())
259-
}
260-
}
261224
}
262225
}
263226

@@ -383,6 +346,11 @@ impl<T> Lock<T> {
383346
self.0.borrow_mut()
384347
}
385348

349+
#[inline(always)]
350+
pub fn with_lock<F: FnOnce(&mut T) -> R, R>(&self, f: F) -> R {
351+
f(&mut *self.lock())
352+
}
353+
386354
#[inline(always)]
387355
pub fn borrow(&self) -> LockGuard<T> {
388356
self.lock()
@@ -401,3 +369,83 @@ impl<T: Clone> Clone for Lock<T> {
401369
Lock::new(self.borrow().clone())
402370
}
403371
}
372+
373+
#[derive(Debug)]
374+
pub struct RwLock<T>(InnerRwLock<T>);
375+
376+
impl<T> RwLock<T> {
377+
#[inline(always)]
378+
pub fn new(inner: T) -> Self {
379+
RwLock(InnerRwLock::new(inner))
380+
}
381+
382+
#[inline(always)]
383+
pub fn into_inner(self) -> T {
384+
self.0.into_inner()
385+
}
386+
387+
#[inline(always)]
388+
pub fn get_mut(&mut self) -> &mut T {
389+
self.0.get_mut()
390+
}
391+
392+
#[cfg(not(parallel_queries))]
393+
#[inline(always)]
394+
pub fn read(&self) -> ReadGuard<T> {
395+
self.0.borrow()
396+
}
397+
398+
#[cfg(parallel_queries)]
399+
#[inline(always)]
400+
pub fn read(&self) -> ReadGuard<T> {
401+
if ERROR_CHECKING {
402+
self.0.try_read().expect("lock was already held")
403+
} else {
404+
self.0.read()
405+
}
406+
}
407+
408+
#[inline(always)]
409+
pub fn with_read_lock<F: FnOnce(&T) -> R, R>(&self, f: F) -> R {
410+
f(&*self.read())
411+
}
412+
413+
#[cfg(not(parallel_queries))]
414+
#[inline(always)]
415+
pub fn write(&self) -> WriteGuard<T> {
416+
self.0.borrow_mut()
417+
}
418+
419+
#[cfg(parallel_queries)]
420+
#[inline(always)]
421+
pub fn write(&self) -> WriteGuard<T> {
422+
if ERROR_CHECKING {
423+
self.0.try_write().expect("lock was already held")
424+
} else {
425+
self.0.write()
426+
}
427+
}
428+
429+
#[inline(always)]
430+
pub fn with_write_lock<F: FnOnce(&mut T) -> R, R>(&self, f: F) -> R {
431+
f(&mut *self.write())
432+
}
433+
434+
#[inline(always)]
435+
pub fn borrow(&self) -> ReadGuard<T> {
436+
self.read()
437+
}
438+
439+
#[inline(always)]
440+
pub fn borrow_mut(&self) -> WriteGuard<T> {
441+
self.write()
442+
}
443+
}
444+
445+
// FIXME: Probably a bad idea
446+
impl<T: Clone> Clone for RwLock<T> {
447+
#[inline]
448+
fn clone(&self) -> Self {
449+
RwLock::new(self.borrow().clone())
450+
}
451+
}

0 commit comments

Comments
 (0)