2022-10-31 16:59:07 +01:00
|
|
|
//! Fixed hashing seeds for stable hashing.
|
|
|
|
//!
|
|
|
|
//! Set to [`None`] to disable stable hashing.
|
|
|
|
//!
|
|
|
|
//! See [`set_rhai_ahash_seed`] for more.
|
|
|
|
//!
|
2022-11-04 07:36:18 +01:00
|
|
|
//! Alternatively, You can also set this at compile time by setting the `RHAI_AHASH_SEED`
|
2022-10-31 16:59:07 +01:00
|
|
|
//! environment variable instead.
|
|
|
|
//!
|
|
|
|
//! E.g. `env RHAI_AHASH_SEED ="[236,800,954,213]"`
|
|
|
|
// [236,800,954,213], haha funny yume nikki reference epic uboachan face numberworld nexus moment 100
|
|
|
|
|
2022-11-04 07:36:18 +01:00
|
|
|
use crate::config::hashing_env;
|
2022-11-01 13:25:45 +01:00
|
|
|
use core::{
|
|
|
|
cell::UnsafeCell,
|
|
|
|
marker::PhantomData,
|
|
|
|
mem::MaybeUninit,
|
|
|
|
panic::{RefUnwindSafe, UnwindSafe},
|
2022-11-04 07:36:18 +01:00
|
|
|
sync::atomic::{AtomicBool, AtomicUsize, Ordering},
|
2022-11-01 13:25:45 +01:00
|
|
|
};
|
|
|
|
|
2022-11-04 07:36:18 +01:00
|
|
|
// omg its hokma from record team here to record our locks
|
|
|
|
// what does this do?
|
|
|
|
// so what this does is keep track of a global address in memory that acts as a global lock
|
|
|
|
// i stole this from crossbeam so read their docs for more
|
|
|
|
struct HokmaLock {
|
|
|
|
lock: AtomicUsize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HokmaLock {
|
|
|
|
pub const fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
lock: AtomicUsize::new(0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write(&'static self) -> WhenTheHokmaSupression {
|
|
|
|
loop {
|
|
|
|
let previous = self.lock.swap(1, Ordering::SeqCst);
|
|
|
|
|
|
|
|
if previous != 1 {
|
|
|
|
return WhenTheHokmaSupression {
|
|
|
|
hokma: self,
|
|
|
|
state: previous,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct WhenTheHokmaSupression {
|
|
|
|
hokma: &'static HokmaLock,
|
|
|
|
|
|
|
|
state: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WhenTheHokmaSupression {
|
|
|
|
pub fn the_price_of_silence(self) {
|
|
|
|
self.hokma.lock.store(self.state, Ordering::SeqCst);
|
|
|
|
|
|
|
|
core::mem::forget(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for WhenTheHokmaSupression {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.hokma
|
|
|
|
.lock
|
|
|
|
.store(self.state.wrapping_add(2), Ordering::SeqCst)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn hokmalock(address: usize) -> &'static HokmaLock {
|
|
|
|
const LEN: usize = 787;
|
|
|
|
const LCK: HokmaLock = HokmaLock::new();
|
|
|
|
static RECORDS: [HokmaLock; LEN] = [LCK; LEN];
|
|
|
|
|
|
|
|
&RECORDS[address % LEN]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Safety: lol, there is a reason its called "SusLock<T>"
|
2022-11-01 13:25:45 +01:00
|
|
|
struct SusLock<T>
|
|
|
|
where
|
2022-11-04 07:36:18 +01:00
|
|
|
T: 'static + Copy,
|
2022-11-01 13:25:45 +01:00
|
|
|
{
|
|
|
|
initalized: AtomicBool,
|
|
|
|
data: UnsafeCell<MaybeUninit<T>>,
|
|
|
|
_marker: PhantomData<T>,
|
|
|
|
}
|
2022-10-31 16:59:07 +01:00
|
|
|
|
2022-11-04 07:36:18 +01:00
|
|
|
impl<T> SusLock<T>
|
|
|
|
where
|
|
|
|
T: 'static + Copy,
|
|
|
|
{
|
2022-11-01 13:25:45 +01:00
|
|
|
pub const fn new() -> SusLock<T> {
|
|
|
|
SusLock {
|
|
|
|
initalized: AtomicBool::new(false),
|
|
|
|
data: UnsafeCell::new(MaybeUninit::uninit()),
|
|
|
|
_marker: PhantomData,
|
|
|
|
}
|
2022-10-31 16:59:07 +01:00
|
|
|
}
|
|
|
|
|
2022-11-04 07:36:18 +01:00
|
|
|
pub fn get(&self) -> Option<T> {
|
2022-11-01 13:25:45 +01:00
|
|
|
if self.initalized.load(Ordering::SeqCst) {
|
2022-11-04 07:36:18 +01:00
|
|
|
let hokma = hokmalock(unsafe { core::mem::transmute(self.data.get()) });
|
|
|
|
// we forgo the optimistic read, because we don't really care
|
|
|
|
let guard = hokma.write();
|
|
|
|
let val = {
|
|
|
|
let cast: *const T = self.data.get().cast();
|
|
|
|
unsafe { cast.read() }
|
|
|
|
};
|
|
|
|
guard.the_price_of_silence();
|
|
|
|
Some(val)
|
2022-11-01 13:25:45 +01:00
|
|
|
} else {
|
|
|
|
return None;
|
2022-10-31 16:59:07 +01:00
|
|
|
}
|
2022-11-01 13:25:45 +01:00
|
|
|
}
|
2022-10-31 16:59:07 +01:00
|
|
|
|
2022-11-04 07:36:18 +01:00
|
|
|
pub fn get_or_init(&self, f: impl FnOnce() -> T) -> Option<T> {
|
2022-11-01 13:25:45 +01:00
|
|
|
let value = f();
|
|
|
|
if !self.initalized.load(Ordering::SeqCst) {
|
2022-11-04 07:36:18 +01:00
|
|
|
self.initalized.store(true, Ordering::SeqCst);
|
|
|
|
let hokma = hokmalock(unsafe { core::mem::transmute(self.data.get()) });
|
|
|
|
hokma.write();
|
2022-11-01 13:25:45 +01:00
|
|
|
unsafe {
|
|
|
|
self.data.get().write(MaybeUninit::new(value));
|
2022-10-31 16:59:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-01 13:25:45 +01:00
|
|
|
self.get()
|
2022-10-31 16:59:07 +01:00
|
|
|
}
|
|
|
|
|
2022-11-01 13:25:45 +01:00
|
|
|
pub fn set(&self, value: T) -> Result<(), T> {
|
|
|
|
if self.initalized.load(Ordering::SeqCst) {
|
|
|
|
Err(value)
|
|
|
|
} else {
|
|
|
|
let _ = self.get_or_init(|| value);
|
|
|
|
Ok(())
|
2022-10-31 16:59:07 +01:00
|
|
|
}
|
|
|
|
}
|
2022-11-01 13:25:45 +01:00
|
|
|
}
|
2022-10-31 16:59:07 +01:00
|
|
|
|
2022-11-04 07:36:18 +01:00
|
|
|
unsafe impl<T: Sync + Send> Sync for SusLock<T> where T: 'static + Copy {}
|
|
|
|
unsafe impl<T: Send> Send for SusLock<T> where T: 'static + Copy {}
|
|
|
|
impl<T: RefUnwindSafe + UnwindSafe> RefUnwindSafe for SusLock<T> where T: 'static + Copy {}
|
2022-10-31 16:59:07 +01:00
|
|
|
|
2022-11-04 07:36:18 +01:00
|
|
|
impl<T> Drop for SusLock<T>
|
|
|
|
where
|
|
|
|
T: 'static + Copy,
|
|
|
|
{
|
2022-11-01 13:25:45 +01:00
|
|
|
fn drop(&mut self) {
|
|
|
|
if self.initalized.load(Ordering::SeqCst) {
|
|
|
|
unsafe { (&mut *self.data.get()).assume_init_drop() };
|
|
|
|
}
|
2022-10-31 16:59:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-01 13:25:45 +01:00
|
|
|
static AHASH_SEED: SusLock<Option<[u64; 4]>> = SusLock::new();
|
|
|
|
|
|
|
|
// #[doc(cfg(feature = "stable_hash"))]
|
|
|
|
/// Sets the Rhai Ahash seed. This is used to hash functions and the like.
|
|
|
|
///
|
|
|
|
/// This is a global variable, and thus will affect every Rhai instance.
|
|
|
|
/// This should not be used _unless_ you know you need it.
|
|
|
|
///
|
|
|
|
/// # Warnings
|
|
|
|
/// - You can only call this function **ONCE** for the whole of your program execution.
|
|
|
|
/// - You should gracefully handle the `Err(())`.
|
|
|
|
/// - You **MUST** call this before **ANY** Rhai operation occurs (e.g. creating an [`Engine`]).
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
/// This will error if the AHashSeed is already set.
|
|
|
|
pub fn set_ahash_seed(new_seed: Option<[u64; 4]>) -> Result<(), Option<[u64; 4]>> {
|
|
|
|
AHASH_SEED.set(new_seed)
|
|
|
|
}
|
2022-10-31 16:59:07 +01:00
|
|
|
|
2022-11-01 13:25:45 +01:00
|
|
|
/// Gets the current Rhai Ahash Seed. If the seed is not yet defined, this will automatically set a seed.
|
|
|
|
/// The default seed is not stable and may change between versions.
|
|
|
|
///
|
|
|
|
/// See [`set_rhai_ahash_seed`] for more.
|
|
|
|
pub fn get_ahash_seed() -> Option<[u64; 4]> {
|
2022-11-04 07:36:18 +01:00
|
|
|
AHASH_SEED.get_or_init(|| hashing_env::AHASH_SEED).flatten()
|
2022-11-01 13:25:45 +01:00
|
|
|
}
|