Skip to content

Commit 51b6afd

Browse files
committed
Add a WorkerLocal abstraction
1 parent fdbf2c8 commit 51b6afd

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

src/librustc_data_structures/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ cfg-if = "0.1.2"
1717
stable_deref_trait = "1.0.0"
1818
parking_lot_core = "0.2.8"
1919
rustc-rayon = { git = "https://github.com/Zoxc/rayon.git", branch = "fiber" }
20+
rustc-rayon-core = { git = "https://github.com/Zoxc/rayon.git", branch = "fiber" }
2021

2122
[dependencies.parking_lot]
2223
version = "0.5"

src/librustc_data_structures/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ extern crate parking_lot;
4545
extern crate cfg_if;
4646
extern crate stable_deref_trait;
4747
extern crate rustc_rayon as rayon;
48+
extern crate rustc_rayon_core as rayon_core;
4849

4950
// See librustc_cratesio_shim/Cargo.toml for a comment explaining this.
5051
#[allow(unused_extern_crates)]

src/librustc_data_structures/sync.rs

+29
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,33 @@ cfg_if! {
100100

101101
use std::cell::Cell;
102102

103+
#[derive(Debug)]
104+
pub struct WorkerLocal<T>(OneThread<T>);
105+
106+
impl<T> WorkerLocal<T> {
107+
/// Creates a new worker local where the `initial` closure computes the
108+
/// value this worker local should take for each thread in the thread pool.
109+
#[inline]
110+
pub fn new<F: FnMut(usize) -> T>(mut initial: F) -> WorkerLocal<T> {
111+
WorkerLocal(OneThread::new(f(0)))
112+
}
113+
114+
/// Returns the worker-local value for each thread
115+
#[inline]
116+
pub fn into_inner(self) -> Vec<T> {
117+
vec![self.0.into_inner()]
118+
}
119+
}
120+
121+
impl<T> Deref for WorkerLocal<T> {
122+
type Target = T;
123+
124+
#[inline(always)]
125+
fn deref(&self) -> &T {
126+
&*self.0
127+
}
128+
}
129+
103130
#[derive(Debug)]
104131
pub struct MTLock<T>(T);
105132

@@ -203,6 +230,8 @@ cfg_if! {
203230
use std::thread;
204231
pub use rayon::{join, scope};
205232

233+
pub use rayon_core::WorkerLocal;
234+
206235
pub use rayon::iter::ParallelIterator;
207236
use rayon::iter::IntoParallelIterator;
208237

0 commit comments

Comments
 (0)