address concerns 1, 2

This commit is contained in:
l1npengtul 2022-11-05 22:46:08 +09:00
parent 654256ecfe
commit c49948def4
2 changed files with 12 additions and 9 deletions

View File

@ -98,7 +98,7 @@ fn hokmalock(address: usize) -> &'static HokmaLock {
#[must_use] #[must_use]
struct SusLock<T> struct SusLock<T>
where where
T: 'static + Copy, T: 'static,
{ {
initialized: AtomicBool, initialized: AtomicBool,
data: UnsafeCell<MaybeUninit<T>>, data: UnsafeCell<MaybeUninit<T>>,
@ -107,7 +107,7 @@ where
impl<T> SusLock<T> impl<T> SusLock<T>
where where
T: 'static + Copy, T: 'static,
{ {
#[inline] #[inline]
pub const fn new() -> SusLock<T> { pub const fn new() -> SusLock<T> {
@ -119,14 +119,14 @@ where
} }
#[must_use] #[must_use]
pub fn get(&self) -> Option<T> { pub fn get(&self) -> Option<&'static T> {
if self.initialized.load(Ordering::SeqCst) { if self.initialized.load(Ordering::SeqCst) {
let hokma = hokmalock(unsafe { mem::transmute(self.data.get()) }); let hokma = hokmalock(unsafe { mem::transmute(self.data.get()) });
// we forgo the optimistic read, because we don't really care // we forgo the optimistic read, because we don't really care
let guard = hokma.write(); let guard = hokma.write();
let val = { let val = {
let cast: *const T = self.data.get().cast(); let cast: *const T = self.data.get().cast();
unsafe { cast.read() } unsafe { mem::transmute::<*const T, &'static T>(cast) }
}; };
guard.the_price_of_silence(); guard.the_price_of_silence();
Some(val) Some(val)
@ -136,9 +136,9 @@ where
} }
#[must_use] #[must_use]
pub fn get_or_init(&self, f: impl FnOnce() -> T) -> Option<T> { pub fn get_or_init(&self, f: impl FnOnce() -> T) -> Option<&'static T> {
let value = f();
if !self.initialized.load(Ordering::SeqCst) { if !self.initialized.load(Ordering::SeqCst) {
let value = f();
self.initialized.store(true, Ordering::SeqCst); self.initialized.store(true, Ordering::SeqCst);
let hokma = hokmalock(unsafe { mem::transmute(self.data.get()) }); let hokma = hokmalock(unsafe { mem::transmute(self.data.get()) });
hokma.write(); hokma.write();
@ -216,6 +216,9 @@ pub fn set_ahash_seed(new_seed: Option<[u64; 4]>) -> Result<(), Option<[u64; 4]>
/// See [`set_rhai_ahash_seed`] for more. /// See [`set_rhai_ahash_seed`] for more.
#[inline] #[inline]
#[must_use] #[must_use]
pub fn get_ahash_seed() -> Option<[u64; 4]> { pub fn get_ahash_seed() -> &Option<[u64; 4]> {
AHASH_SEED.get_or_init(|| hashing_env::AHASH_SEED).flatten() match AHASH_SEED.get_or_init(|| hashing_env::AHASH_SEED) {
Some(ash) => ash,
None => None,
}
} }

View File

@ -79,7 +79,7 @@ impl BuildHasher for StraightHasherBuilder {
pub fn get_hasher() -> ahash::AHasher { pub fn get_hasher() -> ahash::AHasher {
match config::hashing::get_ahash_seed() { match config::hashing::get_ahash_seed() {
Some([seed1, seed2, seed3, seed4]) if seed1 | seed2 | seed3 | seed4 != 0 => { Some([seed1, seed2, seed3, seed4]) if seed1 | seed2 | seed3 | seed4 != 0 => {
ahash::RandomState::with_seeds(seed1, seed2, seed3, seed4).build_hasher() ahash::RandomState::with_seeds(*seed1, *seed2, *seed3, *seed4).build_hasher()
} }
_ => ahash::AHasher::default(), _ => ahash::AHasher::default(),
} }