Skip to content

Commit 0240cc2

Browse files
committed
Seed the PRNG with a hashed counter
1 parent b0b6c13 commit 0240cc2

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

rayon-core/src/registry.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ use log::Event::*;
1010
use sleep::Sleep;
1111
use std::any::Any;
1212
use std::cell::Cell;
13+
use std::collections::hash_map::DefaultHasher;
14+
use std::hash::Hasher;
1315
use std::sync::{Arc, Mutex, Once, ONCE_INIT};
16+
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
1417
use std::thread;
1518
use std::mem;
1619
use std::usize;
@@ -601,7 +604,7 @@ impl WorkerThread {
601604
return None;
602605
}
603606

604-
let start = self.rng.next() as usize % num_threads;
607+
let start = self.rng.next_usize(num_threads);
605608
(start .. num_threads)
606609
.chain(0 .. start)
607610
.filter(|&i| i != self.index)
@@ -716,12 +719,14 @@ struct XorShift64Star {
716719

717720
impl XorShift64Star {
718721
fn new() -> Self {
719-
// Any non-zero seed will do -- this uses a stack address, which should be unique since
720-
// we're only calling this once from each new thread. The address is also shift-XORed to
721-
// give us some high bits even on 32-bit platforms.
722-
let base = 0;
723-
let addr = &base as *const _ as u64;
724-
let seed = addr ^ (addr << 32);
722+
// Any non-zero seed will do -- this uses the hash of a global counter.
723+
let mut seed = 0;
724+
while seed == 0 {
725+
let mut hasher = DefaultHasher::new();
726+
static COUNTER: AtomicUsize = ATOMIC_USIZE_INIT;
727+
hasher.write_usize(COUNTER.fetch_add(1, Ordering::Relaxed));
728+
seed = hasher.finish();
729+
}
725730

726731
XorShift64Star {
727732
state: Cell::new(seed),
@@ -737,4 +742,9 @@ impl XorShift64Star {
737742
self.state.set(x);
738743
x.wrapping_mul(0x2545_f491_4f6c_dd1d)
739744
}
745+
746+
/// Return a value from `0..n`.
747+
fn next_usize(&self, n: usize) -> usize {
748+
(self.next() % n as u64) as usize
749+
}
740750
}

0 commit comments

Comments
 (0)