Skip to content

Commit 3412957

Browse files
committed
Unify parallel and non-parallel APIs
It's confusing for these to be different, even if some of the methods are unused.
1 parent 1a5cc25 commit 3412957

File tree

1 file changed

+54
-2
lines changed
  • compiler/rustc_data_structures/src

1 file changed

+54
-2
lines changed

compiler/rustc_data_structures/src/sync.rs

+54-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ cfg_if! {
4444
use std::panic::{resume_unwind, catch_unwind, AssertUnwindSafe};
4545

4646
/// This is a single threaded variant of `AtomicU64`, `AtomicUsize`, etc.
47-
/// It differs from `AtomicCell` in that it has explicit ordering arguments
48-
/// and is only intended for use with the native atomic types.
47+
/// It has explicit ordering arguments and is only intended for use with
48+
/// the native atomic types.
4949
/// You should use this type through the `AtomicU64`, `AtomicUsize`, etc, type aliases
5050
/// as it's not intended to be used separately.
5151
#[derive(Debug)]
@@ -59,6 +59,11 @@ cfg_if! {
5959
}
6060

6161
impl<T: Copy> Atomic<T> {
62+
#[inline]
63+
pub fn into_inner(self) -> T {
64+
self.0.into_inner()
65+
}
66+
6267
#[inline]
6368
pub fn load(&self, _: Ordering) -> T {
6469
self.0.get()
@@ -68,6 +73,11 @@ cfg_if! {
6873
pub fn store(&self, val: T, _: Ordering) {
6974
self.0.set(val)
7075
}
76+
77+
#[inline]
78+
pub fn swap(&self, val: T, _: Ordering) -> T {
79+
self.0.replace(val)
80+
}
7181
}
7282

7383
impl<T: Copy + PartialEq> Atomic<T> {
@@ -180,6 +190,12 @@ cfg_if! {
180190
pub fn new<F: FnMut(usize) -> T>(mut f: F) -> WorkerLocal<T> {
181191
WorkerLocal(OneThread::new(f(0)))
182192
}
193+
194+
/// Returns the worker-local value for each thread
195+
#[inline]
196+
pub fn into_inner(self) -> Vec<T> {
197+
vec![OneThread::into_inner(self.0)]
198+
}
183199
}
184200

185201
impl<T> Deref for WorkerLocal<T> {
@@ -207,6 +223,16 @@ cfg_if! {
207223
self.0
208224
}
209225

226+
#[inline(always)]
227+
pub fn get_mut(&mut self) -> &mut T {
228+
&mut self.0
229+
}
230+
231+
#[inline(always)]
232+
pub fn lock(&self) -> &T {
233+
&self.0
234+
}
235+
210236
#[inline(always)]
211237
pub fn lock_mut(&mut self) -> &mut T {
212238
&mut self.0
@@ -437,6 +463,16 @@ impl<T> RwLock<T> {
437463
RwLock(InnerRwLock::new(inner))
438464
}
439465

466+
#[inline(always)]
467+
pub fn into_inner(self) -> T {
468+
self.0.into_inner()
469+
}
470+
471+
#[inline(always)]
472+
pub fn get_mut(&mut self) -> &mut T {
473+
self.0.get_mut()
474+
}
475+
440476
#[cfg(not(parallel_compiler))]
441477
#[inline(always)]
442478
pub fn read(&self) -> ReadGuard<'_, T> {
@@ -453,6 +489,11 @@ impl<T> RwLock<T> {
453489
}
454490
}
455491

492+
#[inline(always)]
493+
pub fn with_read_lock<F: FnOnce(&T) -> R, R>(&self, f: F) -> R {
494+
f(&*self.read())
495+
}
496+
456497
#[cfg(not(parallel_compiler))]
457498
#[inline(always)]
458499
pub fn try_write(&self) -> Result<WriteGuard<'_, T>, ()> {
@@ -481,6 +522,11 @@ impl<T> RwLock<T> {
481522
}
482523
}
483524

525+
#[inline(always)]
526+
pub fn with_write_lock<F: FnOnce(&mut T) -> R, R>(&self, f: F) -> R {
527+
f(&mut *self.write())
528+
}
529+
484530
#[inline(always)]
485531
pub fn borrow(&self) -> ReadGuard<'_, T> {
486532
self.read()
@@ -529,6 +575,12 @@ impl<T> OneThread<T> {
529575
inner,
530576
}
531577
}
578+
579+
#[inline(always)]
580+
pub fn into_inner(value: Self) -> T {
581+
value.check();
582+
value.inner
583+
}
532584
}
533585

534586
impl<T> Deref for OneThread<T> {

0 commit comments

Comments
 (0)