Skip to content

Commit 6745c60

Browse files
committed
Auto merge of rust-lang#116185 - Zoxc:rem-one-thread, r=cjgillot
Remove `OneThread` This removes `OneThread` by switching `incr_comp_session` over to `RwLock`.
2 parents 227abac + 63446d0 commit 6745c60

File tree

3 files changed

+8
-64
lines changed

3 files changed

+8
-64
lines changed

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

-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ cfg_match! {
177177
[Vec<T, A> where T: DynSync, A: std::alloc::Allocator + DynSync]
178178
[Box<T, A> where T: ?Sized + DynSync, A: std::alloc::Allocator + DynSync]
179179
[crate::sync::RwLock<T> where T: DynSend + DynSync]
180-
[crate::sync::OneThread<T> where T]
181180
[crate::sync::WorkerLocal<T> where T: DynSend]
182181
[crate::intern::Interned<'a, T> where 'a, T: DynSync]
183182
[crate::tagged_ptr::CopyTaggedPtr<P, T, CP> where P: Sync + crate::tagged_ptr::Pointer, T: Sync + crate::tagged_ptr::Tag, const CP: bool]

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

-56
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
pub use crate::marker::*;
4444
use std::collections::HashMap;
4545
use std::hash::{BuildHasher, Hash};
46-
use std::ops::{Deref, DerefMut};
4746

4847
mod lock;
4948
pub use lock::{Lock, LockGuard, Mode};
@@ -309,8 +308,6 @@ cfg_match! {
309308

310309
use parking_lot::RwLock as InnerRwLock;
311310

312-
use std::thread;
313-
314311
/// This makes locks panic if they are already held.
315312
/// It is only useful when you are running in a single thread
316313
const ERROR_CHECKING: bool = false;
@@ -445,56 +442,3 @@ impl<T: Clone> Clone for RwLock<T> {
445442
RwLock::new(self.borrow().clone())
446443
}
447444
}
448-
449-
/// A type which only allows its inner value to be used in one thread.
450-
/// It will panic if it is used on multiple threads.
451-
#[derive(Debug)]
452-
pub struct OneThread<T> {
453-
#[cfg(parallel_compiler)]
454-
thread: thread::ThreadId,
455-
inner: T,
456-
}
457-
458-
#[cfg(parallel_compiler)]
459-
unsafe impl<T> std::marker::Sync for OneThread<T> {}
460-
#[cfg(parallel_compiler)]
461-
unsafe impl<T> std::marker::Send for OneThread<T> {}
462-
463-
impl<T> OneThread<T> {
464-
#[inline(always)]
465-
fn check(&self) {
466-
#[cfg(parallel_compiler)]
467-
assert_eq!(thread::current().id(), self.thread);
468-
}
469-
470-
#[inline(always)]
471-
pub fn new(inner: T) -> Self {
472-
OneThread {
473-
#[cfg(parallel_compiler)]
474-
thread: thread::current().id(),
475-
inner,
476-
}
477-
}
478-
479-
#[inline(always)]
480-
pub fn into_inner(value: Self) -> T {
481-
value.check();
482-
value.inner
483-
}
484-
}
485-
486-
impl<T> Deref for OneThread<T> {
487-
type Target = T;
488-
489-
fn deref(&self) -> &T {
490-
self.check();
491-
&self.inner
492-
}
493-
}
494-
495-
impl<T> DerefMut for OneThread<T> {
496-
fn deref_mut(&mut self) -> &mut T {
497-
self.check();
498-
&mut self.inner
499-
}
500-
}

Diff for: compiler/rustc_session/src/session.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ use rustc_data_structures::flock;
1414
use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
1515
use rustc_data_structures::jobserver::{self, Client};
1616
use rustc_data_structures::profiling::{SelfProfiler, SelfProfilerRef};
17-
use rustc_data_structures::sync::{AtomicU64, DynSend, DynSync, Lock, Lrc, OneThread};
17+
use rustc_data_structures::sync::{
18+
AtomicU64, DynSend, DynSync, Lock, Lrc, MappedReadGuard, ReadGuard, RwLock,
19+
};
1820
use rustc_errors::annotate_snippet_emitter_writer::AnnotateSnippetEmitter;
1921
use rustc_errors::emitter::{DynEmitter, HumanEmitter, HumanReadableErrorType};
2022
use rustc_errors::json::JsonEmitter;
@@ -35,7 +37,6 @@ use rustc_target::spec::{
3537
};
3638

3739
use std::any::Any;
38-
use std::cell::{self, RefCell};
3940
use std::env;
4041
use std::fmt;
4142
use std::ops::{Div, Mul};
@@ -149,7 +150,7 @@ pub struct Session {
149150
/// Input, input file path and output file path to this compilation process.
150151
pub io: CompilerIO,
151152

152-
incr_comp_session: OneThread<RefCell<IncrCompSession>>,
153+
incr_comp_session: RwLock<IncrCompSession>,
153154

154155
/// Used by `-Z self-profile`.
155156
pub prof: SelfProfilerRef,
@@ -533,9 +534,9 @@ impl Session {
533534
*incr_comp_session = IncrCompSession::InvalidBecauseOfErrors { session_directory };
534535
}
535536

536-
pub fn incr_comp_session_dir(&self) -> cell::Ref<'_, PathBuf> {
537+
pub fn incr_comp_session_dir(&self) -> MappedReadGuard<'_, PathBuf> {
537538
let incr_comp_session = self.incr_comp_session.borrow();
538-
cell::Ref::map(incr_comp_session, |incr_comp_session| match *incr_comp_session {
539+
ReadGuard::map(incr_comp_session, |incr_comp_session| match *incr_comp_session {
539540
IncrCompSession::NotInitialized => panic!(
540541
"trying to get session directory from `IncrCompSession`: {:?}",
541542
*incr_comp_session,
@@ -548,7 +549,7 @@ impl Session {
548549
})
549550
}
550551

551-
pub fn incr_comp_session_dir_opt(&self) -> Option<cell::Ref<'_, PathBuf>> {
552+
pub fn incr_comp_session_dir_opt(&self) -> Option<MappedReadGuard<'_, PathBuf>> {
552553
self.opts.incremental.as_ref().map(|_| self.incr_comp_session_dir())
553554
}
554555

@@ -1176,7 +1177,7 @@ pub fn build_session(
11761177
parse_sess,
11771178
sysroot,
11781179
io,
1179-
incr_comp_session: OneThread::new(RefCell::new(IncrCompSession::NotInitialized)),
1180+
incr_comp_session: RwLock::new(IncrCompSession::NotInitialized),
11801181
prof,
11811182
code_stats: Default::default(),
11821183
optimization_fuel,

0 commit comments

Comments
 (0)