Skip to content

Commit 20c526e

Browse files
committed
Removed RefCell, refs, moved thread_local, applied cargo fmt.
1 parent d9bc1fc commit 20c526e

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

rayon-core/src/join/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ where
125125
// Create virtual wrapper for task b; this all has to be
126126
// done here so that the stack frame can keep it all live
127127
// long enough.
128-
let job_b = StackJob::new(|migrated| oper_b(FnContext::new(migrated)), &latch);
128+
let job_b = StackJob::new(|migrated| oper_b(FnContext::new(migrated)), latch);
129129
let job_b_ref = job_b.as_job_ref();
130130
worker_thread.push(job_b_ref);
131131

@@ -164,7 +164,7 @@ where
164164
log!(LostJob {
165165
worker: worker_thread.index()
166166
});
167-
worker_thread.wait_until(job_b.latch);
167+
worker_thread.wait_until(&job_b.latch);
168168
debug_assert!(job_b.latch.probe());
169169
break;
170170
}

rayon-core/src/latch.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,13 @@ impl LockLatch {
8686
}
8787
}
8888

89-
/// Resets this lock latch so it can be reused again.
90-
pub(super) fn reset(&self) {
91-
*self.m.lock().unwrap() = false;
89+
/// Block until latch is set, then resets this lock latch so it can be reused again.
90+
pub(super) fn wait_and_reset(&self) {
91+
let mut guard = self.m.lock().unwrap();
92+
while !*guard {
93+
guard = self.v.wait(guard).unwrap();
94+
}
95+
*guard = false;
9296
}
9397

9498
/// Block until latch is set.
@@ -192,13 +196,19 @@ impl<'a, L: Latch> Latch for TickleLatch<'a, L> {
192196
}
193197
}
194198

195-
impl<'a, L> LatchProbe for &'a L where L: LatchProbe {
199+
impl<'a, L> LatchProbe for &'a L
200+
where
201+
L: LatchProbe,
202+
{
196203
fn probe(&self) -> bool {
197204
L::probe(self)
198205
}
199206
}
200207

201-
impl<'a, L> Latch for &'a L where L: Latch {
208+
impl<'a, L> Latch for &'a L
209+
where
210+
L: Latch,
211+
{
202212
fn set(&self) {
203213
L::set(self);
204214
}

rayon-core/src/registry.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ use latch::{CountLatch, Latch, LatchProbe, LockLatch, SpinLatch, TickleLatch};
99
use log::Event::*;
1010
use sleep::Sleep;
1111
use std::any::Any;
12-
use std::cell::{Cell, RefCell};
12+
use std::cell::Cell;
1313
use std::collections::hash_map::DefaultHasher;
1414
use std::fmt;
1515
use std::hash::Hasher;
1616
use std::io;
1717
use std::mem;
18-
use std::ops::{Deref};
1918
use std::ptr;
2019
#[allow(deprecated)]
2120
use std::sync::atomic::ATOMIC_USIZE_INIT;
@@ -480,19 +479,16 @@ impl Registry {
480479
}
481480
}
482481
}
483-
484-
482+
485483
#[cold]
486484
unsafe fn in_worker_cold<OP, R>(&self, op: OP) -> R
487485
where
488486
OP: FnOnce(&WorkerThread, bool) -> R + Send,
489487
R: Send,
490488
{
491-
LOCK_LATCH.with(|l| {
492-
// This should not panic since the latch is thread local and we are in the `cold` path.
493-
// If `op` were to call another `ThreadPool::install` it would not end up here.
494-
let latch = l.borrow();
489+
thread_local!(static LOCK_LATCH: LockLatch = LockLatch::new());
495490

491+
LOCK_LATCH.with(|l| {
496492
// This thread isn't a member of *any* thread pool, so just block.
497493
debug_assert!(WorkerThread::current().is_null());
498494
let job = StackJob::new(
@@ -501,11 +497,10 @@ impl Registry {
501497
assert!(injected && !worker_thread.is_null());
502498
op(&*worker_thread, true)
503499
},
504-
latch.deref(),
500+
l,
505501
);
506502
self.inject(&[job.as_job_ref()]);
507-
job.latch.wait();
508-
job.latch.reset(); // Makes sure we can use the same latch again next time.
503+
job.latch.wait_and_reset(); // Make sure we can use the same latch again next time.
509504
job.into_result()
510505
})
511506
}
@@ -620,8 +615,6 @@ pub(super) struct WorkerThread {
620615
// for a RefCell<T> etc.
621616
thread_local! {
622617
static WORKER_THREAD_STATE: Cell<*const WorkerThread> = Cell::new(ptr::null());
623-
624-
static LOCK_LATCH: RefCell<LockLatch> = RefCell::new(LockLatch::new());
625618
}
626619

627620
impl Drop for WorkerThread {

0 commit comments

Comments
 (0)