Skip to content

Commit 969296b

Browse files
committed
Add a WorkerLocal abstraction
1 parent b7aabaa commit 969296b

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 = "0.1.0"
20+
rustc-rayon-core = "0.1.0"
2021
rustc-hash = "1.0.1"
2122

2223
[dependencies.parking_lot]

src/librustc_data_structures/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ extern crate parking_lot;
4444
extern crate cfg_if;
4545
extern crate stable_deref_trait;
4646
extern crate rustc_rayon as rayon;
47+
extern crate rustc_rayon_core as rayon_core;
4748
extern crate rustc_hash;
4849

4950
// See librustc_cratesio_shim/Cargo.toml for a comment explaining this.

src/librustc_data_structures/sync.rs

+29
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,33 @@ cfg_if! {
9999

100100
use std::cell::Cell;
101101

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

@@ -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)